前面几个博文中关于SSM 框架已经搭建完成, 这里来讲下项目中使用到的Dubbo以及自己了解到的关于Dubbo的一些知识.

Dubbo是什么?

Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。

Dubbo[]是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。

其核心部分包含:

  • 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
  • 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
  • 自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。

Dubbo能做什么?

  • 透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。
  • 软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
  • 服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。

Spring集成

Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。

上面简单介绍了Dubbo的一些概念, 这里再给一张图来形象的形容下: 

我们用这张图来形容 , 那么映射到 项目中:
      当我们在多个Tomcat部署不同的系统时, 例如A系统(TomcatA)想调用B系统(TomcatB)中的服务, 这时Dubbo就有了用武之地. 首先我们需要B系统在注册中心将自己的Url注册进去, 然后注册中心将Url返还给系统A, 那么系统A就可以调用了. 当然了我这里说的只是Dubbo的一种用法, 在这个项目中使用的也是Dubbo的远程调用功能. (感觉真的和webservice有点像)

下面就步入正题, 看看Dubbo在项目中的使用实例:
1, 在linux下安装Zookeeper
这个地方就不详细概述Zookeeper的安装了, 前面关于Linux的博文已经有讲过在Linux下软件的安装了, 这里安装好后直接启动 Zookeeper.

2, 使用需求
在这里 当我们有一种需求, 我们需要在后台(console)去对商品(product)做一些操做, 而这里我们只能够使用到公共的service方法, 那么怎么调用product中service的实现呢? 
项目结构:

公共service方法:

TestTbService.java:

1 package cn.itcast.core.service;
2
3 import cn.itcast.core.bean.TestTb;
4
5 public interface TestTbService {
6     public void insertTestTb(TestTb testTb);
7 }

product中的service实现类:
TestTbService.java:

 1 package cn.itcast.core.service;2 3 import org.springframework.beans.factory.annotation.Autowired;4 import org.springframework.stereotype.Service;5 import org.springframework.transaction.annotation.Transactional;6 7 import cn.itcast.core.bean.TestTb;8 import cn.itcast.core.dao.TestTbDao;9
10 @Service("testTbService")
11 @Transactional
12 public class TestTbServiceImpl implements TestTbService {
13
14     @Autowired
15     private TestTbDao testTbDao;
16
17     //保存
18     public void insertTestTb(TestTb testTb){
19         testTbDao.insertTestTb(testTb);
20     }
21 }

在console中使用product中的service实现类:
CenterController.java:

 1 package cn.itcast.core.controller;2 3 import java.util.Date;4 5 import org.junit.runners.model.TestTimedOutException;6 import org.springframework.beans.factory.annotation.Autowired;7 import org.springframework.stereotype.Controller;8 import org.springframework.ui.Model;9 import org.springframework.web.bind.annotation.RequestMapping;
10
11 import cn.itcast.core.bean.TestTb;
12 import cn.itcast.core.service.TestTbService;
13
14 @Controller
15 public class CenterController {
16
17     @Autowired
18     private TestTbService testTbService;
19
20     //测试
21     @RequestMapping(value = "/test/index.do")
22     public void index(Model model){
23
24         TestTb testTb = new TestTb();
25         testTb.setName("范冰冰");
26         testTb.setBirthday(new Date());
27
28         testTbService.insertTestTb(testTb);
29
30     }
31 }

 

如果这样直接调用能够行的通吗?  当然是不行的, 在console里面定义的只是service方法, 那么这里是怎么直接调用到了product中的service实现类呢?

当然了, 这里就需要一些配置文件了:
首先是需要在product中注册服务:
dubbo-provider.xml:

 1 <beans xmlns="http://www.springframework.org/schema/beans"2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"3     xmlns:context="http://www.springframework.org/schema/context"4     xmlns:aop="http://www.springframework.org/schema/aop" 5     xmlns:tx="http://www.springframework.org/schema/tx"6     xmlns:task="http://www.springframework.org/schema/task"7     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"8     xsi:schemaLocation="http://www.springframework.org/schema/beans 9         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
