����Linux�Ż�
Linux����

��Python��RabbitMQ��Namekoʵ��΢����

����ʱ��:2016-07-18 09:33:45��Դ:linux.cn����:LCTT
"΢������һ�����˳�" - ����񣬽���Ŀ��ֳɶ�������ġ�����չ�ķ����DZ��ϴ����ݱ�����ѡ���� Python ��������и����� “Nameko” �Ŀ�ܣ�����΢�����ʵ�ֱ�ü򵥲���ǿ��
 
΢����
������ļ����“΢����ܹ�”��������ӿ�֡�����������һ���ض�������Ӧ����Ʒ�ʽ�����ַ�ʽʹ��Ӧ�ÿ����ɶ����������ķ����Է����׼�����ʽ��ɡ� - M. Fowler
�Ƽ���λ��һ�� Fowler �����£�http://martinfowler.com/articles/microservices.html�� �������������ԭ����
 
�ðɣ�����������ζ��ʲô�أ�
����˵��΢����ܹ����Խ����ϵͳ��ֳɶ������ͬ�����С�ģ���һ�������ڣ����ܿ飬���DZ˴˻��޸�֪������ֻ�ṩ����ͨѶ��ͨ��ָ�����ָ��ͨ�����Ѿ���ͨѶЭ��ͽӿڶ���õ���Ϣ���С�
 
���������ṩһ����ʵ����
�����Ĵ������ͨ�� github: http://github.com/rochacbruno/nameko-example ���ʣ��鿴 service �� api �ļ��п��Ի�ȡ������Ϣ��
����һ�£�����һ�� REST API ����� API ��һ���˵㣨LCTT ��ע��REST ���� API �����ж���˵����ڴ�����ͬһ��Դ�IJ�ͬ���͵����������������ݣ���������Ҫ�����յ������ݽ���һЩ���㹤������ô��������ӿڵ����ߵ�������˵���첽ʵ�ִ˽ӿ���һ�����õ�ѡ��������ȸ��û�����һ�� "OK - ��������Ժ�ᴦ��" ��״̬��Ȼ���ں�̨������������㡣
ͬ�����������Ҫ�ڲ����������̵�ǰ���£��ڼ�����ɺ���һ�������ʼ�����ô��“�ʼ�����”ί�и���������ȥ�������һЩ��
 
��������
��Python��RabbitMQ��Namekoʵ��΢����
 
�ô���˵��
�����ǽ�ϵͳ������������ʵ������������
 
����
������Ҫ�Ļ�����
�������õ� RabbitMQ��LCTT ��ע��RabbitMQ ��һ�����е���Ϣ����ʵ�֣�
�� VirtualEnv �ṩ�� Services ���⻷��
�� VirtualEnv �ṩ�� API�����⻷��
 
Rabbit
�ڿ���������ʹ�� RabbitMQ ��򵥵ķ�ʽ����������ٷ��� docker �����������Ѿ�ӵ�� Docker ������£����У�
docker run -d --hostname my-rabbit --name some-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management
��������з��� http://localhost:15672 ������ܹ�ʹ�� guest:guest ��֤��Ϣ��¼ RabbitMQ �Ŀ�����壬˵�����Ѿ�����Ŀ������������������ˡ�
��Python��RabbitMQ��Namekoʵ��΢����
 
���񻷾�
���������Ǵ���΢�������������ǵ�������Ҫ������һ����������ִ�м���������һ�����������ʼ��������²���ִ�У�
�� Shell �д�����Ŀ�ĸ�Ŀ¼
$ mkdir myproject
$ cd myproject
�� virtualenv ���ߴ������Ҽ���һ�����⻷������Ҳ����ʹ�� virtualenv-wrapper��
$ virtualenv service_env
$ source service_env/bin/activate
��װ nameko ��ܺ� yagmail
(service_env)$ pip install nameko
(service_env)$ pip install yagmail
 
