springboot dubbo的java配置方式

引入jar包

   <dubbo.version>2.6.2</dubbo.version><curator.version>2.12.0</curator.version><zookeeper.version>3.4.3</zookeeper.version><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>${dubbo.version}</version><exclusions><exclusion><groupId>org.springframework</groupId><artifactId>*</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>${curator.version}</version></exclusions></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>${zookeeper.version}</version></dependency>

配置yaml

spring:## dubbo配置dubbo:application:name: spring-boot-template-devregistry:##注册中心地址 可以配置多个address: xxxxxxxx##dubbo服务test:##版本version: 2.0.0init: truecheck: trueservice:tradeTest:version: 1.0.0-test1

基础配置

import com.alibaba.dubbo.config.spring.ReferenceBean;
import com.alibaba.dubbo.config.spring.ServiceBean;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;/*** @Author: shadow* @Date:: 2020-06-24 14:22*/
public class BaseDubboConfig implements EnvironmentAware {protected static final String DUBBO_PREFIX = "spring.dubbo.";protected Environment environment;/*** 消费者*/protected <T> ReferenceBean<T> referenceBean(Class<T> clazz, String serverName) {String version = environment.getProperty(DUBBO_PREFIX + serverName + ".version");return referenceBean(clazz, version, 3);}protected <T> ReferenceBean<T> referenceBean(Class<T> clazz, String version, int retry) {return referenceBean(clazz, version, null, retry);}protected <T> ReferenceBean<T> referenceBean(Class<T> clazz, String version, String filter, int retrty) {ReferenceBean ref = new ReferenceBean<T>();ref.setInterface(clazz);ref.setRetries(retrty < 0 ? 3 : retrty);ref.setVersion(version);if (StringUtils.isNotEmpty(filter)) {ref.setFilter(filter);}return ref;}protected <T> ReferenceBean<T> referenceBeanAndCheck(ReferenceBean<T> ref, String serverName) {String init = environment.getProperty(DUBBO_PREFIX + serverName + ".init");String check = environment.getProperty(DUBBO_PREFIX + serverName + ".check");String timeout = environment.getProperty(DUBBO_PREFIX + ".timeout", "5000");ref.setInit(StringUtils.isBlank(init) ? true : Boolean.valueOf(init));ref.setCheck(StringUtils.isBlank(check) ? true : Boolean.valueOf(check));ref.setTimeout(Integer.valueOf(timeout));return ref;}/*** 生产者*/protected <T> ServiceBean<T> serviceBean(Class<T> clazz, T claxx, String version) {return serviceBean(clazz, claxx, version, 0);}protected <T> ServiceBean<T> serviceBean(Class<T> clazz, T claxx, String version, int timout) {return serviceBean(clazz, claxx, version, timout, 0);}protected <T, S> ServiceBean<T> serviceBean(Class<T> clazz, S claxx, String version, int timeOut, int retry) {ServiceBean serviceBean = new ServiceBean();serviceBean.setInterface(clazz);serviceBean.setRef(claxx);serviceBean.setTimeout(timeOut < 0 ? 5000 : timeOut);serviceBean.setRetries(retry < 3 ? 3 : retry);serviceBean.setVersion(version);return serviceBean;}public <T> ServiceBean<T> createServiceBean(String inter, T clazz, String version, Integer retries, Integer timout) {ServiceBean<T> serviceBean = new ServiceBean();serviceBean.setInterface(inter);serviceBean.setRef(clazz);serviceBean.setVersion(version);serviceBean.setRetries(retries != null ? retries : 3);serviceBean.setTimeout(timout != null ? timout : 5000);return serviceBean;}@Overridepublic void setEnvironment(Environment environment) {this.environment = environment;}
}

消费服务

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.ReferenceBean;
import com.springboot.dubbo.service.TestService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @Author: shadow* @Date:: 2020-06-24 14:53* dubbo消费者配置*/
@Configuration
public class ConsumeDubboConfig extends BaseDubboConfig {/*** 注册test服务**/@Beanpublic ReferenceBean<TestService> secretRequest() {ReferenceBean<TestService> bean = referenceBean(TestService.class, "test");return referenceBeanAndCheck(bean, "test");}/*** 基本配置*/@Beanpublic ProtocolConfig protocolConfig() {ProtocolConfig protocolConfig = new ProtocolConfig();protocolConfig.setThreadpool("cached");protocolConfig.setThreads(20);protocolConfig.setDispatcher("all");return protocolConfig;}/*** 默认注册地址*/@Beanpublic RegistryConfig registry() {RegistryConfig registryConfig = new RegistryConfig();registryConfig.setAddress(environment.getProperty(DUBBO_PREFIX + "registry.address"));registryConfig.setProtocol(environment.getProperty(DUBBO_PREFIX + "registry.protocol", "zookeeper"));return registryConfig;}@Beanpublic ApplicationConfig application() {ApplicationConfig applicationConfig = new ApplicationConfig();applicationConfig.setName(environment.getProperty(DUBBO_PREFIX + "application.name"));applicationConfig.setOwner(environment.getProperty(DUBBO_PREFIX + "application.owner"));return applicationConfig;}}

