从14年开始就陆续看到新浪微博RPC框架Motan的介绍,时隔两年后,微博团队终于宣布开源轻量级RPC框架Motan,项目地址:

https://github.com/weibocom/motan/

项目文档介绍比较详细,搭建开发环境非常简单,如果只是使用而不需要源码的话,只配置maven依赖项目即可,按照示例几分钟就可以搭建起一个Hello world。当然这也是官方介绍中的优点之一。

我们来扩展一下官方的示例,并测试一下集群式部署。首先创建一个maven项目

1 公共部分

pom.xml

	<dependencies>
<dependency>
<groupId>com.weibo</groupId>
<artifactId>motan-core</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.weibo</groupId>
<artifactId>motan-transport-netty</artifactId>
<version>0.0.1</version>
</dependency>
<!-- only needed for spring-based features -->
<dependency>
<groupId>com.weibo</groupId>
<artifactId>motan-springsupport</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.weibo</groupId>
<artifactId>motan-registry-zookeeper</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>

项目结构如图

User类,注意必须实现Serializable

public class User implements Serializable {

	private static final long serialVersionUID = 2925168737226033271L;

	public User(int id, String name) {
this.id = id;
this.name = name;
} private int id;
private String name; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return String.format("{id=%d,name=%s}", this.id, this.name);
}
}

UserService

public interface UserService {
public User find(int id);
}

2 服务器部分

UserServiceImpl

public class UserServiceImpl implements UserService {
public User find(int id) {
System.out.println(id + " invoked rpc service");
return new User(id, "name" + id);
}
}

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-2.5.xsd
http://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd"> <motan:protocol name="motan" loadbalance="roundrobin" maxWorkerThread="500" minWorkerThread="20" />
<bean id="serviceImpl" class="quickstart.UserServiceImpl" />
<motan:registry regProtocol="zookeeper" name="my_zookeeper" address="127.0.0.1:2181"/>
<motan:service interface="quickstart.UserService" ref="serviceImpl" registry="my_zookeeper" export="8002" />
</beans>

Server.java

public class Server {
public static void main(String[] args) throws InterruptedException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"classpath:motan_server.xml");
System.out.println("server start...");
}
}

集群部署需要使用Zookeeper做注册中心

3 部署Zookeeper

以windows环境为例

下载地址 http://zookeeper.apache.org/

下载后得到gz包如zookeeper-3.4.8.tar.gz,解压到任意目录如d:\zookeeper-3.4.8

在zookeeper-3.4.8目录下建立data文件夹

进入zookeeper-3.4.8/conf目录,把zoo_sample.cfg重命名为zoo.cfg,并修改dataDir选项

dataDir=d:\\zookeeper-3.4.8\\data

进入zookeeper-3.4.8/bin目录,执行zkServer.cmd

运行Server.java测试,启动成功

4 部署多服务器实例

下面把服务器端部署两个实例,修改pom.xml,添加

<build>
<finalName>motan-examples</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>.</classpathPrefix>
<mainClass>quickstart.Server</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

注意正确配置mainClass的类全路径

进入项目目录执行

mvn dependency:copy-dependencies

mvn clean package

把生成的可执行jar和依赖jar包复制到一起,并执行

java -jar motan-examples.jar

修改motan_server.xml中的export端口为8003,重复上面步骤再启动一个在8003端口上的实例

5 客户端部分

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"> <motan:protocol name="motan" haStrategy="failover" />
<motan:registry regProtocol="zookeeper" name="my_zookeeper" address="127.0.0.1:2181"/>
<motan:referer id="remoteService" interface="quickstart.UserService" registry="my_zookeeper"/>
</beans>

Client.java

public class Client {

	public static void main(String[] args) throws InterruptedException {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:motan_client.xml");
final UserService service = (UserService) ctx.getBean("remoteService"); final int SIZE = 100;
final AtomicInteger atom = new AtomicInteger();
ExecutorService exec = Executors.newFixedThreadPool(SIZE);
for(int i = 0; i < SIZE; i++) {
exec.execute(new Thread(){
public void run() {
User user = service.find(atom.addAndGet(1));
System.out.println(user);
}
});
}
}
}

运行Client.java

从输出结果可以看出,客户端请求被分配到两个服务器实例中。

关闭其中一个服务器实例,重新运行客户端

因为Zookeeper处理服务器断开的消息有一定延时,一部分请求仍然被提交到已关闭的端口上,导致抛出异常。但是这里并没有按配置执行失败切换服务器的策略,是配置问题还是不支持网络拒绝连接的失败类型,限于时间关系,没有做更多测试,暂时打个问号。

6 总结

同是RPC框架,就不可避免的要和另一个优秀开源框架dubbo/dubbox比较

缺点:

1 功能较少,不支持跨语言调用

2 年轻,稳定性和可能出现的问题尚待检验

优点:

1 轻量级,开发和学习简单

2 年轻,有无限的发展可能性。dubbo因为原创团队的原因已经停止更新,motan能否后来居上,让我们拭目以待。

