博客鏈接地址:https://www.cnblogs.com/wang-meng/p/5791598.html
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)程通訊: 提供對(duì)多種基于長連接的NIO框架抽象封裝,包括多種線程模型啸盏,序列化重贺,以及“請(qǐng)求-響應(yīng)”模式的信息交換方式。
集群容錯(cuò): 提供基于接口方法的透明遠(yuǎn)程過程調(diào)用,包括多協(xié)議支持檬姥,以及軟負(fù)載均衡曾我,失敗容錯(cuò),地址路由健民,動(dòng)態(tài)配置等集群支持抒巢。
自動(dòng)發(fā)現(xiàn): 基于注冊(cè)中心目錄服務(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)注冊(cè)與發(fā)現(xiàn)幔摸,不再需要寫死服務(wù)提供方地址摸柄,注冊(cè)中心基于接口名查詢服務(wù)提供者的IP地址,并且能夠平滑添加或刪除服務(wù)提供者既忆。
Spring集成
Dubbo采用全Spring配置方式驱负,透明化接入應(yīng)用,對(duì)應(yīng)用沒有任何API侵入患雇,只需用Spring加載Dubbo的配置即可跃脊,Dubbo基于Spring的Schema擴(kuò)展進(jìn)行加載。如果不想使用Spring配置庆亡,而希望通過API的方式進(jìn)行調(diào)用(不推薦)匾乓。
上面簡單介紹了Dubbo的一些概念, 這里再給一張圖來形象的形容下:
下面就步入正題, 看看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)去對(duì)商品(product)做一些操做, 而這里我們只能夠使用到公共的service方法, 那么怎么調(diào)用product中service的實(shí)現(xiàn)呢?
項(xiàng)目結(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);
}
product中的service實(shí)現(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);
? ? }
}
在console中使用product中的service實(shí)現(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;
? ? //測(cè)試
? ? @RequestMapping(value = "/test/index.do")
? ? public void index(Model model){
? ? ? ? TestTb testTb = new TestTb();
? ? ? ? testTb.setName("范冰冰");
? ? ? ? testTb.setBirthday(new Date());
? ? ? ? testTbService.insertTestTb(testTb);
? ? }
}
如果這樣直接調(diào)用能夠行的通嗎? ?當(dāng)然是不行的, 在console里面定義的只是service方法, 那么這里是怎么直接調(diào)用到了product中的service實(shí)現(xiàn)類呢?
當(dāng)然了, 這里就需要一些配置文件了:
首先是需要在product中注冊(cè)服務(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起名稱? ? 計(jì)算用此名稱來區(qū)分? -->
? ? ? ? <dubbo:application name="babasport-service-product"/>
? ? ? ? <!-- 第二步:中介? 注冊(cè)中心: 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的端口號(hào)? ? 192.168.40.88:20880/接口? -->
? ? ? ? <dubbo:protocol name="dubbo" port="20880"/>
? ? ? ? <!-- 第四步:設(shè)置服務(wù)提供方 提供的接口 -->
? ? ? ? <dubbo:service interface="cn.itcast.core.service.TestTbService" ref="testTbService"/>
</beans>
接下來就是在console中使用了:
服務(wù)消費(fèi)方:
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起名稱? ? 計(jì)算用此名稱來區(qū)分? -->
? ? ? ? <dubbo:application name="babasport-console"/>
? ? ? ? <!-- 第二步:中介? 注冊(cè)中心? ? 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>
剩下的就是啟動(dòng)服務(wù)了:
注意先啟動(dòng)服務(wù)提供方, 然后再啟動(dòng)服務(wù)消費(fèi)方.