再探motan


前言:上周使用motan是通過group遠程調用超級土豆的服務,但是因為我需要寫一些服務,不得不在本地啟動服務,於是就詳細的自己配置了一次motan。

上一篇博客也說到了,motan主要有3部分組成:registry,server和client。其中我們的registry用的是consul。下面就這3個部分一個一個的說明:

1、pom.xml 添加motan依賴:

    一般來說,在公共模塊的pom文件里添加依賴就可以了,比如在server、client、dao和common的SNA編程模型下,只要在common的pom文件中添加依賴即可

 1 <dependency>
 2      <groupId>com.weibo</groupId>
 3      <artifactId>motan-core</artifactId>
 4      <version>RELEASE</version>
 5  </dependency>
 6  <dependency>
 7      <groupId>com.weibo</groupId>
 8      <artifactId>motan-transport-netty</artifactId>
 9      <version>RELEASE</version>
10  </dependency>
11  
12  <!-- only needed for spring-based features -->
13  <dependency>
14      <groupId>com.weibo</groupId>
15      <artifactId>motan-springsupport</artifactId>
16      <version>RELEASE</version>
17  </dependency>
18  <dependency>
19      <groupId>org.springframework</groupId>
20      <artifactId>spring-context</artifactId>
21      <version>4.2.4.RELEASE</version>
22  </dependency>

2、配置motan

Motan框架中將功能模塊抽象為四個可配置的元素,分別為:

  • protocol:服務通信協議。服務提供方與消費方進行遠程調用的協議,默認為Motan協議,使用hessian2進行序列化,netty作為Endpoint以及使用Motan自定義的協議編碼方式。

  • registry:注冊中心。服務提供方將服務信息(包含ip、端口、服務策略等信息)注冊到注冊中心,服務消費方通過注冊中心發現服務。當服務發生變更,注冊中心負責通知各個消費方。

  • service:服務提供方提供的服務。使用方將核心業務抽取出來,作為獨立的服務。通過暴露服務並將服務注冊至注冊中心,從而使調用方調用。

  • referer:服務消費方對服務的引用,即服務調用方。

       一般來說,在server端需要配置registry、protocol和service;在client端需要配置registry、protocol和referer。

Motan推薦使用spring配置rpc服務,目前Motan擴展了6個自定義Spring xml標簽:

  • motan:protocol
  • motan:registry
  • motan:basicService
  • motan:service
  • motan:basicReferer
  • motan:referer

詳細配置:

<motan:registry/>

  注冊中心配置。用於配置注冊中心的注冊協議、地址端口、超時時間等。motan:registry包含以下常用屬性:

    • name:標識配置名稱
    • regProtocol:標識注冊中心協議
    • address:標識注冊中心地址

  Motan支持使用多種Registry模塊,使用不同注冊中心需要依賴對應jar包。

  以consul為注冊中心舉例:

<motan:registry  regProtocol="consul" 
          name
="my_consul"
          address
="${my.consul.address}"/>

  下表是registry的所有屬性說明:

Property name Type Default Comment
name String   注冊配置名稱
regProtocol String   注冊協議
address String   注冊中心地址
port int 0 注冊中心缺省端口
connectTimeout int 1000 注冊中心連接超時時間(毫秒)
requestTimeout int 200 注冊中心請求超時時間(毫秒)
registrySessionTimeout int 60s 注冊中心會話超時時間(毫秒)
registryRetryPeriod int 30s 失敗后重試的時間間隔
check boolean true 啟動時檢查失敗后是否仍然啟動
register boolean true 在該注冊中心上服務是否暴露
subscribe boolean true 在該注冊中心上服務是否引用
default boolean   是否缺省的配置

 

 

 

 

 

 

 

 

 

 

 

 

<motan:service/> 和 <motan:basicService/>

protocol、basic service、extConfig、service中定義相同屬性時,優先級為service > extConfig > basic service > protocol

  <motan:service .../>

  motan:service包含以下常用屬性:

    • interface:標識服務的接口類名
    • ref:標識服務的實現類,引用具體的spring業務實現對象
    • export:標識服務的暴露方式,格式為“protocolId:port”(使用的協議及對外提供的端口號),其中protocolId:應與motan:protocol中的id一致
    • group:標識服務的分組
    • module:標識模塊信息
    • basicService:標識使用的基本配置,引用motan:basicService對象

  Motan在注冊中心的服務是以group的形式保存的,一般推薦一個分組以機房+業務線進行命名,如yf-user-rpc。一個分組中包含若干的Service,一個Service即是java中的一個接口類名,每個Service下有一組能夠提供對應服務的Server。

<motan:basicService .../>

  rpc服務的通用配置,用於配置所有服務接口的公共配置,減少配置冗余。basicService包含以下常用屬性:

    • id:標識配置項
    • export:標識服務的暴露方式,格式為“protocolId:port”(使用的協議及對外提供的端口號),其中protocolId:應與motan:protocol中的id一致
    • group:標識服務的分組
    • module:標識模塊信息
    • registry:標識service使用的注冊中心,與motan:registry中的name對應

  motan:service可以通過以下方式引用基本配置。

<!-- 通用配置,多個rpc服務使用相同的基礎配置. group和module定義具體的服務池。export格式為“protocol id:提供服務的端口” -->
<motan:basicService id="serviceBasicConfig" export="demoMotan:8002" group="motan-demo-rpc" module="motan-demo-rpc" registry="registry"/>
<!-- 通用配置,多個rpc服務使用相同的基礎配置. group和module定義具體的服務池。export格式為“protocol id:提供服務的端口” -->
<motan:service interface="com.weibo.motan.demo.service.MotanDemoService" ref="demoServiceImpl" basicService="serviceBasicConfig"/>

 

