微博RPC框架motan入门笔记

Motan

是一套高性能、易于使用的分布式远程服务调用(RPC)框架。

功能

  • 支持通过spring配置方式集成,无需额外编写代码即可为服务提供分布式调用能力。
  • 支持集成consul、zookeeper等配置服务组件,提供集群环境的服务发现及治理能力。
  • 支持动态自定义负载均衡、跨机房流量调整等高级服务调度能力。
  • 基于高并发、高负载场景进行优化,保障生产环境下RPC服务高可用。

微博的Motan RPC服务,底层通讯引擎采用了Netty网络框架,序列化协议支持Hessian和Java序列化,通讯协议支持Motan、http、tcp、mc等,Motan框架在内部大量使用,在系统的健壮性和服务治理方面,有较为成熟的技术解决方案,健壮性上,基于Config配置管理服务实现了High Availability与Load Balance策略(支持灵活的FailOver和FailFast HA策略,以及Round Robin、LRU、Consistent Hash等Load Balance策略),服务治理方面,生成完整的服务调用链数据,服务请求性能数据,响应时间(Response Time)、QPS以及标准化Error、Exception日志信息。

Motan 属于服务治理类型,是一个基于 Java 开发的高性能的轻量级 RPC 框架,Motan 提供了实用的服务治理功能和优秀的 RPC 协议扩展能力。
Motan 提供的主要功能包括:

  • 服务发现 :服务发布、订阅、通知
  • 高可用策略 :失败重试(Failover)、快速失败(Failfast)、异常隔离(Server 连续失败超过指定次数置为不可用,然后定期进行心跳探测)
  • 负载均衡 :支持低并发优先、一致性 Hash、随机请求、轮询等
  • 扩展性 :支持 SPI 扩展(service provider interface)
  • 其他 :调用统计、访问日志等

Motan 可以支持不同的 RPC 协议、传输协议。Motan 能够无缝支持 Spring 配置方式使用 RPC 服务,通过简单、灵活的配置就可以提供或使用 RPC 服务。通过使用 Motan 框架,可以十分方便的进行服务拆分、分布式服务部署。

简单调用

服务提供方

<dependency>
    <groupId>com.weibo</groupId>
    <artifactId>motan-core</artifactId>
    <version>0.2.1</version>
</dependency>
<dependency>
    <groupId>com.weibo</groupId>
    <artifactId>motan-transport-netty</artifactId>
    <version>0.2.1</version>
</dependency>

<!-- only needed for spring-based features -->
<dependency>
    <groupId>com.weibo</groupId>
    <artifactId>motan-springsupport</artifactId>
    <version>0.2.1</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.2.4.RELEASE</version>
</dependency>

服务接口

public interface FooService {
    public String hello(String name);
}

服务接口实现

public class FooServiceImpl implements FooService {
    
    public String hello(String name){
        return "Hello," + name;
    }
}

Spring配置文件motan-server.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:motan="http://api.weibo.com/schema/motan"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd">

    <!-- 新版本对protocol默认值配置处理有些问题,已经更新.实际使用中推荐对protocol进行配置 -->
    <motan:protocol default="true" name="motan"
                    maxServerConnection="80000" maxContentLength="1048576"
                    maxWorkerThread="800" minWorkerThread="20"/>

    <!-- service implemention bean -->
    <bean id="fooService" class="com.motan.demo.biz.FooServiceImpl" />
    <!-- exporting service by motan -->
    <motan:service interface="com.motan.demo.api.FooService" ref="fooService" export="8002" />
</beans>

服务提供者启动类

public class Server {
    public static void main(String[] args){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/spring/motan-server.xml");
        System.out.println("started...");
    }
}

运行main方法即可启动Spring容器。

服务消费方

pom依赖与服务提供方相同

接口

//通过jar包引入
public interface FooService {
    public String hello(String name);
}

Spring配置文件motan-client.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:motan="http://api.weibo.com/schema/motan"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd">

    <!-- reference to the remote service -->
    <motan:referer id="fooService" interface="com.motan.demo.api.FooService" directUrl="localhost:8002"/>
</beans>

消费者main方法

public class Main {
    public static void main(String[] args){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/spring/motan-client.xml");
        FooService fooService = (FooService) ctx.getBean("fooService");
        System.out.println(fooService.hello("motan"));
    }
}

运行main方法,控制台打印如下

 (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
Hello,motan

集群环境下可使用zookeeper作为注册中心,与Dubbo类似,通过注册方、服务方、调用方三方来实现。具体可以参考Github主页

479975-20160914094227898-400784845.jpg

  1. Server 向 Registry 注册服务,并向注册中心发送心跳汇报状态。
  2. Client 需要向注册中心订阅 RPC 服务,Client 根据 Registry 返回的服务列表,对具体的 Sever 进行 RPC 调用。
  3. 当 Server 发生变更时,Registry 会同步变更,Client 感知后会对本地的服务列表作相应调整。

添加pom依赖

<dependency>
    <groupId>com.weibo</groupId>
    <artifactId>motan-registry-zookeeper</artifactId>
    <version>0.2.1</version>
</dependency>

在server和client的配置文件中分别增加registry定义

<!-- zookeeper单节点 -->
<motan:registry regProtocol="zookeeper" name="my_zookeeper" address="127.0.0.1:2181"/>
<!-- zookeeper多节点集群 -->
<motan:registry regProtocol="zookeeper" name="my_zookeeper" address="127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183"/>

在motan client及server配置改为通过registry服务发现

<!-- client -->
<motan:referer id="remoteService" interface="com.motan.demo.api.FooService" registry="my_zookeeper"/>
<!-- server -->
<motan:service interface="com.motan.demo.api.FooService" ref="fooService" registry="my_zookeeper" export="8002" />

motan支持Consul和Zookeeper两种外部服务发现组件.Consul的配置方法详见官方Github文档。

参考

猜你喜欢

转载自blog.csdn.net/umgsai/article/details/54288432