Spring Boot

示例演示了如何使用 Spring Boot 方式快速开发 Dubbo 应用。

Dubbo 还提供了包括XMLAPI等多种启动与接入方式,更多开发方式和配置细节可参见配置手册

下载示例代码

完整示例代码在 dubbo-samples 中。

  1. 下载源码
  1. git clone -b master https://github.com/apache/dubbo-samples.git
  1. 进入示例目录
  1. cd dubbo-samples/1-basic/dubbo-samples-spring-boot
  2. ls # 查看目录结构

快速运行示例

  1. 编译 Provider 在 dubbo-samples-spring-boot 目录执行 maven 命令
  1. mvn clean package
  1. 运行 Provider 进入 dubbo-samples-spring-boot-provider/target 目录并启动 java 进程
  1. cd ./dubbo-samples-spring-boot-provider
  2. java -jar ./target/dubbo-samples-spring-boot-provider-1.0-SNAPSHOT.jar
  1. 运行 consumer 进入 dubbo-samples-spring-boot-consumer 目录并启动 java 进程
  1. java -jar ./target/dubbo-samples-spring-boot-consumer-1.0-SNAPSHOT.jar
  1. 查看结果 在 consumer 侧会输出如下信息:
  1. result: Hello World

在 provider 侧会输出如下信息:

  1. Hello World, request from consumer: xxx.xxx.xxx.xxx

至此就把 Dubbo 的基本功能都实现了,可以在 Dubbo 的基础上进行更多的开发。

示例核心流程

关于示例的更详细解读可参见 注解配置

1. 定义服务接口

dubbo-samples-spring-boot-interface/DemoService.java

  1. package org.apache.dubbo.samples.basic.api;
  2. public interface DemoService {
  3. String sayHello(String name);
  4. }

2. 提供方实现接口并暴露服务

dubbo-samples-spring-boot-provider/DemoServiceImpl.java

  1. @DubboService
  2. public class DemoServiceImpl implements DemoService {
  3. @Override
  4. public String sayHello(String name) {
  5. System.out.println("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
  6. return "Hello " + name;
  7. }
  8. }

3. 配置 application.yml 文件

dubbo-samples-spring-boot-provider/resources/application.yml

  1. dubbo:
  2. application:
  3. name: dubbo-springboot-demo-provider
  4. protocol:
  5. name: dubbo
  6. port: -1
  7. registry:
  8. id: zk-registry
  9. address: zookeeper://127.0.0.1:2181
  10. config-center:
  11. address: zookeeper://127.0.0.1:2181
  12. metadata-report:
  13. address: zookeeper://127.0.0.1:2181

4. 定义 Spring Boot 主函数

dubbo-samples-spring-boot-provider/ProviderApplication.java

  1. @SpringBootApplication
  2. @EnableDubbo
  3. public class ProviderApplication {
  4. public static void main(String[] args) throws Exception {
  5. new EmbeddedZooKeeper(2181, false).start();
  6. SpringApplication.run(ProviderApplication.class, args);
  7. System.out.println("dubbo service started");
  8. new CountDownLatch(1).await();
  9. }
  10. }

其中,@EnableDubbo 必须配置。

5. 引用远程服务

dubbo-samples-spring-boot-consumer/ConsumerApplication.java

  1. public class ConsumerApplication {
  2. @DubboReference
  3. private DemoService demoService;
  4. }

6. 定义 application.yml

dubbo-samples-spring-boot-consumer/application.yml

  1. dubbo:
  2. application:
  3. name: dubbo-springboot-demo-consumer
  4. protocol:
  5. name: dubbo
  6. port: -1
  7. registry:
  8. id: zk-registry
  9. address: zookeeper://127.0.0.1:2181
  10. config-center:
  11. address: zookeeper://127.0.0.1:2181
  12. metadata-report:
  13. address: zookeeper://127.0.0.1:2181

7. 加载 Spring 配置,并调用远程服务

dubbo-samples-spring-boot-consumer/ConsumerApplication.java

  1. @SpringBootApplication
  2. @Service
  3. @EnableDubbo
  4. public class ConsumerApplication {
  5. @DubboReference
  6. private DemoService demoService;
  7. public String doSayHello(String name) {
  8. return demoService.sayHello(name);
  9. }
  10. public static void main(String[] args) {
  11. ConfigurableApplicationContext context = SpringApplication.run(ConsumerApplication.class, args);
  12. ConsumerApplication application = context.getBean(ConsumerApplication.class);
  13. String result = application.doSayHello("world");
  14. System.out.println("result: " + result);
  15. }
  16. }

其中,@EnableDubbo 必须配置。

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