����Ĵ���
���������Ѿ�׼������ virtualenv ���ṩ�����⻷����������������ǵķ�����������һ�������������ϵģ������ǵ� API ��������һ���������ϣ��������������DZ��룬ʵ�� nameko �� RPC ����
���ǻὫ�������������ͬһ�� python ģ���У���Ȼ��������⣬Ҳ���԰����Ƿ��ڵ�����ģ���ﲢ�ҵ��ɲ�ͬ�ķ������У�
����Ϊ service.py ���ļ���
import yagmail
from nameko.rpc import rpc, RpcProxy
class Mail(object):
name = "mail"
@rpc
def send(self, to, subject, contents):
yag = yagmail.SMTP('myname@gmail.com', 'mypassword')
# ���ϵ���֤��Ϣ��Ӱ�ȫ�ĵط����ж�ȡ
# ��ʿ: ����ȥ���� Dynaconf ����ģ��
yag.send(to=to.encode('utf-8), 
subject=subject.encode('utf-8), 
contents=[contents.encode('utf-8)])
class Compute(object):
name = "compute"
mail = RpcProxy('mail')
@rpc
def compute(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 Exception as e:
self.mail.send.async(email, "An error occurred", str(e))
raise
else:
self.mail.send.async(
email, 
"Your operation is complete!", 
"The result is: %s" % result
)
return result
���������Ѿ������ϴ��붨����������������������ǽ� Nameko RPC service ����������
ע�⣺���ǻ��ڿ���̨�����������������������������У�������ʹ�� supervisord �������̨���
�� Shell �������������
(service_env)$ nameko run service --broker amqp://guest:guest@localhost
starting services: mail, compute
Connected to amqp://guest:**@127.0.0.1:5672//
Connected to amqp://guest:**@127.0.0.1:5672//
 
����
������һ�� Shell �У�ʹ����ͬ�����⻷�������� nameko shell ���в��ԣ�
(service_env)$ nameko shell --broker amqp://guest:guest@localhost
Nameko Python 2.7.9 (default, Apr  2 2015, 15:33:21) 
[GCC 4.9.2] shell on linux2
Broker: amqp://guest:guest@localhost
>>>
�������Ѿ����� RPC �ͻ������ˣ�Shell �IJ��Թ�����ͨ�� n.rpc ���������еģ�����ʹ�÷������£�
>>> n.rpc.mail.send("name@email.com", "testing", "Just testing")
�ϱߵĴ���ᷢ��һ���ʼ�������ͬ�����Ե��ü�����������в��ԡ���Ҫע����ǣ��˲��Ի��ḽ�������첽���ʼ����͡�
>>> 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
 
�� API �е���΢����
������һ�� Shell �У���������������һ̨�������ϣ���׼���� API ������
�� virtualenv ���ߴ������Ҽ���һ�����⻷������Ҳ����ʹ�� virtualenv-wrapper��
$ virtualenv api_env
$ source api_env/bin/activate
��װ Nameko�� Flask �� Flasgger
(api_env)$ pip install nameko
(api_env)$ pip install flask
(api_env)$ pip install flasgger
ע�⣺ �� API �в�����Ҫ yagmail ����Ϊ����������ʼ��Ƿ����ְ��
���������������ݵ� api.py �ļ���
from flask import Flask, request
from flasgger import Swagger
from nameko.standalone.rpc import ClusterRpcProxy
app = 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, 200
app.run(debug=True)
�������� shell ���߷����������д��ļ�
(api_env) $ python api.py 
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Ȼ����� http://localhost:5000/apidocs/index.html ��� url���Ϳ��Կ��� Flasgger �Ľ����ˣ����������Խ��� API �Ľ��������Է������񵽶����Թ�����������ѡ�
��Python��RabbitMQ��Namekoʵ��΢����
ע�⣺������� shell �в鿴�������������־����ӡ��Ϣ�ʹ�����Ϣ��Ҳ���Է��� RabbitMQ ����������鿴��Ϣ�ڶ����еĴ��������
Nameko ��ܻ�Ϊ�����ṩ�˺ܶ�߼����ԣ�����Դ� https://nameko.readthedocs.org/en/stable/ ��ȡ�������Ϣ��
 
�������ø��µ�ַ��//m.ajphoenix.com/linux/22471.html