微博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主页
- Server 向 Registry 注册服务,并向注册中心发送心跳汇报状态。
- Client 需要向注册中心订阅 RPC 服务,Client 根据 Registry 返回的服务列表,对具体的 Sever 进行 RPC 调用。
- 当 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文档。
参考
- http://www.oschina.net/p/motan-weibo
- http://www.jianshu.com/p/2c4a0bc9d8f7
- https://github.com/weibocom/motan
微博RPC框架motan入门笔记的更多相关文章
- Dobbo的继任者?试用微博RPC框架Motan
从14年开始就陆续看到新浪微博RPC框架Motan的介绍,时隔两年后,微博团队终于宣布开源轻量级RPC框架Motan,项目地址: https://github.com/weibocom/motan/ ...
- 微博开源框架Motan初体验
前两天,我在开源中国的微信公众号看到新浪微博的轻量Rpc框架--Motan开源了.上网查了下,才得知这个Motan来头不小,支撑着新浪微博的千亿调用,曾经在2014年的春晚中有着千亿次的调用,对抗了春 ...
- 微博轻量级RPC框架Motan正式开源:支撑千亿调用
支撑微博千亿调用的轻量级 RPC 框架 Motan 正式开源了,项目地址为https://github.com/weibocom/motan. 微博轻量级RPC框架Motan正式开源 Motan 是微 ...
- golang微服务框架go-micro 入门笔记2.2 micro工具之微应用利器micro web
micro web micro 功能非常强大,本文将详细阐述micro web 命令行的功能 阅读本文前你可能需要进行如下知识储备 golang分布式微服务框架go-micro 入门笔记1:搭建go- ...
- golang微服务框架go-micro 入门笔记2.1 micro工具之micro api
micro api micro 功能非常强大,本文将详细阐述micro api 命令行的功能 重要的事情说3次 本文全部代码https://idea.techidea8.com/open/idea.s ...
- golang微服务框架go-micro 入门笔记2.4 go-micro service解读
本章节阐述go-micro 服务发现原理 go-micro架构 下图来自go-micro官方 阅读本文前你可能需要进行如下知识储备 golang分布式微服务框架go-micro 入门笔记1:搭建go- ...
- golang微服务框架go-micro 入门笔记2.3 micro工具之消息接收和发布
本章节阐述micro消息订阅和发布相关内容 阅读本文前你可能需要进行如下知识储备 golang分布式微服务框架go-micro 入门笔记1:搭建go-micro环境, golang微服务框架go-mi ...
- 微博轻量级RPC框架Motan
Motan 是微博技术团队研发的基于 Java 的轻量级 RPC 框架,已在微博内部大规模应用多年,每天稳定支撑微博上亿次的内部调用.Motan 基于微博的高并发和高负载场景优化,成为一套简单.易用. ...
- 动手实现一个简单的 rpc 框架到入门 grpc (上)
rpc 全称 Remote Procedure Call 远程过程调用,即调用远程方法.我们调用当前进程中的方法时很简单,但是想要调用不同进程,甚至不同主机.不同语言中的方法时就需要借助 rpc 来实 ...
随机推荐
- 烂泥:haproxy学习之https配置
本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb. 在前一段时间,我写了几篇有关学习haproxy的文章.今天我们再来介绍下haproxy ...
- FusionCharts参数说明 (中文)
FushionCharts是把抽象数据图示化的套件,使用方便,配置简单.其相关参数中文说明如下. FusionCharts Free中文开发指南第二版.pdf 功能特性 animation ...
- Maven设置代理服务器
在setting.xml中设置以下信息即可,请修正对应信息 <proxies> <proxy> <id>optional</id> <active ...
- PHP_Bibel阅读笔记(二)——脸黑的一天(?一年)
一早上起来把50包开了,一张橙卡...就问还有谁...........本命年啊,我去买红内裤还不行么.... 实时更新,老哥的号的30包什么都没有....不过中午又开了5包,皇帝,好评啊!!! 五.代 ...
- 【读书笔记《Bootstrap 实战》】3.优化站点资源、完成响应式图片、让传送带支持手势
A.优化站点资源 速度很重要.用户很关心.我们的站点必须加载够快,否则用户就会走人.SEO 也很重要.我们的站点必须加载够快,否者搜索排名就会下降. 明白了这样,我们就来清点一下 [Bootstrap ...
- NOIP2009最优贸易[spfa变形|tarjan 缩点 DP]
题目描述 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个 城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分 为双向通行的道路 ...
- jenkins,jmeter,ant报告模板
http://www.cnblogs.com/yangxia-test/p/5283139.html
- Python黑客编程ARP欺骗
Python灰帽编程 3.1 ARP欺骗 ARP欺骗是一种在局域网中常用的攻击手段,目的是让局域网中指定的(或全部)的目标机器的数据包都通过攻击者主机进行转发,是实现中间人攻击的常用手段,从而实现数据 ...
- MySQL的基本知识 -- 函数
MySQL基本知识 -- 进阶(常用的函数) Tags: MySQL MySQL进阶 1.计算字段 1.概念 : 如果客户端想要的数据格式不是数据库直接在表中存储的数据格式的话,有两种处理方式.第一种 ...
- 【笔记5】用pandas实现矩阵数据格式的推荐算法 (基于物品的协同)
''' 基于物品的协同推荐 矩阵数据 说明: 1.修正的余弦相似度是一种基于模型的协同过滤算法.我们前面提过,这种算法的优势之 一是扩展性好,对于大数据量而言,运算速度快.占用内存少. 2.用户的评价 ...