微博RPC框架Motan

微博RPC框架Motan原文来自 http blog csdn net autfish article details 从 14 年开始就陆续看到新浪微博 RPC 框架 Motan 的介绍 时隔两年后 微博团队终于宣布开源轻量级 RPC 框架 Motan 项目地址 https github com weibocom motan 项目文档介绍比较详细 搭建开发环境非常简单 如果只是使用

原文来自:http://blog.csdn.net/autfish/article/details/

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

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

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

1 公共部分

pom.xml

[java] view plain copy print ?
在CODE上查看代码片
派生到我的代码片

  1. <dependencies>  
  2.     <dependency>  
  3.         <groupId>com.weibo</groupId>  
  4.         <artifactId>motan-core</artifactId>  
  5.         <version>0.0.1</version>  
  6.     </dependency>  
  7.     <dependency>  
  8.         <groupId>com.weibo</groupId>  
  9.         <artifactId>motan-transport-netty</artifactId>  
  10.         <version>0.0.1</version>  
  11.     </dependency>  
  12.     <!– only needed for spring-based features –>  
  13.     <dependency>  
  14.         <groupId>com.weibo</groupId>  
  15.         <artifactId>motan-springsupport</artifactId>  
  16.         <version>0.0.1</version>  
  17.     </dependency>  
  18.     <dependency>  
  19.         <groupId>org.springframework</groupId>  
  20.         <artifactId>spring-context</artifactId>  
  21.         <version>4.2.4.RELEASE</version>  
  22.     </dependency>  
  23.     <dependency>  
  24.         <groupId>com.weibo</groupId>  
  25.         <artifactId>motan-registry-zookeeper</artifactId>  
  26.         <version>0.0.1</version>  
  27.     </dependency>  
  28. </dependencies>  

项目结构如图

微博RPC框架Motan

User类,注意必须实现Serializable

[java] view plain copy print ?
在CODE上查看代码片
派生到我的代码片

  1. public class User implements Serializable {  
  2.   
  3.     private static final long serialVersionUID = L;  
  4.   
  5.     public User(int id, String name) {  
  6.         this.id = id;  
  7.         this.name = name;  
  8.     }  
  9.       
  10.     private int id;  
  11.     private String name;  
  12.       
  13.     public int getId() {  
  14.         return id;  
  15.     }  
  16.     public void setId(int id) {  
  17.         this.id = id;  
  18.     }  
  19.     public String getName() {  
  20.         return name;  
  21.     }  
  22.     public void setName(String name) {  
  23.         this.name = name;  
  24.     }  
  25.     public String toString() {  
  26.         return String.format(“{id=%d,name=%s}”this.id, this.name);  
  27.     }  
  28. }  

UserService

[java] view plain copy print ?
在CODE上查看代码片
派生到我的代码片

  1. public interface UserService {  
  2.     public User find(int id);  
  3. }  

2 服务器部分

UserServiceImpl

[java] view plain copy print ?
在CODE上查看代码片
派生到我的代码片

  1. public class UserServiceImpl implements UserService {  
  2.     public User find(int id) {  
  3.         System.out.println(id + ” invoked rpc service”);  
  4.         return new User(id, “name” + id);  
  5.     }  
  6. }  

motan_server.xml

[java] view plain copy print ?
在CODE上查看代码片
派生到我的代码片

  1. <?xml version=“1.0” encoding=“UTF-8”?>  
  2. <beans xmlns=“http://www.springframework.org/schema/beans”  
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:motan=“http://api.weibo.com/schema/motan”  
  4.     xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  5.    http://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd”>  
  6.   
  7.     <motan:protocol name=“motan” loadbalance=“roundrobin” maxWorkerThread=“500” minWorkerThread=“20” />  
  8.     <bean id=“serviceImpl” class=“quickstart.UserServiceImpl” />  
  9.     <motan:registry regProtocol=“zookeeper” name=“my_zookeeper” address=“127.0.0.1:2181”/>  
  10.     <motan:service interface=“quickstart.UserService” ref=“serviceImpl” registry=“my_zookeeper” export=“8002” />  
  11. </beans>  

Server.java

[java] view plain copy print ?
在CODE上查看代码片
派生到我的代码片

  1. public class Server {  
  2.     public static void main(String[] args) throws InterruptedException {  
  3.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext(  
  4.                 “classpath:motan_server.xml”);  
  5.         System.out.println(“server start…”);  
  6.     }  
  7. }  

集群部署需要使用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选项

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

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

微博RPC框架Motan

4 部署多服务器实例

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

[java] view plain copy print ?
在CODE上查看代码片
派生到我的代码片

  1. <build>  
  2.         <finalName>motan-examples</finalName>  
  3.         <plugins>  
  4.             <plugin>  
  5.                 <groupId>org.apache.maven.plugins</groupId>  
  6.                 <artifactId>maven-jar-plugin</artifactId>  
  7.                 <version>2.4</version>  
  8.                 <configuration>  
  9.                     <archive>  
  10.                         <manifest>  
  11.                             <addClasspath>true</addClasspath>  
  12.                             <classpathPrefix>.</classpathPrefix>  
  13.                             <mainClass>quickstart.Server</mainClass>  
  14.                         </manifest>  
  15.                     </archive>  
  16.                 </configuration>  
  17.             </plugin>  
  18.         </plugins>  
  19.     </build>  

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

