國(guó)內(nèi)分布式框架Dubbo使用詳解
介紹
Dubbo 是一款高性能、輕量級(jí)的 Java RPC 框架,由阿里巴巴開(kāi)源并貢獻(xiàn)至 Apache 基金會(huì)。它能夠提供服務(wù)的注冊(cè)與發(fā)現(xiàn)、負(fù)載均衡、服務(wù)治理等功能,簡(jiǎn)化了分布式系統(tǒng)的開(kāi)發(fā)過(guò)程。下面我們將詳細(xì)介紹 Dubbo 的原理和使用方法,并附上相關(guān)的 Java 代碼示例。
Dubbo的原理
Dubbo 的核心是一個(gè)基于 Java 序列化的遠(yuǎn)程過(guò)程調(diào)用(RPC)框架,它的工作流程可以分為如下幾個(gè)步驟:
- 服務(wù)提供者向注冊(cè)中心注冊(cè)自己提供的服務(wù)。
- 服務(wù)消費(fèi)者從注冊(cè)中心獲取服務(wù)提供者的地址,并建立連接。
- 服務(wù)消費(fèi)者通過(guò) RPC 調(diào)用遠(yuǎn)程服務(wù),實(shí)現(xiàn)分布式調(diào)用
Dubbo 的架構(gòu)中包含以下幾個(gè)重要組件:
- Provider:服務(wù)提供者,將服務(wù)發(fā)布到注冊(cè)中心,供 Consumer 調(diào)用。
- Consumer:服務(wù)消費(fèi)者,從注冊(cè)中心獲取 Provider 的地址,并發(fā)起 RPC 調(diào)用。
- Registry:注冊(cè)中心,存儲(chǔ) Provider 的地址信息,供 Consumer 獲取。
Monitor:監(jiān)控中心,用于統(tǒng)計(jì) Provider 的運(yùn)行狀態(tài)和性能指標(biāo)。
Dubbo 支持多種協(xié)議和序列化方式,包括 Dubbo 協(xié)議、HTTP 協(xié)議、Hessian 協(xié)議、Thrift 協(xié)議等。同時(shí),它還提供了負(fù)載均衡、服務(wù)容錯(cuò)、動(dòng)態(tài)路由等功能,可以根據(jù)不同的需求進(jìn)行配置。
基本使用
- 編寫(xiě)服務(wù)接口
public interface HelloService { String sayHello(String name); }
- 實(shí)現(xiàn)服務(wù)接口
public class HelloServiceImpl implements HelloService { public String sayHello(String name) { return "Hello, " + name; } }
- 配置Dubbo 在Dubbo的XML配置文件中定義服務(wù)提供者和注冊(cè)中心,配置服務(wù)接口和實(shí)現(xiàn)類的關(guān)聯(lián)。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 指定服務(wù)提供者應(yīng)用名 --> <dubbo:application name="hello-provider"/> <!-- 指定注冊(cè)中心地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 指定通信協(xié)議和端口號(hào) --> <dubbo:protocol name="dubbo" port="20880"/> <!-- 暴露服務(wù) --> <dubbo:service interface="com.example.HelloService" ref="helloService"/> <!-- 服務(wù)接口和實(shí)現(xiàn)類的關(guān)聯(lián) --> <bean id="helloService" class="com.example.provider.HelloServiceImpl"/> </beans>
- 啟動(dòng)服務(wù)提供者 在服務(wù)提供者的main方法中啟動(dòng)Dubbo。
public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml"); context.start(); System.in.read(); // 按任意鍵退出 } }
服務(wù)提供者通過(guò)啟動(dòng) Spring 容器來(lái)啟動(dòng) Dubbo 服務(wù),這里使用的是 ClassPathXmlApplicationContext,它會(huì)從類路徑下加載 provider.xml 文件,初始化 Spring 容器并啟動(dòng) Dubbo 服務(wù)。
- 編寫(xiě)服務(wù)消費(fèi)者
public class Consumer { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml"); HelloService helloService = (HelloService) context.getBean("helloService"); String result = helloService.sayHello("world"); System.out.println(result); } }
- 配置Dubbo 在Dubbo的XML配置文件中定義服務(wù)消費(fèi)者和注冊(cè)中心。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 指定服務(wù)消費(fèi)者應(yīng)用名 --> <dubbo:application name="hello-consumer"/> <!-- 指定注冊(cè)中心地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 引用遠(yuǎn)程服務(wù) --> <dubbo:reference id="helloService" interface="com.example.HelloService"/> </beans>
- 啟動(dòng)服務(wù)消費(fèi)者
public class Consumer { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml"); HelloService helloService = (HelloService) context.getBean("helloService"); String result = helloService.sayHello("world"); System.out.println(result); } }
簡(jiǎn)單的使用就是這樣,關(guān)于zookeeper我們下次在詳細(xì)說(shuō)一下。
以上就是國(guó)內(nèi)分布式框架Dubbo使用詳解的詳細(xì)內(nèi)容,更多關(guān)于Dubbo分布式框架的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring自動(dòng)配置之condition條件判斷下篇
這篇文章主要為大家介紹了SpringBoot?condition條件判斷功能的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Java數(shù)據(jù)結(jié)構(gòu)之常見(jiàn)排序算法(上)
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之常見(jiàn)排序算法,本文章是匯總篇,且對(duì)每個(gè)排序都進(jìn)行了說(shuō)明,可以很好的理清思路,對(duì)排序算法有個(gè)總體的框架,需要的朋友可以參考下2023-01-01解決java 查看JDK中底層源碼的實(shí)現(xiàn)方法
本篇文章是對(duì)在java中查看JDK中底層源碼的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05教你用Java在個(gè)人電腦上實(shí)現(xiàn)微信掃碼支付
今天給大家?guī)?lái)的是Java實(shí)戰(zhàn)的相關(guān)知識(shí),文章圍繞著Java在個(gè)人電腦上實(shí)現(xiàn)微信掃碼支付展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06Activiti explorer.war示例工程使用過(guò)程圖解
這篇文章主要介紹了Activiti explorer.war示例工程使用過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03字節(jié)二面SpringBoot可以同時(shí)處理多少請(qǐng)求
這篇文章主要為大家介紹了字節(jié)二面之SpringBoot可以同時(shí)處理多少請(qǐng)求面試分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07關(guān)于Mybatis使用collection分頁(yè)問(wèn)題
項(xiàng)目中mybatis分頁(yè)的場(chǎng)景是非常高頻的,當(dāng)使用ResultMap并配置collection做分頁(yè)的時(shí)候,我們可能會(huì)遇到獲取當(dāng)前頁(yè)的數(shù)據(jù)少于每頁(yè)大小的數(shù)據(jù)問(wèn)題。接下來(lái)通過(guò)本文給大家介紹Mybatis使用collection分頁(yè)問(wèn)題,感興趣的朋友一起看看吧2021-11-11