这里主要是按照teaey作者的spring-boot-starter-dubbo框架进行一些变化的使用

依赖包:

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>

注意dubbo里面要去除mybatis依赖,不然会跟现有的冲突

工具类库:

DubboProperties:

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.MethodConfig;
import com.alibaba.dubbo.config.ModuleConfig;
import com.alibaba.dubbo.config.MonitorConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; /**
* Created by qhong on 2018/12/29 11:18
**/
@ConfigurationProperties(prefix = "spring.dubbo")
@Data
public class DubboProperties { private ApplicationConfig application; private RegistryConfig registry; private ProtocolConfig protocol; private MonitorConfig monitor; private ProviderConfig provider; private ModuleConfig module; private MethodConfig method; private ConsumerConfig consumer; }

DubboAutoConfiguration

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.MethodConfig;
import com.alibaba.dubbo.config.ModuleConfig;
import com.alibaba.dubbo.config.MonitorConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* Created by qhong on 2018/12/29 11:19
**/
@Configuration
@EnableConfigurationProperties(DubboProperties.class)
public class DubboAutoConfiguration {
@Autowired
private DubboProperties dubboProperties; @Bean
public ApplicationConfig requestApplicationConfig() {
ApplicationConfig applicationConfig = dubboProperties.getApplication();
if (applicationConfig == null) {
applicationConfig = new ApplicationConfig();
}
return applicationConfig;
} @Bean
public RegistryConfig requestRegistryConfig() {
RegistryConfig registryConfig = dubboProperties.getRegistry();
if (registryConfig == null) {
registryConfig = new RegistryConfig();
}
return registryConfig;
} @Bean
public ProtocolConfig requestProtocolConfig() {
ProtocolConfig protocolConfig = dubboProperties.getProtocol();
if (protocolConfig == null) {
protocolConfig = new ProtocolConfig();
}
return protocolConfig;
} @Bean
public MonitorConfig requestMonitorConfig() {
MonitorConfig monitorConfig = dubboProperties.getMonitor();
if (monitorConfig == null) {
monitorConfig = new MonitorConfig();
}
return monitorConfig;
} @Bean
public ProviderConfig requestProviderConfig() {
ProviderConfig providerConfig = dubboProperties.getProvider();
if (providerConfig == null) {
providerConfig = new ProviderConfig();
}
return providerConfig;
} @Bean
public ModuleConfig requestModuleConfig() {
ModuleConfig moduleConfig = dubboProperties.getModule();
if (moduleConfig == null) {
moduleConfig = new ModuleConfig();
}
return moduleConfig;
} @Bean
public MethodConfig requestMethodConfig() {
MethodConfig methodConfig = dubboProperties.getMethod();
if (methodConfig == null) {
methodConfig = new MethodConfig();
}
return methodConfig;
} @Bean
public ConsumerConfig requestConsumerConfig() {
ConsumerConfig consumerConfig = dubboProperties.getConsumer();
if (consumerConfig == null) {
consumerConfig = new ConsumerConfig();
}
return consumerConfig;
} }

服务提供者:

import com.alibaba.dubbo.config.ServiceConfig;
import com.alibaba.dubbo.config.annotation.Service;
import com.jsy.payment.api.CustomerOpenAccountFacade;
import com.jsy.payment.api.PaymentFacade;
import com.jsy.payment.api.PaymentQueryFacade;
import io.dubbo.springboot.DubboProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration; import javax.annotation.PostConstruct; /**
* Dubbo服务配置BEAN风格暴露
*
*/
@Configuration
public class DubboProviderConfig implements ApplicationContextAware { private DubboProperties dubboProperties; private ApplicationContext applicationContext; private final static Logger LOGGER = LoggerFactory.getLogger(ExportConfig.class); @Value("${spring.dubbo.export.group}")
private String exportGroup; /**
* 暴露接口
*/
public static Class<?>[] EXPORT_SERVICE_CLASSES = null; static {
EXPORT_SERVICE_CLASSES = new Class<?>[] {
PaymentFacade.class,
PaymentQueryFacade.class,
CustomerOpenAccountFacade.class
};
} @PostConstruct
public void init() {
exportServices(EXPORT_SERVICE_CLASSES);
} private void exportServices(Class<?>[] exportServiceClasses) {
if (exportServiceClasses != null) {
for (Class<?> serviceInterfaceClaz : exportServiceClasses) {
try {
Service serviceAnnotation = serviceInterfaceClaz.getAnnotation(Service.class);
ServiceConfig serviceConfig = serviceAnnotation == null? new ServiceConfig<>() : new ServiceConfig<>(serviceAnnotation);
serviceConfig.setApplication(dubboProperties.getApplication());
serviceConfig.setRegistry(dubboProperties.getRegistry()); // 多个注册中心可以用setRegistries()
serviceConfig.setProtocol(dubboProperties.getProtocol()); // 多个协议可以用setProtocols()
serviceConfig.setInterface(serviceInterfaceClaz);
serviceConfig.setRef(this.applicationContext.getBean(serviceInterfaceClaz));
serviceConfig.setVersion(dubboProperties.getRegistry().getVersion()); // 接口暴露版本号
serviceConfig.setGroup(exportGroup);
serviceConfig.export();
LOGGER.info("Service:{} exported successfully.", serviceInterfaceClaz.getName());
} catch (Exception e) {
LOGGER.error("Service interface export error happened, interface className:{}, err:{}", serviceInterfaceClaz.getName(), e);
}
}
}
} @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
this.dubboProperties = this.applicationContext.getBean(DubboProperties.class);
}
}

