|
1 | | -## Python Client For Apache Dubbo |
2 | | -## Achieve load balancing on the client side、auto discovery service function with Zookeeper |
3 | | -### Python calls the Dubbo interface's jsonrpc protocol |
4 | | -Please use dubbo-rpc-jsonrpc and configure protocol in Dubbo for jsonrpc protocol |
5 | | -*Reference* [https://github.com/apache/incubator-dubbo-rpc-jsonrpc](https://github.com/apache/incubator-dubbo-rpc-jsonrpc) |
6 | | - |
7 | | -### Installation |
8 | | - |
9 | | -Download code |
10 | | -python setup.py install |
11 | | -pip install |
12 | | -pip install dubbo-client==1.0.0b5 |
13 | | -Git install |
14 | | -pip install git+[http://git.dev.qianmi.com/tda/dubbo-client-py.git@1.0.0b5](http://git.dev.qianmi.com/tda/dubbo-client-py.git@1.0.0b5) |
15 | | -or |
16 | | -pip install git+[https://github.com/qianmiopen/dubbo-client-py.git@1.0.0b5](https://github.com/qianmiopen/dubbo-client-py.git@1.0.0b5) |
17 | | - |
18 | | -### Load balancing on the client side, service discovery |
19 | | - |
20 | | -Get the registration information of the service through the zookeeper of the registry. |
21 | | -Dubbo-client-py supports configuring multiple zookeeper service addresses. |
22 | | -"host":"192.168.1.183:2181,192.168.1.184:2181,192.168.1.185:2181" |
23 | | -Then the load balancing algorithm is implemented by proxy, and the server is called. |
24 | | -Support Version and Group settings. |
25 | | -### Example |
26 | | - config = ApplicationConfig('test_rpclib') |
27 | | - service_interface = 'com.ofpay.demo.api.UserProvider' |
28 | | - #Contains a connection to zookeeper, which needs caching. |
29 | | - registry = ZookeeperRegistry('192.168.59.103:2181', config) |
30 | | - user_provider = DubboClient(service_interface, registry, version='1.0') |
31 | | - for i in range(1000): |
32 | | - try: |
33 | | - print user_provider.getUser('A003') |
34 | | - print user_provider.queryUser( |
35 | | - {u'age': 18, u'time': 1428463514153, u'sex': u'MAN', u'id': u'A003', u'name': u'zhangsan'}) |
36 | | - print user_provider.queryAll() |
37 | | - print user_provider.isLimit('MAN', 'Joe') |
38 | | - print user_provider('getUser', 'A005') |
39 | | - |
40 | | - except DubboClientError, client_error: |
41 | | - print client_error |
42 | | - time.sleep(5) |
43 | | - |
44 | | -### TODO |
45 | | -Optimize performance, minimize the impact of service upper and lower lines. |
46 | | -Support Retry parameters |
47 | | -Support weight call |
48 | | -Unit test coverage |
49 | | -### Licenses |
50 | | -Apache License |
51 | | -### Thanks |
52 | | -Thank @jingpeicomp for being a Guinea pig. It has been running normally for several months in the production environment. Thank you! |
| 1 | +# Apache Dubbo for python |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +<p align="center"> |
| 8 | + <img src="https://cn.dubbo.apache.org/imgs/nav_logo2.png" alt="Logo" width="40%" /> |
| 9 | +</p> |
| 10 | + |
| 11 | +Apache Dubbo is an easy-to-use, high-performance WEB and RPC framework with builtin service discovery, traffic management, observability, security features, tools and best practices for building enterprise-level microservices. |
| 12 | + |
| 13 | +Dubbo-python is a Python implementation of the [triple protocol](https://dubbo.apache.org/zh-cn/overview/reference/protocols/triple-spec/) (a protocol fully compatible with gRPC and friendly to HTTP) and various features designed by Dubbo for constructing microservice architectures. |
| 14 | + |
| 15 | +Visit [the official website](https://dubbo.apache.org/) for more information. |
| 16 | + |
| 17 | +### 🚧 Early-Stage Project 🚧 |
| 18 | + |
| 19 | +> **Disclaimer:** This project is in the early stages of development. Features are subject to change, and some components may not be fully stable. Contributions and feedback are welcome as the project evolves. |
| 20 | +
|
| 21 | +## Features |
| 22 | + |
| 23 | +- **Service Discovery**: Zookeeper |
| 24 | +- **Load Balance**: Random |
| 25 | +- **RPC Protocols**: Triple(gRPC compatible and HTTP-friendly) |
| 26 | +- **Transport**: asyncio(uvloop) |
| 27 | +- **Serialization**: Customizable(protobuf, json...) |
| 28 | + |
| 29 | + |
| 30 | +## Getting started |
| 31 | + |
| 32 | +Before you begin, ensure that you have **`python 3.11+`**. Then, install Dubbo-Python in your project using the following steps: |
| 33 | + |
| 34 | +```shell |
| 35 | +git clone https://github.com/apache/dubbo-python.git |
| 36 | +cd dubbo-python && pip install . |
| 37 | +``` |
| 38 | + |
| 39 | +Get started with Dubbo-Python in just 5 minutes by following our [Quick Start Guide](https://github.com/apache/dubbo-python/tree/main/samples). |
| 40 | + |
| 41 | +It's as simple as the following code snippet. With just a few lines of code, you can launch a fully functional point-to-point RPC service : |
| 42 | + |
| 43 | +1. Build and start the Server |
| 44 | + |
| 45 | + ```python |
| 46 | + import dubbo |
| 47 | + from dubbo.configs import ServiceConfig |
| 48 | + from dubbo.proxy.handlers import RpcServiceHandler, RpcMethodHandler |
| 49 | + |
| 50 | + |
| 51 | + def handle_unary(request): |
| 52 | + s = request.decode("utf-8") |
| 53 | + print(f"Received request: {s}") |
| 54 | + return (s + " world").encode("utf-8") |
| 55 | + |
| 56 | + |
| 57 | + if __name__ == "__main__": |
| 58 | + # build a method handler |
| 59 | + method_handler = RpcMethodHandler.unary(handle_unary) |
| 60 | + # build a service handler |
| 61 | + service_handler = RpcServiceHandler( |
| 62 | + service_name="org.apache.dubbo.samples.HelloWorld", |
| 63 | + method_handlers={"unary": method_handler}, |
| 64 | + ) |
| 65 | + |
| 66 | + service_config = ServiceConfig(service_handler) |
| 67 | + |
| 68 | + # start the server |
| 69 | + server = dubbo.Server(service_config).start() |
| 70 | + |
| 71 | + input("Press Enter to stop the server...\n") |
| 72 | + ``` |
| 73 | + |
| 74 | +2. Build and start the Client |
| 75 | + |
| 76 | + ```python |
| 77 | + import dubbo |
| 78 | + from dubbo.configs import ReferenceConfig |
| 79 | + |
| 80 | + |
| 81 | + class UnaryServiceStub: |
| 82 | + |
| 83 | + def __init__(self, client: dubbo.Client): |
| 84 | + self.unary = client.unary(method_name="unary") |
| 85 | + |
| 86 | + def unary(self, request): |
| 87 | + return self.unary(request) |
| 88 | + |
| 89 | + |
| 90 | + if __name__ == "__main__": |
| 91 | + reference_config = ReferenceConfig.from_url( |
| 92 | + "tri://127.0.0.1:50051/org.apache.dubbo.samples.HelloWorld" |
| 93 | + ) |
| 94 | + dubbo_client = dubbo.Client(reference_config) |
| 95 | + |
| 96 | + unary_service_stub = UnaryServiceStub(dubbo_client) |
| 97 | + |
| 98 | + result = unary_service_stub.unary("hello".encode("utf-8")) |
| 99 | + print(result.decode("utf-8")) |
| 100 | + ``` |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +## License |
| 105 | + |
| 106 | +Apache Dubbo-python software is licensed under the Apache License Version 2.0. See |
| 107 | +the [LICENSE](https://github.com/apache/dubbo-python/blob/main/LICENSE) file for details. |
0 commit comments