Java 快速开始

下载示例代码

示例代码在 dubbo-samples

  1. 下载源码
  1. $ git clone -b master https://github.com/apache/dubbo-samples.git
  1. 进入示例目录
  1. $ cd dubbo-samples/dubbo-samples-protobuf

快速运行示例

在 dubbo-samples-protobuf 目录

  1. 编译并打包示例项目
  1. $ mvn clean package
  1. 运行 Provider
  1. $ java -jar ./protobuf-provider/target/protobuf-provider-1.0-SNAPSHOT.jar
  1. 运行 consumer
  1. $ java -jar ./protobuf-consumer/target/protobuf-consumer-1.0-SNAPSHOT.jar
  2. 输出以下结果
  3. result: Hello Hello, response from provider: 30.225.20.43:20880

以上就是一个简单的 Dubbo 服务定义、服务调用流程

详细讲解

  1. 服务定义
  1. syntax = "proto3";
  2. option java_multiple_files = true;
  3. option java_package = "org.apache.dubbo.demo";
  4. option java_outer_classname = "DemoServiceProto";
  5. option objc_class_prefix = "DEMOSRV";
  6. package demoservice;
  7. // The demo service definition.
  8. service DemoService {
  9. rpc SayHello (HelloRequest) returns (HelloReply) {}
  10. }
  11. // The request message containing the user's name.
  12. message HelloRequest {
  13. string name = 1;
  14. }
  15. // The response message containing the greetings
  16. message HelloReply {
  17. string message = 1;
  18. }
  1. Protobuf Compiler 插件配置
  1. <plugin>
  2. <groupId>org.xolstice.maven.plugins</groupId>
  3. <artifactId>protobuf-maven-plugin</artifactId>
  4. <version>0.5.1</version>
  5. <configuration>
  6. <protocArtifact>com.google.protobuf:protoc:3.7.1:exe:${os.detected.classifier}</protocArtifact>
  7. <outputDirectory>build/generated/source/proto/main/java</outputDirectory>
  8. <clearOutputDirectory>false</clearOutputDirectory>
  9. <protocPlugins>
  10. <protocPlugin>
  11. <id>dubbo</id>
  12. <groupId>org.apache.dubbo</groupId>
  13. <artifactId>dubbo-compiler</artifactId>
  14. <version>${dubbo.compiler.version}</version>
  15. <mainClass>org.apache.dubbo.gen.dubbo.Dubbo3Generator</mainClass>
  16. </protocPlugin>
  17. </protocPlugins>
  18. </configuration>
  19. <executions>
  20. <execution>
  21. <goals>
  22. <goal>compile</goal>
  23. <goal>test-compile</goal>
  24. </goals>
  25. </execution>
  26. </executions>
  27. </plugin>
  1. 编译与stub 运行 mvn clean compile 后

生成代码路径 dubbo-samples-protobuf/protobuf-provider/build/generated/source/proto/main/java/org/apache/dubbo/demo

生成文件列表

  1. .
  2. ├── DemoService.java
  3. ├── DemoServiceDubbo.java
  4. ├── DemoServiceProto.java
  5. ├── HelloReply.java
  6. ├── HelloReplyOrBuilder.java
  7. ├── HelloRequest.java
  8. └── HelloRequestOrBuilder.java

DemoService.java 定义如下

  1. @javax.annotation.Generated(
  2. value = "by Dubbo generator",
  3. comments = "Source: DemoService.proto")
  4. public interface DemoService {
  5. static final String JAVA_SERVICE_NAME = "org.apache.dubbo.demo.DemoService";
  6. static final String SERVICE_NAME = "demoservice.DemoService";
  7. static final boolean inited = DemoServiceDubbo.init();
  8. org.apache.dubbo.demo.HelloReply sayHello(org.apache.dubbo.demo.HelloRequest request);
  9. CompletableFuture<org.apache.dubbo.demo.HelloReply> sayHelloAsync(org.apache.dubbo.demo.HelloRequest request);
  10. }
  1. 发布服务
  1. public class DemoServiceImpl implements DemoService {
  2. private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);
  3. @Override
  4. public HelloReply sayHello(HelloRequest request) {
  5. logger.info("Hello " + request.getName() + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
  6. return HelloReply.newBuilder()
  7. .setMessage("Hello " + request.getName() + ", response from provider: "
  8. + RpcContext.getContext().getLocalAddress())
  9. .build();
  10. }
  11. @Override
  12. public CompletableFuture<HelloReply> sayHelloAsync(HelloRequest request) {
  13. return CompletableFuture.completedFuture(sayHello(request));
  14. }
  15. }
  1. <bean id="demoServiceImpl" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>
  2. <dubbo:service serialization="protobuf" interface="org.apache.dubbo.demo.DemoService"
  3. ref="demoServiceImpl"/>
  1. 使用服务
  1. <dubbo:reference scope="remote" id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService"/>
  1. public class ConsumerApplication {
  2. public static void main(String[] args) throws Exception {
  3. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
  4. context.start();
  5. DemoService demoService = context.getBean("demoService", DemoService.class);
  6. HelloRequest request = HelloRequest.newBuilder().setName("Hello").build();
  7. HelloReply reply = demoService.sayHello(request);
  8. System.out.println("result: " + reply.getMessage());
  9. System.in.read();
  10. }
  11. }

其他

示例运行过程中还用到了服务发现等机制,详情请参见相关章节说明

最后修改 September 21, 2021: Bug fix miss mialbox (#953) (57cf51b)