首先需要引入pom 这里使用nacos注册中心 所以引入了nacos-client 使用zookeeper注册中心的话需要引入其相应的client

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency> <dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.2</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>0.0.2</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.6</version>
</dependency> <dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.0.0</version>
</dependency> <dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.36.Final</version>
</dependency>

然后新建DubboProperties

@ConfigurationProperties(prefix = "dubbo")
public class DubboProperties { @NestedConfigurationProperty
private ApplicationBean application;
@NestedConfigurationProperty
private ProtocolBean protocol;
@NestedConfigurationProperty
private RegistryBean registry; public ApplicationBean getApplication() {
return application;
} public void setApplication(ApplicationBean application) {
this.application = application;
} public ProtocolBean getProtocol() {
return protocol;
} public void setProtocol(ProtocolBean protocol) {
this.protocol = protocol;
} public RegistryBean getRegistry() {
return registry;
} public void setRegistry(RegistryBean registry) {
this.registry = registry;
}
} //下面定义的几个相关类 其实不定义也可以直接使用dubbo内置的类ApplicationConfig RegistryConfig ProtocolConfig
//因为这些字段是对象所以需要添加 @NestedConfigurationProperty否则无法提供正确的提示,并且参数注入可能也会出现问题
//这里自定义主要提供一些常用的配置 并且添加注解提示的时候也容易看
//生成的 spring-configuration-metadata.json 中文是正常的但是使用的时候中文提示是乱码,暂时不清楚怎么回事
public class ApplicationBean implements Serializable {
/**
* 名称
*/
private String name;
/**
* 版本
*/
private String version;
/**
* 是否使用qos
*/
private Boolean qosEnable;
/**
* qos端口
*/
private Integer qosPort; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getVersion() {
return version;
} public void setVersion(String version) {
this.version = version;
} public Boolean getQosEnable() {
return qosEnable;
} public void setQosEnable(Boolean qosEnable) {
this.qosEnable = qosEnable;
} public Integer getQosPort() {
return qosPort;
} public void setQosPort(Integer qosPort) {
this.qosPort = qosPort;
}
} public class RegistryBean implements Serializable { /**
* 注册中心地址 服务提供端和消费端都需要注册
* zookeeper,nacos,redis等注册中心的地址
*/
private String address;
/**
* 注册中心的用户名
*/
private String username;
/**
* 注册中心的密码
*/
private String password;
/**
* 服务注册的分组 一般小项目默认分组就可以了
*/
private String group;
/**
* 启动时是否检测注册中心 默认检测
*/
private Boolean check = true; public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getGroup() {
return group;
} public void setGroup(String group) {
this.group = group;
} public Boolean getCheck() {
return check;
} public void setCheck(Boolean check) {
this.check = check;
}
} public class ProtocolBean implements Serializable { /**
* 使用 dubbo 协议就可以
*/
private String name = "dubbo";
/**
*
*/
private String host;
/**
* 端口号20880
*/
private Integer port = 20880;
/**
* 线程数 200个
*/
private Integer threads = 200; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getHost() {
return host;
} public void setHost(String host) {
this.host = host;
} public Integer getPort() {
return port;
} public void setPort(Integer port) {
this.port = port;
} public Integer getThreads() {
return threads;
} public void setThreads(Integer threads) {
this.threads = threads;
}
}

然后写几个自动配置类

DubboAutoConfiguration

