A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.md 19KB

5 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. title: Nameko for Microservices
  2. url: http://lucumr.pocoo.org/2015/4/8/microservices-with-nameko/
  3. hash_url: ab01c9532d507c86951542839a45c026
  4. <p>In December some of the tech guys at <a class="reference external" href="http://www.onefinestay.com/">onefinestay</a> invited me over to London to do some
  5. general improvements on their their <a class="reference external" href="http://nameko.readthedocs.org/en/latest/">nameko</a> library. This collaboration
  6. came together because nameko was pretty similar to how I generally like to
  7. build certain infrastructure and I had some experience with very similar
  8. systems.</p>
  9. <p>So now that some of those improvements hit the release version of nameko I
  10. figured it might be a good idea to give some feedback on why I like this
  11. sort of architecture.</p>
  12. <h2>Freeing your Mind</h2>
  13. <p>Right now if you want to build a web service in Python there are many
  14. tools you can pick from, but most of them live in a very specific part of
  15. your stack. The most common tool is a web framework and that will
  16. typically provide you with whatever glue is necessary to connect your own
  17. code to an incoming HTTP request that comes from your client.</p>
  18. <p>However that's not all you need in an application. For instance very
  19. often you have periodic tasks that you need to execute and in that case,
  20. your framework is often not just not helping, it's also in your way. For
  21. instance because you might have built your code with the assumption that
  22. it has access to the HTTP request object. If you now want to run it from
  23. a cronjob that request object is unavailable.</p>
  24. <p>In addition to crons there is often also the wish to execute something as
  25. the result of the request of a client, but without blocking that request.
  26. For instance imagine there is an admin panel in which you can trigger some
  27. very expensive data conversion task. What you actually want is for the
  28. current request to finish but the conversion task to keep on working in
  29. the background until your data set is converted.</p>
  30. <p>There are obviously many existing solutions for that. Celery comes to
  31. mind. However they are typically very separated from the rest of the
  32. stack.</p>
  33. <p>Having a system which treats all of this processes the same frees up your
  34. mind. This is what makes microservices interesting. Away with having
  35. HTTP request handlers that have no direct relationship with message queue
  36. worker tasks or cronjobs. Instead you can have a coherent system where
  37. any component can talk through well defined points with other parts of the
  38. system.</p>
  39. <p>This is especially useful in Python where traditionally our support for
  40. parallel execution has been between very bad to abysmal.</p>
  41. <h2>Enter Nameko</h2>
  42. <p>Nameko is an implementation of this idea. It's very similar in
  43. architecture to how we structure code at Fireteam. It's based on
  44. distributing work between processes through AMQP. It's not just AMQP
  45. though. Nameko abstracts away from that and allows you to write your own
  46. transports, while staying true to the AMQP patterns.</p>
  47. <p>Nameko does a handful of things and you can build very complex systems
  48. with it. The idea is that you build individual services which can emit
  49. events to which other services can subscribe to or they can directly
  50. invoke each other via RPC. All communication between the services happens
  51. through AMQP. You don't need to manually deal with any connectivity of
  52. those.</p>
  53. <p>In addition to message exchange, services also use a lifecycle management
  54. to find useful resources through dependency injection. That sounds like a
  55. mouthful but is actually very simple. Because services are classes, you
  56. can add special attributes to them which will be resolved at runtime. The
  57. lifetime of the value resolved can be customized. For instance it becomes
  58. possible to attach a property to the class which can provide access to a
  59. database connection. The lifetime of that database connection can be
  60. automatically managed.</p>
  61. <p>So how does that look in practice? Something like this:</p>
  62. <div class="highlight"><pre><span class="kn">from</span> <span class="nn">nameko.rpc</span> <span class="kn">import</span> <span class="n">rpc</span>
  63. <span class="k">class</span> <span class="nc">HelloWorldService</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  64. <span class="n">name</span> <span class="o">=</span> <span class="s">&#39;helloworld&#39;</span>
  65. <span class="nd">@rpc</span>
  66. <span class="k">def</span> <span class="nf">hello</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
  67. <span class="k">return</span> <span class="s">&quot;Hello, {}!&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
  68. </pre></div>
  69. <p>This defines a basic service that provides one method that can be invoked
  70. via RPC. Either another service can do that, or any other process that
  71. runs nameko can also invoke that, for as long as they connect to the same
  72. AMQP server. To experiment with this service, Nameko provides a shell
  73. helper that launches an interactive Python shell with an <tt class="docutils literal">n</tt> object that
  74. provides RPC access:</p>
  75. <div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span><span class="o">.</span><span class="n">rpc</span><span class="o">.</span><span class="n">helloworld</span><span class="o">.</span><span class="n">hello</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;John&#39;</span><span class="p">)</span>
  76. <span class="go">u&#39;Hello, John!&#39;</span>
  77. </pre></div>
  78. <p>If the AMQP server is running, <tt class="docutils literal">rpc.helloworld.hello</tt> contacts the
  79. <tt class="docutils literal">helloworld</tt> service and resolves the <tt class="docutils literal">hello</tt> method on it. Upon
  80. calling this method a message will be dispatched via the AMQP broker and
  81. be picked up by a nameko process. The shell will then block and wait for
  82. the result to come back.</p>
  83. <p>A more useful example is what happens when services want to collaborate on
  84. some activity. For instance it's quite common that one service wants to
  85. respond to the changes another service performs to update it's own state.
  86. This can be achieved through events:</p>
  87. <div class="highlight"><pre><span class="kn">from</span> <span class="nn">nameko.events</span> <span class="kn">import</span> <span class="n">EventDispatcher</span><span class="p">,</span> <span class="n">event_handler</span>
  88. <span class="kn">from</span> <span class="nn">nameko.rpc</span> <span class="kn">import</span> <span class="n">rpc</span>
  89. <span class="k">class</span> <span class="nc">ServiceA</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  90. <span class="n">name</span> <span class="o">=</span> <span class="s">&#39;servicea&#39;</span>
  91. <span class="n">dispatch</span> <span class="o">=</span> <span class="n">EventDispatcher</span><span class="p">()</span>
  92. <span class="nd">@rpc</span>
  93. <span class="k">def</span> <span class="nf">emit_an_event</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  94. <span class="bp">self</span><span class="o">.</span><span class="n">dispatch</span><span class="p">(</span><span class="s">&#39;my_event_type&#39;</span><span class="p">,</span> <span class="s">&#39;payload&#39;</span><span class="p">)</span>
  95. <span class="k">class</span> <span class="nc">ServiceB</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  96. <span class="n">name</span> <span class="o">=</span> <span class="s">&#39;serviceb&#39;</span>
  97. <span class="nd">@event_handler</span><span class="p">(</span><span class="s">&#39;servicea&#39;</span><span class="p">,</span> <span class="s">&#39;my_event_type&#39;</span><span class="p">)</span>
  98. <span class="k">def</span> <span class="nf">handle_an_event</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">payload</span><span class="p">):</span>
  99. <span class="k">print</span> <span class="s">&#39;service b received&#39;</span><span class="p">,</span> <span class="n">payload</span>
  100. </pre></div>
  101. <p>The default behavior is that one service instance of each service type
  102. will pick up the event. However nameko can also route an event to every
  103. single instance of every single service. This is useful for in-process
  104. cache invalidation for instance.</p>
  105. <h2>The Web</h2>
  106. <p>Nameko is not just good for internal communication however. It uses
  107. Werkzeug to provide a bridge to the outside world. This allows you to
  108. accept an HTTP request and to ingest a task into your service world:</p>
  109. <div class="highlight"><pre><span class="kn">import</span> <span class="nn">json</span>
  110. <span class="kn">from</span> <span class="nn">nameko.web.handlers</span> <span class="kn">import</span> <span class="n">http</span>
  111. <span class="kn">from</span> <span class="nn">werkzeug.wrappers</span> <span class="kn">import</span> <span class="n">Response</span>
  112. <span class="k">class</span> <span class="nc">HttpServiceService</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  113. <span class="n">name</span> <span class="o">=</span> <span class="s">&#39;helloworld&#39;</span>
  114. <span class="nd">@http</span><span class="p">(</span><span class="s">&#39;GET&#39;</span><span class="p">,</span> <span class="s">&#39;/get/&lt;int:value&gt;&#39;</span><span class="p">)</span>
  115. <span class="k">def</span> <span class="nf">get_method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
  116. <span class="k">return</span> <span class="n">Response</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">({</span><span class="s">&#39;value&#39;</span><span class="p">:</span> <span class="n">value</span><span class="p">}),</span>
  117. <span class="n">mimetype</span><span class="o">=</span><span class="s">&#39;application/json&#39;</span><span class="p">)</span>
  118. </pre></div>
  119. <p>The endpoint function can itself invoke other parts of the system via RPC
  120. or other methods.</p>
  121. <p>This functionality generally also extends into the websocket world, even
  122. though that part is currently quite experimental. It for instance is
  123. possible to listen to events and forward them into websocket connections.</p>
  124. <h2>Dependency Injection</h2>
  125. <p>One of the really neat design concepts in Nameko is the use of dependency
  126. injection to find resources. A good example is the SQLAlchemy bridge
  127. which attaches a SQLAlchemy database session to a service through
  128. dependency injection. The descriptor itself will hook into the lifecycle
  129. management to automatically manage the database resources:</p>
  130. <div class="highlight"><pre><span class="kn">from</span> <span class="nn">nameko_sqlalchemy</span> <span class="kn">import</span> <span class="n">Session</span>
  131. <span class="kn">import</span> <span class="nn">sqlalchemy</span> <span class="kn">as</span> <span class="nn">sa</span>
  132. <span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declarative_base</span>
  133. <span class="n">Base</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">()</span>
  134. <span class="k">class</span> <span class="nc">User</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
  135. <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;users&#39;</span>
  136. <span class="nb">id</span> <span class="o">=</span> <span class="n">sa</span><span class="o">.</span><span class="n">Column</span><span class="p">(</span><span class="n">sa</span><span class="o">.</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
  137. <span class="n">username</span> <span class="o">=</span> <span class="n">sa</span><span class="o">.</span><span class="n">Column</span><span class="p">(</span><span class="n">sa</span><span class="o">.</span><span class="n">String</span><span class="p">)</span>
  138. <span class="k">class</span> <span class="nc">MyService</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  139. <span class="n">name</span> <span class="o">=</span> <span class="s">&#39;myservice&#39;</span>
  140. <span class="n">session</span> <span class="o">=</span> <span class="n">Session</span><span class="p">(</span><span class="n">Base</span><span class="p">)</span>
  141. <span class="nd">@rpc</span>
  142. <span class="k">def</span> <span class="nf">get_username</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">user_id</span><span class="p">):</span>
  143. <span class="n">user</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">User</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">user_id</span><span class="p">)</span>
  144. <span class="k">if</span> <span class="n">user</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
  145. <span class="k">return</span> <span class="n">user</span><span class="o">.</span><span class="n">username</span>
  146. </pre></div>
  147. <p>The implementation of the <tt class="docutils literal">Session</tt> dependency provider itself is
  148. ridiculously simple. The whole functionality could be implemented like
  149. this:</p>
  150. <div class="highlight"><pre><span class="kn">from</span> <span class="nn">weakref</span> <span class="kn">import</span> <span class="n">WeakKeyDictionary</span>
  151. <span class="kn">from</span> <span class="nn">nameko.extensions</span> <span class="kn">import</span> <span class="n">DependencyProvider</span>
  152. <span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">create_engine</span>
  153. <span class="kn">from</span> <span class="nn">sqlalchemy.orm</span> <span class="kn">import</span> <span class="n">sessionmaker</span>
  154. <span class="k">class</span> <span class="nc">Session</span><span class="p">(</span><span class="n">DependencyProvider</span><span class="p">):</span>
  155. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">declarative_base</span><span class="p">):</span>
  156. <span class="bp">self</span><span class="o">.</span><span class="n">declarative_base</span> <span class="o">=</span> <span class="n">declarative_base</span>
  157. <span class="bp">self</span><span class="o">.</span><span class="n">sessions</span> <span class="o">=</span> <span class="n">WeakKeyDictionary</span><span class="p">()</span>
  158. <span class="k">def</span> <span class="nf">get_dependency</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">worker_ctx</span><span class="p">):</span>
  159. <span class="n">db_uri</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">container</span><span class="o">.</span><span class="n">config</span><span class="p">[</span><span class="s">&#39;DATABASE_URL&#39;</span><span class="p">]</span>
  160. <span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="n">db_uri</span><span class="p">)</span>
  161. <span class="n">session_cls</span> <span class="o">=</span> <span class="n">sessionmaker</span><span class="p">(</span><span class="n">bind</span><span class="o">=</span><span class="n">engine</span><span class="p">)</span>
  162. <span class="bp">self</span><span class="o">.</span><span class="n">sessions</span><span class="p">[</span><span class="n">worker_ctx</span><span class="p">]</span> <span class="o">=</span> <span class="n">session</span> <span class="o">=</span> <span class="n">session_cls</span><span class="p">()</span>
  163. <span class="k">return</span> <span class="n">session</span>
  164. <span class="k">def</span> <span class="nf">worker_teardown</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">worker_ctx</span><span class="p">):</span>
  165. <span class="n">sess</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sessions</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">worker_ctx</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
  166. <span class="k">if</span> <span class="n">sess</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
  167. <span class="n">sess</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  168. </pre></div>
  169. <p>The actual implementation is only a tiny bit more complicated, and that is
  170. basically just a bit of extra code to support different database URLs for
  171. different services and declarative bases. Overall the concept is the same
  172. however. When the dependency is needed, a connection to the database is
  173. established and when the worker shuts down, the session is closed.</p>
  174. <h2>Concurrency and Parallelism</h2>
  175. <p>What makes nameko interesting is that scales out really well through the
  176. use of AMQP and eventlet. First of all, when nameko starts a service
  177. container it uses eventlet to patch up the Python interpreter to support
  178. green concurrency. This allows a service container to become quite
  179. concurrent to do multiple things at once. This is very useful when a
  180. service waits on another service as threads in Python are a very
  181. disappointing story. As this however largely eliminates the possibility
  182. of true parallelism it becomes necessary to start multiple instances of
  183. services to scale up. Thanks to the use of AMQP however, this becomes a
  184. very transparent process. For as long as services do not need to store
  185. local state, it becomes very trivial to run as many of those service
  186. containers as necessary.</p>
  187. <h2>My Take On It</h2>
  188. <p>Nameko as it stands has all the right principles for building a platform
  189. out of small services and it's probably the best Open Source solution for
  190. this problem in the Python world so far.</p>
  191. <p>It's a bit disappointing that Python's async story is so diverging between
  192. different Python versions and frameworks, but eventlet and gevent are by
  193. far the cleanest and most practical implementations, so for most intents
  194. and purposes the eventlet base in nameko is probably the best you can
  195. currently get for async IO. Fear not though, Nameko 2.0 now also runs on
  196. Python3.</p>
  197. <p>If you haven't tried this sort of service setup yet, you might want to
  198. give Nameko a try.</p>