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:
Use virtualenv to create and activate a virtual environment (you can also use virtualenv-wrapper)
Service CodeNow 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//
TestIn 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 ApisIn 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