application.properties:

# dubbo配置
spring.dubbo.application.name=assets-payment-server
spring.dubbo.registry.address=zookeeper://18.16.200.41:2181?backup=18.16.200.43:2181,18.16.200.44:2181
spring.dubbo.registry.timeout=300000
spring.dubbo.registry.version=1.0.0
spring.dubbo.registry.group=
spring.dubbo.protocol.port=20882
## 本机的IP地址
spring.dubbo.protocol.host=
spring.dubbo.protocol.name=dubbo
spring.dubbo.scan=
## 设置Module
spring.dubbo.module.default=false spring.dubbo.export.group=ddv

这个方法不需要使用dubbo原生的@Service,这样就可以随时切换esb等其他rpc框架,低耦合

服务消费者:

DubboConsumerConfig:

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.jsy.payment.api.CustomerOpenAccountFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class DubboConsumeConfig { @Value("${spring.dubbo.reference.group}")
private String group;
@Value("${spring.dubbo.reference.version}")
private String version;
@Value("${spring.dubbo.reference.protocol}")
private String protocol; @Autowired
private RegistryConfig registryConfig;
@Autowired
private ConsumerConfig consumerConfig;
@Autowired
private ApplicationConfig applicationConfig; private void setCommon(ReferenceConfig reference) {
reference.setRegistry(registryConfig);
reference.setConsumer(consumerConfig);
reference.setApplication(applicationConfig);
reference.setProtocol(protocol);
reference.setVersion(version);
if(StringUtils.isNotBlank(group)){
reference.setGroup(group); //解决生产double引用product1.2的注册服务问题
}
} @Bean
public CustomerOpenAccountFacade getCustomerOpenAccountFacade() {
ReferenceConfig<CustomerOpenAccountFacade> reference = new ReferenceConfig<>();
reference.setInterface(CustomerOpenAccountFacade.class);
setCommon(reference);
CustomerOpenAccountFacade customerOpenAccountFacade = reference.get();
return customerOpenAccountFacade;
}
}

application.properties:

# dubbo配置
spring.dubbo.application.name=huishi-server
spring.dubbo.registry.address=zookeeper://18.16.200.41:2181?backup=18.16.200.43:2181,18.16.200.44:2181
spring.dubbo.registry.check=true
spring.dubbo.registry.register=true
spring.dubbo.registry.timeout=10000
spring.dubbo.registry.group= spring.dubbo.consumer.timeout=30000
spring.dubbo.consumer.retries=0
#spring.dubbo.consumer.check=true spring.dubbo.reference.group=DEV
spring.dubbo.reference.version=1.0.0
spring.dubbo.reference.protocol=dubbo

然后直接调用CustomerOpenAccountFacade实例即可。

参考:

官方文档

incubator-dubbo 源码

SpringBoot整合dubbo

Spring boot 整合Dubbo

Springboot 整合 Dubbo/ZooKeeper 详解 SOA 案例

apache/incubator-dubbo-spring-boot-project

Springboot+dubbo 解决@Reference 注解为null情况

Dubbo+SpringBoot整合: 依赖注入Null Pointer Exception的问题

使用spring-boot-starter-dubbo

teaey/spring-boot-starter-dubbo

