這里我們只介紹motan的注解方式。
1、server端配置
(1)聲明Annotation用來指定需要解析的包名
@Bean public AnnotationBean motanAnnotationBean() { AnnotationBean motanAnnotationBean = new AnnotationBean(); motanAnnotationBean.setPackage("com.weibo.motan.demo.server"); return motanAnnotationBean; }
(2)配置ProtocolConfig、RegistryConfig、BasicServiceConfig的bean對象,功能與xml配置中的<motan:protocol ....>、<motan:registry .....>、<motan:basicService ....>標簽一致。
@Bean(name = "demoMotan") public ProtocolConfigBean protocolConfig1() { ProtocolConfigBean config = new ProtocolConfigBean(); config.setDefault(true); config.setName("motan"); config.setMaxContentLength(1048576); return config; } @Bean(name = "registryConfig1") public RegistryConfigBean registryConfig() { RegistryConfigBean config = new RegistryConfigBean(); config.setRegProtocol("local"); return config; } @Bean public BasicServiceConfigBean baseServiceConfig() { BasicServiceConfigBean config = new BasicServiceConfigBean(); config.setExport("demoMotan:8002"); config.setGroup("testgroup"); config.setAccessLog(false); config.setShareChannel(true); config.setModule("motan-demo-rpc"); config.setApplication("myMotanDemo"); config.setRegistry("registryConfig1"); return config; }
(3)service的實現類上添加@MotanService注解,注解的配置參數與xml配置方式的<motan:service ....>標簽一致。
@MotanService(export = "demoMotan:8002") public class MotanDemoServiceImpl implements MotanDemoService { public String hello(String name) { System.out.println(name); return "Hello " + name + "!"; } }
(4)使用spring-boot啟動服務
@EnableAutoConfiguration @SpringBootApplication public class SpringBootRpcServerDemo { public static void main(String[] args) { System.setProperty("server.port", "8081"); ConfigurableApplicationContext context = SpringApplication.run(SpringBootRpcServerDemo.class, args); MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true); System.out.println("server start..."); } }
這里為什么要設置一個開關,引用作者在github中的解釋:
motan是通過這個開關控制是否對外提供rpc服務的。
一般正式環境使用時是需要驗證服務正常后才正式對外提供服務的,防止有問題的服務注冊到線上。另外在停機的時候,可以先通過開關暫停服務,等沒有請求后在進行shutdown。
2、client端的配置
(1)聲明Annotation、protocolConfig、RegistryConfig的配置bean。方式與server端配置類似。
(2)配置basicRefererConfig bean
@Bean(name = "motantestClientBasicConfig") public BasicRefererConfigBean baseRefererConfig() { BasicRefererConfigBean config = new BasicRefererConfigBean(); config.setProtocol("demoMotan"); config.setGroup("motan-demo-rpc"); config.setModule("motan-demo-rpc"); config.setApplication("myMotanDemo"); config.setRegistry("registry"); config.setCheck(false); config.setAccessLog(true); config.setRetries(2); config.setThrowException(true); return config; }
(3)在使用motan service 的對象上添加@MotanReferer注解,注冊配置與xml方式的<motan:referer ....>標簽一致
@RestController public class HelloController { @MotanReferer(basicReferer = "motantestClientBasicConfig", group = "testgroup", directUrl = "127.0.0.1:8002") MotanDemoService service; @RequestMapping("/") @ResponseBody public String home() { String result = service.hello("test"); return result; } }
3、配置說明
這里只說下<motan:service .../>和<motan:basicService .../>兩個(<motan:refer .../>和<motan:basicRefer .../>同理)
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可以通過以下方式引用基本配置。
協議配置列表
<motan:protocol/>
Property name | Type | Default | Comment |
---|---|---|---|
name | String | 服務協議名 | |
serialization | String | hessian2 | 序列化方式 |
payload | int | 最大請求數據長度 | |
buffer | int | 緩存區大小 | |
heartbeat | int | 心跳間隔 | |
transporter | String | 網絡傳輸方式 | |
threads | int | 線程池大小 | |
iothreads | int | availableProcessors+1 | IO線程池大小 |
requestTimeout | int | 200 | 請求超時 |
minClientConnection | int | 2 | client最小連接數 |
maxClientConnection | int | 10 | client最大連接數 |
minWorkerThread | int | 20 | 最小工作pool線程數 |
maxWorkerThread | int | 200 | 最大工作pool線程數 |
maxContentLength | int | 10M | 請求響應包的最大長度限制 |
maxServerConnection | int | 100000 | server支持的最大連接數 |
poolLifo | boolean | true | 連接池管理方式,是否lifo |
lazyInit | boolean | false | 是否延遲init |
endpointFactory | boolean | motan | endpoint factory |
cluster | String | default | 采用哪種cluster的實現 |
loadbalance | String | activeWeight | 負載均衡策略 |
haStrategy | String | failover | 高可用策略 |
workerQueueSize | String | 0 | Server工作隊列大小 |
acceptConnections | int | 0 | Server可接受連接數 |
proxy | String | jdk | proxy type, like jdk or javassist |
filter | String | filter, 多個filter用","分割,blank String 表示采用默認的filter配置 | |
retries | int | 0 | 調用失敗時重試次數 |
async | boolean | false | if the request is called async, a taskFuture result will be sent back |
queueSize | Int | 線程池隊列大小 | |
accepts | Int | 最大接收連接數 | |
dispatcher | String | 信息線程模型派發方式 | |
server | String | 服務器端實現 | |
client | String | 客戶端端實現 | |
default | boolean | 是否缺省的配置 | |
switcherService | String | localSwitcherService | |
heartbeatFactory | String | motan |
注冊中心配置列表
<motan: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
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 | 協議編碼 |
client配置列表
<motan:referer/>
<motan:basicReferer/>
protocol、basic referer、extConfig、referer中定義相同屬性時,優先級為referer > extConfig > basic referer > protocol
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 | 協議編碼 |
<motan:method/>
需要定義在motan:referer內,用於控制某個函數的行為
Property name | Type | Default | Comment |
---|---|---|---|
name | String | 函數名 | |
argumentTypes | String | 參數類型(逗號分隔), 無參數用void. 如果方法無重載,則可不寫 | |
requestTimeout | int | 200 | 請求超時時間(毫秒) |
connectTimeout | int | 1000 | 連接超時時間(毫秒) |