IDL 定义跨语言服务

从零演示如何基于 IDL 方式来定义 Dubbo 服务并使用 Triple 协议

使用 IDL 定义服务具有更好的跨语言友好性,对于 Dubbo3 新用户而言,我们推荐使用这种方式。 然而 Triple 协议并不是和 IDL 强绑定的,也可以使用 Java Interface + Pojo 的方式定义服务并启用 Triple 协议,具体可参见示例

更多 Triple 和 IDL 使用方式,请参考官方示例

前置条件

创建工程

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

    1. $ mvn archetype:generate \
    2. -DgroupId=org.apache.dubbo \
    3. -DartifactId=tri-stub-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-stub-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>
    30. <build>
    31. <extensions>
    32. <extension>
    33. <groupId>kr.motd.maven</groupId>
    34. <artifactId>os-maven-plugin</artifactId>
    35. <version>1.6.1</version>
    36. </extension>
    37. </extensions>
    38. <plugins>
    39. <plugin>
    40. <groupId>org.xolstice.maven.plugins</groupId>
    41. <artifactId>protobuf-maven-plugin</artifactId>
    42. <version>0.6.1</version>
    43. <configuration>
    44. <protocArtifact>com.google.protobuf:protoc:3.19.4:exe:${os.detected.classifier}</protocArtifact>
    45. <protocPlugins>
    46. <protocPlugin>
    47. <id>dubbo</id>
    48. <groupId>org.apache.dubbo</groupId>
    49. <artifactId>dubbo-compiler</artifactId>
    50. <version>0.0.4.1-SNAPSHOT</version>
    51. <mainClass>org.apache.dubbo.gen.tri.Dubbo3TripleGenerator</mainClass>
    52. </protocPlugin>
    53. </protocPlugins>
    54. </configuration>
    55. <executions>
    56. <execution>
    57. <goals>
    58. <goal>compile</goal>
    59. </goals>
    60. </execution>
    61. </executions>
    62. </plugin>
    63. </plugins>
    64. </build>
  4. 添加接口定义文件src/main/proto/hello.proto,Dubbo 使用 Protobuf 作为 IDL

    1. syntax = "proto3";
    2. option java_multiple_files = true;
    3. option java_package = "org.apache.dubbo.hello";
    4. option java_outer_classname = "HelloWorldProto";
    5. option objc_class_prefix = "HLW";
    6. package helloworld;
    7. message HelloRequest {
    8. string name = 1;
    9. }
    10. message HelloReply {
    11. string message = 1;
    12. }
    13. service Greeter{
    14. rpc greet(HelloRequest) returns (HelloReply);
    15. }
  5. 编译 IDL

    1. $ mvn clean install

    编译成功后,可以看到target/generated-sources/protobuf/java 目录下生成了代码文件

    1. $ ls org/apache/dubbo/hello/
    2. DubboGreeterTriple.java HelloReply.java HelloRequest.java HelloWorldProto.java
    3. Greeter.java HelloReplyOrBuilder.java HelloRequestOrBuilder.java
  6. 添加服务端接口实现src/main/java/org/apache/dubbo/GreeterImpl.java

    1. package org.apache.dubbo;
    2. import org.apache.dubbo.hello.DubboGreeterTriple;
    3. import org.apache.dubbo.hello.HelloReply;
    4. import org.apache.dubbo.hello.HelloRequest;
    5. public class GreeterImpl extends DubboGreeterTriple.GreeterImplBase {
    6. @Override
    7. public HelloReply greet(HelloRequest request) {
    8. return HelloReply.newBuilder()
    9. .setMessage("Hello," + request.getName() + "!")
    10. .build();
    11. }
    12. }
  7. 添加服务端启动类 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 org.apache.dubbo.hello.Greeter;
    9. import java.io.IOException;
    10. public class MyDubboServer {
    11. public static void main(String[] args) throws IOException {
    12. ServiceConfig<Greeter> service = new ServiceConfig<>();
    13. service.setInterface(Greeter.class);
    14. service.setRef(new GreeterImpl());
    15. DubboBootstrap bootstrap = DubboBootstrap.getInstance();
    16. bootstrap.application(new ApplicationConfig("tri-stub-server"))
    17. .registry(new RegistryConfig("multicast://127.0.0.1:2181"))
    18. .protocol(new ProtocolConfig(CommonConstants.TRIPLE, 50051))
    19. .service(service)
    20. .start();
    21. System.out.println("Dubbo triple stub server started");
    22. System.in.read();
    23. }
    24. }
  8. 添加客户端启动类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. import org.apache.dubbo.hello.Greeter;
    8. import org.apache.dubbo.hello.HelloReply;
    9. import org.apache.dubbo.hello.HelloRequest;
    10. public class MyDubboClient {
    11. public static void main(String[] args) {
    12. DubboBootstrap bootstrap = DubboBootstrap.getInstance();
    13. ReferenceConfig<Greeter> ref = new ReferenceConfig<>();
    14. ref.setInterface(Greeter.class);
    15. ref.setProtocol(CommonConstants.TRIPLE);
    16. ref.setProxy(CommonConstants.NATIVE_STUB);
    17. ref.setTimeout(3000);
    18. bootstrap.application(new ApplicationConfig("tri-stub-client"))
    19. .registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
    20. .reference(ref)
    21. .start();
    22. Greeter greeter = ref.get();
    23. HelloRequest request = HelloRequest.newBuilder().setName("Demo").build();
    24. HelloReply reply = greeter.greet(request);
    25. System.out.println("Received reply:" + reply);
    26. }
    27. }
  9. 编译代码

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

  1. $ mvn org.codehaus.mojo:exec-maven-plugin:3.0.0:java -Dexec.mainClass="org.apache.dubbo.MyDubboServer"
  2. Dubbo triple stub server started
  1. 打开新的终端,启动客户端
  1. $ mvn org.codehaus.mojo:exec-maven-plugin:3.0.0:java -Dexec.mainClass="org.apache.dubbo.MyDubboClient"
  2. Received reply:message: "Hello,Demo!"

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