官方参考文档
http://dubbo.apache.org/zh-cn/docs/user/configuration/annotation.html
-
Maven
依赖配置
<!-- dubbo all-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!-- dubbo 依赖字节码处理包 -->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.20.0-GA</version>
</dependency>
<!-- Curator Recipes: Zookeeper客户端 - 提供了 Zookeeper 命令行的封装 (不用可以不设置)-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
- 生产者配置
DubboProviderConfiguration
/**
* @Author: zyn
* @Description: 必须配置扫描路径 - 否则无法加载 dubbo
* @Date: Created in 2018-10-10 14:14
* @Modified By:
*/
@Configuration
@DubboComponentScan(basePackages = DubboProviderConfiguration.BASE_PACKAGES)
@EnableConfigurationProperties(DubboProperties.class)
public class DubboProviderConfiguration {
public static final String BASE_PACKAGES = "com.linlong.platform.back.trade.**.service";
@Autowired
private DubboProperties dubboProperties;
/**
* 当前应用配置
* @return ApplicationConfig
*/
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig config = new ApplicationConfig();
config.setName(dubboProperties.getApplicationName());
return config;
}
/**
* 连接注册中心配置
* @return RegistryConfig
*/
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress(dubboProperties.getAddress());
registryConfig.setGroup(dubboProperties.getGroup());
registryConfig.setClient("curator");
return registryConfig;
}
/**
* 服务提供者【协议】配置
* @return ProtocolConfig
*/
@Bean
public ProtocolConfig protocolConfig() {
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName(dubboProperties.getName());
protocolConfig.setPort(dubboProperties.getPort());
protocolConfig.setThreads(dubboProperties.getThreads());
return protocolConfig;
}
/**
* 服务提供者配置
* @return ProviderConfig
*/
@Bean
public ProviderConfig providerConfig() {
ProviderConfig providerConfig = new ProviderConfig();
providerConfig.setGroup("back-trade-provider-group");
providerConfig.setVersion(dubboProperties.getVersion());
providerConfig.setTimeout(dubboProperties.getTimeout());
return providerConfig;
}
}
由于指定了 Registry 的 Group 方便在 Idea ZooKeeper 插件中分组查看生产者,消费者
image.png
DubboProperties
配置 Bean
设置
@Getter
@Setter
@PropertySource(value = "classpath:env/dubbo.properties", ignoreResourceNotFound = true, encoding = "UTF-8")
@ConfigurationProperties(prefix = "dubbo.protocol")
@Configuration
public class DubboProperties {
@Value("${dubbo.registry.address:'zookeeper://127.0.0.1:2181'}")
private String address = "zookeeper://127.0.0.1:2181";
/**
* 协议名称
*/
private String name = "dubbo";
/**
* 协议端口
*/
private Integer port = 20880;
/**
* 协议线程数量
*/
private Integer threads = 100;
/**
* 应用程序名称
*/
@Value("${dubbo.application.name:'测试应用程序名称'}")
private String applicationName;
@Value("${dubbo.provider.version:'1.0.0'}")
private String version = "1.0.0";
@Value("${dubbo.timeout:3000}")
private Integer timeout = 3000;
@Value("${dubbo.hessian.timeout:10000}")
private Integer hessianTimeout = 10000;
/**
* 注册组
*/
@Value("${dubbo.registry.group:'registry-group-test'}")
private String group;
}
- 消费者配置
DubboConsumerConfiguration
@Configuration
@DubboComponentScan(basePackages = DubboConsumerConfiguration.BASE_PACKAGES)
@EnableConfigurationProperties(DubboProperties.class)
@ConditionalOnExpression("${framework.config.dubbo.enabled: true}")
public class DubboConsumerConfiguration {
public static final String BASE_PACKAGES = "com.linlong.platform.back.trade.**.service";
@Autowired
private DubboProperties dubboProperties;
/**
* 当前应用配置
* @return ApplicationConfig
*/
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig config = new ApplicationConfig();
config.setName(dubboProperties.getApplicationName());
return config;
}
/**
* 连接注册中心配置
* @return RegistryConfig
*/
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress(dubboProperties.getAddress());
registryConfig.setGroup(dubboProperties.getGroup());
registryConfig.setClient("curator");
return registryConfig;
}
/**
* 消费者配置
* @return ConsumerConfig
*/
@Bean
public ConsumerConfig consumerConfig() {
ConsumerConfig consumerConfig = new ConsumerConfig();
consumerConfig.setGroup("back-trade-provider-group");
consumerConfig.setTimeout(dubboProperties.getTimeout());
consumerConfig.setVersion(dubboProperties.getVersion());
return consumerConfig;
}
}
dubbo.properties
配置
## Dubbo 服务提供者配置
# 应用程序名称
dubbo.application.name=linlong-platform-back-trade-dubbo-provider
# 注册中心地址
dubbo.registry.address=zookeeper://172.27.55.10:2181
# 注册组
dubbo.registry.group=registry-group-back-trade
# dubbo 协议
dubbo.protocol.name=dubbo
# dubbo 端口
dubbo.protocol.port=20882
dubbo.protocal.threads=100
dubbo.timeout=3000
dubbo.hessian.timeout=10000
dubbo.provider.version=v1.0.0
# 声明需要暴露的服务接口
dubbo.scan=com.magic.platform.**.dubbo
网友评论