这里主要是按照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. 学习笔记<4>初步控件布局

    一.控件布局基本概念 指控制控件在Activity当中的位置.大小.颜色以及其他控件样式属性 二.控件布局两种方法 1.使用布局文件完成控件布局(eclipse可视化拖拽控件实现) 2.在JAVA代码 ...

  2. 前端-CSS样式

    一.CSS介绍 CSS(Cascading Style Sheet),全称层叠样式,定义如何显示HTML内的元素,浏览器读取HTML文件时,读取到CSS样式时根据CSS规则来对内容进行渲染 1.CSS ...

  3. scrapy:get cookie from response

    scrapy shell fetch('your_url') response.headers.getlist("Set-Cookie")https://stackoverflow ...

  4. codeforces 979C Kuro and Walking Route

    题意: 给出一棵树,其中有两个点,x和y,限制走了x之后的路径上不能有y,问可以走的路径(u,v)有多少条,(u,v)和(v,u)考虑为两条不同的路径. 思路: 简单树形dp,dfs统计在x到y路径( ...

  5. highcharts插件

    详见官网:https://www.highcharts.com.cn/demo/highcharts 详细使用代码: <!DOCTYPE html> <html lang=" ...

  6. Future复习笔记

    1. Future就是对于具体的Runnable或者Callable任务的执行结果进行取消.查询是否完成.获取结果.必要时可以通过get方法获取执行结果,该方法会阻塞直到任务返回结果. Future类 ...

  7. 转:三值逻辑与NULL的处理方式

    来自:<Microsoft SQL SERVER 2008技术内幕 T-SQL查询>P7 在SQL中谓词(逻辑表达式)的可能值为TRUE.FALSE和UNKNOWN.这就是所谓的三值逻辑, ...

  8. windows下多个python版本共存,如何在Windows7系统上安装最新的64位Python3.6.2

    windows下多个python版本共存,如何在Windows7系统上安装最新的64位Python3.6.2 1.官网下载python3.6.2https://www.python.org/ftp/p ...

  9. highchart应用示例1--2个不同类型变量2个y轴

    1.ajax调用接口和处理数据 function getCityData() { var date1 = $('#datetimepicker1').val(); var date2 = $('#da ...

  10. ajax 检测用户名是否可用

    下面是一个 ajax 检测用户名是否可用的例子. django  项目中. —— views.py 里—— from django.shortcuts import render,HttpRespon ...