SpringBoot 整合使用dubbo的更多相关文章

  1. 【java框架】SpringBoot(5)--SpringBoot整合分布式Dubbo+Zookeeper

    1.理论概述 1.1.分布式 分布式系统是若干独立计算机的集合,这些计算机对于用户来讲就像单个系统. 由多个系统集成成一个整体,提供多个功能,组合成一个板块,用户在使用上看起来是一个服务.(比如淘宝网 ...

  2. SpringBoot 整合 Dubbo 进行分布式开发

    自从Dubbo支持SpringBoot后,Dubbo与Spring的整合变得更加的简单了,下面就是完整的步骤: 1. 引入依赖 <dependency> <groupId>co ...

  3. springboot整合dubbo\zookeeper做注册中心

    springboot整合dubbo发布服务,zookeeper做注册中心.前期的安装zookeeper以及启动zookeeper集群就不说了. dubbo-admin-2.5.4.war:dubbo服 ...

  4. 【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)

    http://blog.csdn.net/a67474506/article/details/61640548 Dubbo是什么东西我这里就不详细介绍了,自己可以去谷歌 SpringBoot整合Dub ...

  5. SpringBoot整合Zookeeper和Dubbo

    一.Dubbo 1. Dubbo定义 Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合).从服务模型的角度来 ...

  6. SpringBoot整合dubbo(yml格式配置)

    yml文件 如果只作为服务的消费者不用暴露端口号,扫描的包名根据自己service改 dubbo: application: name: springboot-dubbo-demo #应用名 regi ...

  7. Springboot整合Dubbo和Zookeeper

    Dubbo是一款由阿里巴巴开发的远程服务调用框架(RPC),其可以透明化的调用远程服务,就像调用本地服务一样简单.截至目前,Dubbo发布了基于Spring Boot构建的版本,版本号为0.2.0,这 ...

  8. dubbo学习实践(4)之Springboot整合Dubbo及Hystrix服务熔断降级

    1. springboot整合dubbo 在provider端,添加maven引入,修改pom.xml文件 引入springboot,版本:2.3.2.RELEASE,dubbo(org.apache ...

  9. spring-boot整合dubbo:Spring-boot-dubbo-starter

    为什么要写这个小工具 如果你用过Spring-boot来提供dubbo服务,相信使用中有很多"不爽"的地方.既然使用spring boot,那么能用注解的地方绝不用xml配置,这才 ...

随机推荐

  1. eclipse设置字体_字符编码_快捷键

    eclipse设置字体.字符编码.快捷键 1.设置字体: preferences->general->appearnce->colors and fonts-->basic-- ...

  2. 页面每隔n分钟轮换一个微信名和微信名

    1.前端index.html <head> <meta charset="UTF-8"> <title>n号循环</title> & ...

  3. Vue系列之 => 全局,私有过滤器

    私有过滤器也称局部过滤器 <script> // 全局过滤器 Vue.filter("datatime",function(timestr){ var tm = new ...

  4. hive的find_in_set函数

    集合查找函数: find_in_set语法: find_in_set(string str, string strList) 返回值: int说明: 返回str在strlist第一次出现的位置,str ...

  5. crontab 在指定时间范围每隔2小时执行一次和指定时间执行实例

    crontab 在指定时间范围每隔2小时执行一次和指定时间执行,下面实例实现了:10-23点每两个小时执行一次,2点执行一次,分钟依次是1 2 3 ,没有24点的,晚上12点是0点注:*代表所有的取值 ...

  6. [转载]localStorage使用总结

    一.什么是localStorage.sessionStorage 在HTML5中,新加入了一个localStorage特性,这个特性主要是用来作为本地存储来使用的,解决了cookie存储空间不足的问题 ...

  7. mac shell终端编辑命令行快捷键——行首,行尾

    Ctrl + d        删除一个字符,相当于通常的Delete键(命令行若无所有字符,则相当于exit:处理多行标准输入时也表示eof) Ctrl + h        退格删除一个字符,相当 ...

  8. checkbox 全选效果

    html部分 <p id="all">全选</p> <input type="checkbox" /><br/> ...

  9. 面试必问之JVM篇

    前言 只有光头才能变强 JVM在准备面试的时候就有看了,一直没时间写笔记.现在到了一家公司实习,闲的时候就写写,刷刷JVM博客,刷刷电子书. 学习JVM的目的也很简单: 能够知道JVM是什么,为我们干 ...

  10. CXF+Spring+Hibernate实现RESTful webservice服务端实例

    1.RESTful API接口定义 /* * Copyright 2016-2017 WitPool.org All Rights Reserved. * * You may not use this ...