Dubbo的使用

博客鏈接地址:https://www.cnblogs.com/wang-meng/p/5791598.html



Dubbo是什么?

Dubbo是阿里巴巴SOA服務(wù)化治理方案的核心框架,每天為2,000+個服務(wù)提供3,000,000,000+次訪問量支持,并被廣泛應(yīng)用于阿里巴巴集團的各成員站點。

Dubbo[]是一個分布式服務(wù)框架,致力于提供高性能和透明化的RPC遠程服務(wù)調(diào)用方案,以及SOA服務(wù)治理方案。

其核心部分包含:

遠程通訊: 提供對多種基于長連接的NIO框架抽象封裝,包括多種線程模型,序列化,以及“請求-響應(yīng)”模式的信息交換方式。

集群容錯: 提供基于接口方法的透明遠程過程調(diào)用,包括多協(xié)議支持,以及軟負載均衡,失敗容錯,地址路由,動態(tài)配置等集群支持。

自動發(fā)現(xiàn): 基于注冊中心目錄服務(wù),使服務(wù)消費方能動態(tài)的查找服務(wù)提供方,使地址透明,使服務(wù)提供方可以平滑增加或減少機器。


Dubbo能做什么?

透明化的遠程方法調(diào)用,就像調(diào)用本地方法一樣調(diào)用遠程方法,只需簡單配置,沒有任何API侵入。

軟負載均衡及容錯機制,可在內(nèi)網(wǎng)替代F5等硬件負載均衡器,降低成本,減少單點。

服務(wù)自動注冊與發(fā)現(xiàn),不再需要寫死服務(wù)提供方地址,注冊中心基于接口名查詢服務(wù)提供者的IP地址,并且能夠平滑添加或刪除服務(wù)提供者。


Spring集成

Dubbo采用全Spring配置方式,透明化接入應(yīng)用,對應(yīng)用沒有任何API侵入,只需用Spring加載Dubbo的配置即可,Dubbo基于Spring的Schema擴展進行加載。如果不想使用Spring配置,而希望通過API的方式進行調(diào)用(不推薦)。

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


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



下面就步入正題, 看看Dubbo在項目中的使用實例:

1, 在linux下安裝Zookeeper

這個地方就不詳細概述Zookeeper的安裝了, 前面關(guān)于Linux的博文已經(jīng)有講過在Linux下軟件的安裝了, 這里安裝好后直接啟動 Zookeeper.


zookeeper

2, 使用需求

在這里 當我們有一種需求, 我們需要在后臺(console)去對商品(product)做一些操做, 而這里我們只能夠使用到公共的service方法, 那么怎么調(diào)用product中service的實現(xiàn)呢?

項目結(jié)構(gòu):


項目結(jié)構(gòu)

公共service方法:

TestTbService.java:

package cn.itcast.core.service;

import cn.itcast.core.bean.TestTb;

public interface TestTbService {

????? public void insertTestTb(TestTb testTb);

}


公共service方法

product中的service實現(xiàn)類:

TestTbService.java:

package cn.itcast.core.service;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import cn.itcast.core.bean.TestTb;

import cn.itcast.core.dao.TestTbDao;

@Service("testTbService")

@Transactional

public class TestTbServiceImpl implements TestTbService {

? ? @Autowired

? ? private TestTbDao testTbDao;


? ? //保存

? ? public void insertTestTb(TestTb testTb){

? ? ? ? testTbDao.insertTestTb(testTb);

? ? }

}


product中的service實現(xiàn)類

在console中使用product中的service實現(xiàn)類:

CenterController.java:

package cn.itcast.core.controller;

import java.util.Date;

import org.junit.runners.model.TestTimedOutException;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import cn.itcast.core.bean.TestTb;

import cn.itcast.core.service.TestTbService;

@Controller

public class CenterController {


? ? @Autowired

? ? private TestTbService testTbService;


? ? //測試

? ? @RequestMapping(value = "/test/index.do")

? ? public void index(Model model){


? ? ? ? TestTb testTb = new TestTb();

? ? ? ? testTb.setName("范冰冰");

? ? ? ? testTb.setBirthday(new Date());


? ? ? ? testTbService.insertTestTb(testTb);


? ? }

}


在console中使用product中的service實現(xiàn)類

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

當然了, 這里就需要一些配置文件了:

首先是需要在product中注冊服務(wù):

dubbo-provider.xml:

<beans xmlns="http://www.springframework.org/schema/beans"

? ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

? ? xmlns:context="http://www.springframework.org/schema/context"

? ? xmlns:aop="http://www.springframework.org/schema/aop"

? ? xmlns:tx="http://www.springframework.org/schema/tx"

? ? xmlns:task="http://www.springframework.org/schema/task"

? ? xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

? ? xsi:schemaLocation="http://www.springframework.org/schema/beans

? ? ? ? http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

? ? ? ? http://www.springframework.org/schema/mvc

? ? ? ? http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

? ? ? ? http://www.springframework.org/schema/context

? ? ? ? http://www.springframework.org/schema/context/spring-context-4.0.xsd

? ? ? ? http://www.springframework.org/schema/aop

? ? ? ? http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

? ? ? ? http://www.springframework.org/schema/tx

? ? ? ? http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

? ? ? ? http://www.springframework.org/schema/task

? ? ? ? ? http://www.springframework.org/schema/task/spring-task-4.0.xsd

? ? ? ? http://code.alibabatech.com/schema/dubbo? ? ? ?

? ? ? ? http://code.alibabatech.com/schema/dubbo/dubbo.xsd">



? ? ? ? <!-- 整合Dubbo -->

? ? ? ? <!-- 第一步:Dubbo起名稱? ? 計算用此名稱來區(qū)分? -->

? ? ? ? <dubbo:application name="babasport-service-product"/>

? ? ? ? <!-- 第二步:中介? 注冊中心: zookeeper? redis ... -->

? ? ? ? <!-- <dubbo:registry address="192.168.200.128:2181,192.168.200.129:2181,192.168.200.130:2181" protocol="zookeeper"/> -->

? ? ? ? <dubbo:registry address="192.168.200.128:2181" protocol="zookeeper"/>

? ? ? ? <!-- 第三步:設(shè)置dubbo的端口號? ? 192.168.40.88:20880/接口? -->

? ? ? ? <dubbo:protocol name="dubbo" port="20880"/>

? ? ? ? <!-- 第四步:設(shè)置服務(wù)提供方 提供的接口 -->

? ? ? ? <dubbo:service interface="cn.itcast.core.service.TestTbService" ref="testTbService"/>


</beans>


在product中注冊服務(wù)

接下來就是在console中使用了:

服務(wù)消費方:

dubbo-cusmer.xml:

<beans xmlns="http://www.springframework.org/schema/beans"

? ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

? ? xmlns:context="http://www.springframework.org/schema/context"

? ? xmlns:aop="http://www.springframework.org/schema/aop"

? ? xmlns:tx="http://www.springframework.org/schema/tx"

? ? xmlns:task="http://www.springframework.org/schema/task"

? ? xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

? ? xsi:schemaLocation="http://www.springframework.org/schema/beans

? ? ? ? http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

? ? ? ? http://www.springframework.org/schema/mvc

? ? ? ? http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

? ? ? ? http://www.springframework.org/schema/context

? ? ? ? http://www.springframework.org/schema/context/spring-context-4.0.xsd

? ? ? ? http://www.springframework.org/schema/aop

? ? ? ? http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

? ? ? ? http://www.springframework.org/schema/tx

? ? ? ? http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

? ? ? ? http://www.springframework.org/schema/task

? ? ? ? ? http://www.springframework.org/schema/task/spring-task-4.0.xsd

? ? ? ? http://code.alibabatech.com/schema/dubbo? ? ? ?

? ? ? ? http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


? ? ? ? <!-- 整合Dubbo -->

? ? ? ? <!-- 第一步:Dubbo起名稱? ? 計算用此名稱來區(qū)分? -->

? ? ? ? <dubbo:application name="babasport-console"/>

? ? ? ? <!-- 第二步:中介? 注冊中心? ? zookeeper? redis ... -->

? ? ? ? <!--<dubbo:registry address="192.168.200.128:2181,192.168.200.129:2181,192.168.200.130:2181" protocol="zookeeper"/> -->

? ? ? ? <dubbo:registry address="192.168.200.128:2181" protocol="zookeeper"/>

? ? ? ? <!-- 第三步:調(diào)用服務(wù)提供方 提供的接口 -->

? ? ? ? <dubbo:reference interface="cn.itcast.core.service.TestTbService" id="testTbService"/>


</beans>


在console中使用

剩下的就是啟動服務(wù)了:

注意先啟動服務(wù)提供方, 然后再啟動服務(wù)消費方.


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容