Spring Boot
Dubbo 提供了对 Spring 框架的完整支持,我们推荐使用官方提供的丰富的 dubbo-spring-boot-starter 高效开发 Dubbo 微服务应用。
Dubbo 提供了对 Spring 框架的完整支持,我们推荐使用官方提供的 dubbo-spring-boot-starter 高效开发 Dubbo 微服务应用。
创建项目
创建 Dubbo 应用最快捷的方式就是直接复制快速开始中的示例并进行定制化修改。
另一种更灵活的方式是使用官方项目脚手架工具 - start.dubbo.apache.org 在线服务。它可以帮助开发者创建 Spring Boot 结构应用,根据用户的选择自动管理 dubbo-spring-boot-starter 等依赖和必要配置。

注意
另外,Jetbrain 官方也提供了 Apache Dubbo 项目插件,可用于快速创建 Dubbo Spring Boot 项目,能力与 start.dubbo.apache.org 对等,具体安装使用请查看 博客文章
dubbo-spring-boot-starter
在 快速开始 中,我们已经详细介绍了典型的 Dubbo Spring Boot 工程源码及其项目结构,不熟悉的开发者可以前往查看。
dubbo-spring-boot-starter 可为项目引入 dubbo 核心依赖,自动扫描 dubbo 相关配置与注解。
Maven 依赖
使用 Dubbo Spring Boot Starter,首先引入以下 Maven 依赖
1. <dependencyManagement>
2. <dependencies>
3. <dependency>
4. <groupId>org.apache.dubbo</groupId>
5. <artifactId>dubbo-bom</artifactId>
6. <version>3.3.0</version>
7. <type>pom</type>
8. <scope>import</scope>
9. </dependency>
10. </dependencies>
11. </dependencyManagement>
然后,在相应模块的 pom 中增加必要的 starter 依赖:
1. <dependencies>
2. <dependency>
3. <groupId>org.apache.dubbo</groupId>
4. <artifactId>dubbo-spring-boot-starter</artifactId>
5. </dependency>
6. <dependency>
7. <groupId>org.apache.dubbo</groupId>
8. <artifactId>dubbo-zookeeper-spring-boot-starter</artifactId>
9. </dependency>
10. </dependencies>
dubbo-spring-boot-starter 和 dubbo-zookeeper-spring-boot-starter 是官方提供的 starter,提供了 Spring Boot 的集成适配,它们的版本号与 Dubbbo 主框架版本号完全一致。Dubbo 还支持 dubbo-nacos-spring-boot-starter 等 starter,具体可查看参见 starter 列表。
application.yml 配置文件
以下是一个示例文件配置
1. dubbo:
2. application:
3. name: dubbo-springboot-demo-provider
4. logger: slf4j
5. protocol:
6. name: tri
7. port: -1
8. registry:
9. address: zookeeper://127.0.0.1:2181
除 service、reference 之外的组件都可以在 application.yml 文件中设置,具体可参考 配置列表。
service、reference 组件也可以通过 id 与 application 中的全局组件做关联,以下面配置为例。如果要扩展 service 或 reference 的注解配置,则需要增加 dubbo.properties 配置文件或使用其他非注解如 Java Config 方式,具体请看下文 扩展注解的配置。
1. dubbo:
2. application:
3. name: dubbo-springboot-demo-provider
4. protocol:
5. name: tri
6. port: -1
7. registry:
8. id: zk-registry
9. address: zookeeper://127.0.0.1:2181
通过注解将 service 关联到上文定义的特定注册中心(通过id关联)
1. @DubboService(registry="zk-registry")
2. public class DemoServiceImpl implements DemoService {}
通过 Java Config 配置进行关联也是同样道理
1. @Configuration
2. public class ProviderConfiguration {
3. @Bean
4. public ServiceConfig demoService() {
5. ServiceConfig service = new ServiceConfig();
6. service.setRegistry("zk-registry");
7. return service;
8. }
9. }
Dubbo 注解
application.properties或application.yml配置文件。@DubboService、@DubboReference与EnableDubbo注解。其中@DubboService与@DubboReference用于标记 Dubbo 服务,EnableDubbo启动 Dubbo 相关配置并指定 Spring Boot 扫描包路径。
@DubboService 注解
@Service注解从 3.0 版本开始就已经废弃,改用@DubboService,以区别于 Spring 的@Service注解
定义好 Dubbo 服务接口后,提供服务接口的实现逻辑,并用 @DubboService 注解标记,就可以实现 Dubbo 的服务暴露
1. @DubboService
2. public class DemoServiceImpl implements DemoService {}
如果要设置服务参数,@DubboService 也提供了常用参数的设置方式。如果有更复杂的参数设置需求,则可以考虑使用其他设置方式
1. @DubboService(version = "1.0.0", group = "dev", timeout = 5000)
2. public class DemoServiceImpl implements DemoService {}
@DubboReference 注解
@Reference注解从 3.0 版本开始就已经废弃,改用@DubboReference,以区别于 Spring 的@Reference注解
1. @Component
2. public class DemoClient {
3. @DubboReference
4. private DemoService demoService;
5. }
@DubboReference 注解将自动注入为 Dubbo 服务代理实例,使用 demoService 即可发起远程服务调用
@EnableDubbo 注解
@EnableDubbo 注解必须配置,否则将无法加载 Dubbo 注解定义的服务,@EnableDubbo 可以定义在主类上
1. @SpringBootApplication
2. @EnableDubbo
3. public class ProviderApplication {
4. public static void main(String[] args) throws Exception {
5. SpringApplication.run(ProviderApplication.class, args);
6. }
7. }
Spring Boot 注解默认只会扫描 main 类所在的 package,如果服务定义在其它 package 中,需要增加配置 EnableDubbo(scanBasePackages = {"org.apache.dubbo.springboot.demo.provider"})
扩展注解配置
虽然可以通过 @DubboService 和 DubboReference 调整配置参数(如下代码片段所示),但总体来说注解是为易用性设计的,其提供的仅仅是 80% 场景下常用的配置项。在这种情况下,如果有更复杂的参数设置需求,可以使用 Java Config 或 dubbo.properties 两种方式。
1. @DubboService(version = "1.0.0", group = "dev", timeout = 5000)
2. @DubboReference(version = "1.0.0", group = "dev", timeout = 5000)
使用 Java Config 代替注解
注意,Java Config 是 DubboService 或 DubboReference 的替代方式,对于有复杂配置需求的服务建议使用这种方式。
1. @Configuration
2. public class ProviderConfiguration {
3. @Bean
4. public ServiceBean demoService() {
5. ServiceBean service = new ServiceBean();
6. service.setInterface(DemoService.class);
7. service.setRef(new DemoServiceImpl());
8. service.setGroup("dev");
9. service.setVersion("1.0.0");
10. Map<String, String> parameters = new HashMap<>();
11. service.setParameters(parameters);
12. return service;
13. }
14. }
通过 dubbo.properties 补充配置
对于使用 DubboService 或 DubboReference 的场景,可以通过在项目 resources 目录下增加 dubbo.properties 文件作为配置补充,具体格式这里有更详细解释。
1. dubbo.service.org.apache.dubbo.springboot.demo.DemoService.timeout=5000
2. dubbo.service.org.apache.dubbo.springboot.demo.DemoService.parameters=[{myKey:myValue},{anotherKey:anotherValue}]
3. dubbo.reference.org.apache.dubbo.springboot.demo.DemoService.timeout=6000
properties 格式配置目前结构性不太强,比如体现在 key 字段冗余较多,后续会考虑提供对于 yaml 格式的支持。
更多微服务开发模式
- 纯 API 开发模式
- 其他 Spring 开发模式
- Spring XML
Dubbo 与 Spring Cloud 的关系
Dubbo 与 Spring Cloud 是两套平行的微服务开发与解决方案,两者都提供了微服务定义、发布、治理的相关能力,对于微服务开发者来说,我们建议在开发之初就确定好 Apache Dubbo 与 Spring Cloud 之间的选型,尽量避免两个不同体系在同一集群中出现,以降低集群维护复杂度。而对于一些确需两套体系共存的场景,为了解决相互之间的通信问题,我们提供了 Dubbo 与 Spring Cloud 异构微服务体系互通最佳实践 解决方案。
Dubbo 与 Spring Boot 是互补的关系,Dubbo 在 Spring Boot 体系之上提供了完整的微服务开发、治理能力,关于这一点我们在另一篇文章中有更详尽的说明:Dubbo、Spring Cloud 与 Istio。
最后修改 October 11, 2024: fixed protocol.name wrong spelling (#3046) (35f5e799156)