motan:service中的basicService屬性用來標識引用哪個motan:basicService對象,對於basicService中已定義的內容,service不必重復配置。

下表是service的所有屬性說明:

Property name Type Default Comment
export String   服務暴露的方式,包含協議及端口號,多個協議端口用"," 分隔
basicService     基本service配置
interface Class   服務接口名
ref String   接口實現的類
class String   實現service的類名
host String   如果有多個ip,但只想暴露指定的某個ip,設置該參數
path String   服務路徑
serialization String hessian2 序列化方式
extConfig String   擴展配置
proxy String   代理類型
group String default_rpc 服務分組
version String 1.0 版本
throwException String true 拋出異常
requestTimeout String 200 (目前未用)請求超時時間(毫秒)
connectTimeout String 1000 (目前未用)連接超時時間(毫秒)
retries int 0 (目前未用)重試次數
filter String   過濾器配置
listener String   監聽器配置
connections int   連接數限制,0表示共享連接,否則為該服務獨享連接數;默認共享
application String motan 應用信息
module String motan 模塊信息
shareChannel boolean false 是否共享channel
timeout int   方法調用超時時間
actives int 0 最大請求數,0為不做並發限制
async boolean false 方法是否異步
mock String false 設為true,表示使用缺省Mock類名,即:接口名+Mock 后綴,服務接口調用失敗Mock實現類
check boolean true 檢查服務提供者是否存在
registry String   注冊中心的id 列表,多個用“,”分隔,如果為空,則使用所有的配置中心
register boolean true 在該注冊中心上服務是否暴露
subscribe boolean true 在該注冊中心上服務是否引用
accessLog String false 設為true,將向logger 中輸出訪問日志
usegz boolean false 是否開啟gzip壓縮.只有compressMotan的codec才能支持
mingzSize int 1000 開啟gzip壓縮的閾值.usegz開關開啟,且傳輸數據大於此閾值時,才會進行gzip壓縮。只有compressMotan的codec才能支持
codec String motan 協議編碼

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<motan:referer/>和<motan:basicReferer/>

protocol、basic referer、extConfig、referer中定義相同屬性時,優先級為referer > extConfig > basic referer > protocol

 

<motan:referer/>

調用方對象,motan:referer包含以下常用屬性:

  • id:標識配置項
  • group:標識服務的分組
  • module:標識模塊信息
  • protocol:標識referer使用的協議,與motan:protocol中的name對應,默認為Motan協議
  • registry:標識referer使用的注冊中心,與motan:registry中的name對應
  • basicReferer:標識使用的基本配置,引用motan:basicReferer對象

Client端訂閱Service后,會從Registry中得到能夠提供對應Service的一組Server,Client把這一組Server看作一個提供服務的cluster。當cluster中的Server發生變更時,Client端的register模塊會通知Client進行更新。

<motan:basicReferer/>

調用方基礎配置。用於配置所有服務代理的公共屬性。

  • id:標識配置項
  • group:標識服務的分組
  • module:標識模塊信息
  • protocol:標識referer使用的協議,與motan:protocol中的name對應,默認為Motan協議
  • registry:標識referer使用的注冊中心,與motan:registry中的name對應

motan:referer可以通過以下方式引用基本配置。

<!-- 通用referer基礎配置 -->
<motan:basicReferer id="clientBasicConfig" group="motan-demo-rpc" module="motan-demo-rpc"  registry="registry" protocol="motan"/>

<!-- 具體referer配置。使用方通過beanid使用服務接口類 -->
<motan:referer id="demoReferer" interface="com.weibo.motan.demo.service.MotanDemoService"  basicReferer="clientBasicConfig"/>

 

motan:referer中的basicService屬性用來標識引用哪個motan:basicReferer對象,對於basicReferer中已定義的內容,service不必重復配置。

下表是referer的所有屬性說明:

 

Property name Type Default Comment
id String   服務引用 BeanId
protocol String motan 使用的協議
interface Class   服務接口名
client String   客戶端類型
directUrl String   點對點直連服務提供地址
basicReferer String   基本 referer 配置
extConfig String   擴展配置
proxy String   代理類型
group String default_rpc 服務分組
version String 1.0 版本
throwException String true 拋出異常
requestTimeout String 200 請求超時時間(毫秒)
connectTimeout String 1000 連接超時時間(毫秒)
retries int 0 重試次數
filter String   過濾器配置
listener String   監聽器配置
connections int   連接數限制,0表示共享連接,否則為該服務獨享連接數;默認共享
application String motan 應用信息
module String motan 模塊信息
shareChannel boolean false 是否共享channel
timeout int   (目前未用)方法調用超時時間
actives int 0 最大請求數,0為不做並發限制
async boolean false 方法是否異步
mock String false 設為true,表示使用缺省Mock類名,即:接口名+Mock 后綴,服務接口調用失敗Mock實現類
check boolean true 檢查服務提供者是否存在
registry String   注冊中心的id 列表,多個用“,”分隔,如果為空,則使用所有的配置中心
register boolean true 在該注冊中心上服務是否暴露
subscribe boolean true 在該注冊中心上服務是否引用
accessLog String false 設為true,將向logger 中輸出訪問日志
usegz boolean false 是否開啟gzip壓縮.只有compressMotan的codec才能支持
mingzSize int 1000 開啟gzip壓縮的閾值.usegz開關開啟,且傳輸數據大於此閾值時,才會進行gzip壓縮。只有compressMotan的codec才能支持
codec String motan 協議編碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM