現(xiàn)在在項(xiàng)目中已經(jīng)應(yīng)用了spring-boot赵辕,其有很多starter方便使用,因此打算寫一個(gè)motan的starter遭京,用起來(lái)更像spring-boot的風(fēng)格圃酵。另一個(gè)是官方的例子還不是很完善雏门,后續(xù)可能會(huì)修改吧嘿歌。
- 首先參照官方demo并與xml配置做下對(duì)照
- 需要一個(gè)返回類型為AnnotationBean的bean定義,這個(gè)用來(lái)掃描包來(lái)解析motan的注解進(jìn)行暴露服務(wù)或引用服務(wù)
- 需要一個(gè)返回類型為RegistryConfigBean的bean定義茁影,這個(gè)是注冊(cè)中心配置bean宙帝,對(duì)應(yīng)xml配置:
<motan:registry />
- 需要一個(gè)返回類型為BasicServiceConfigBean的bean定義,這個(gè)是RPC基礎(chǔ)服務(wù)配置募闲,可以避免每個(gè)服務(wù)暴露時(shí)都要配置相同的屬性步脓,由它統(tǒng)一配置好,服務(wù)暴露或引用時(shí)浩螺,直接使用這個(gè)配置即可靴患,對(duì)應(yīng)xml配置:
<motan:basicService />
- 需要一個(gè)返回類型為ProtocolConfigBean的bean定義,這個(gè)是協(xié)議配置bean要出,對(duì)應(yīng)xml配置:
<motan:protocol />
- 對(duì)于客戶端需要一個(gè)返回類型為BasicRefererConfigBean的bean定義鸳君,對(duì)應(yīng)xml配置:
<motan:basicReferer/>
- 官方例子配置數(shù)據(jù)直接寫在類里,下面利用spring-boot配置特性修改這部分代碼
- 建立對(duì)應(yīng)上面的4個(gè)配置類BasicRefererConfigProperties患蹂、
BasicServiceConfigProperties或颊、ProtocolConfigProperties、
RegistryConfigProperties用于對(duì)應(yīng)配置文件的配置屬性 - 建立5個(gè)bean传于,其中4個(gè)對(duì)應(yīng)上的4個(gè)配置類囱挑,用于設(shè)置屬性,另一個(gè)是AnnotationBean用于掃描的配置
- 增加注冊(cè)中心的心跳配置
// 非local配置時(shí)沼溜,才去配置
if (!registryConfig.getRegProtocol().toLowerCase().equals("local")) {
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);
}
- 增加對(duì)BasicServiceConfigBean的條件配置平挑,避免客戶端調(diào)用時(shí)也會(huì)創(chuàng)建該bean,對(duì)于BasicRefererConfigBean不做限制
目前大概實(shí)現(xiàn)這些系草,還有一些屬性不全弹惦,Motan官方的配置文檔中有些屬性已經(jīng)不能使用否淤,估計(jì)是對(duì)之前的版本兼容
具體代碼參考github
使用的方法參照demo
主要是:
- 引入pom
<dependency>
<groupId>com.github.chenxing2.motan</groupId>
<artifactId>spring-boot-starter-motan</artifactId>
<version>0.1</version>
</dependency>
- 服務(wù)器端配置
# Motan Aonnotation config
motan.annotation.package=com.github.chenxing2.demo.server
# Motan Registry config
#
# 不使用注冊(cè)中心
motan.registry.regProtocol=local
#
# 使用注冊(cè)中心,zookeeper
#motan.registry.regProtocol=zookeeper
#motan.registry.address=127.0.0.1:2181
#motan.registry.connectTimeout=2000
#
# Motan Protocol config
# Motan BasicService config
motan.basicservice.application=demo
motan.basicservice.exportPort=8888
motan.basicservice.accessLog=false
motan.basicservice.shareChannel=true
- 客戶端配置
# Motan Aonnotation config
motan.annotation.package=com.github.chenxing2.demo.client
# Motan Registry config
#
# 不使用注冊(cè)中心棠隐,這里配置為 direct,而不是local
motan.registry.regProtocol=direct
motan.registry.address=localhost:8888
#
# 使用注冊(cè)中心檐嚣,zookeeper
#motan.registry.regProtocol=zookeeper
#motan.registry.address=127.0.0.1:2181
#motan.registry.connectTimeout=2000
#
# Motan Protocol config
# Motan BasicService config
motan.basicreferer.application=demo
motan.basicreferer.accessLog=false
motan.basicreferer.retries=3
motan.basicreferer.throwException=true