找了很久都没有找到 Python 合适的 RPC 服务治理框架, 学习了下 motan-go 的源码, 自己造了个轮子, 努力更新中
https://github.com/zhu327/doge
Doge
Doge is a Python RPC framework like Alibaba Dubbo and Weibo Motan.
Features
- 服务治理, 服务注册, 服务发现
- 高可用策略, failover, backupRequestHA
- 负载均衡策略, RandomLB, RoundrobinLB
- 限流策略, gevent Pool
Quick Start
Installation
pip install dogerpc 你可以在examples找到以下实例
Doge server
- 新建 server 端配置文件
{ "registry": { // 注册中心 "protocol": "etcd", // 注册协议, 支持 etcd 与 direct, 默认 etcd "host": "127.0.0.1", // 注册中心 host "port": 2379, // 注册中心 port // "address": "127.0.0.1:2379,127.0.0.1:4001", // 注册中心地址, 如果有 etcd 集群, 可配置多个 node "ttl": 10 // etcd 注册 ttl, 用于 server 的心跳检查, 默认 10s }, "service": { "name": "test", // 服务名称 "node": "n1", // 节点名称 "host": "127.0.0.1", // 服务暴露 ip "port": 4399, // 服务暴露 port "limitConn": 100 // 服务最大连接数, 可选, 默认不限制 } } - 定义 RPC methods 类, 启动服务
# coding: utf-8 from gevent import monkey monkey.patch_socket() # 依赖 gevent import logging logging.basicConfig(level=logging.DEBUG) from doge.rpc.server import new_server # 定义 rpc 方法类 class Sum(object): def sum(self, x, y): return x + y if __name__ == '__main__': server = new_server('server.json') # 基于配置文件实例化 server 对象 server.load(Sum) # 加载暴露 rpc 方法类 server.run() # 启动服务并注册节点信息到注册中心 Doge client
- 新建 client 端配置文件
{ "registry": { // 注册中心 "protocol": "etcd", // 注册协议, 支持 etcd 与 direct, 默认 etcd "host": "127.0.0.1", // 注册中心 host "port": 2379, // 注册中心 port // "address": "127.0.0.1:2379,127.0.0.1:4001", // 注册中心地址, 如果有 etcd 集群, 可配置多个 node "ttl": 10 // etcd 注册 ttl, 用于 server 的心跳检查, 默认 10s }, "refer": { "haStrategy": "failover", // 高可用策略, 支持 failover backupRequestHA, 默认 failover "loadbalance": "RoundrobinLB", // 负载均衡策略, 支持 RandomLB RoundrobinLB, 默认 RoundrobinLB } } - 创建 client 并 call 远程方法
# coding: utf-8 from __future__ import print_function from gevent import monkey monkey.patch_socket() import logging logging.basicConfig(level=logging.DEBUG) from doge.rpc.client import Cluster if __name__ == '__main__': cluster = Cluster('client.json') # 基于配置文件实例化 Cluster 对象 client = cluster.get_client("test") # 获取服务名对应的 Client 对象 print(client.call('sum', 1, 2)) # 远程调用服务 Sum 类下的 sum 方法 Requirements
License
Apache License, Version 2.0
