Pojo 序列化兼容模式

这篇教程会通过从零构建一个简单的工程来演示如何基于 POJO 方式使用 Dubbo Triple, 在应用不改变已有接口定义的同时升级到 Triple 协议。此模式下 Triple 使用方式与 Dubbo 协议一样。

具体用例可以参考:dubbo-samples-triple/pojo;

前置条件

创建工程

  1. 首先创建一个空的 maven 工程

    1. $ mvn archetype:generate \
    2. -DgroupId=org.apache.dubbo \
    3. -DartifactId=tri-pojo-demo \
    4. -DarchetypeArtifactId=maven-archetype-quickstart \
    5. -DarchetypeVersion=1.4 \
    6. -DarchetypeGroupId=org.apache.maven.archetypes \
    7. -Dversion=1.0-SNAPSHOT
  2. 切换到工程目录

    1. $ cd tri-pojo-demo
  3. pom.xml 中设置 JDK 版本,添加 Dubbo 依赖和插件

    1. <properties>
    2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    3. <maven.compiler.source>1.8</maven.compiler.source>
    4. <maven.compiler.target>1.8</maven.compiler.target>
    5. </properties>
    6. <dependencies>
    7. <dependency>
    8. <groupId>junit</groupId>
    9. <artifactId>junit</artifactId>
    10. <version>4.13</version>
    11. <scope>test</scope>
    12. </dependency>
    13. <dependency>
    14. <groupId>org.apache.dubbo</groupId>
    15. <artifactId>dubbo</artifactId>
    16. <version>3.0.8</version>
    17. </dependency>
    18. <dependency>
    19. <groupId>org.apache.dubbo</groupId>
    20. <artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
    21. <type>pom</type>
    22. <version>3.0.8</version>
    23. </dependency>
    24. <dependency>
    25. <groupId>com.google.protobuf</groupId>
    26. <artifactId>protobuf-java</artifactId>
    27. <version>3.19.4</version>
    28. </dependency>
    29. </dependencies>
  4. 添加接口定义src/main/java/org/apache/dubbo/Greeter.java

    1. package org.apache.dubbo;
    2. public interface Greeter {
    3. String sayHello(String name);
    4. }
  5. 添加服务端接口实现src/main/java/org/apache/dubbo/GreeterImpl.java

    1. package org.apache.dubbo;
    2. public class GreeterImpl implements Greeter {
    3. @Override
    4. public String sayHello(String name) {
    5. return "Hello," + name + "!";
    6. }
    7. }
  6. 添加服务端启动类 src/main/java/org/apache/dubbo/MyDubboServer.java

    1. package org.apache.dubbo;
    2. import org.apache.dubbo.common.constants.CommonConstants;
    3. import org.apache.dubbo.config.ApplicationConfig;
    4. import org.apache.dubbo.config.ProtocolConfig;
    5. import org.apache.dubbo.config.RegistryConfig;
    6. import org.apache.dubbo.config.ServiceConfig;
    7. import org.apache.dubbo.config.bootstrap.DubboBootstrap;
    8. import java.io.IOException;
    9. public class MyDubboServer {
    10. public static void main(String[] args) throws IOException {
    11. ServiceConfig<Greeter> service = new ServiceConfig<>();
    12. service.setInterface(Greeter.class);
    13. service.setRef(new GreeterImpl());
    14. DubboBootstrap bootstrap = DubboBootstrap.getInstance();
    15. bootstrap.application(new ApplicationConfig("tri-pojo-server"))
    16. .registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
    17. .protocol(new ProtocolConfig(CommonConstants.TRIPLE, 50051))
    18. .service(service)
    19. .start();
    20. System.out.println("Dubbo triple pojo server started");
    21. System.in.read();
    22. }
    23. }
  7. 添加客户端启动类src/main/java/org/apache/dubbo/MyDubboClient.java

    1. package org.apache.dubbo;
    2. import org.apache.dubbo.common.constants.CommonConstants;
    3. import org.apache.dubbo.config.ApplicationConfig;
    4. import org.apache.dubbo.config.ReferenceConfig;
    5. import org.apache.dubbo.config.RegistryConfig;
    6. import org.apache.dubbo.config.bootstrap.DubboBootstrap;
    7. public class MyDubboClient {
    8. public static void main(String[] args) {
    9. DubboBootstrap bootstrap = DubboBootstrap.getInstance();
    10. ReferenceConfig<Greeter> ref = new ReferenceConfig<>();
    11. ref.setInterface(Greeter.class);
    12. ref.setProtocol(CommonConstants.TRIPLE);
    13. ref.setTimeout(3000);
    14. bootstrap.application(new ApplicationConfig("tri-pojo-client"))
    15. .registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
    16. .reference(ref)
    17. .start();
    18. Greeter greeter = ref.get();
    19. String reply = greeter.sayHello("pojo");
    20. System.out.println("Received reply:" + reply);
    21. }
    22. }
  8. 编译代码

    1. $ mvn clean install
  9. 启动服务端

    1. $ mvn org.codehaus.mojo:exec-maven-plugin:3.0.0:java -Dexec.mainClass="org.apache.dubbo.MyDubboServer"
    2. Dubbo triple pojo server started
  10. 打开新的终端,启动客户端

  1. $ mvn org.codehaus.mojo:exec-maven-plugin:3.0.0:java -Dexec.mainClass="org.apache.dubbo.MyDubboClient"
  2. Received reply:message: "Hello,Demo!"

常见问题

  1. protobuf 类找不到

由于 Triple 协议底层需要依赖 protobuf 协议进行传输,即使定义的服务接口不使用 protobuf 也需要在环境中引入 protobuf 的依赖。

  1. <dependency>
  2. <groupId>com.google.protobuf</groupId>
  3. <artifactId>protobuf-java</artifactId>
  4. <version>3.19.4</version>
  5. </dependency>

最后修改 December 16, 2022: Fix check (#1736) (97972c1)