首先需要引入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. 转载 解决Android与服务器交互大容量数据问题

    对于目前的状况来说,移动终端的网络状况没有PC网络状况那么理想.在一个Android应用中,如果需要接收来自服务器的大容量数据,那么就不得不考虑客户的流量问题.本文根据笔者的一个项目实战经验出发,解决 ...

  2. I.MX6 FFmpeg 录制视频

    /************************************************************************* * I.MX6 FFmpeg 录制视频 * 说明: ...

  3. 什么是DMIPS

    MIPS: Million Instructions executed Per Second,每秒百万条指令,用来计算同一秒内系统的处理能力 DMIPS: Dhrystone Million Inst ...

  4. noip退役赛

    上下午 6 题一起考 自闭了 T1 小明要参加一场比赛,赛制给你一个表格 $p$ ,$p_{(i,j)}$ 表示他在第 $i$ 场比赛前如果输了 $j$ 场,他这一场赢的概率,他也可以故意输掉任意多场 ...

  5. windows cmd 看服务cpu、内存

    开始菜单-运行-cmd-输入systeminfo-回车 不用命令从以下两个地方都可以看出CPU个数 使用命令看CPU 利用win+r键打开运行,输入cmd回车即会出现 查看cpu信息 通过上图可以看出 ...

  6. bzoj 1001 狼抓兔子 —— 平面图最小割(最短路)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 平面图最小割可以转化成最短路问题: 建图时看清楚题目的 input ... 代码如下: ...

  7. IP 地址漂移

    1.概念 应用访问虚拟ip,当主服务器正常工作时,虚拟ip指向主服务器,当主服务器宕掉后,虚拟ip自动指向从服务器,当主服务器被人修好后,再自动指向主服务器, 这种虚拟ip的指向方式称为ip地址漂移. ...

  8. sysbench安装、使用

    二.编译安装 编译非常简单,可参考 README 文档,简单步骤如下:   cd/tmp/sysbench-0.4.12-1.1./autogen.sh./configure --with-mysql ...

  9. linux下dns设置详解

    DNS就是Domain Name System,它能够把形如www.21php.com这样的域名转换为211.152.50.35这样的IP地址;没有DNS,浏览21php.com这个网站时,就必须用2 ...

  10. JavaScript-Tool-富文本:UEditor

    ylbtech-JavaScript-Tool-富文本:UEditor UEditor是由百度WEB前端研发部开发的所见即所得的开源富文本编辑器,具有轻量.可定制.用户体验优秀等特点.开源基于BSD协 ...