10         http://www.springframework.org/schema/mvc
11         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
12         http://www.springframework.org/schema/context
13         http://www.springframework.org/schema/context/spring-context-4.0.xsd
14         http://www.springframework.org/schema/aop
15         http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
16         http://www.springframework.org/schema/tx
17         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
18         http://www.springframework.org/schema/task
19            http://www.springframework.org/schema/task/spring-task-4.0.xsd
20         http://code.alibabatech.com/schema/dubbo
21         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
22
23
24         <!-- 整合Dubbo -->
25         <!-- 第一步:Dubbo起名称    计算用此名称来区分   -->
26         <dubbo:application name="babasport-service-product"/>
27         <!-- 第二步:中介  注册中心: zookeeper  redis ... -->
28         <!-- <dubbo:registry address="192.168.200.128:2181,192.168.200.129:2181,192.168.200.130:2181" protocol="zookeeper"/> -->
29         <dubbo:registry address="192.168.200.128:2181" protocol="zookeeper"/>
30         <!-- 第三步:设置dubbo的端口号     192.168.40.88:20880/接口  -->
31         <dubbo:protocol name="dubbo" port="20880"/>
32         <!-- 第四步:设置服务提供方 提供的接口 -->
33         <dubbo:service interface="cn.itcast.core.service.TestTbService" ref="testTbService"/>
34
35 </beans>

接下来就是在console中使用了:
服务消费方:
dubbo-cusmer.xml:

 1 <beans xmlns="http://www.springframework.org/schema/beans"2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"3     xmlns:context="http://www.springframework.org/schema/context"4     xmlns:aop="http://www.springframework.org/schema/aop" 5     xmlns:tx="http://www.springframework.org/schema/tx"6     xmlns:task="http://www.springframework.org/schema/task"7     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"8     xsi:schemaLocation="http://www.springframework.org/schema/beans 9         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
10         http://www.springframework.org/schema/mvc
11         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
12         http://www.springframework.org/schema/context
13         http://www.springframework.org/schema/context/spring-context-4.0.xsd
14         http://www.springframework.org/schema/aop
15         http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
16         http://www.springframework.org/schema/tx
17         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
18         http://www.springframework.org/schema/task
19            http://www.springframework.org/schema/task/spring-task-4.0.xsd
20         http://code.alibabatech.com/schema/dubbo
21         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
22
23         <!-- 整合Dubbo -->
24         <!-- 第一步:Dubbo起名称    计算用此名称来区分   -->
25         <dubbo:application name="babasport-console"/>
26         <!-- 第二步:中介  注册中心    zookeeper  redis ... -->
27         <!--<dubbo:registry address="192.168.200.128:2181,192.168.200.129:2181,192.168.200.130:2181" protocol="zookeeper"/> -->
28         <dubbo:registry address="192.168.200.128:2181" protocol="zookeeper"/>
29         <!-- 第三步:调用服务提供方 提供的接口 -->
30         <dubbo:reference interface="cn.itcast.core.service.TestTbService" id="testTbService"/>
31
32 </beans>

剩下的就是启动服务了:
注意先启动服务提供方, 然后再启动服务消费方.

 

from: https://www.cnblogs.com/wang-meng/p/5791598.html

