本文源码:GitHub·点这里 || GitEE·点这里

一、Dubbo框架简介

1、框架依赖



图例说明:

1)图中小方块 Protocol, Cluster, Proxy, Service, Container, Registry, Monitor 代表层或模块,蓝色的表示与业务有交互,绿色的表示只对 Dubbo 内部交互。

2)图中背景方块 Consumer, Provider, Registry, Monitor 代表部署逻辑拓扑节点。

3)图中蓝色虚线为初始化时调用,红色虚线为运行时异步调用,红色实线为运行时同步调用。

4)图中只包含 RPC 的层,不包含 Remoting 的层,Remoting 整体都隐含在 Protocol 中。

2、核心角色说明

1)Provider 暴露服务的服务提供方

2)Consumer 调用远程服务的服务消费方(负载均衡)

3)Registry 服务注册与发现的注册中心(监控、心跳、踢出、重入)

4)Monitor 服务消费者和提供者在内存中累计调用次数和调用时间,主动定时每分钟发送一次统计数据到监控中心。

5)Container 服务运行容器:远程调用、序列化

二、与SpringBoot2.0整合

1、核心依赖

<!-- 这里包含了Zookeeper依赖和Dubbo依赖 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>

2、项目结构说明



结构说明

dubbo-consume:服务消费方
dubbo-provider:服务提供方
dubbo-common:公共代码块,Dubbo接口,实体类

3、核心配置

1)提供方配置

server:
tomcat:
uri-encoding: UTF-8
max-threads: 1000
min-spare-threads: 30
port: 7007
connection-timeout: 5000ms
spring:
application:
name: block-dubbo-provider
# Dubbo 配置文件
dubbo:
application:
name: block-dubbo-provider
registry:
address: 127.0.0.1:2181
protocol: zookeeper
protocol:
name: dubbo
port: 20880
scan:
base-packages: com.boot.consume

2)消费方配置

server:
tomcat:
uri-encoding: UTF-8
max-threads: 1000
min-spare-threads: 30
port: 7008
connection-timeout: 5000ms spring:
application:
name: block-dubbo-consume
# Dubbo 配置文件
dubbo:
application:
name: block-dubbo-consume
registry:
address: 127.0.0.1:2181
protocol: zookeeper

三、演示案例

1、服务远程调用

1)提供方服务接口

  • 注意这里的注解
  • com.alibaba.dubbo.config.annotation.Service
@Service
@Component
public class DubboServiceImpl implements DubboService {
private static Logger LOGGER = LoggerFactory.getLogger(DubboServiceImpl.class) ;
@Override
public String getInfo(String param) {
LOGGER.info("字符参数:{}",param);
return "[Hello,Cicada]";
}
@Override
public UserEntity getUserInfo(UserEntity userEntity) {
LOGGER.info("实体类参数:{}",userEntity);
return userEntity;
}
}

2)消费方接口

  • 注意这里注解
  • com.alibaba.dubbo.config.annotation.Reference
  • org.springframework.stereotype.Service
@Service
public class ConsumeService implements DubboService {
@Reference
private DubboService dubboService ;
@Override
public String getInfo(String param) {
return dubboService.getInfo(param);
}
@Override
public UserEntity getUserInfo(UserEntity userEntity) {
return dubboService.getUserInfo(userEntity);
}
}

2、接口超时配置

该配置可以在服务提供方配置,也可以在服务消费方配置,这里演示在提供方的配置。

注解:timeout

1)服务接口注解

@Service(timeout = 2000)
@Component
public class DubboServiceImpl implements DubboService {
}

2)消费方调用

 @Override
public String timeOut(Integer time) {
return dubboService.timeOut(time);
}

3)测试接口

服务超时抛出异常

com.alibaba.dubbo.remoting.TimeoutException

3、接口多版本配置

1)服务提供方

相同接口提供两个版本实现。注解:version。

版本一:

@Service(version = "1.0.0")
@Component
public class VersionOneImpl implements VersionService {
@Override
public String getVersion() {
return "{当前版本:1.0.0}";
}
}

版本二:

@Service(version = "2.0.0")
@Component
public class VersionTwoImpl implements VersionService {
@Override
public String getVersion() {
return "{当前版本:2.0.0}";
}
}

2)消费方调用

通过@Reference(version)注解,将指向不同版本的接口实现。

@Service
public class VersionServiceImpl implements VersionService {
@Reference(version = "1.0.0")
private VersionService versionService1 ;
@Reference(version = "2.0.0")
private VersionService versionService2 ;
@Override
public String getVersion() {
return versionService1.getVersion();
}
public String version2 (){
return versionService2.getVersion() ;
}
}

以上案例都是参照Dubbo官网的流程编写的,Dubbo许多强大功能都可以参考官网一步步的配置。

四、源代码地址

GitHub地址:知了一笑
https://github.com/cicadasmile/middle-ware-parent
码云地址:知了一笑
https://gitee.com/cicadasmile/middle-ware-parent

最新文章

  1. NOIP200806 火柴棒等式【B005】
  2. Mongodb Manual阅读笔记:CH9 Sharding
  3. An internal error occured during :&quot;C/C++&quot; . java.lang.NullPointerException
  4. hdu 1286:找新朋友(数论,欧拉函数)
  5. 在阿里云 CentOS 服务器(ECS)上搭建 nginx + mysql + php-fpm 环境
  6. web.xml中webAppRootKey
  7. ckeditor 升级到 4.5
  8. [iOS基础控件 - 3.4] 汤姆猫
  9. skip-grant-tables
  10. 什么是VSync
  11. Qwt安装(转)
  12. Css实现checkbox及radio样式自定义
  13. 统计git代码提交量
  14. IDEA 编译 Jmeter 4.0 ( 二次开发_1 )
  15. R语言学习网址
  16. python中的元组
  17. windows下angularJs环境搭建和遇到的问题解决
  18. [LeetCode&amp;Python] Problem 746. Min Cost Climbing Stairs
  19. Bootstrap 网格系统(Grid System)的工作原理 - 媒体查询
  20. poj_2774 后缀数组

热门文章

  1. django----Sweetalert bulk_create批量插入数据 自定义分页器
  2. item()方法遍历字典
  3. 两个实例轻松理解js函数预解析
  4. PyTorch-网络的创建,预训练模型的加载
  5. MongoDB(五):更新文档、删除文档
  6. Ubuntu1804中重新认识docker
  7. 在Mac上安装JDK1.8及环境变量配置
  8. JS---DOM---点击操作---part1---20个案例
  9. JavaScript 标准内置对象Promise使用学习总结
  10. ASP.NET MVC快速开发框架FastExecutor开发全过程感受及总结