python利用第三方模块nameko使用rpc模块实现微服务

客户端
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
from nameko.standalone.rpc import ClusterRpcProxy
CONFIG = {'AMQP_URI': "amqp://mq用户名:mq密码@ip地址"}
def xx_rpc(name):
try:
with ClusterRpcProxy(CONFIG) as rpc:
data = rpc.agent_device_log_service.hello1(name)
except Exception as e:
return False
return data

服务端
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
from nameko.rpc import rpc

class DeviceService:
    name = "agent_device_log_service    @rpc    def hello1(self,name):
        return "hello world{name}"

@rpc
def hello2(self):
return "hello world2"


配置文件
config.yaml
AMQP_URI: 'pyamqp://账号:密码@ip地址'
WEB_SERVER_ADDRESS: '0.0.0.0:9000'
rpc_exchange: 'nameko-rpc'
max_workers: 10
parent_calls_tracked: 10

LOGGING:
version: 1
handlers:
console:
class: logging.StreamHandler
root:
level: DEBUG
handlers: [console]
使用nameko必须使用rabbitmq 没有下载可以参考 ---》 https://www.cnblogs.com/yz-w/p/15132156.html

需要在文件__init__导入DeviceService 例: from src.entry import DeviceService
切换src上级目录至entry命令行:nameko run --config config.yaml src:DeviceService



 
 
posted @ 2021-08-12 14:02  wyz_1  阅读(335)  评论(0编辑  收藏  举报