注解配置

注解配置使用注解和 Spring Boot 开发 Dubbo 应用程序本文使用 Spring Boot + 注解模式描述 Dubbo 应用程序开发。查看没有 Spring Boot 的 Spring 注解开发模式,请点击这里 [完整示例](https://github.com/apache/dubbo-samples/tree/master/1-basic/ dubbo-samples-annotation)

在 Dubbo Spring Boot 开发中,您只需要添加一些注解并配置 application.properties 或 application.yml 文件即可完成 Dubbo 服务定义

注解包括 @DubboService、@DubboReference 和 EnableDubbo。其中,@DubboService 和 @DubboReference 用于标记 Dubbo 服务,EnableDubbo 启动 Dubbo 相关配置并指定 Spring Boot 扫描包路径。配置文件 application.properties 或 application.yml以下内容的完整示例,请参考 dubbo-samples

添加 Maven 依赖使用 Dubbo Spring Boot Starter 首先引入以下 Maven 依赖

org.springframework.boot

spring-boot-dependencies

${spring-boot.version}

pom

import

org.apache.dubbo

dubbo-bom

${dubbo.version}

pom

import

org.apache.dubbo

dubbo-dependencies-zookeeper

${dubbo.version}

pom

然后将其添加到相应模块的 pom 中

org.apache.dubbo

dubbo

org.apache.dubbo

dubbo-dependencies-zookeeper

pom

org.apache.dubbo

dubbo-spring-boot-starter

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-autoconfigure

区分上面的 ** 和 **

application.yml 或 application.properties服务和引用以外的组件可以在 application.yml 文件中设置。如果您想扩展服务或引用的注解配置,则需要添加 dubbo.properties 配置文件或使用其他非注解方法,例如 Java Config。详情请参见下面的 [扩展注解配置](#扩展注解配置)。

服务和引用组件也可以通过 id 与应用程序中的全局组件关联,以下配置为例

dubbo:

application:

name: dubbo-springboot-demo-provider

protocol:

name: dubbo

port: -1

registry:

id: zk-registry

address: zookeeper://127.0.0.1:2181

config-center:

address: zookeeper://127.0.0.1:2181

metadata-report:

address: zookeeper://127.0.0.1:2181

通过注解将服务与上面定义的特定注册中心关联

@DubboService(registry="zk-registry")

public class DemoServiceImpl implements DemoService {}

通过 Java Config 配置关联也是一样的

@Configuration

public class ProviderConfiguration {

@Bean

public ServiceConfig demoService() {

ServiceConfig service = new ServiceConfig();

service.setRegistry("zk-registry");

return service;

}

}

注解@DubboService 注解@Service 注解从 3.0 版本开始已弃用,使用 @DubboService 与 Spring 的 @Service 注解区分开

定义 Dubbo 服务接口后,提供服务接口的实现逻辑,并用 @DubboService 注解标记,实现 Dubbo 的服务暴露

@DubboService

public class DemoServiceImpl implements DemoService {}

如果您想设置服务参数,@DubboService 也提供了一种设置通用参数的方式。如果您有更复杂的参数设置需求,可以考虑使用其他设置方法

@DubboService(version = "1.0.0", group = "dev", timeout = 5000)

public class DemoServiceImpl implements DemoService {}

@DubboReference 注解@Reference 注解从 3.0 版本开始已弃用,使用 @DubboReference 与 Spring 的 @Reference 注解区分开

@Component

public class DemoClient {

@DubboReference

private DemoService demoService;

}

@DubboReference 注解将自动注入为 Dubbo 服务代理实例,可以使用 demoService 发起远程服务调用

@EnableDubbo 注解必须配置 @EnableDubbo 注解,否则 Dubbo 注解定义的服务将不会被加载,@EnableDubbo 可以定义在主类上

@SpringBootApplication

@EnableDubbo

public class ProviderApplication {

public static void main(String[] args) throws Exception {

SpringApplication.run(ProviderApplication.class, args);

}

}

Spring Boot 注解默认只扫描主类所在的包。如果服务定义在其他包中,则需要添加配置 EnableDubbo(scanBasePackages = {"org.apache.dubbo.springboot.demo.provider"})

扩展注解配置虽然可以通过 @DubboService 和 DubboReference 调整配置参数(如以下代码片段所示),但总体而言,注解提供的配置项仍然非常有限。在这种情况下,如果存在更复杂的参数设置需求,可以使用 Java Config 或 dubbo.properties 两种方式。

@DubboService(version = "1.0.0", group = "dev", timeout = 5000)

@DubboReference(version = "1.0.0", group = "dev", timeout = 5000)

使用 Java Config 代替注解请注意,Java Config 是 DubboService 或 DubboReference 的替代方案,建议用于具有复杂配置需求的服务。

@Configuration

public class ProviderConfiguration {

@Bean

public ServiceConfig demoService() {

ServiceConfig service = new ServiceConfig();

service.setInterface(DemoService.class);

service.setRef(new DemoServiceImpl());

service.setGroup("dev");

service.setVersion("1.0.0");

Map parameters = new HashMap<>();

service. setParameters(parameters);

return service;

}

}

通过 dubbo.properties 进行补充配置对于使用 DubboService 或 DubboReference 的场景,可以使用 dubbo.properties 作为配置补充,[具体格式](../principle/#1-configuration format) 在这里有更详细的解释。

dubbo.service.org.apache.dubbo.springboot.demo.DemoService.timeout=5000

dubbo.service.org.apache.dubbo.springboot.demo.DemoService.parameters=[{myKey:myValue},{anotherKey:anotherValue}]

dubbo.reference.org.apache.dubbo.springboot.demo.DemoService.timeout=6000

属性格式配置目前不是非常结构化,例如,键字段比较冗余,未来会考虑支持 yaml 格式。

反馈此页面是否有帮助?

否上次修改时间:2023 年 1 月 2 日:增强英文文档 (#1798) (95a9f4f6c1c)