Dubbo架构

Dubbo是Java的RPC框架,具有三大核心功能:面向接口的远程方法调用,智能容错和负载均衡,以及服务的自动注册和发现

Dubbo架构图:

 

节点角色说明:

节点 说明
Provider 需要暴露服务的服务提供方
Consumer 调用远程服务的服务消费方
Registry 服务注册与发现的注册中心
Monitor 统计服务次数和调用时间的监控中心
Container

服务运行容器

调用关系说明:

0. Container负责启动、加载、运行Provider

1. Provider在启动时,向Registry注册自己提供的服务

2. Consumer在启动时,向Registry订阅自己所需的服务

3. Registry返回Provider地址列表给Consumer,若地址有变更,将基于长连接推送变更数据给Consumer

4. Consumer从Provider地址列表中,基于软负载均衡算法,选一台Provider进行调用

5. Consumer和Provider在内存中累计调用次数和调用时间,定时每分钟发一次统计数据到Monitor

模块目录说明:

1. API:存放service接口以及实体类、数据传输对象等

2. Consumer:消费者目录,远程调用Provider提供的接口实现,即交互层

3. Provider:生产者目录,提供给Consumer的接口实现类

Dubbo整合

写个SpringBoot整合Dubbo的demo,清晰明了地了解这几层分层的含义,以及Dubbo的相关配置

1)API层

UserInfo实体类

 1 @ToString
2 @AllArgsConstructor
3 @NoArgsConstructor
4 @Data
5 public class userInfo implements Serializable {
6 /**
7 * Serializable 序列化
8 * 实体类必须实现序列化接口
9 **/
10 private Long id;
11 private String userName;
12 private Integer userId;
13 private String userEmail;
14 private String userAddress;
15 private String userGender;
16 }

UserInfoService接口

1 public interface UserInfoService {
2 public List<userInfo> getUserInfo();
3 }

2)Provider层

pom.xml添加相关依赖

 1 <dependencies>
2 <!-- 添加api依赖 -->
3 <dependency>
4 <groupId>com.tttori</groupId>
5 <artifactId>api</artifactId>
6 <version>0.0.1-SNAPSHOT</version>
7 <scope>compile</scope>
8 </dependency>
9
10 <!-- dubbo依赖 -->
11 <dependency>
12 <groupId>com.alibaba.boot</groupId>
13 <artifactId>dubbo-spring-boot-starter</artifactId>
14 <version>0.2.0</version>
15 </dependency>
16
17 <!-- zookeeper cli端 -->
18 <dependency>
19 <groupId>org.apache.curator</groupId>
20 <artifactId>curator-framework</artifactId>
21 <version>2.13.0</version>
22 </dependency>
23 </dependencies>

dubbo-config.xml配置dubbo

ServiceImpl类使用@Service("x")注解,其中x与配置中ref一致

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 声明服务名dubbo-provider -->
<dubbo:application name="dubbo-provider"/> <!-- 声明zookeeper注册中心,暴露服务地址127.0.0.1:2181 -->
<dubbo:registry id="tttori" protocol="zookeeper" address="127.0.0.1:2181"/> <!-- 使用dubbo协议,暴露服务端口随机 -->
<dubbo:protocol name="dubbo" port="-1"/> <!-- 暴露服务接口,ref指向实现对象 -->
<dubbo:service interface="com.tttori.api.service.UserInfoService" ref="userInfoService" registry="tttori" timeout="5000"/> <dubbo:consumer check="false" timeout="5000"/> </beans>

UserInfoServiceImpl提供服务的接口实现类

 1 @Service("userInfoService") //dubbo包下的Service 暴露服务
2 @Component //POJO实例化入容器中
3 public class UserInfoServiceImpl implements UserInfoService{
4 @Override
5 public List<userInfo> getUserInfo(){
6 userInfo userinfo1 = new userInfo(1001L,"Jack",1413112,"01414112@qq.com","China","M");
7 userInfo userinfo2 = new userInfo(1002L,"Dell",1414116,"01414116@qq.com","China","M");
8 return Arrays.asList(userinfo1,userinfo2);
9 }
10 }

