再探motan

原文章:http://www.10qianwan.com/articledetail/219355.html

motan主要有3部分组成:registry,server和client。其中我们的registry用的是consul。下面就这3个部分一个一个的说明:

1、pom.xml 添加motan依赖:

    一般来说,在公共模块的pom文件里添加依赖就可以了,比如在server、client、dao和common的sna编程模型下,只要在common的pom文件中添加依赖即可

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

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为注册中心举例:

1

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

  下表是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/>

1

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。

1

<motan:basicservice .../><br>

  rpc服务的通用配置,用于配置所有服务接口的公共配置,减少配置冗余。basicservice包含以下常用属性:

    • id:标识配置项
    • export:标识服务的暴露方式,格式为“protocolid:port”(使用的协议及对外提供的端口号),其中protocolid:应与motan:protocol中的id一致
    • group:标识服务的分组
    • module:标识模块信息
    • registry:标识service使用的注册中心,与motan:registry中的name对应

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

1

2

3

4

<!-- 通用配置,多个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/>

1

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可以通过以下方式引用基本配置。

1

2

3

4

5

<!-- 通用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 协议编码

猜你喜欢

转载自blog.csdn.net/haponchang/article/details/90746435