微博RPC框架Motan

article/2025/9/11 4:29:12

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


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

https://github.com/weibocom/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>  
项目结构如图

User类,注意必须实现Serializable

[java] view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public class User implements Serializable {  
  2.   
  3.     private static final long serialVersionUID = 2925168737226033271L;  
  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选项

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

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

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


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 dependency:copy-dependencies

mvn clean package

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

java -jar motan-examples.jar

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

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


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

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


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

6 总结

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

缺点:

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

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

优点:

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

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



http://chatgpt.dhexx.cn/article/LH4bUw3J.shtml

相关文章

motan rpc 接口统一异常处理

1.hello word 一个Motan扩展 大概需要下面的三点&#xff1a; 实现SPI扩展点接口 package com.weibo.api.motan.filter; Spi public interface Filter {Response filter(Caller<?> caller, Request request); }业务代码实现Filter public class PlsProviderExceptionF…

motan用户开发指南

目录 基本介绍 架构概述 模块概述 配置概述 使用Motan 工程依赖 处理调用异常 配置说明 协议与连接&#xff08;motan:protocol) 介绍 Motan协议 本地调用 注册中心与服务发现(motan:registry) 介绍 使用Consul作为注册中心 使用Zookeeper作为注册中心 不使用…

从motan看RPC框架设计

kris的文章开始 计算机科学领域的任何问题都可以通过增加一个间接的中间层来解决 从零开发一款RPC框架&#xff0c;说难也难说简单也简单。难的是你的设计将如何面对实际中的复杂应用场景&#xff1b;简单的是其思想可以仅仅浓缩成一行方法调用。motan是今年(2016年)新浪微博…

motan与zookeeper框架

新浪科技讯 2016年5月10日&#xff0c;微博方面宣布&#xff0c;支撑微博千亿调用的轻量级 RPC 框架 Motan 正式开源了。微博技术团队希望未来能有更多优秀的开源人入驻&#xff0c;并进一步完善优化。 搭建新浪RPC框架motan Demo&#xff1a;http://blog.csdn.net/linuu/arti…

java rpc motan_RPC框架motan使用

简介 motan是新浪微博开源的一套轻量级、方便使用的RPC框架 Hello World 使用的过程分为Server端和Client端&#xff0c;Server提供RCP的服务接口&#xff0c;Client端发起调用获取结果。 maven的pom文件配置 0.2.1 com.weibo motan-core ${motan.version} com.weibo motan-tra…

轻量级Rpc框架设计--motan源码解析一:框架介绍及框架使用初体验

一, 框架介绍 1.1 概况 motan是新浪微博开源出来的一套高性能、易于使用的分布式远程服务调用(RPC)框架。 1.2 功能 可以spring的配置方式与项目集成. 支持zookeeper服务发现组件, 实现集群环境下服务注册与发现. 保证高并发, 高负载场景下的稳定高性能, , 实现生产环境…

Motan原理、使用、JavaAPI简化、为什么使用Motan

前言&#xff0c;本文包括&#xff0c;rpc解释与为什么使用rpc、rpc性能对比、Motan依赖问题、Motan源码梳理、Motan功能、特点、使用。 主要中心&#xff1a;为什么使用Motan? 一、什么是RPC 官方解释&#xff1a;RPC&#xff08;Remote Procedure Call&#xff09;—远程…

jplayer自动播放

音乐网站的播放器一直都没有解决自动播放的问题&#xff0c;小哲说这样不行的&#xff0c;我也知道不可以这样&#xff0c;毕竟是自己提出要做的&#xff0c;所以要尽自己最大的能力去做好它&#xff01;本周末我一直都在围绕这个问题而研究。 我曾经想过在播放器初始化的时候…

JWPlayer

原文&#xff1a; http://www.cnblogs.com/yukui/archive/2009/03/12/1409469.html The JW MP3 Player (built with Adobes Flash) is the easiest way to add live music or podcasts to your website. It supports playback of a single MP3 file or an RSS, XSPF or ASX pla…

今天开始写些随笔,就从Jplayer开始吧

今天才开始用Jplayer&#xff0c;可能有点落伍了&#xff0c;但是看到网上千篇一律的使用说明&#xff0c;开始决定把自己的使用心得分享一下&#xff0c;废话不多说&#xff0c;开始吧。 Step1&#xff1a; 官网上有具体的搭建顺序&#xff0c;URL&#xff1a;http://www.jp…

关于播放器JPlayer的使用及遇到的问题

jPlayer是一个用于控制和播放mp3文件的jQuery插件。它在后台使用Flash来播放mp3文件&#xff0c;前台播放器外观完全可以使用XHML/CSS自定义。支持&#xff1a; 有一点比较好的是&#xff0c;在支持html5的浏览器上会使用html5的标签audio或者video&#xff0c;而不支持的浏览…

ijkplayer支持播放rtsp、jpeg、gif

ijkplayer版本&#xff1a;k.0.8.8 编译环境&#xff1a;Ubuntu 18.04.6 LTS 使用平台&#xff1a;android 支持rtsp播放 默认的ijkplayer并不支持rtsp流的播放&#xff0c;因为在编译ffmpeg的时候并没有开启rtsp的demuxer&#xff0c;所以在编译ffmpeg的时候需要开启rtsp的d…

【ijkplayer】介绍

【ijkplayer】介绍 0x1 系统架构 ijkplayer是由b站开源的播放器项目&#xff0c;底层基于ffmpeg, 支持Android和iOS。下面我们来简单介绍一下Android上的实现。 Android上的系统架构图如下。 下面分别对各个模块进行介绍&#xff1a; 0x11 ijkplayer-example app的实现&a…

一款简洁的 jplayer 音乐播放器完整版

一款简洁 jplayer 音乐播放器&#xff0c;做音乐站很漂亮&#xff0c;直接套用就好了。 效果图&#xff1a; 部分源代码&#xff1a; <div id"lei_jplayer"></div> <div id"jp_container_1"><div class"jp-controls">…

Ijkplayer Android介绍

Ijkplayer Android目录结构 Ijkplayer Android可以将该工程导入android studio进行编译&#xff0c;当它下载好需要的资源文件&#xff08;例如sdk build tool&#xff0c;gradle等&#xff09;就可以进行编译了&#xff0c;该工程的目录结构如下图所示: 链接库&#xff1a…

ijkplayer播放器

播放器系列 android播放器&#xff1a;MediaPlayer ExoPlayer ijkplayer_步基的博客-CSDN博客_mediacodec流程 一 概述 IJKPlayer是一款基于ffmpeg/ffplay的开源播放器&#xff0c;可支持rtmp/rtsp/hls等多种媒体协议&#xff0c;支持Android/IOS等移动平台。项目地址&#xff…

jplayer详解

下载官网&#xff1a;http://www.jplayer.org/ 当前版本&#xff1a;2.3.0 功能&#xff1a;视频播放&#xff08;可全屏&#xff09;、音乐播放 全部原教程&#xff0c;说明并不详细&#xff0c;要结合查看其网页源代码来学习&#xff1a;http://www.jplayer.org/latest/demos…

Javplayer 整合插件版下载使用

Javplayer - 马赛克去除工具 工作中去除图片模糊像素&#xff0c;还原高清图片Javplayer - 马赛克去除工具简介版本安装使用调整示例注意事项体验视频小结咨询 工作中去除图片模糊像素&#xff0c;还原高清图片 Javplayer - 马赛克去除工具简介 JavPlayer是一种视频播放器&am…

jPlayer使用详解

原文地址&#xff1a;http://blog.csdn.net/z295304557/article/details/17298889?_t_t_t0.39512087353182124 下载官网&#xff1a;http://www.jplayer.org/ 当前版本&#xff1a;2.3.0 功能&#xff1a;视频播放&#xff08;可全屏&#xff09;、音乐播放 全部原教程&#x…

jPlayer插件的使用

jPlayer插件的使用 2017年04月21日 11:03:19 JENIFURS 阅读数&#xff1a;3952 标签&#xff1a; jPlayer视频插件兼容bug 更多 个人分类&#xff1a; jQuery 版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。 https://blog.csdn.net/fuyujiaof/arti…