简单的消费服务创建成功

提供服务


import com.alibaba.dubbo.config.spring.ServiceBean;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @Author: shadow* @Date:: 2020-07-14 20:55*/
@Configuration
@AllArgsConstructor
public class ProvideDubboConfig extends BaseDubboConfig {private final TestProvideServiceImpl test1ProvideService;@Beanpublic ServiceBean<TestProvideService> tradeSyncFacade() {String version = environment.getProperty(DUBBO_PREFIX + "service.tradeTest.version");return serviceBean(TestProvideService.class, test1ProvideService, version);}}

springboot dubbo的java配置相关推荐

  1. springboot dubbo负载均衡配置

    springboot dubbo负载均衡配置 1.多zookeeper yml配置 2.负载配置 random roundRobin leastActive consistentHash 1.多zoo ...

  2. SpringBoot+Dubbo整合

    说明: 由于此文章是博主2017年学习时写的文章,项目dubbo版本过低,新项目不应该继续采用此版本.阿里巴巴已经将dubbo重新维护并捐给Apache开源组织,新起了比较新版本的项目整合.学习整合的 ...

  3. dubbo yml配置_利用springboot+dubbo,构建分布式微服务,全程注解开发(一)

    随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 一.先来一张图 说起 Dubbo,相信大家都不 ...

  4. 常用注解[spring 的 java 配置] ||springboot 热部署

    常用注解[spring 的 java 配置] 回顾学过的 spring 的注解 @Controller @RestController @Service @Repository @Component ...

  5. SpringBoot中使用AMQ的两种方式(Java配置、注解方式)

    Java配置方式使用AMQ 1. 引入依赖(pom.xml) <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ...

  6. springboot dubbo 多模块项目dubbo提供者和消费者配置及代码

    注:本文只是介绍我成功使用springboot dubbo 多模块项目的配置及核心代码,若问题没得到解决或需要可运行的源码,文章末尾有说明. springboot集成dubbo过程坑太多,dubbo提 ...

  7. spring之java配置(springboot推荐的配置方式)

    java配置概述 java配置各个注解的作用 /*** 该类是一个配置类,它的作用和bean.xml是一样的* spring中的新注解* Configuration* 作用:指定当前类是一个配置类* ...

  8. SpringBoot+Dubbo集成ELK实战

    一直以来,日志始终伴随着我们的开发和运维过程.当系统出现了Bug,往往就是通过Xshell连接到服务器,定位到日志文件,一点点排查问题来源. 随着互联网的快速发展,我们的系统越来越庞大.依赖肉眼分析日 ...

  9. springboot+dubbo+mybatis(注册中心使用的是zookeeper)学习

    1.dubbo简单介绍(废话节点) Dubbo开始于电商系统,是一款分布式服务框架,拥有高性能和透明化的RPC远程服务调用方案以及SOA服务治理方案.她每天为2千多个服务提供大于30亿次访问量支持,并 ...

最新文章

  1. 利用python安装opencv_Linux下安装OpenCV+Python支持
  2. 《剑指Offer》题目:树的子结构
  3. iOS点滴- ViewController详解
  4. LIVE555建立RTSP服务记录
  5. USB外接摄像头不能用怎么办
  6. 浅谈JSP自定义标签实现过程
  7. CMake with WinMinGW
  8. Tomcat 学习过程4
  9. 【白皮书分享】2020全球数字治理白皮书.pdf(附下载链接)
  10. 开发者必备的6款源码搜索引擎
  11. 使用webgl(three.js)搭建一个3D智慧园区、3D建筑,3D消防模拟,web版3D,bim管理系统——第四课(炫酷版一)...
  12. BS7799,ISO17799与ISO27001的关系
  13. 游戏使用html签名,利用HTML5实现电子签名板文字涂鸦代码
  14. 银河麒麟桌面操作系统V10node.js 14安装小助手
  15. 华为正式发布方舟编译器,相关源码已开放下载;微软开源量子开发工具包 QDK;GitHub回应突然断供:也很无可奈何的样子……...
  16. cmd返回上一级和根目录
  17. 堆内存和栈内存详解(转载)
  18. 基于SpringBoot+MyBatis的餐饮点餐系统
  19. 《牧羊少年奇幻之旅》读书笔记
  20. .net基础学java系列(五)慢性自杀 之 沉沦在IDE中

热门文章

  1. c语言检测tft是否有触摸,带有触摸数据(包括各种历史记录的全套数据)的2.4英寸TFT_LCD...
  2. 原理+代码|深入浅出Python随机森林预测实战
  3. 二分查找详解(Java)
  4. javascript将JSON数据导出为Excle表格
  5. 阿里云IoT物联网平台
  6. 一起自学SLAM算法:7.4 基于贝叶斯网络的状态估计
  7. Open3D-GUI系列教程(三)界面布局
  8. 如何用PADS建立元件封装
  9. rsync 匹配通配符 * 失败 link_stat failed: No such file or directory
  10. 【DSP实验】软件仿真方法在VisualDSP++中用延时线实现Riitta Schroeder混响算法