ProviderApplication主启动类

 1 @SpringBootApplication
2 @ComponentScan(basePackages = {
3 "com.tttori.provider.serviceImpl"
4 })
5 @MapperScan(basePackages = {"com.tttori.provider.mapper"})
6 @ImportResource(locations = {"classpath:dubbo-config.xml"})
7 @EnableScheduling
8 public class ProviderApplication {
9
10 public static void main(String[] args) {
11 SpringApplication.run(ProviderApplication.class, args);
12
13 try {
14 System.in.read();
15 } catch (IOException e) {
16 e.printStackTrace();
17 }
18 }
19
20 }

3)Consumer层

pom.xml添加API的依赖

 1     <dependencies>
2 <!-- api依赖 -->
3 <dependency>
4 <groupId>com.maziyao</groupId>
5 <artifactId>api</artifactId>
6 <version>0.0.1-SNAPSHOT</version>
7 <scope>compile</scope>
8 </dependency>
9 ...
10 </dependencies>

dubbo-config.xml配置dubbo

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans.xsd
7 http://code.alibabatech.com/schema/dubbo
8 http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
9
10 <!-- 配置服务名 -->
11 <dubbo:application name="dubbo-consumer"/>
12
13 <!-- 指定注册中心地址 -->
14 <dubbo:registry id="tttori" address="zookeeper://127.0.0.1:2181"/>
15
16 <!-- 订阅接口,生成远程服务代理 -->
17 <dubbo:reference id="userInfoService" interface="com.tttori.api.service.UserInfoService" registry="tttori"/>
18
19 </beans>

UserController交互层

@RestController //控制层
public class UserController {
@Autowired
UserService UserService; @ResponseBody //返回Json数据
@RequestMapping("/info")
public List<userInfo> getInfo(){
/**
* Consumer接口实现类调用远程服务Provider接口
**/
return userService.getUserInfo();
}
}

ConsumerApplication启动类

1 @SpringBootApplication
2 @ImportResource(locations = {"classpath:dubbo-config.xml"})
3 public class ConsumerApplication {
4
5 public static void main(String[] args) {
6 SpringApplication.run(ConsumerApplication.class, args);
7 }
8
9 }

总结

SpringBoot整合Dubbo步骤

1)pom.xml

0. Provider和Consumer配置Dubbo相关的Starter(ZK)

2)dubbo-config.xml

0. dubbo.application.name 声明当前服务名

1. dubbo.protocol.name 指定通信规则,dubbo协议

2. dubbo.registry.protocol.address 声明注册中心,暴露服务地址

3. dubbo.service.interface.ref.registry 暴露服务接口(Provider)

4. dubbo.registry.interface.ref.registry 订阅服务接口(Consumer)

3)暴露服务

服务实现类使用@Service和@Component注释

4)消费服务

@Reference/@Autowired实现自动注入

