前言:上周使用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 | 协议编码 |