@Configuration
@EnableConfigurationProperties(DubboProperties.class)
public class DubboAutoConfiguration { @Autowired
private DubboProperties dubboProperties; @Bean
@ConditionalOnMissingBean
public ApplicationConfig applicationConfig(){
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName(dubboProperties.getApplication().getName());
applicationConfig.setVersion(dubboProperties.getApplication().getVersion());
applicationConfig.setQosEnable(dubboProperties.getApplication().getQosEnable());
applicationConfig.setQosPort(dubboProperties.getApplication().getQosPort());
return applicationConfig;
} @Bean
@ConditionalOnMissingBean
public RegistryConfig registryConfig(){
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress(dubboProperties.getRegistry().getAddress());
registryConfig.setCheck(dubboProperties.getRegistry().getCheck());
registryConfig.setUsername(dubboProperties.getRegistry().getUsername());
registryConfig.setPassword(dubboProperties.getRegistry().getPassword());
registryConfig.setGroup(dubboProperties.getRegistry().getGroup());
return registryConfig;
} @Bean
@ConditionalOnMissingBean
public ProtocolConfig protocolConfig(){
ProtocolConfig protocolConfig = new ProtocolConfig();
if(dubboProperties.getProtocol() != null){
protocolConfig.setName(dubboProperties.getProtocol().getName());
protocolConfig.setHost(dubboProperties.getProtocol().getHost());
protocolConfig.setPort(dubboProperties.getProtocol().getPort());
protocolConfig.setThreads(dubboProperties.getProtocol().getThreads());
}
return protocolConfig;
} }

DubboProviderAutoConfiguration

@Configuration
@AutoConfigureAfter(DubboAutoConfiguration.class)
@EnableConfigurationProperties(DubboProperties.class)
public class DubboProviderAutoConfiguration { @Autowired
private DubboProperties dubboProperties; @Bean
@ConditionalOnMissingBean
public ProviderConfig providerConfig(){
ProviderConfig providerConfig = new ProviderConfig();
//重试次数
providerConfig.setRetries(0);
//过滤器
providerConfig.setFilter(null);
providerConfig.setTimeout(3000);
providerConfig.setGroup(dubboProperties.getRegistry().getGroup());
return providerConfig;
} }

DubboConsumerAutoConfiguration

@Configuration
@AutoConfigureAfter(DubboAutoConfiguration.class)
@EnableConfigurationProperties(DubboProperties.class)
public class DubboConsumerAutoConfiguration { @Autowired
private DubboProperties dubboProperties; @Bean
@ConditionalOnMissingBean
public ConsumerConfig consumerConfig(){
ConsumerConfig consumerConfig = new ConsumerConfig();
consumerConfig.setRetries(0);
consumerConfig.setTimeout(3000);
consumerConfig.setCheck(true);
consumerConfig.setGroup(dubboProperties.getRegistry().getGroup());
return consumerConfig;
} }

在resources/META-INF下新建spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.chao.dubbo.DubboAutoConfiguration,\
com.chao.dubbo.DubboProviderAutoConfiguration,\
com.chao.dubbo.DubboConsumerAutoConfiguration

在resources/META-INF 下新建spring.provides

provides: dubbo-spring-boot-autoconfigure

使用的时候(提供端和消费端)的启动类上要加入 com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan 注解 @DubboComponentScan

不能使用com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo 注解 @EnableDubbo

DubboProperties 添加前缀比如spring.dubbo(前缀随便填写,不一定是spring,可是是别的) 可以使用@EnableDubbo注解

如果仅引入pom依赖使用的时候可以使用@EnableDubbo注解使用dubbio 配置直接从dubbo官网或者nacos官网查找即可

其实感觉也没必要自己定义配置ApplicationConfig,RegistryConfig 等类的配置主要自己也不太熟悉能使用但是也不知道有什么bug

## 服务端配置
dubbo.application.name = dubbo-provider-demo
dubbo.registry.address = nacos://127.0.0.1:8848
dubbo.protocol.name = dubbo
dubbo.protocol.port = -1
## 消费端配置
dubbo.application.name = dubbo-consumer-demo
dubbo.registry.address = nacos://127.0.0.1:8848

就可以使用dubbo的注解 @Service @Reference使用了

