Quick Start

Dubbo-python Quick Start

This guide will help you get started with Dubbo in Python with a simple working example. See the full example here.

1. Prerequisites

  • Python 3.11 or higher
  • Compatible pip version

2. Install Dubbo-Python

  1. git clone https://github.com/apache/dubbo-python.git
  2. cd dubbo-python && pip install .

3. Build a Dubbo Service

Building the Dubbo Server

  1. import dubbo
  2. from dubbo.configs import ServiceConfig
  3. from dubbo.proxy.handlers import RpcMethodHandler, RpcServiceHandler
  4. class UnaryServiceServicer:
  5. def say_hello(self, message: bytes) -> bytes:
  6. print(f"Received message from client: {message}")
  7. return b"Hello from server"
  8. def build_service_handler():
  9. # build a method handler
  10. method_handler = RpcMethodHandler.unary(
  11. method=UnaryServiceServicer().say_hello, method_name="unary"
  12. )
  13. # build a service handler
  14. service_handler = RpcServiceHandler(
  15. service_name="org.apache.dubbo.samples.HelloWorld",
  16. method_handlers=[method_handler],
  17. )
  18. return service_handler
  19. if __name__ == "__main__":
  20. # build service config
  21. service_handler = build_service_handler()
  22. service_config = ServiceConfig(
  23. service_handler=service_handler, host="127.0.0.1", port=50051
  24. )
  25. # start the server
  26. server = dubbo.Server(service_config).start()
  27. input("Press Enter to stop the server...\n")

Building the Dubbo Client

  1. import dubbo
  2. from dubbo.configs import ReferenceConfig
  3. class UnaryServiceStub:
  4. def __init__(self, client: dubbo.Client):
  5. self.unary = client.unary(method_name="unary")
  6. def say_hello(self, message: bytes) -> bytes:
  7. return self.unary(message)
  8. if __name__ == "__main__":
  9. # Create a client
  10. reference_config = ReferenceConfig.from_url(
  11. "tri://127.0.0.1:50051/org.apache.dubbo.samples.HelloWorld"
  12. )
  13. dubbo_client = dubbo.Client(reference_config)
  14. unary_service_stub = UnaryServiceStub(dubbo_client)
  15. # Call the remote method
  16. result = unary_service_stub.say_hello(b"Hello from client")
  17. print(result)

4. Run the Dubbo Service

Navigate to the Quick Start example directory:

  1. cd samples/helloworld

Run the Server:

  1. python server.py

Run the Client:

  1. python client.py

5. Code Walkthrough

5.1 Exposing the Service

First, define the service methods you want to expose, as shown below:

  1. class UnaryServiceServicer:
  2. def say_hello(self, message: bytes) -> bytes:
  3. print(f"Received message from client: {message}")
  4. return b"Hello from server"

Next, we use the RpcMethodHandler constructor methods (unary, client_stream, server_stream, bi_stream) to build the method and define its properties. Key parameters include:

  • callable_method: The method itself.
  • method_name: The exposed method name, defaulting to the method’s own name.
  • request_deserializer: The deserialization function for requests (or method parameters). By default, it returns raw bytes.
  • response_serializer: The serialization function for responses (or return values). If not set, the return value must be in bytes, bytearray, or memoryview.

If the method’s parameters and return values are in bytes, we can get a minimal RpcMethodHandler as follows:

  1. method_handler = RpcMethodHandler.unary(
  2. method=UnaryServiceServicer().say_hello, method_name="unary"
  3. )

Next, we use RpcServiceHandler to build the service, with parameters including:

  • service_name: The name of the exposed service.
  • method_handlers: The set of methods for the service, as a List[RpcMethodHandler].

This results in the following RpcServiceHandler:

  1. service_handler = RpcServiceHandler(
  2. service_name="org.apache.dubbo.samples.HelloWorld",
  3. method_handlers=[method_handler],
  4. )

Finally, we configure ServiceConfig, passing in the RpcServiceHandler as well as the host (address), port, protocol, and other details for the service. Passing this to dubbo.Server exposes the service, as shown below:

  1. service_config = ServiceConfig(
  2. service_handler=service_handler, host="127.0.0.1", port=50051
  3. )
  4. # start the server
  5. server = dubbo.Server(service_config).start()

5.2 Referencing the Service

To reference the service on the Server, we first configure ReferenceConfig with the following parameters:

  • protocol: The protocol used by the Server, such as tri (Triple).
  • service: The name of the referenced service.
  • host: The service address.
  • port: The service port.

Additionally, we can use ReferenceConfig.from_url to configure ReferenceConfig by parsing a URL string automatically. Once configured, we can create a dubbo.Client.

Example:

  1. reference_config = ReferenceConfig.from_url(
  2. "tri://127.0.0.1:50051/org.apache.dubbo.samples.HelloWorld"
  3. )
  4. dubbo_client = dubbo.Client(reference_config)

Then, we can use the unary, client_stream, server_stream, and bi_stream methods of dubbo.Client to construct and call the service. Parameters for these methods are:

  • method_name: The name of the referenced method.
  • request_serializer: The serialization function for the request (or method parameters). If not set, ensure that the return value is bytes, bytearray, or memoryview.
  • response_deserializer: The deserialization function for the response (or return value). By default, it returns raw bytes.

This lets us use the RpcCallable object returned by these methods to make the service call. Example:

  1. class UnaryServiceStub:
  2. def __init__(self, client: dubbo.Client):
  3. self.unary = client.unary(method_name="unary")
  4. def say_hello(self, message: bytes) -> bytes:
  5. return self.unary(message)

6 More examples

Custom Serialization

Dubbo-python Custom Serialization

Streaming Communication Mode

Implementing a Streaming Communication Model with Dubbo-Python


Feedback

Was this page helpful?

Yes No

Last modified November 7, 2024: docs: add python sdk manual (#3056) (26c58b388ff)