首先需要引入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. Struts2 - 表单的重复提交问题

    用户重复提交表单在某些场合将会造成非常严重的后果.例如,在使用信用卡进行在线支付的时候,如果服务器的响应速度太慢,用户有可能会多次点击提交按钮,而这可能导致那张信用卡上的金额被消费了多次.因此,重复提 ...

  2. 【遍历二叉树】02二叉树的中序遍历【Binary Tree Inorder Traversal】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的中序遍历的 ...

  3. FFmpeg 的sws_getContext函数 、sws_scale函数

    FFmpeg里面的sws_scale库可以在一个函数里面同时实现:1.图像色彩空间转换:2.分辨率缩放:3.前后图像滤波处理. 其核心函数主要有三个: // 初始化sws_scalestruct Sw ...

  4. Gym - 101196:F Removal Game(区间DP)

    题意:一个环状数组,给定可以删去一个数,代价的相邻两个数的gcd,求最小代价. 思路:区间DP即可,dp[i][j]表示[i,j]区间只剩下i和j时的最小代价,那么dp[i][j]=min  dp[i ...

  5. POJ2891Strange Way to Express Integers (线性同余方程组)

    Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative ...

  6. Java中读取输入方式的性能比较

    程序开发过程中,需要从键盘获取输入值是常有的事,但Java它偏偏就没有像c语言给我们提供的scanf(),C++给我们提供的cin()获取键盘输入值的现成函数!Java没有提供这样的函数也不代表遇到这 ...

  7. Foreman-porxy负载均衡搭建

    本文接上篇puppet负载均衡的环境实验. Foreman-proxy可以采用四层或者七层负载,都可以实现,在foreman的web界面添加一个smart-proxy,后端多个真实foreman-pr ...

  8. web攻击之三:SQL注入攻击的种类和防范手段

    观察近来的一些安全事件及其后果,安全专家们已经得到一个结论,这些威胁主要是通过SQL注入造成的.虽然前面有许多文章讨论了SQL注入,但今天所讨论的内容也许可帮助你检查自己的服务器,并采取相应防范措施. ...

  9. JS中数组方法小总结

    1.array.concat(item……) 返回:一个新数组 该方法产生一个新数组,它包含一份array的浅复制,并把一个或多个参数item附加在其后.如果参数item是一个数组,那么它的每个元素会 ...

  10. CSDN优秀博客连接,博客之星连接。

    点击链接 获得[红杏出墙]插件,FQ上网无压力!谷歌搜索无压力! 2013年度CSDN十大博客之星 TOP 作者 专注领域 博客地址 邹晓艺 机器学习及算法 zouxy09 2 王然 潜在的集大成者 ...