Dubbo的使用及原理浅析相关推荐

  1. Dubbo的使用及原理浅析.

    前面几个博文中关于SSM 框架已经搭建完成, 这里来讲下项目中使用到的Dubbo以及自己了解到的关于Dubbo的一些知识. Dubbo是什么? Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天 ...

  2. dubbo原理_Dubbo原理浅析

    Apache Dubbo™ 是一款微服务框架(Microservices Framework),它提供高性能 RPC 通信.服务发现.流量管理等服务治理能力,提供构建大规模微服务集群所需的全套解决方案 ...

  3. Seata 分布式事务的使用和原理浅析

    Seata 分布式事务的精简使用教程和原理浅析 一.说明 二.Seata 简介 2.1.Seata 是什么? 2.2.Seata 的整体架构 2.2.1.主要角色 2.2.2.整体架构和工作流程图 2 ...

  4. Python标准库queue模块原理浅析

    Python标准库queue模块原理浅析 本文环境python3.5.2 queue模块的实现思路 作为一个线程安全的队列模块,该模块提供了线程安全的一个队列,该队列底层的实现基于Python线程th ...

  5. Python标准库threading模块Condition原理浅析

    Python标准库threading模块Condition原理浅析 本文环境python3.5.2 threading模块Condition的实现思路 在Python的多线程实现过程中,在Linux平 ...

  6. 第一篇: 词向量之Word2vector原理浅析

    第一篇: 词向量之Word2vector原理浅析 作者 Aroundtheworld 2016.11.05 18:50 字数 1353 阅读 5361评论 1喜欢 9 一.概述 本文主要是从deep ...

  7. Alibaba Dubbo框架同步调用原理分析-1

    2019独角兽企业重金招聘Python工程师标准>>> 由于Dubbo底层采用Socket进行通信,自己对通信理理论也不是很清楚,所以顺便把通信的知识也学习一下. n  通信理论 计 ...

  8. SPI及其工作原理浅析

    说明.文章摘自:SPI协议及其工作原理浅析 http://bbs.chinaunix.net/thread-1916003-1-1.html 一.概述. SPI, Serial Perripheral ...

  9. .NET1.1中预编译ASP.NET页面实现原理浅析[1]自动预编译机制浅析

    .NET1.1中预编译ASP.NET页面实现原理浅析[1]自动预编译机制浅析 .NET1.1中预编译ASP.NET页面实现原理浅析[1]自动预编译机制浅析 作者:&;nbsp来自:网络 htt ...

最新文章

  1. Django 模型查询2.3
  2. 日事清【员工绩效】功能活动上线!
  3. # 2017-2018-1 20155224 《信息安全系系统设计基础》实验四
  4. 一个 MVC 框架以 MVVM 之「魂」复活了!
  5. 分布式事务科普(终结篇)
  6. stm32 GPIO简单介绍及初始化配置(库函数)
  7. qt中生成并读取配置文件Ini
  8. python模块:JSON模块
  9. CV之Harris特征点检测器-兴趣点检测(详解)
  10. web server的性能统计
  11. java实验多线程机制_使用Java多线程的同步机制编写应用程序 PDF 下载
  12. 企业管理系统原型、HRM、工作报告统计、0A、商机、合同、客户管理、产品管理、企业oa、行政办公系统、数据报表、销售分析、客户分析、产品管理、人力资源管理系统、crm、axure原型、rp源文件
  13. iOS应用内付费详解
  14. [Python] Python学习笔记之常用模块总结[持续更新...]
  15. Linux与Xshell:登陆服务器与后台执行程序
  16. 点击 tomcat9.exe闪退 问题的解决方法
  17. Windows7中搭建Android x86 64及armv8-a操作步骤
  18. matlab 如何使用虚数,编程高手帮我解决下怎么用matlab解含有虚数的微分方程组...
  19. activeMQ启动失败61616port被占用问题
  20. Microsoft Edge逃离360之路

热门文章

  1. 数据挖掘十大经典算法之——PageRank 算法
  2. 【采用】信贷业务风控逾期指标及风控模型评估指标
  3. 二次创业成功人士的19个经验与教训
  4. 360金融CEO徐军发明:资金饥渴分模型
  5. 【深度学习】caffe 中的一些参数介绍
  6. 官方乌镇定调互联网金融,P2P、众筹专项对待
  7. 若能回到五年前,我会告诉自己这些创业道理
  8. 关于Rocchio算法和向量空间模型反馈
  9. Spring Boot-Spring Tool Suit + Gradle 构建第一个Spring Boot 项目02
  10. 剑指Offer Ⅱ 003.二进制加法(力扣剑指Offer专项突击版——整数_3)