springboot 简单自定义starter - dubbo
首先需要引入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的更多相关文章
- springboot 简单自定义starter - beetl
使用idea新建springboot项目beetl-spring-boot-starter 首先添加pom依赖 packaging要设置为jar不能设置为pom<packaging>jar ...
- (springboot)自定义Starter
要引入的jar项目,即自定义的Starter项目: pom:(这里不能引入springboot整合否则测试项目注入失败) <?xml version="1.0" encodi ...
- SpringBoot编写自定义Starter
根据SpringBoot的Starter编写规则,需要编写xxxStarter依赖xxxAutoConfigurer,xxxStarter是一个空的jar,仅提供辅助性的依赖管理,引入其他类库 1.建 ...
- dubbo+zookeeper+springboot简单示例
目录 dubbo+zookeeper+springboot简单示例 zookeeper安装使用 api子模块 生产者producer 消费者consumer @(目录) dubbo+zookeeper ...
- SpringBoot之旅第六篇-启动原理及自定义starter
一.引言 SpringBoot的一大优势就是Starter,由于SpringBoot有很多开箱即用的Starter依赖,使得我们开发变得简单,我们不需要过多的关注框架的配置. 在日常开发中,我们也会自 ...
- SpringBoot应用篇(一):自定义starter
一.码前必备知识 1.SpringBoot starter机制 SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在mave ...
- SpringBoot第十六篇:自定义starter
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11058502.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言 这一段时间 ...
- SpringBoot自定义starter及自动配置
SpringBoot的核心就是自动配置,而支持自动配置的是一个个starter项目.除了官方已有的starter,用户自己也可以根据规则自定义自己的starter项目. 自定义starter条件 自动 ...
- SpringBoot Starter机制 - 自定义Starter
目录 前言 1.起源 2.SpringBoot Starter 原理 3.自定义 Starter 3.1 创建 Starter 3.2 测试自定义 Starter 前言 最近在学习Sp ...
随机推荐
- 2017-2018-1 20179203 《Linux内核原理与分析》第七周作业及第三周测试总结
攥写人:李鹏举 学号:20179203 ( 原创作品转载请注明出处) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163.com/course/US ...
- dbcc练习1
dbcc tranceon(2588,3604) dbcc ind() dbcc page()
- 比线程更NB的存在
阅读目录 一 引子 二 协程介绍 三 Greenlet模块 四 Gevent模块 引子 之前我们学习了线程.进程的概念,了解了在操作系统中进程是资源分配的最小单位,线程是CPU调度的最小单位.按道理来 ...
- ACM学习历程—SNNUOJ1132 余数之和(数论)
Description F(n) = (n % 1) + (n % 2) + (n % 3) + ...... (n % n).其中%表示Mod,也就是余数.例如F(6) = 6 % 1 + 6 % ...
- ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)
Problem Description One day, a useless calculator was being built by Kuros. Let's assume that number ...
- ACM学习历程—HDU 5012 Dice(ACM西安网赛)(bfs)
Problem Description There are 2 special dices on the table. On each face of the dice, a distinct num ...
- BZOJ1067:[SCOI2007]降雨量
浅谈\(RMQ\):https://www.cnblogs.com/AKMer/p/10128219.html 题目传送门:https://lydsy.com/JudgeOnline/problem. ...
- log4net 使用
1. 代码中使用配置文件: log4net.Config.DOMConfigurator.Configure(new FileInfo("log4netConfig.xml")); ...
- varnish安装和配置
实验环境:CentOS7 Varnish是高性能开源的反向代理服务器和HTTP缓存服务器. #varnish服务器:172.16.252.142 [root@varnish localhost]#yu ...
- cdh 安装组件 异常总结
hive 启动 要 把mysql的jar包放到/opt/cloudera/parcels/CDH-5.9.3-1.cdh5.9.3.p0.4/lib/hive/lib 下 假设有3个节点就要放3次