首先需要引入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之Widget学习总结

    1.Widget设计步骤 需要修改三个XML,一个class: 1)第一个xml是布局XML文件(如:main.xml),是这个widget的.一般来说如果用这个部件显示时间,那就只在这个布局XML中 ...

  2. 编写dockerfile

    参考:http://www.cnblogs.com/liuyansheng/p/6098470.html 一.dockerfile介绍: 是一种被Docker程序解释的脚本,Dockerfile由一条 ...

  3. ES6学习之字符串的扩展

    字符的Unicode表示法 \uxxxx可以表示一个双字节的字符(xxxx表示字符编码,范围在0000---FFFF),超出此范围用两个双字节表示. console.log("\u0061& ...

  4. man syslog | col -b > syslog.txt

    linux man手册导出,解决man乱码 man syslog | col -b > syslog.txt

  5. 关于Confusion Matrix

    from sklearn.metrics import confusion_matrixy_true = [2, 0, 2, 2, 0, 1]y_pred = [0, 0, 2, 2, 0, 2]pr ...

  6. rlwrap:让控制台程序使用input.rc里的设置

    习惯了vi模式,在bash里配置input.rc可以使平时命令行操作都使用vi习惯,翻页和跳动光标都很方便 但是当运行一些交互式程序时,比如redis-cli,lua等,它们却不按input.rc的配 ...

  7. R: factor & list 生成和操作因子、列表

    ################################################### 问题:生成.操作列表 & 因子   18.4.27 怎么生成列表 list.因子 fac ...

  8. 使用python ftplib包递归下载文件夹及文件

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2018-06-11 09:35:49 # @Author : Yaheng Wang ...

  9. LEADTOOLS V19: 世界领先的图像处理开发工具包强势来袭

      投递人 itwriter 发布于 2014-12-22 16:04 评论(0) 有214人阅读   原文链接  [收藏]   « » LEAD 科技于 2014 年 12 月 11 日发布 LEA ...

  10. c/c++进制转换练习

    1 下列数最大的是( ).括号内为数字,括号外为进制.(360集团) (10010101)2 (227)8------>10010111 (96)16------>10010110 (14 ...