Dobbo的继任者?试用微博RPC框架Motan的更多相关文章

  1. 微博RPC框架motan入门笔记

    Motan 是一套高性能.易于使用的分布式远程服务调用(RPC)框架. 功能 支持通过spring配置方式集成,无需额外编写代码即可为服务提供分布式调用能力. 支持集成consul.zookeeper ...

  2. 微博开源框架Motan初体验

    前两天,我在开源中国的微信公众号看到新浪微博的轻量Rpc框架--Motan开源了.上网查了下,才得知这个Motan来头不小,支撑着新浪微博的千亿调用,曾经在2014年的春晚中有着千亿次的调用,对抗了春 ...

  3. 微博轻量级RPC框架Motan正式开源:支撑千亿调用

    支撑微博千亿调用的轻量级 RPC 框架 Motan 正式开源了,项目地址为https://github.com/weibocom/motan. 微博轻量级RPC框架Motan正式开源 Motan 是微 ...

  4. 微博轻量级RPC框架Motan

    Motan 是微博技术团队研发的基于 Java 的轻量级 RPC 框架,已在微博内部大规模应用多年,每天稳定支撑微博上亿次的内部调用.Motan 基于微博的高并发和高负载场景优化,成为一套简单.易用. ...

  5. RPC框架motan: 通信框架netty( 1)

    服务器端编程都离不开底层的通信框架,在我们刚学习java的时候,主要接触都是Socket和ServerSocket 的阻塞编程,后来开始了解NIO,这种非阻塞的编程模式,它可以一个线程管理很多的Soc ...

  6. 轻量级RPC框架-motan

    https://github.com/weibocom/motan/wiki/zh_quickstart#%E7%AE%80%E5%8D%95%E8%B0%83%E7%94%A8%E7%A4%BA%E ...

  7. RPC框架motan: 通信框架netty之Netty4Client

    上文已经初步探讨了如何实现一个具体的transport,本文就来讨论一个具体的transport,本文讨论netty4的的相关实现.老规矩,看看motan-transport的目录结构. 其中最重要的 ...

  8. [转]新兵训练营系列课程——平台RPC框架介绍

    原文:http://weibo.com/p/1001643875439147097368 课程大纲 1.RPC简介 1.1 什么是RPC 1.2 RPC与其他远程调用方式比较 2.Motan RPC框 ...

  9. 分布式RPC框架性能大比拼 dubbo、motan、rpcx、gRPC、thrift的性能比较

    Dubbo 是阿里巴巴公司开源的一个Java高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring框架无缝集成.不过,略有遗憾的是,据说在淘宝内部,dub ...

随机推荐

  1. (实用篇)php数组查找函数in_array()、array_search()、array_key_exists()使用

    php在数组中查找指定值是否存在的方法有很多,记得很久以前我一直都是傻傻的用foreach循环来查找的,下面我主要分享一下用php内置的三个数组函数来查找指定值是否存在于数组中,这三个数组分别是 in ...

  2. jQuery plugin: Autocomplete 参数及实例

    官网:jQuery plugin: Autocomplete          (注:此插件已经不再更新.它的继任者是jQuery UI的一部分,) 此插件依赖于 jquery 1.2.6 --- j ...

  3. javascript confirm()函数的用法

    javascript confirm()函数的用法 confirm():确认消息对话框.用于允许用户做选择的动作.弹出的对话框中包含一确定按钮和一取消按钮. confirm(str) 参数说明: st ...

  4. caffe的python接口

    http://blog.csdn.net/lu597203933/article/details/46742199 hadoop改成自己名字

  5. 在Bootstrap中 强调相关的类

    .text-muted:提示,使用浅灰色(#999) .text-primary:主要,使用蓝色(#428bca) .text-success:成功,使用浅绿色(#3c763d) .text-info ...

  6. Android——配置文件的保存SharedPreferences进行数据存储

    很多时候我们开发的软件需要向用户提供软件参数设置功能,例如我们常用的QQ,用户可以设置是否允许陌生人添加自己为好友.对于软件配置参数的保存,如果是window软件通常我们会采用ini文件进行保存,如果 ...

  7. 【转】WebKit 与 V8 的关系

    页面的绘制(绘制,就是把一个HTML文件变成一个活灵活现的页面展示的过程...),只有一半轮子是Chrome自己做的,还有一部分来自于WebKit,这个Apple打造的Web渲染器...之所以说是一半 ...

  8. 【线性代数】 06 - Jordan标准型

    现在就来研究将空间分割为不变子空间的方法,最困难的是我们还不知道从哪里着手.你可能想到从循环子空间出发,一块一块地进行分割,但这个方案的存在性和唯一性都不能解决.不变子空间分割不仅要求每个子空间\(V ...

  9. 通过Oracle补充日志,找到锁阻塞源头的SQL

    问题背景: 有时会考虑一件事情,如果在Oracle环境下出现了锁阻塞的情况,如何定位到SQL源头(通过session.lock.transaction等视图仅能定位到会话)?或许有人会想有没有可能通过 ...

  10. SQL总结(一)基本查询

    SQL总结(一)基本查询 SQL查询的事情很简单,但是常常因为很简单的事情而出错.遇到一些比较复杂的查询我们更是忘记了SQL查询的基本语法. 本文希望通过简单的总结,把常用的查询方法予以总结,希望能够 ...