Java-Dubbo学习及整合SpringBoot的更多相关文章

  1. Dubbo学习-6-springboot整合dubbo

    1.在前面帖子和工程的基础上,这里使用springboot整合dubbo,首先创建springboot项目: https://start.spring.io/  进入spring Initializr ...

  2. Java开发学习(三十七)----SpringBoot多环境配置及配置文件分类

    一.多环境配置 在工作中,对于开发环境.测试环境.生产环境的配置肯定都不相同,比如我们开发阶段会在自己的电脑上安装 mysql ,连接自己电脑上的 mysql 即可,但是项目开发完毕后要上线就需要该配 ...

  3. Netty学习篇③--整合springboot

    经过前面的netty学习,大概了解了netty各个组件的概念和作用,开始自己瞎鼓捣netty和我们常用的项目的整合(很简单的整合) 项目准备 工具:IDEA2017 jar包导入:maven 项目框架 ...

  4. 22.Java面试学习平台-整合OSS对象存储

    SpringCloud实战项目全套学习教程连载中 PassJava 学习教程 简介 PassJava-Learning项目是PassJava(佳必过)项目的学习教程.对架构.业务.技术要点进行讲解. ...

  5. dubbo入门学习(三)-----dubbo整合springboot

    springboot节省了大量的精力去配置各种bean,因此通过一个简单的demo来整合springboot与dubbo 一.创建boot-user-service-provider 本篇博文基于上篇 ...

  6. SpringBoot学习之整合Mybatis

    本博客使用IDEA开发工具,通过Maven构建SpringBoot项目,初始化项目添加的依赖有:spring-boot-starter-jdbc.spring-boot-starter-web.mys ...

  7. java JDK8 学习笔记——第16章 整合数据库

    第十六章 整合数据库 16.1 JDBC入门 16.1.1 JDBC简介 1.JDBC是java联机数据库的标准规范.它定义了一组标准类与接口,标准API中的接口会有数据库厂商操作,称为JDBC驱动程 ...

  8. Dubbo整合SpringBoot

    目前的dubbo已支持和springboot集成,还是之前的例子,这次我们通过springboot容器来实现.借此了解一下基于springboot容器启动的dubbo的配置及使用. 1. 准备工作 创 ...

  9. Dubbo整合Springboot框架

    本文使用的是alibaba的Dubbo. Dubbo整合Springboot可以分为四步: 第一步:首先需要了解Dubbo官方给的建议,至少有三个工程: 接口工程:主要存实体bean和业务接口 服务提 ...

随机推荐

  1. hdu 6050 Funny Function 矩阵快速幂

    就算告诉我是矩阵快速幂我也推不出递推式呀!!! 官方题解: 对于任意i>=1,当j>=3时,有通过归纳法可以得到 进而推导出 后来自己重新推导了一遍 #include <iostre ...

  2. HDU 4438 Hunters 区域赛水题

    本文转载于 http://blog.csdn.net/major_zhang/article/details/52197538 2012天津区域赛最水之题: 题意容易读懂,然后就是分情况求出A得分的数 ...

  3. HDU 4445 Crazy Tank 高中物理知识忘得差不多了

    题意不难理解,仔细看题吧,就不说题意了 #include <iostream> #include <cstdio> #include <cstring> #incl ...

  4. CSS水平居中与垂直居中的方法

    一.水平居中 1.行内元素水平居中 在父元素里添加text-align:center即可.代码如下: <style> .container-1 { height: 50px; border ...

  5. Flask(8)- jinja2 模板入门

    前言 之前的文章有个栗子,视图函数可以直接返回一段 html 代码,浏览器可以自动渲染 但是当你的 HTML 非常复杂的话,也要整串写在代码里面吗,这显然不合理的,可阅读性也非常差 所以,就诞生了 J ...

  6. 前端 | Vue 路由返回恢复页面状态

    需求场景:首页搜索内容,点击跳转至详情页,页面后退返回主页,保留搜索结果. 方案:路由参数:路由守卫 需求描述 在使用 Vue 开发前端的时候遇到一个场景:在首页进行一些数据搜索,点击搜索结果进入详情 ...

  7. 在jsp中显示List中的数据

    <% ArrayList list = (ArrayList)request.getAttribute("class"); for(int i = 0; i < lis ...

  8. TestComplete 64位和32位之间的区别

    在64位系统上,有两种版本的TestComplete:32位和64位.本主题描述了TestComplete x64及其32位版本之间的区别.关于TestComplete x64启动TestComple ...

  9. 「AGC020F」 Arcs on a Circle

    「AGC020F」 Arcs on a Circle Link 这个题非常 Amazing 啊.果然AtCoder全是智商题 首先你可以注意到数据范围真的是小得离谱,让你想要爆搜. 然后你发现不可做, ...

  10. 数据库:随机显示n条记录

    1.sqlite3数据库select  * from QG  order by random() limit 6 以下显示前10条记录 2.SQL Server数据库select top 10 * f ...