Use Python, RabbitMQ, and Nameko to implement microservices

Source: Internet
Author: User
Tags virtual environment virtualenv rabbitmq docker run

Use Python, RabbitMQ, and Nameko to implement microservices
Guide"Microservices are a new wave"-nowadays, splitting projects into multiple independent and scalable services is the best choice to ensure code evolution. In the world of Python, there is a framework called "Nameko", which makes the implementation of microservices simple and powerful.

In recent years, "microservice architecture" has sprung up. It is used to describe a specific software application design method, which allows an application to be composed of multiple independently deployed services in the form of a service suite. -M. Fowler

To put it simply, the microservice architecture can split your system into multiple small (within a single context) functional blocks responsible for different tasks. They are imperceptible to each other, each of them provides only common directions for Communication (. This point is generally a Message Queue that has defined communication protocols and interfaces.

Imagine that you have a rest api, which has an endpoint (a rest api can have multiple endpoints for processing different types of requests to the same resource) to accept data, in addition, you need to perform some operations on the received data. Therefore, asynchronous implementation of this interface is a better choice than blocking interface callers. You can first return a "OK-your request will be processed later" status, and then complete the operation in the background task.

Similarly, if you want to send a reminder email after the computation is complete without blocking the main process, it would be better to delegate "email sending" to other services.

Scenario Description

Let's create the system and understand it in practice.

Environment

Environment we need:

  • Well-running RabbitMQ (LCTT: RabbitMQ is a popular message queue implementation)
  • Services virtual environment provided by VirtualEnv
  • Virtual API environment provided by VirtualEnv
Rabbit

The simplest way to use RabbitMQ in the development environment is to run its official docker container. Run:

docker run -d --hostname my-rabbit --name some-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management

Access http: // localhost: 15672 in a browser. If you can use the guest: guest verification information to log on to the RabbitMQ control panel, it indicates that it has been running in your development environment.

Service Environment

Now let's create microservices to meet our task needs. One service is used to execute computing tasks, and the other is used to send emails. Follow these steps:

Create the project root directory in Shell

$ mkdir myproject$ cd myproject

Use virtualenv to create and activate a virtual environment (you can also use virtualenv-wrapper)

$ virtualenv service_env$ source service_env/bin/activate

Install the nameko framework and yagmail

(service_env)$ pip install nameko(service_env)$ pip install yagmail
Service Code

Now we have prepared the virtual environment provided by virtualenv (we can imagine that our service runs on an independent server and our API runs on another server ), next, let's code and implement the nameko RPC service.

We will place these two services in the same python module. If you are willing to, you can also put them in separate modules and run them as different services.

In the file named service. py

Import yagmailfrom nameko. rpc import rpc, RpcProxyclass Mail (object): name = "mail" @ rpc def send (self, to, subject, contents): yag = yagmail. SMTP ('myname @ gmail.com ', 'mypassword') # Read the above verification information from a safe place # tip: You can check the Dynaconf setting module. send (to =. encode ('utf-8), subject = subject. encode ('utf-8), contents = [contents. encode ('utf-8)]) class Compute (object): name = "compute" mail = RpcProxy ('mail') @ rpc def co Mpute (self, operation, value, other, email): operations = {'sum': lambda x, y: int (x) + int (y), 'mul ': lambda x, y: int (x) * int (y), 'div ': lambda x, y: int (x)/int (y), 'sub ': lambda x, y: int (x)-int (y)} try: result = operations [operation] (value, other) except t Exception as e: self. mail. send. async (email, "An error occurred", str (e) raise else: self. mail. send. async (email, "Your operation is com Plete! "," The result is: % s "% result) return result

Now we have defined two services with the above Code. Let's run the Nameko RPC service.

Note: We will start and run it on the console. However, in the production environment, we recommend that you use supervisord to replace console commands.

Start and run the service in Shell

(service_env)$ nameko run service --broker amqp://guest:guest@localhoststarting services: mail, computeConnected to amqp://guest:**@127.0.0.1:5672//Connected to amqp://guest:**@127.0.0.1:5672//
Test

In another Shell (using the same virtual environment), use nameko shell for testing:

(service_env)$ nameko shell --broker amqp://guest:guest@localhostNameko Python 2.7.9 (default, Apr  2 2015, 15:33:21) [GCC 4.9.2] shell on linux2Broker: amqp://guest:guest@localhost>>>

Now that you are in the RPC client, the Shell test is performed through the n. rpc object. The usage is as follows:

>>> n.rpc.mail.send("name@email.com", "testing", "Just testing")

The above code will send an email, and we can also call the computing service to test it. It should be noted that this test will also include asynchronous mail sending.

>>> n.rpc.compute.compute('sum', 30, 10, "name@email.com")40>>> n.rpc.compute.compute('sub', 30, 10, "name@email.com")20>>> n.rpc.compute.compute('mul', 30, 10, "name@email.com")300>>> n.rpc.compute.compute('div', 30, 10, "name@email.com")3
Call microservices in Apis

In another Shell (or even another server), prepare the API environment.

Use virtualenv to create and activate a virtual environment (you can also use virtualenv-wrapper)

$ virtualenv api_env$ source api_env/bin/activate

Install Nameko, Flask, and Flasgger

(api_env)$ pip install nameko(api_env)$ pip install flask(api_env)$ pip install flasgger

Note: yagmail is not required in APIs, because it is the service's responsibility to process emails here.

Create an api. py file containing the following content:

from flask import Flask, requestfrom flasgger import Swaggerfrom nameko.standalone.rpc import ClusterRpcProxyapp = Flask(__name__)Swagger(app)CONFIG = {'AMQP_URI': "amqp://guest:guest@localhost"}@app.route('/compute', methods=['POST'])def compute():    """    Micro Service Based Compute and Mail API    This API is made with Flask, Flasgger and Nameko    ---    parameters:      - name: body        in: body        required: true        schema:          id: data          properties:            operation:              type: string              enum:                - sum                - mul                - sub                - div            email:              type: string            value:              type: integer            other:              type: integer    responses:      200:        description: Please wait the calculation, you'll receive an email with results    """    operation = request.json.get('operation')    value = request.json.get('value')    other = request.json.get('other')    email = request.json.get('email')    msg = "Please wait the calculation, you'll receive an email with results"    subject = "API Notification"    with ClusterRpcProxy(CONFIG) as rpc:        # asynchronously spawning and email notification        rpc.mail.send.async(email, subject, msg)        # asynchronously spawning the compute task        result = rpc.compute.compute.async(operation, value, other, email)        return msg, 200app.run(debug=True)

Run this file on another shell or server

(api_env) $ python api.py  * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Access the url http: // localhost: 5000/apidocs/index.html to view the Flasgger interface, you can use it to interact with APIs and publish tasks to queues for service consumption.


Note: You can view the Service Running logs and print information and error information in shell. You can also access the RabbitMQ Control Panel to view the processing status of messages in the queue.
The Nameko framework also provides us with many advanced features that you can get more information from https://nameko.readthedocs.org/en/stable.

Don't just look at it, just pick up your sleeves and implement microservices!

From: https://linux.cn/article-7584-1.html
Address: http://www.linuxprobe.com/python-rabbitmq-nameko.html


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.