Dubbo的使用及原理浅析

1. Dubbo是什么?

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

2. Dubbo能做什么?

  • 透明化的远程方法调用

就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。

  • 软负载均衡及容错机制

可在内网替代F5等硬件负载均衡器,降低成本,减少单点。

  • 服务自动注册与发现

不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。

工作原理:

  • 专业名词:
  1. provider:dubbo服务提供方(暴露服务到注册中心)
  2. consumer:dubbo服务消费方(远程调用暴露的服务)
  3. registory:dubbo的注册中心 ---- 在生产环境下推荐使用zookeeper作为dubbo的注册中心。 zokeeper (注册中心)主要功能是服务注册与发现的注册中心 。
  4. monitor:统计服务的调用次调和调用时间的监控中心。
  5. Container: 服务运行容器。
  • 图解:

  • 0. 服务容器负责启动,加载,运行服务提供者。

  • 1. 服务提供者在启动时,向注册中心注册自己提供的服务。

  • 2. 服务消费者在启动时,向注册中心订阅自己所需的服务。

  • 3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

  • 4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

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

使用场景

        应用于:各个子系统间的相互调用

项目中的使用

  • 安装注册中心---zookeeper
  1. 将zookeeper上传到Linux OS下
  2. 略。
  3. 解压---这里将zookeeper解压到了”/usr/local/src”目录下 d9b64b5616cf548943554efda2c6dfde89f.jpg
  4. 进入解压后的目录并创建data目录652f4352ba27883b464a8121c42e2938074.jpg
  5. 进入conf目录下修改配置文件的名称e39f546e4988d37e84656daa28332dbd46c.jpg
  6. 修改zoo.cfg配置文件中的dataDir的值

9a822eb18e88f29c3217fe45f0bf591cbbf.jpg

     7.进入bin目录下启动zookeeper

6a1a97ec7402c12e228cce3342e2e7524ba.jpg

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>



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

 

转载于:https://my.oschina.net/u/4009436/blog/3015621

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值