进入项目目录执行

mvn clean package

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

java -jar motan-examples.jar

5 客户端部分

motan_client.xml

[java] view plain copy print ?
在CODE上查看代码片
派生到我的代码片

  1. <?xml version=“1.0” encoding=“UTF-8”?>  
  2. <beans xmlns=“http://www.springframework.org/schema/beans”  
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:motan=“http://api.weibo.com/schema/motan”  
  4.     xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  5.    http://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd”>  
  6.   
  7.     <motan:protocol name=“motan” haStrategy=“failover” />  
  8.     <motan:registry regProtocol=“zookeeper” name=“my_zookeeper” address=“127.0.0.1:2181”/>  
  9.     <motan:referer id=“remoteService” interface=“quickstart.UserService” registry=“my_zookeeper”/>  
  10. </beans>  

Client.java

[java] view plain copy print ?
在CODE上查看代码片
派生到我的代码片

  1. public class Client {  
  2.   
  3.     public static void main(String[] args) throws InterruptedException {  
  4.         ApplicationContext ctx = new ClassPathXmlApplicationContext(  
  5.                 “classpath:motan_client.xml”);  
  6.         final UserService service = (UserService) ctx.getBean(“remoteService”);  
  7.   
  8.         final int SIZE = 100;  
  9.         final AtomicInteger atom = new AtomicInteger();  
  10.         ExecutorService exec = Executors.newFixedThreadPool(SIZE);  
  11.         for(int i = 0; i < SIZE; i++) {  
  12.             exec.execute(new Thread(){  
  13.                 public void run() {  
  14.                     User user = service.find(atom.addAndGet(1));  
  15.                     System.out.println(user);  
  16.                 }  
  17.             });  
  18.         }  
  19.     }  
  20. }  

运行Client.java

微博RPC框架Motan

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

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

微博RPC框架Motan

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

6 总结

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

缺点:

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

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

优点:

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

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


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233753.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • centos 7-aarch64如何替换yum源「建议收藏」

    centos 7-aarch64如何替换yum源「建议收藏」一、进入yum.repo.d[root@node-01~]#cd/etc/yum.repos.d/[root@node-01yum.repos.d]#lsCentOS-Base.repoCentOS-Sources.repo二、备份原yum源[root@node-01yum.repos.d]#mkdiryum-back[root@node-01yu…

    2022年9月25日
    3
  • 自动化测试平台(八):列表组件公共化封装和用例项目管理功能开发

    自动化测试平台(八):列表组件公共化封装和用例项目管理功能开发上一章我们完成了整个用户管理模块的功能,能够正确的增、删、改、查用户。但其中有很多判断实际上是其他类似的模块也会有的,例如:1.创建用户后回到首页刷新列表;2.删除次页最后一条数据,回到前一页刷新列表;3.查询条件的格式化;—难道我们每写一个类似的模块,都要去写一遍这些重复的逻辑代码吗?显然是没必要的,所以我们需要将其抽离成公共列表组件提供给其他模块使用,避免大量的做重复的事情,并让代码更容易维护。本章还将完成用例项目管理功能,它主要用于管理不同类型(API、UI),不同项目

    2022年6月25日
    21
  • jvm触发full gc条件(Linux内存管理机制)

    1、Java垃圾回收机制GC,即就是Java垃圾回收机制。目前主流的JVM(HotSpot)采用的是分代收集算法。作为Java开发者,一般不需要专门编写内存回收和垃圾清理代码,对内存泄露和溢出的问题。与C++不同的是,Java采用的是类似于树形结构的可达性分析法来判断对象是否还存在引用。即:从gcroot开始,把所有可以搜索得到的对象标记为存活对象。缺点就是:1.有可能不知不…

    2022年4月12日
    79
  • Spring Boot 打的包为什么能直接运行?

    Spring Boot 提供了一个插件 spring-boot-maven-plugin 用于把程序打包成一个可执行的jar包。 在pom文件里加入这个插件即可: <buil…

    2021年6月22日
    120
  • tkmapper教程_tkmapper

    tkmapper教程_tkmapperTKmapper初学springboot的集成,方式分为两大类:基于starter的自动配置基于@MapperScan注解的手工配置在starter的逻辑中,如果你没有使用@MapperScan注解,你就需要在你的接口上增加@Mapper注解,否则MyBatis无法判断扫描哪些接口。<dependency><groupId>tk.mybatis</groupId><artifactId>mapper-spri

    2022年10月7日
    3
  • 零基础学习JAVA其实并不难!不相信?进来看看你就知道了

    零基础学习JAVA其实并不难!不相信?进来看看你就知道了其实Java并没有想象中的那么难,首先想要入这个行,要做好一个心理准备,那就是你想走远点,就得不间断的去学习,去汲取知识,前期不能怕辛苦,不要闲下来就打LOL、吃鸡、王者农药,有空就得多看看各种开源项目的代码,API的设计方式,各大网站的设计架构,理解各个环节的作用。补齐自己的知识视野。  当然这个行业也并不是什么门槛都没有,不要再私信我初中生、高中生、中专生能不能学习Java了。反正我个人是认为不可行的,或许你可以去问问其他大神?或许他们会觉得可以的。  下图是我更新过的自学表,分别分为4个阶段。按

    2022年7月7日
    40

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号