Python中如何使用thrift和zookeeper搭建一个简单的微服务demo
https://github.com/zgbgx/MicroserviceUseThriftAndZookeeper
老板想要 spring cloud 的那样的微服务用于 机器学习模型调用和爬虫开发,eureka 的 python api 不能看,折腾两天 搞了 一个基本 demo,用的 thrift 和 zookeeper,两个的 java 和 python api 都算完善。
搞了个简单的 demo,只有个基本思路
大佬们 有兴趣的可以看下,觉得可以的 给个 star
Python中如何使用thrift和zookeeper搭建一个简单的微服务demo
3 回复
我来帮你用Python搭建一个基于Thrift和ZooKeeper的微服务demo。这个示例包含服务端和客户端,使用ZooKeeper作为服务注册中心。
首先安装依赖:
pip install thrift kazoo
1. 创建Thrift接口定义文件(calculator.thrift)
namespace py calculator
service CalculatorService {
i32 add(1:i32 num1, 2:i32 num2),
i32 subtract(1:i32 num1, 2:i32 num2)
}
2. 生成Python代码
thrift -gen py calculator.thrift
3. 服务端代码(server.py)
import json
from kazoo.client import KazooClient
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
from gen_py.calculator import CalculatorService
class CalculatorHandler:
def add(self, num1, num2):
return num1 + num2
def subtract(self, num1, num2):
return num1 - num2
def register_service(zk_hosts, service_name, host, port):
"""向ZooKeeper注册服务"""
zk = KazooClient(hosts=zk_hosts)
zk.start()
# 创建服务节点路径
service_path = f"/services/{service_name}"
if not zk.exists(service_path):
zk.create(service_path, makepath=True)
# 创建临时节点存储服务地址
node_data = json.dumps({"host": host, "port": port}).encode()
node_path = f"{service_path}/node_"
zk.create(node_path, node_data, ephemeral=True, sequence=True)
print(f"服务注册成功: {node_path}")
return zk
def main():
handler = CalculatorHandler()
processor = CalculatorService.Processor(handler)
transport = TSocket.TServerSocket(host='127.0.0.1', port=9090)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
# 向ZooKeeper注册服务
zk = register_service(
zk_hosts='127.0.0.1:2181',
service_name='calculator',
host='127.0.0.1',
port=9090
)
try:
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
print("Calculator服务启动在端口 9090...")
server.serve()
finally:
zk.stop()
if __name__ == '__main__':
main()
4. 客户端代码(client.py)
import json
from kazoo.client import KazooClient
from thrift.transport import TSocket, TTransport
from thrift.protocol import TBinaryProtocol
from gen_py.calculator import CalculatorService
def discover_service(zk_hosts, service_name):
"""从ZooKeeper发现服务"""
zk = KazooClient(hosts=zk_hosts)
zk.start()
service_path = f"/services/{service_name}"
if not zk.exists(service_path):
raise Exception(f"服务 {service_name} 不存在")
# 获取所有服务节点
children = zk.get_children(service_path)
if not children:
raise Exception(f"服务 {service_name} 没有可用实例")
# 选择第一个可用节点(简单负载均衡)
node_path = f"{service_path}/{children[0]}"
data, _ = zk.get(node_path)
service_info = json.loads(data.decode())
zk.stop()
return service_info['host'], service_info['port']
def main():
# 发现服务
host, port = discover_service('127.0.0.1:2181', 'calculator')
# 创建Thrift客户端连接
transport = TSocket.TSocket(host, port)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = CalculatorService.Client(protocol)
transport.open()
try:
# 调用服务
result1 = client.add(10, 20)
result2 = client.subtract(30, 15)
print(f"10 + 20 = {result1}")
print(f"30 - 15 = {result2}")
finally:
transport.close()
if __name__ == '__main__':
main()
5. 运行步骤
- 启动ZooKeeper服务
- 运行服务端:
python server.py - 运行客户端:
python client.py
这个demo展示了微服务的核心概念:服务注册、服务发现和RPC调用。服务端启动时向ZooKeeper注册自己的地址,客户端通过查询ZooKeeper获取可用服务地址进行调用。
总结:Thrift负责RPC通信,ZooKeeper负责服务注册与发现。
…为啥发新帖 显示 四小时前
用 Zookeeper 干啥

