日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Dubbo的使用及原理浅析.

發(fā)布時(shí)間:2023/11/27 生活经验 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Dubbo的使用及原理浅析. 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前面幾個(gè)博文中關(guān)于SSM 框架已經(jīng)搭建完成, 這里來講下項(xiàng)目中使用到的Dubbo以及自己了解到的關(guān)于Dubbo的一些知識(shí).

Dubbo是什么?

Dubbo是阿里巴巴SOA服務(wù)化治理方案的核心框架,每天為2,000+個(gè)服務(wù)提供3,000,000,000+次訪問量支持,并被廣泛應(yīng)用于阿里巴巴集團(tuán)的各成員站點(diǎn)。 Dubbo[]是一個(gè)分布式服務(wù)框架,致力于提供高性能和透明化的RPC遠(yuǎn)程服務(wù)調(diào)用方案,以及SOA服務(wù)治理方案。
其核心部分包含:
  • 遠(yuǎn)程通訊: 提供對多種基于長連接的NIO框架抽象封裝,包括多種線程模型,序列化,以及“請求-響應(yīng)”模式的信息交換方式。
  • 集群容錯(cuò): 提供基于接口方法的透明遠(yuǎn)程過程調(diào)用,包括多協(xié)議支持,以及軟負(fù)載均衡,失敗容錯(cuò),地址路由,動(dòng)態(tài)配置等集群支持。
  • 自動(dòng)發(fā)現(xiàn): 基于注冊中心目錄服務(wù),使服務(wù)消費(fèi)方能動(dòng)態(tài)的查找服務(wù)提供方,使地址透明,使服務(wù)提供方可以平滑增加或減少機(jī)器。

Dubbo能做什么?

  • 透明化的遠(yuǎn)程方法調(diào)用,就像調(diào)用本地方法一樣調(diào)用遠(yuǎn)程方法,只需簡單配置,沒有任何API侵入。
  • 軟負(fù)載均衡及容錯(cuò)機(jī)制,可在內(nèi)網(wǎng)替代F5等硬件負(fù)載均衡器,降低成本,減少單點(diǎn)。
  • 服務(wù)自動(dòng)注冊與發(fā)現(xiàn),不再需要寫死服務(wù)提供方地址,注冊中心基于接口名查詢服務(wù)提供者的IP地址,并且能夠平滑添加或刪除服務(wù)提供者。

Spring集成

Dubbo采用全Spring配置方式,透明化接入應(yīng)用,對應(yīng)用沒有任何API侵入,只需用Spring加載Dubbo的配置即可,Dubbo基于Spring的Schema擴(kuò)展進(jìn)行加載。

上面簡單介紹了Dubbo的一些概念, 這里再給一張圖來形象的形容下:?

我們用這張圖來形容 , 那么映射到 項(xiàng)目中:
? ? ? 當(dāng)我們在多個(gè)Tomcat部署不同的系統(tǒng)時(shí), 例如A系統(tǒng)(TomcatA)想調(diào)用B系統(tǒng)(TomcatB)中的服務(wù), 這時(shí)Dubbo就有了用武之地. 首先我們需要B系統(tǒng)在注冊中心將自己的Url注冊進(jìn)去, 然后注冊中心將Url返還給系統(tǒng)A, 那么系統(tǒng)A就可以調(diào)用了. 當(dāng)然了我這里說的只是Dubbo的一種用法, 在這個(gè)項(xiàng)目中使用的也是Dubbo的遠(yuǎn)程調(diào)用功能. (感覺真的和webservice有點(diǎn)像)

下面就步入正題, 看看Dubbo在項(xiàng)目中的使用實(shí)例:
1, 在linux下安裝Zookeeper
這個(gè)地方就不詳細(xì)概述Zookeeper的安裝了, 前面關(guān)于Linux的博文已經(jīng)有講過在Linux下軟件的安裝了, 這里安裝好后直接啟動(dòng) Zookeeper.


2, 使用需求
在這里 當(dāng)我們有一種需求, 我們需要在后臺(tái)(console)去對商品(product)做一些操做, 而這里我們只能夠使用到公共的service方法, 那么怎么調(diào)用product中service的實(shí)現(xiàn)呢??
項(xiàng)目結(jié)構(gòu):

公共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實(shí)現(xiàn)類:
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實(shí)現(xiàn)類:
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 }

?

如果這樣直接調(diào)用能夠行的通嗎? ?當(dāng)然是不行的, 在console里面定義的只是service方法, 那么這里是怎么直接調(diào)用到了product中的service實(shí)現(xiàn)類呢?

當(dāng)然了, 這里就需要一些配置文件了:
首先是需要在product中注冊服務(wù):
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起名稱    計(jì)算用此名稱來區(qū)分   -->
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         <!-- 第三步:設(shè)置dubbo的端口號     192.168.40.88:20880/接口  -->
31         <dubbo:protocol name="dubbo" port="20880"/>
32         <!-- 第四步:設(shè)置服務(wù)提供方 提供的接口 -->
33         <dubbo:service interface="cn.itcast.core.service.TestTbService" ref="testTbService"/>
34         
35 </beans>

接下來就是在console中使用了:
服務(wù)消費(fèi)方:
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起名稱    計(jì)算用此名稱來區(qū)分   -->
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         <!-- 第三步:調(diào)用服務(wù)提供方 提供的接口 -->
30         <dubbo:reference interface="cn.itcast.core.service.TestTbService" id="testTbService"/>
31         
32 </beans>



剩下的就是啟動(dòng)服務(wù)了:
注意先啟動(dòng)服務(wù)提供方, 然后再啟動(dòng)服務(wù)消費(fèi)方.

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/wang-meng/p/5791598.html

總結(jié)

以上是生活随笔為你收集整理的Dubbo的使用及原理浅析.的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。