一、項(xiàng)目架構(gòu)演變過(guò)程
隨著互聯(lián)網(wǎng)的發(fā)展笙纤,用戶群體逐漸壯大,網(wǎng)站的流量成倍增長(zhǎng)买置,常規(guī)的單體架構(gòu)已無(wú)法滿足請(qǐng)求壓力暴增和業(yè)務(wù)的快速迭代粪糙,架構(gòu)的變化勢(shì)在必行。
1.1忿项、單體架構(gòu)
單體架構(gòu)所有模塊和功能都集中在一個(gè)項(xiàng)目中 蓉冈,部署時(shí)也是將項(xiàng)目所有功能部整體署到服務(wù)器中。如下圖:
優(yōu)點(diǎn)
小項(xiàng)目開發(fā)快 成本低
架構(gòu)簡(jiǎn)單
易于測(cè)試
易于部署
缺點(diǎn)
大項(xiàng)目模塊耦合嚴(yán)重 不易開發(fā) 維護(hù) 溝通成本高
新增業(yè)務(wù)困難
核心業(yè)務(wù)與邊緣業(yè)務(wù)混合在一塊轩触,出現(xiàn)問(wèn)題互相影響
1.2寞酿、垂直架構(gòu)
根據(jù)業(yè)務(wù)把項(xiàng)目垂直切割成多個(gè)項(xiàng)目,因此這種架構(gòu)稱之為垂直架構(gòu)脱柱。
為了避免上面提到的那些問(wèn)題伐弹,我們開始做模塊的垂直劃分,做垂直劃分的原則是基于拉勾的業(yè)務(wù)特性榨为,核心目標(biāo)惨好,第一個(gè)是為了業(yè)務(wù)之間互不影響,第二個(gè)是在研發(fā)團(tuán)隊(duì)的壯大后為了提高效率随闺,減少之間的依賴日川。
優(yōu)點(diǎn)
系統(tǒng)拆分實(shí)現(xiàn)了流量分擔(dān),解決了并發(fā)問(wèn)題
可以針對(duì)不同系統(tǒng)進(jìn)行優(yōu)化
方便水平擴(kuò)展矩乐,負(fù)載均衡龄句,容錯(cuò)率提高
系統(tǒng)間相互獨(dú)立回论,互不影響,新的業(yè)務(wù)迭代時(shí)更加高效
缺點(diǎn)
服務(wù)系統(tǒng)之間接口調(diào)用硬編碼
搭建集群之后分歇,實(shí)現(xiàn)負(fù)載均衡比較復(fù)雜
服務(wù)系統(tǒng)接口調(diào)用監(jiān)控不到位 調(diào)用方式不統(tǒng)一
服務(wù)監(jiān)控不到位
數(shù)據(jù)庫(kù)資源浪費(fèi)傀蓉,充斥慢查詢,主從同步延遲大
1.3职抡、分布式-SOA架構(gòu)
1.3.1葬燎、分布式-SOA介紹
SOA全稱為Service Oriented Architecture,即面向服務(wù)的架構(gòu) 繁调。它是在垂直劃分的基礎(chǔ)上,將每個(gè)項(xiàng)目拆分出多個(gè)具備松耦合的服務(wù),一個(gè)服務(wù)通常以獨(dú)立的形式存在于操作系統(tǒng)進(jìn)程中萨蚕。各個(gè)服務(wù)之間通過(guò)網(wǎng)絡(luò)調(diào)用,這使得構(gòu)建在各種各樣的系統(tǒng)中的服務(wù)可以 以一種統(tǒng)一和通用的方式進(jìn)行交互蹄胰。
我們?cè)谧隽舜怪眲澐忠院笤酪#K隨之增多,系統(tǒng)之間的RPC逐漸增多裕寨,維護(hù)的成本也越來(lái)越高浩蓉,一些通用的業(yè)務(wù)和模塊重復(fù)的也越來(lái)越多,這個(gè)時(shí)候上面提到的接口協(xié)議不統(tǒng)一宾袜、服務(wù)無(wú)法監(jiān)控捻艳、服務(wù)的負(fù)載均衡等問(wèn)題更加突出,為了解決上面的這些問(wèn)題庆猫,我們將通用的業(yè)務(wù)邏輯下沉到服務(wù)層认轨,通過(guò)接口暴露,供其他業(yè)務(wù)場(chǎng)景調(diào)用月培。同時(shí)引入了阿里巴巴開源的Dubbo嘁字,一款高性能、輕量級(jí)的開源Java RPC框架杉畜,它提供了三大核心能力:面向接口的遠(yuǎn)程方法調(diào)用纪蜒,智能容錯(cuò)和負(fù)載均衡,以及服務(wù)自動(dòng)注冊(cè)和發(fā)現(xiàn)此叠。
解釋說(shuō)明:
分層: 按照業(yè)務(wù)性質(zhì)分層 每一層要求簡(jiǎn)單 和 容易維護(hù)
應(yīng)用層:
距離用戶最近的一層 也稱之為接入層 使用tomcat 作為web容器 接收用戶請(qǐng)求 使用下游的dubbo提供的接口來(lái)返回?cái)?shù)據(jù) 并且該層禁止訪問(wèn)數(shù)據(jù)庫(kù)
業(yè)務(wù)服務(wù)層:
根據(jù)具體的業(yè)務(wù)場(chǎng)景 演變而來(lái)的模塊 比如 簡(jiǎn)歷投遞 職位搜索 職位推薦等
基礎(chǔ)業(yè)務(wù)層:
拉勾網(wǎng)招聘業(yè)務(wù)的核心 賬號(hào) 簡(jiǎn)歷 公司 職位
基礎(chǔ)服務(wù)層:
這一層 是與業(yè)務(wù)無(wú)關(guān)的模塊 是一些通用的服務(wù)
這類服務(wù)的特點(diǎn):請(qǐng)求量大 邏輯簡(jiǎn)單 特性明顯 功能獨(dú)立
消息服務(wù)(發(fā)郵件 短信 微信)
附件解析 50% 自己上傳附件簡(jiǎn)歷 需要解析成pdf
存儲(chǔ)層:
不同的存儲(chǔ)類型 Mysql Mongodb ES fastDFS
分級(jí):按照業(yè)務(wù)性質(zhì)分層 同一層的業(yè)務(wù)也要做好分級(jí) 依據(jù)業(yè)務(wù)的重要性進(jìn)行分級(jí) 按照二八定律 網(wǎng)站80%的流量 都在核心功能上面 要優(yōu)先保證核心業(yè)務(wù)的穩(wěn)定纯续。
隔離:不同性質(zhì) 不同重要性的業(yè)務(wù)做好隔離 包括 業(yè)務(wù) 緩存 DB 中間件 都要做好隔離 比如 核心業(yè)務(wù)的數(shù)據(jù)庫(kù) 要和活動(dòng)相關(guān)的數(shù)據(jù)庫(kù)隔離
調(diào)用 :總體上調(diào)用要單向 可以跨層調(diào)用 但不能出現(xiàn)逆向調(diào)用
1.3.2、分布式-SOA優(yōu)缺點(diǎn)和微服務(wù)
優(yōu)點(diǎn)
服務(wù)以接口為粒度灭袁,為開發(fā)者屏蔽遠(yuǎn)程調(diào)用底層細(xì)節(jié) 使用Dubbo 面向接口遠(yuǎn)程方法調(diào)用 屏蔽了底層調(diào)用細(xì)節(jié)
業(yè)務(wù)分層以后架構(gòu)更加清晰 并且每個(gè)業(yè)務(wù)模塊職責(zé)單一 擴(kuò)展性更強(qiáng)
數(shù)據(jù)隔離猬错,權(quán)限回收,數(shù)據(jù)訪問(wèn)都通過(guò)接口 讓系統(tǒng)更加穩(wěn)定 安全
服務(wù)應(yīng)用本身無(wú)狀態(tài)化 這里的無(wú)狀態(tài)化指的是應(yīng)用本身不做內(nèi)存級(jí)緩存 而是把數(shù)據(jù)存入db
服務(wù)責(zé)任易確定 每個(gè)服務(wù)可以確定責(zé)任人 這樣更容易保證服務(wù)質(zhì)量和穩(wěn)定
缺點(diǎn)
1.粒度控制復(fù)雜 如果沒(méi)有控制好服務(wù)的粒度 服務(wù)的模塊就會(huì)越來(lái)越多 就會(huì)引發(fā) 超時(shí) 分布式事務(wù)等問(wèn)題
2.服務(wù)接口數(shù)量不宜控制 容易引發(fā)接口爆炸 所以服務(wù)接口建議以業(yè)務(wù)場(chǎng)景進(jìn)行單位劃分 并對(duì)相近的業(yè)務(wù)做抽象 防止接口爆炸
3.版本升級(jí)兼容困難 盡量不要?jiǎng)h除方法 字段 枚舉類型的新增字段也可能不兼容
4.調(diào)用鏈路長(zhǎng) 服務(wù)質(zhì)量不可監(jiān)控 調(diào)用鏈路變長(zhǎng) 下游抖動(dòng)可能會(huì)影響到上游業(yè)務(wù) 最終形成連鎖反應(yīng) 服務(wù)質(zhì)量不穩(wěn)定 同時(shí)鏈路的變成使得服務(wù)質(zhì)量的監(jiān)控變得困難
1.4茸歧、微服務(wù)架構(gòu)
微服務(wù)架構(gòu)是一種將單個(gè)應(yīng)用程序 作為一套小型服務(wù)開發(fā)的方法兔魂,每種應(yīng)用程序都在其自己的進(jìn)程中獨(dú)立運(yùn)行,并使用輕量級(jí)機(jī)制(通常是HTTP資源的API)進(jìn)行通信举娩。這些服務(wù)是圍繞業(yè)務(wù)功能構(gòu)建的,可以通過(guò)全自動(dòng)部署機(jī)制進(jìn)行獨(dú)立部署。這些服務(wù)的集中化管理非常少铜涉,它們可以用不同的編程語(yǔ)言編寫智玻,并使用不同的數(shù)據(jù)存儲(chǔ)技術(shù)。
微服務(wù)是在SOA上做的升華 , 粒度更加細(xì)致芙代,微服務(wù)架構(gòu)強(qiáng)調(diào)的一個(gè)重點(diǎn)是“業(yè)務(wù)需要徹底的組件化和服務(wù)化”吊奢。
關(guān)于微服務(wù)架構(gòu)的知識(shí),后面Spring Cloud課程中會(huì)詳細(xì)講解。
二纹烹、Dubbo 架構(gòu)與實(shí)戰(zhàn)
2.1 Dubbo 架構(gòu)概述
2.1.1 什么是Dubbo
Apache Dubbo是一款高性能的Java RPC框架页滚。其前身是阿里巴巴公司開源的一個(gè)高性能、輕量級(jí)的開源Java RPC框架铺呵,可以和Spring框架無(wú)縫集成裹驰。
2.1.2 dubbo 的特性
參考官網(wǎng)首頁(yè)特性
2.1.3 Dubbo 的服務(wù)治理
服務(wù)治理(SOA governance),企業(yè)為了確保項(xiàng)目順利完成而實(shí)施的過(guò)程片挂,包括最佳實(shí)踐幻林、架構(gòu)原則、治理規(guī)程音念、規(guī)律以及其他決定性的因素沪饺。服務(wù)治理指的是用來(lái)管理SOA的采用和實(shí)現(xiàn)的過(guò)程。
2.2. Dubbo 處理流程
調(diào)用關(guān)系說(shuō)明:
虛線 代表異步調(diào)用 實(shí)線代表同步訪問(wèn)
藍(lán)色虛線 是在啟動(dòng)時(shí)完成的功能
紅色虛線 是程序運(yùn)行中執(zhí)行的功能
調(diào)用流程:
1.服務(wù)提供者在服務(wù)容器啟動(dòng)時(shí) 向注冊(cè)中心 注冊(cè)自己提供的服務(wù)
2.服務(wù)消費(fèi)者在啟動(dòng)時(shí) 向注冊(cè)中心訂閱自己所需的服務(wù)
3.注冊(cè)中心返回服務(wù)提供者地址列表給消費(fèi)者 如果有變更 注冊(cè)中心會(huì)基于長(zhǎng)連接推送變更數(shù)據(jù)給消費(fèi)者
4.服務(wù)消費(fèi)者 從提供者地址列表中 基于軟負(fù)載均衡算法 選一臺(tái)提供者進(jìn)行調(diào)用 如果調(diào)用失敗 則重新選擇一臺(tái)
5.服務(wù)提供者和消費(fèi)者 在內(nèi)存中的調(diào)用次數(shù) 和 調(diào)用時(shí)間 定時(shí)每分鐘發(fā)送給監(jiān)控中心
2.3闷愤、服務(wù)注冊(cè)中心Zookeeper
通過(guò)前面的Dubbo架構(gòu)圖可以看到整葡,Registry(服務(wù)注冊(cè)中心)在其中起著至關(guān)重要的作用。Dubbo官
方推薦使用Zookeeper作為服務(wù)注冊(cè)中心讥脐。Zookeeper 是 Apache Hadoop 的子項(xiàng)目遭居,作為 Dubbo 服
務(wù)的注冊(cè)中心,工業(yè)強(qiáng)度較高攘烛,可用于生產(chǎn)環(huán)境魏滚,并推薦使用 。
Zookeeper的安裝及其使用見上一模塊坟漱,此處不再贅述鼠次。
2.4 、Dubbo開發(fā)實(shí)戰(zhàn)
2.4.1 實(shí)戰(zhàn)案例介紹
在Dubbo中所有的的服務(wù)調(diào)用都是基于接口去進(jìn)行雙方交互的芋齿。雙方協(xié)定好Dubbo調(diào)用中的接口腥寇,提供者來(lái)提供實(shí)現(xiàn)類并且注冊(cè)到注冊(cè)中心上
調(diào)用方則只需要引入該接口,并且同樣注冊(cè)到相同的注冊(cè)中心上(消費(fèi)者)觅捆。即可利用注冊(cè)中心來(lái)實(shí)現(xiàn)集群感知功能赦役,之后消費(fèi)者即可對(duì)提供者進(jìn)行調(diào)用。
我們所有的項(xiàng)目都是基于Maven去進(jìn)行創(chuàng)建栅炒,這樣相互在引用的時(shí)候只需要以依賴的形式進(jìn)行展現(xiàn)就可以了掂摔。
并且這里我們會(huì)通過(guò)maven的父工程來(lái)統(tǒng)一依賴的版本术羔。
程序?qū)崿F(xiàn)分為以下幾步驟:
(1)建立maven工程 并且 創(chuàng)建API模塊: 用于規(guī)范雙方接口協(xié)定
(2)提供provider模塊,引入API模塊乙漓,并且對(duì)其中的服務(wù)進(jìn)行實(shí)現(xiàn)级历。將其注冊(cè)到注冊(cè)中心上,對(duì)外來(lái)統(tǒng)一提供服務(wù)叭披。
(3)提供consumer模塊寥殖,引入API模塊,并且引入與提供者相同的注冊(cè)中心涩蜘。再進(jìn)行服務(wù)調(diào)用嚼贡。
2.4.2 Annotation 的開發(fā)過(guò)程
1.定義maven。
<groupId>com.lagou</groupId>
<artifactId>service-api</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
? ? <dubbo.version>2.7.5 </dubbo.version>
</properties>
<dependencyManagement>
? ? <dependencies>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo</artifactId>
? ? ? ? ? ? <version>${dubbo.version}</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-common</artifactId>
? ? ? ? ? ? <version>${dubbo.version}</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-registry-zookeeper</artifactId>
? ? ? ? ? ? <version>${dubbo.version}</version>
? ? ? ? ? ? <exclusions>
? ? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? ? ? <artifactId>dubbo-remoting-api</artifactId>
? ? ? ? ? ? ? ? </exclusion>
? ? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? ? ? <artifactId>dubbo-common</artifactId>
? ? ? ? ? ? ? ? </exclusion>
? ? ? ? ? ? </exclusions>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-registry-nacos</artifactId>
? ? ? ? ? ? <version>${dubbo.version}</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-rpc-dubbo</artifactId>
? ? ? ? ? ? <version>${dubbo.version}</version>
? ? ? ? ? ? <exclusions>
? ? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? ? ? <artifactId>dubbo-remoting-api</artifactId>
? ? ? ? ? ? ? ? </exclusion>
? ? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? ? ? <artifactId>dubbo-common</artifactId>
? ? ? ? ? ? ? ? </exclusion>
? ? ? ? ? ? </exclusions>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-remoting-netty4</artifactId>
? ? ? ? ? ? <version>${dubbo.version}</version>
? ? ? ? ? ? <exclusions>
? ? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? ? ? <artifactId>dubbo-remoting-api</artifactId>
? ? ? ? ? ? ? ? </exclusion>
? ? ? ? ? ? </exclusions>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-serialization-hessian2</artifactId>
? ? ? ? ? ? <version>${dubbo.version}</version>
? ? ? ? ? ? <exclusions>
? ? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? ? ? <artifactId>dubbo-common</artifactId>
? ? ? ? ? ? ? ? </exclusion>
? ? ? ? ? ? </exclusions>
? ? ? ? </dependency>
? ? </dependencies>
</dependencyManagement>
<dependencies>
? ? <!-- 日志配置 -->
? ? <dependency>
? ? ? ? <groupId>log4j</groupId>
? ? ? ? <artifactId>log4j</artifactId>
? ? ? ? <version>1.2.16</version>
? ? </dependency>
? ? <dependency>
? ? ? ? <groupId>org.slf4j</groupId>
? ? ? ? <artifactId>slf4j-api</artifactId>
? ? ? ? <version>1.7.5</version>
? ? </dependency>
? ? <dependency>
? ? ? ? <groupId>org.slf4j</groupId>
? ? ? ? <artifactId>slf4j-log4j12</artifactId>
? ? ? ? <version>1.7.5</version>
? ? </dependency>
? ? <!-- json數(shù)據(jù)化轉(zhuǎn)換 -->
? ? <dependency>
? ? ? ? <groupId>com.alibaba</groupId>
? ? ? ? <artifactId>fastjson</artifactId>
? ? ? ? <version>1.2.62</version>
? ? </dependency>
</dependencies>
<build>
? ? <plugins>
? ? ? ? <plugin>
? ? ? ? ? ? <groupId>org.apache.maven.plugins</groupId>
? ? ? ? ? ? <artifactId>maven-compiler-plugin</artifactId>
? ? ? ? ? ? <version>3.3</version>
? ? ? ? ? ? <configuration>
? ? ? ? ? ? ? ? <source>1.8</source>
? ? ? ? ? ? ? ? <target>1.8</target>
? ? ? ? ? ? </configuration>
? ? ? ? </plugin>
? ? </plugins>
</build>
2.4.2.2 接口提供者(service-api module)
定義接口同诫,這里為了方便粤策,只是寫一個(gè)基本的方法。
package com.lagou.service;
public interface HelloService {
? ? String sayHello(String name);
}
2.4.2.3 提供者(service-providermodule)
1.pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
? ? ? ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? ? ? xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
? ? <parent>
? ? ? ? <artifactId>duboo-base-annotation</artifactId>
? ? ? ? <groupId>com.lagou</groupId>
? ? ? ? <version>1.0-SNAPSHOT</version>
? ? </parent>
? ? <modelVersion>4.0.0</modelVersion>
? ? <artifactId>service-provider</artifactId>
? ? <dependencies>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.lagou</groupId>
? ? ? ? ? ? <artifactId>service-api</artifactId>
? ? ? ? ? ? <version>1.0-SNAPSHOT</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.curator</groupId>
? ? ? ? ? ? <artifactId>curator-recipes</artifactId>
? ? ? ? ? ? <version>4.0.1</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.curator</groupId>
? ? ? ? ? ? <artifactId>curator-recipes</artifactId>
? ? ? ? ? ? <version>4.2.0</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-registry-zookeeper</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-rpc-dubbo</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-remoting-netty4</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-serialization-hessian2</artifactId>
? ? ? ? </dependency>
? ? </dependencies>
</project>
2.service 的實(shí)現(xiàn)
package com.lagou.service.impl;
import com.lagou.service.HelloService;
import org.apache.dubbo.config.annotation.Service;
@Service
public class HelloServiceImpl? implements HelloService {
? ? @Override
? ? public String sayHello(String name) {
//? ? ? ? try {
//? ? ? ? ? ? Thread.sleep(3000);
//? ? ? ? } catch (InterruptedException e) {
//? ? ? ? ? ? e.printStackTrace();
//? ? ? ? }
? ? ? ? return "hello:"+name;
? ? }
}
3.啟動(dòng)類
package com.lagou;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
public class DubboPureMain {
? ? public static void main(String[] args) throws? Exception{
? ? ? ? AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
? ? ? ? context.start();
? ? ? ? System.in.read();
? ? }
? ? @Configuration
? ? @EnableDubbo(scanBasePackages = "com.lagou.service.impl")
? ? @PropertySource("dubbo-provider.properties")
? ? static? class? ProviderConfiguration{
? ? ? ? @Bean
? ? ? ? public RegistryConfig? registryConfig(){
? ? ? ? ? ? RegistryConfig? registryConfig? = new RegistryConfig();
? ? ? ? ? ? registryConfig.setAddress("zookeeper://127.0.0.1:2181?timeout=10000");
? ? ? ? ? ? //registryConfig.setTimeout(10000);
? ? ? ? ? ? return? registryConfig;
? ? ? ? }
? ? }
}
4.配置
dubbo.application.name=dubbo-demo-annotation-provider
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
2.4.2.4 編寫服務(wù)消費(fèi)者
1.pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
? ? ? ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? ? ? xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
? ? <parent>
? ? ? ? <artifactId>duboo-base-annotation</artifactId>
? ? ? ? <groupId>com.lagou</groupId>
? ? ? ? <version>1.0-SNAPSHOT</version>
? ? </parent>
? ? <modelVersion>4.0.0</modelVersion>
? ? <artifactId>service-commuser</artifactId>
? ? <dependencies>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.lagou</groupId>
? ? ? ? ? ? <artifactId>service-api</artifactId>
? ? ? ? ? ? <version>1.0-SNAPSHOT</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-registry-zookeeper</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-rpc-dubbo</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-remoting-netty4</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-serialization-hessian2</artifactId>
? ? ? ? </dependency>
? ? </dependencies>
</project>
2.service 的實(shí)現(xiàn)
package com.lagou.bean;
import com.lagou.service.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
@Component
public class ComsumerComponet {
? ? @Reference
? ? private HelloService helloService;
? ? public String sayHello(String name){
? ? ? ? return helloService.sayHello(name);
? ? }
}
3.啟動(dòng)類
package com.lagou;
import com.lagou.bean.ComsumerComponet;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.io.IOException;
public class AnnotationConsumerMain {
? ? public static void main(String[] args) throws IOException {
? ? ? ? System.out.println("------");
? ? ? ? AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
? ? ? ? context.start();
? ? ? ? ComsumerComponet service = context.getBean(ComsumerComponet.class);
? ? ? ? while (true){
? ? ? ? ? ? System.in.read();
? ? ? ? ? ? String hello = service.sayHello("world");
? ? ? ? ? ? System.out.println("result:"+hello);
? ? ? ? }
? ? }
? ? @Configuration
? ? @PropertySource("classpath:/dubbo-consumer.properties")
? ? @ComponentScan(basePackages =? "com.lagou.bean" )
? ? @EnableDubbo
? ? static class ConsumerConfiguration{
? ? }
}
2.4.3 xml 的開發(fā)過(guò)程
1.定義maven
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
? ? ? ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? ? ? xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
? ? <modelVersion>4.0.0</modelVersion>
? ? <groupId>com.lagou</groupId>
? ? <artifactId>dubbo-base-xml</artifactId>
? ? <packaging>pom</packaging>
? ? <version>1.0-SNAPSHOT</version>
? ? <modules>
? ? ? ? <module>service-api-xml</module>
? ? ? ? <module>service-provider-xml</module>
? ? ? ? <module>service-conusumer-xml</module>
? ? </modules>
? ? <properties>
? ? ? ? <dubbo.version>2.7.5</dubbo.version>
? ? </properties>
? ? <dependencyManagement>
? ? ? ? <dependencies>
? ? ? ? ? ? <dependency>
? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? <artifactId>dubbo</artifactId>
? ? ? ? ? ? ? ? <version>${dubbo.version}</version>
? ? ? ? ? ? ? ? <exclusions>
? ? ? ? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? ? ? ? ? <artifactId>dubbo-common</artifactId>
? ? ? ? ? ? ? ? ? ? </exclusion>
? ? ? ? ? ? ? ? </exclusions>
? ? ? ? ? ? </dependency>
? ? ? ? ? ? <dependency>
? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? <artifactId>dubbo-common</artifactId>
? ? ? ? ? ? ? ? <version>${dubbo.version}</version>
? ? ? ? ? ? </dependency>
? ? ? ? ? ? <dependency>
? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? <artifactId>dubbo-registry-zookeeper</artifactId>
? ? ? ? ? ? ? ? <version>${dubbo.version}</version>
? ? ? ? ? ? ? ? <exclusions>
? ? ? ? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? ? ? ? ? <artifactId>dubbo-remoting-api</artifactId>
? ? ? ? ? ? ? ? ? ? </exclusion>
? ? ? ? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? ? ? ? ? <artifactId>dubbo-common</artifactId>
? ? ? ? ? ? ? ? ? ? </exclusion>
? ? ? ? ? ? ? ? </exclusions>
? ? ? ? ? ? </dependency>
? ? ? ? ? ? <dependency>
? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? <artifactId>dubbo-registry-nacos</artifactId>
? ? ? ? ? ? ? ? <version>${dubbo.version}</version>
? ? ? ? ? ? </dependency>
? ? ? ? ? ? <dependency>
? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? <artifactId>dubbo-rpc-dubbo</artifactId>
? ? ? ? ? ? ? ? <version>${dubbo.version}</version>
? ? ? ? ? ? ? ? <exclusions>
? ? ? ? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? ? ? ? ? <artifactId>dubbo-remoting-api</artifactId>
? ? ? ? ? ? ? ? ? ? </exclusion>
? ? ? ? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? ? ? ? ? <artifactId>dubbo-common</artifactId>
? ? ? ? ? ? ? ? ? ? </exclusion>
? ? ? ? ? ? ? ? </exclusions>
? ? ? ? ? ? </dependency>
? ? ? ? ? ? <dependency>
? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? <artifactId>dubbo-remoting-netty4</artifactId>
? ? ? ? ? ? ? ? <version>${dubbo.version}</version>
? ? ? ? ? ? ? ? <exclusions>
? ? ? ? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? ? ? ? ? <artifactId>dubbo-remoting-api</artifactId>
? ? ? ? ? ? ? ? ? ? </exclusion>
? ? ? ? ? ? ? ? </exclusions>
? ? ? ? ? ? </dependency>
? ? ? ? ? ? <dependency>
? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? <artifactId>dubbo-serialization-hessian2</artifactId>
? ? ? ? ? ? ? ? <version>${dubbo.version}</version>
? ? ? ? ? ? ? ? <exclusions>
? ? ? ? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? ? ? ? ? <artifactId>dubbo-common</artifactId>
? ? ? ? ? ? ? ? ? ? </exclusion>
? ? ? ? ? ? ? ? </exclusions>
? ? ? ? ? ? </dependency>
? ? ? ? </dependencies>
? ? </dependencyManagement>
? ? <dependencies>
? ? ? ? <!-- 日志配置 -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>log4j</groupId>
? ? ? ? ? ? <artifactId>log4j</artifactId>
? ? ? ? ? ? <version>1.2.16</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.slf4j</groupId>
? ? ? ? ? ? <artifactId>slf4j-api</artifactId>
? ? ? ? ? ? <version>1.7.5</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.slf4j</groupId>
? ? ? ? ? ? <artifactId>slf4j-log4j12</artifactId>
? ? ? ? ? ? <version>1.7.5</version>
? ? ? ? </dependency>
? ? ? ? <!-- json數(shù)據(jù)化轉(zhuǎn)換 -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.alibaba</groupId>
? ? ? ? ? ? <artifactId>fastjson</artifactId>
? ? ? ? ? ? <version>1.2.62</version>
? ? ? ? </dependency>
? ? </dependencies>
? ? <build>
? ? ? ? <plugins>
? ? ? ? ? ? <plugin>
? ? ? ? ? ? ? ? <groupId>org.apache.maven.plugins</groupId>
? ? ? ? ? ? ? ? <artifactId>maven-compiler-plugin</artifactId>
? ? ? ? ? ? ? ? <version>3.3</version>
? ? ? ? ? ? ? ? <configuration>
? ? ? ? ? ? ? ? ? ? <source>1.8</source>
? ? ? ? ? ? ? ? ? ? <target>1.8</target>
? ? ? ? ? ? ? ? </configuration>
? ? ? ? ? ? </plugin>
? ? ? ? </plugins>
? ? </build>
</project>
2.4.3.2 接口提供者(service-api-xml module)
定義接口剩辟,這里為了方便掐场,只是寫一個(gè)基本的方法
package com.lagou.service;
public interface HelloService {
? ? String sayHello(String name);
}
2.4.3.3 提供者(service-provider-xml module)
1.pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
? ? ? ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? ? ? xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
? ? <parent>
? ? ? ? <artifactId>dubbo-base-xml</artifactId>
? ? ? ? <groupId>com.lagou</groupId>
? ? ? ? <version>1.0-SNAPSHOT</version>
? ? </parent>
? ? <modelVersion>4.0.0</modelVersion>
? ? <artifactId>service-provider-xml</artifactId>
? ? <dependencies>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.lagou</groupId>
? ? ? ? ? ? <artifactId>service-api</artifactId>
? ? ? ? ? ? <version>1.0-SNAPSHOT</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-registry-zookeeper</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-registry-nacos</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-rpc-dubbo</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-remoting-netty4</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-serialization-hessian2</artifactId>
? ? ? ? </dependency>
? ? </dependencies>
</project>
2.service 的實(shí)現(xiàn)
package com.lagou.service.impl;
import com.lagou.service.HelloService;
public class HelloServiceImpl implements HelloService {
? ? public String sayHello(String name) {
? ? ? ? return "hello:"+name;
? ? }
}
3.啟動(dòng)類
package com.lagou;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class ProviderApplication {
? ? public static void main(String[] args) throws IOException {
? ? ? ? ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-provider.xml");
? ? ? ? context.start();
? ? ? ? System.in.read();
? ? }
}
4.配置
<?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-4.3.xsd
? ? ? ? http://dubbo.apache.org/schema/dubbo
? ? ? ? http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
? ? <!-- 提供方應(yīng)用信息,用于計(jì)算依賴關(guān)系 -->
? ? <dubbo:application name="service-provider" ></dubbo:application>
? ? <!-- 使用zookeeper 注冊(cè)中心暴露 服務(wù)地址 -->
? ? <dubbo:registry address="zookeeper://127.0.0.1:2181" id="r1" timeout="1000"/>
? ? <!--用 dubbo 協(xié)議在20882 端口暴露服務(wù) -->
? ? <dubbo:protocol name="dubbo" port="20882"/>
? ? <!--聲明需要暴露的服務(wù)接口 -->
? ? <dubbo:service interface="com.lagou.service.HelloService" ref="helloService"/>
? ? <!-- 和本地一樣實(shí)現(xiàn)服務(wù) -->
? ? <bean id="helloService" class="com.lagou.service.impl.HelloServiceImpl"></bean>
</beans>
2.4.3.4 編寫服務(wù)消費(fèi)者
1.pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
? ? ? ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? ? ? xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
? ? <parent>
? ? ? ? <artifactId>dubbo-base-xml</artifactId>
? ? ? ? <groupId>com.lagou</groupId>
? ? ? ? <version>1.0-SNAPSHOT</version>
? ? </parent>
? ? <modelVersion>4.0.0</modelVersion>
? ? <artifactId>service-conusumer-xml</artifactId>
? ? <dependencies>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.lagou</groupId>
? ? ? ? ? ? <artifactId>service-api</artifactId>
? ? ? ? ? ? <version>1.0-SNAPSHOT</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo</artifactId>
? ? ? ? ? ? <exclusions>
? ? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? ? ? ? ? <artifactId>dubbo-common</artifactId>
? ? ? ? ? ? ? ? </exclusion>
? ? ? ? ? ? </exclusions>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-registry-zookeeper</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-registry-nacos</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-rpc-dubbo</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-remoting-netty4</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.dubbo</groupId>
? ? ? ? ? ? <artifactId>dubbo-serialization-hessian2</artifactId>
? ? ? ? </dependency>
? ? </dependencies>
</project>
2.service 的實(shí)現(xiàn)
package com.lagou.service.impl;
import com.lagou.service.HelloService;
public class HelloServiceMock implements HelloService {
? ? @Override
? ? public String sayHello(String name) {
? ? ? ? return "hello mock";
? ? }
}
3.啟動(dòng)類
package com.lagou.service;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ConsumerApplication {
? ? public static void main(String[] args) {
? ? ? ? ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("dubbo-comsumer.xml");
? ? ? ? classPathXmlApplicationContext.start();
? ? ? ? HelloService helloService = classPathXmlApplicationContext.getBean("helloService", HelloService.class);
? ? ? ? String result = helloService.sayHello("world");
? ? ? ? System.out.println("result="+result);
? ? }
}
4.配置
<?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-4.3.xsd? ? ? ? http://dubbo.apache.org/schema/dubbo? ? ? ? http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
? ? <!-- 消費(fèi)房應(yīng)用名 用于計(jì)算一欄關(guān)系贩猎,不是匹配條件熊户,不要與提供方一樣 -->
? ? <dubbo:application name="service-consumer" >
? ? ? ? <dubbo:parameter key="qos.enable" value="true"/>
? ? ? ? <dubbo:parameter key="qos.port" value="3333"/>
? ? ? ? <dubbo:parameter key="qos.accept.foreign.ip" value="true"/>
? ? </dubbo:application>
? ? <!-- -->
? ? <dubbo:consumer check="false"/>
? ? <!-- 使用zookeeper 注冊(cè)中心暴露發(fā)現(xiàn)服務(wù)地址 -->
? ? <dubbo:registry address="zookeeper://127.0.0.1:2181" timeout="2000" />
? ? <!--? 生成遠(yuǎn)程服務(wù)代理,可以和本地 bean一樣使用 demoService -->
? ? <dubbo:reference id="helloService" interface="com.lagou.service.HelloService" timeout="4000" retries="2"/>
</beans>
2.5 Dubbo管理控制臺(tái) dubbo-admin
5.1 作用
主要包含:服務(wù)管理 吭服、 路由規(guī)則嚷堡、動(dòng)態(tài)配置、服務(wù)降級(jí)艇棕、訪問(wèn)控制蝌戒、權(quán)重調(diào)整、負(fù)載均衡等管理功能
如我們?cè)陂_發(fā)時(shí)沼琉,需要知道Zookeeper注冊(cè)中心都注冊(cè)了哪些服務(wù)北苟,有哪些消費(fèi)者來(lái)消費(fèi)這些服務(wù)。我們可以通過(guò)部署一個(gè)管理中心來(lái)實(shí)現(xiàn)打瘪。其實(shí)管理中心就是一個(gè)web應(yīng)用友鼻,原來(lái)是war(2.6版本以前)包需要部署到tomcat即可。現(xiàn)在是jar包可以直接通過(guò)java命令運(yùn)行闺骚。
5.2 控制臺(tái)安裝步驟
1.從git 上下載項(xiàng)目 https://github.com/apache/dubbo-admin
2.修改項(xiàng)目下的dubbo.properties文件 注意dubbo.registry.address對(duì)應(yīng)的值需要對(duì)應(yīng)當(dāng)前使用的Zookeeper的ip地址和端口號(hào)
? dubbo.registry.address=zookeeper://zk所在機(jī)器ip:zk端口
? dubbo.admin.root.password=root
? dubbo.admin.guest.password=guest
3.切換到項(xiàng)目所在的路徑 使用mvn 打包 mvn clean package -Dmaven.test.skip=true
4.java 命令運(yùn)行 java -jar 對(duì)應(yīng)的jar包
5.3 使用控制臺(tái)
1.訪問(wèn)http://IP:端口
2.輸入用戶名root,密碼root
3.點(diǎn)擊菜單查看服務(wù)提供者和服務(wù)消費(fèi)者信息
6.Dubbo配置項(xiàng)說(shuō)明
6.1 dubbo:application
對(duì)應(yīng) org.apache.dubbo.config.ApplicationConfig, 代表當(dāng)前應(yīng)用的信息
name: 當(dāng)前應(yīng)用程序的名稱彩扔,在dubbo-admin中我們也可以看到,這個(gè)代表這個(gè)應(yīng)用名稱僻爽。我們?cè)谡嬲龝r(shí)是時(shí)也會(huì)根據(jù)這個(gè)參數(shù)來(lái)進(jìn)行聚合應(yīng)用請(qǐng)求虫碉。
owner: 當(dāng)前應(yīng)用程序的負(fù)責(zé)人,可以通過(guò)這個(gè)負(fù)責(zé)人找到其相關(guān)的應(yīng)用列表胸梆,用于快速定位到責(zé)任人敦捧。
qosEnable : 是否啟動(dòng)QoS 默認(rèn)true
qosPort : 啟動(dòng)QoS綁定的端口 默認(rèn)22222
qosAcceptForeignIp: 是否允許遠(yuǎn)程訪問(wèn) 默認(rèn)是false
6.2 dubbo:registry
org.apache.dubbo.config.RegistryConfig, 代表該模塊所使用的注冊(cè)中心须板。一個(gè)模塊中的服務(wù)可以將其注冊(cè)到多個(gè)注冊(cè)中心上,也可以注冊(cè)到一個(gè)上兢卵。后面再service和reference也會(huì)引入這個(gè)注冊(cè)中心逼纸。
id : 當(dāng)當(dāng)前服務(wù)中provider或者consumer中存在多個(gè)注冊(cè)中心時(shí),則使用需要增加該配置济蝉。在一些公司,會(huì)通過(guò)業(yè)務(wù)線的不同選擇不同的注冊(cè)中心菠发,所以一般都會(huì)配置該值王滤。
address : 當(dāng)前注冊(cè)中心的訪問(wèn)地址。
protocol : 當(dāng)前注冊(cè)中心所使用的協(xié)議是什么滓鸠。也可以直接在 address 中寫入雁乡,比如使用zookeeper,就可以寫成 zookeeper://xx.xx.xx.xx:2181 4. timeout : 當(dāng)與注冊(cè)中心不再同一個(gè)機(jī)房時(shí)糜俗,大多會(huì)把該參數(shù)延長(zhǎng)踱稍。
6.3 dubbo:protocol
org.apache.dubbo.config.ProtocolConfig, 指定服務(wù)在進(jìn)行數(shù)據(jù)傳輸所使用的協(xié)議。
id : 在大公司悠抹,可能因?yàn)楦鱾€(gè)部門技術(shù)棧不同珠月,所以可能會(huì)選擇使用不同的協(xié)議進(jìn)行交互。這里在多個(gè)協(xié)議使用時(shí)楔敌,需要指定啤挎。
name : 指定協(xié)議名稱。默認(rèn)使用 dubbo 卵凑。
6.4 dubbo:reference
org.apache.dubbo.config.ReferenceConfig, 消費(fèi)者的配置庆聘,這里只做簡(jiǎn)單說(shuō)明柒竞,后面會(huì)具體講解竭沫。
id : 指定該Bean在注冊(cè)到Spring中的id。 2. interface: 服務(wù)接口名
version : 指定當(dāng)前服務(wù)版本衔憨,與服務(wù)提供者的版本一致黑忱。
registry : 指定所具體使用的注冊(cè)中心地址宴抚。這里面也就是使用上面在 dubbo:registry 中所聲明的id。
6.5 dubbo:service
org.apache.dubbo.config.ServiceConfig, 用于指定當(dāng)前需要對(duì)外暴露的服務(wù)信息杨何,后面也會(huì)具體講解酱塔。和 dubbo:reference 大致相同。
interface : 指定當(dāng)前需要進(jìn)行對(duì)外暴露的接口是什么危虱。
ref : 具體實(shí)現(xiàn)對(duì)象的引用羊娃,一般我們?cè)谏a(chǎn)級(jí)別都是使用Spring去進(jìn)行Bean托管的,所以這里面一般也指的是Spring中的BeanId埃跷。
version : 對(duì)外暴露的版本號(hào)蕊玷。不同的版本號(hào)邮利,消費(fèi)者在消費(fèi)的時(shí)候只會(huì)根據(jù)固定的版本號(hào)進(jìn)行消費(fèi)。
6.6 dubbo:method
org.apache.dubbo.config.MethodConfig, 用于在制定的 dubbo:service 或者 dubbo:reference 中的更具體一個(gè)層級(jí)垃帅,指定具體方法級(jí)別在進(jìn)行RPC操作時(shí)候的配置延届,可以理解為對(duì)這上面層級(jí)中的配置針對(duì)于具體方法的特殊處理。
name : 指定方法名稱贸诚,用于對(duì)這個(gè)方法名稱的RPC調(diào)用進(jìn)行特殊配置方庭。
async: 是否異步 默認(rèn)false
6.7 dubbo:service和dubbo:reference詳解
這兩個(gè)在dubbo中是我們最為常用的部分,其中有一些我們必然會(huì)接觸到的屬性酱固。并且這里會(huì)講到一些設(shè)置上的使用方案械念。
mock: 用于在方法調(diào)用出現(xiàn)錯(cuò)誤時(shí),當(dāng)做服務(wù)降級(jí)來(lái)統(tǒng)一對(duì)外返回結(jié)果运悲,后面我們也會(huì)對(duì)這個(gè)方法做更多的介紹龄减。
timeout: 用于指定當(dāng)前方法或者接口中所有方法的超時(shí)時(shí)間。我們一般都會(huì)根據(jù)提供者的時(shí)長(zhǎng)來(lái)具體規(guī)定班眯。比如我們?cè)谶M(jìn)行第三方服務(wù)依賴時(shí)可能會(huì)對(duì)接口的時(shí)長(zhǎng)做放寬希停,防止第三方服務(wù)不穩(wěn)定導(dǎo)致服務(wù)受損。
check: 用于在啟動(dòng)時(shí)署隘,檢查生產(chǎn)者是否有該服務(wù)宠能。我們一般都會(huì)將這個(gè)值設(shè)置為false,不讓其進(jìn)行檢查定踱。因?yàn)槿绻霈F(xiàn)模塊之間循環(huán)引用的話棍潘,那么則可能會(huì)出現(xiàn)相互依賴,都進(jìn)行check的話崖媚,那么這兩個(gè)服務(wù)永遠(yuǎn)也啟動(dòng)不起來(lái)亦歉。
retries: 用于指定當(dāng)前服務(wù)在執(zhí)行時(shí)出現(xiàn)錯(cuò)誤或者超時(shí)時(shí)的重試機(jī)制。
注意提供者是否有冪等畅哑,否則可能出現(xiàn)數(shù)據(jù)一致性問(wèn)題
注意提供者是否有類似緩存機(jī)制肴楷,如出現(xiàn)大面積錯(cuò)誤時(shí),可能因?yàn)椴煌V卦噷?dǎo)致雪崩
executes: 用于在提供者做配置荠呐,來(lái)確保最大的并行度赛蔫。
可能導(dǎo)致集群功能無(wú)法充分利用或者堵塞
但是也可以啟動(dòng)部分對(duì)應(yīng)用的保護(hù)功能
可以不做配置,結(jié)合后面的熔斷限流使用
6.8 其它配置 參考官網(wǎng)