springboot 简单自定义starter - dubbo的更多相关文章

  1. springboot 简单自定义starter - beetl

    使用idea新建springboot项目beetl-spring-boot-starter 首先添加pom依赖 packaging要设置为jar不能设置为pom<packaging>jar ...

  2. (springboot)自定义Starter

    要引入的jar项目,即自定义的Starter项目: pom:(这里不能引入springboot整合否则测试项目注入失败) <?xml version="1.0" encodi ...

  3. SpringBoot编写自定义Starter

    根据SpringBoot的Starter编写规则,需要编写xxxStarter依赖xxxAutoConfigurer,xxxStarter是一个空的jar,仅提供辅助性的依赖管理,引入其他类库 1.建 ...

  4. dubbo+zookeeper+springboot简单示例

    目录 dubbo+zookeeper+springboot简单示例 zookeeper安装使用 api子模块 生产者producer 消费者consumer @(目录) dubbo+zookeeper ...

  5. SpringBoot之旅第六篇-启动原理及自定义starter

    一.引言 SpringBoot的一大优势就是Starter,由于SpringBoot有很多开箱即用的Starter依赖,使得我们开发变得简单,我们不需要过多的关注框架的配置. 在日常开发中,我们也会自 ...

  6. SpringBoot应用篇(一):自定义starter

    一.码前必备知识 1.SpringBoot starter机制 SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在mave ...

  7. SpringBoot第十六篇:自定义starter

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11058502.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言   这一段时间 ...

  8. SpringBoot自定义starter及自动配置

    SpringBoot的核心就是自动配置,而支持自动配置的是一个个starter项目.除了官方已有的starter,用户自己也可以根据规则自定义自己的starter项目. 自定义starter条件 自动 ...

  9. SpringBoot Starter机制 - 自定义Starter

    目录 前言 1.起源 2.SpringBoot Starter 原理 3.自定义 Starter 3.1 创建 Starter 3.2 测试自定义 Starter 前言         最近在学习Sp ...

随机推荐

  1. php断点续传

    http://www.cnblogs.com/xproer/archive/2012/10/26/2741264.html

  2. 雅礼集训 2017 Day2 水箱 可并堆

    题目描述 给出一个长度为 n 宽度为 1 ,高度无限的水箱,有 n−1  个挡板将其分为 n 个 1 - 1 的小格,然后向每个小格中注水,水如果超过挡板就会溢出到挡板的另一边,这里的水是满足物理定律 ...

  3. redisCheckMem脚本

    最近维护的redis cluster需要扫描每个实例的内存使用率,首先我们需要获取实例已经使用的内存,获取实例的最大内存配额,两个值相比就能获取到内存使用比例. 实例的最大内存获取方法: $REDIS ...

  4. POJ3565 Ants 和 POJ2195 Going Home

    Ants Language:Default Ants Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7975 Accepted: ...

  5. ACM学习历程—HDU5490 Simple Matrix (数学 && 逆元 && 快速幂) (2015合肥网赛07)

    Problem Description As we know, sequence in the form of an=a1+(n−1)d is called arithmetic progressio ...

  6. Git远程克隆仓库出现Permission denied (publickey)

    $ git clone git@github.com:DavidWanderer/test1.git Cloning into 'test1'... Warning: Permanently adde ...

  7. 洛谷【P1561】[USACO12JAN]爬山Mountain Climbing

    我对\(Jhonson\)算法的理解:https://www.cnblogs.com/AKMer/p/9863620.html 题目传送门:https://www.luogu.org/problemn ...

  8. Poj 2299 Ultra-QuickSort(归并排序求逆序数)

    一.题意 给定数组,求交换几次相邻元素能是数组有序. 二.题解 刚开始以为是水题,心想这不就是简单的冒泡排序么.但是毫无疑问地超时了,因为题目中n<500000,而冒泡排序总的平均时间复杂度为, ...

  9. 删除老的Azure Blob Snapshot

    客户有这样的需求:每天需要对VM的数据进行备份,但如果备份的时间超过一定的天数,需要进行清除. 本文也是在前一篇Azure Blob Snapshot上的优化. "Azure blob St ...

  10. 类方法,实例方法,静态方法,@property的应用

    class test(object): h = 'hello' w = 'world' def demo(self): print("demo") def test_class(s ...