自定义序列化

Dubbo Python 自定义序列化

在此查看完整示例

Python 是一种动态语言,其灵活性使得在其他语言中设计通用的序列化层具有挑战性。因此,我们移除了框架级别的序列化层,而是提供接口,允许用户根据需求自行实现(因为用户更了解他们要传输的数据格式)。

序列化通常分为两个部分:序列化和反序列化。我们为这些函数定义了类型,自定义的序列化/反序列化函数必须遵循这些 “格式”。

对于序列化函数,我们规定:

  1. # A function that takes any number of arguments and returns data of type bytes
  2. SerializingFunction = Callable[..., bytes]

对于反序列化函数,我们规定:

  1. # A function that takes an argument of type bytes and returns data of any type
  2. DeserializingFunction = Callable[[bytes], Any]

下面,我将演示如何使用 protobufjson 进行序列化。

protobuf

  1. 有关定义和编译 protobuf 文件的详细说明,请参阅 protobuf tutorial

  2. 在Client的unaryclient_streamserver_streambi_stream 方法中通过设置request_serializerresponse_deserializer 设置对应的序列化和反序列化函数。

    1. class GreeterServiceStub:
    2. def __init__(self, client: dubbo.Client):
    3. self.unary = client.unary(
    4. method_name="sayHello",
    5. request_serializer=greeter_pb2.GreeterRequest.SerializeToString,
    6. response_deserializer=greeter_pb2.GreeterReply.FromString,
    7. )
    8. def say_hello(self, request):
    9. return self.unary(request)
    10. if __name__ == "__main__":
    11. reference_config = ReferenceConfig.from_url(
    12. "tri://127.0.0.1:50051/org.apache.dubbo.samples.data.Greeter"
    13. )
    14. dubbo_client = dubbo.Client(reference_config)
    15. stub = GreeterServiceStub(dubbo_client)
    16. result = stub.say_hello(greeter_pb2.GreeterRequest(name="Dubbo-python"))
    17. print(f"Received reply: {result.message}")
  3. 同理在Server中设置对应的序列化和反序列化函数。

    1. class GreeterServiceServicer:
    2. def say_hello(self, request):
    3. print(f"Received request: {request}")
    4. return greeter_pb2.GreeterReply(message=f"Hello, {request.name}")
    5. def build_service_handler():
    6. # build a method handler
    7. method_handler = RpcMethodHandler.unary(
    8. GreeterServiceServicer().say_hello,
    9. method_name="sayHello",
    10. request_deserializer=greeter_pb2.GreeterRequest.FromString,
    11. response_serializer=greeter_pb2.GreeterReply.SerializeToString,
    12. )
    13. # build a service handler
    14. service_handler = RpcServiceHandler(
    15. service_name="org.apache.dubbo.samples.data.Greeter",
    16. method_handlers=[method_handler],
    17. )
    18. return service_handler
    19. if __name__ == "__main__":
    20. # build a service handler
    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")

Json

我们已经使用 protobuf 实现了单参数的序列化和反序列化。现在,我将演示如何编写一个支持多参数的 Json 序列化和反序列化函数,从而实现多参数方法的远程调用。

  1. 安装orjson

    1. pip install orjson
  2. 定义并设置Client的序列化和反序列化函数

    1. def request_serializer(name: str, age: int) -> bytes:
    2. return orjson.dumps({"name": name, "age": age})
    3. def response_deserializer(data: bytes) -> str:
    4. json_dict = orjson.loads(data)
    5. return json_dict["message"]
    6. class GreeterServiceStub:
    7. def __init__(self, client: dubbo.Client):
    8. self.unary = client.unary(
    9. method_name="unary",
    10. request_serializer=request_serializer,
    11. response_deserializer=response_deserializer,
    12. )
    13. def say_hello(self, name: str, age: int):
    14. return self.unary(name, age)
    15. if __name__ == "__main__":
    16. reference_config = ReferenceConfig.from_url(
    17. "tri://127.0.0.1:50051/org.apache.dubbo.samples.serialization.json"
    18. )
    19. dubbo_client = dubbo.Client(reference_config)
    20. stub = GreeterServiceStub(dubbo_client)
    21. result = stub.say_hello("dubbo-python", 18)
    22. print(result)
  3. 定义并设置Server的序列化和反序列化函数

    1. def request_deserializer(data: bytes) -> Tuple[str, int]:
    2. json_dict = orjson.loads(data)
    3. return json_dict["name"], json_dict["age"]
    4. def response_serializer(message: str) -> bytes:
    5. return orjson.dumps({"message": message})
    6. class GreeterServiceServicer:
    7. def say_hello(self, request):
    8. name, age = request
    9. print(f"Received request: {name}, {age}")
    10. return f"Hello, {name}, you are {age} years old."
    11. def build_service_handler():
    12. # build a method handler
    13. method_handler = RpcMethodHandler.unary(
    14. GreeterServiceServicer().say_hello,
    15. method_name="unary",
    16. request_deserializer=request_deserializer,
    17. response_serializer=response_serializer,
    18. )
    19. # build a service handler
    20. service_handler = RpcServiceHandler(
    21. service_name="org.apache.dubbo.samples.serialization.json",
    22. method_handlers=[method_handler],
    23. )
    24. return service_handler
    25. if __name__ == "__main__":
    26. # build server config
    27. service_handler = build_service_handler()
    28. service_config = ServiceConfig(
    29. service_handler=service_handler, host="127.0.0.1", port=50051
    30. )
    31. # start the server
    32. server = dubbo.Server(service_config).start()
    33. input("Press Enter to stop the server...\n")

最后修改 November 7, 2024: docs: add python sdk manual (#3056) (26c58b388ff)