前言
好消息,支撑微博千亿调用的轻量级 RPC 框架 Motan 在2016年5月份正式开源了,业界现在除了Dubbo 和 DubboX典型的分布式RPC服务治理型框架外,又多了一个优秀的分布式RPC了。心动了吗?使用过dubbo的话,so easy的上手,官方实例如下,动起来吧
我的demo地址,参考官方实例的简单demo,包含zookeeper注册中心,以及服务监控平台:https://coding.net/u/kailingchen/p/motan_Test/git
概述
Motan是一套高性能、易于使用的分布式远程服务调用(RPC)框架。
github项目地址:https://github.com/weibocom/motan
功能
- 支持通过spring配置方式集成,无需额外编写代码即可为服务提供分布式调用能力。
- 支持集成consul、zookeeper等配置服务组件,提供集群环境的服务发现及治理能力。
- 支持动态自定义负载均衡、跨机房流量调整等高级服务调度能力。
- 基于高并发、高负载场景进行优化,保障生产环境下RPC服务高可用。
简单调用示例
在pom中添加依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
< dependency > < groupId >com.weibogroupId> < artifactId >motan-coreartifactId> < version >0.1.1version> dependency> < dependency > < groupId >com.weibogroupId> < artifactId >motan-transport-nettyartifactId> < version >0.1.1version> dependency> < dependency > < groupId >com.weibogroupId> < artifactId >motan-springsupportartifactId> < version >0.1.1version> dependency> < dependency > < groupId >org.springframeworkgroupId> < artifactId >spring-contextartifactId> < version >4.2.4.RELEASEversion> < dependency > |
为调用方和服务方创建公共接口
src/main/java/quickstart/FooService.java
1
2
|
package quickstart; public interface FooService { public String hello(String name); } |
编写业务接口逻辑、创建并启动RPC Server
src/main/java/quickstart/FooServiceImpl.java
1
2
3
|
package quickstart; public class FooServiceImpl implements FooService { public String hello(String name) { System.out.println(name + " invoked rpc service" ); return "hello " + name; } } |
src/main/resources/motan_server.xml
1
2
|
< 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" > < bean id = "serviceImpl" class = "quickstart.FooServiceImpl" /> < motan:service interface = "quickstart.FooService" ref = "serviceImpl" export = "8002" /> |
src/main/java/quickstart/Server.java
1
2
3
|
package quickstart; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Server { public static void main(String[] args) throws InterruptedException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "classpath:motan_server.xml" ); System.out.println( "server start..." ); } } |
执行Server类中的main函数将会启动Motan服务,并监听8002端口.
创建并执行RPC Client
src/main/resources/motan_client.xml
1
2
|
< 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:referer id = "remoteService" interface = "quickstart.FooService" directUrl = "localhost:8002" /> |
src/main/java/quickstart/Client.java
1
2
3
|
package quickstart; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args) throws InterruptedException { ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:motan_client.xml"); FooService service = (FooService) ctx.getBean("remoteService"); System.out.println(service.hello("motan")); } } |
执行Client类中的main函数将执行一次远程调用,并输出结果。
集群调用示例
在集群环境下使用Motan需要依赖外部服务发现组件,目前支持consul或zookeeper。
使用CONSUL作为注册中心
Consul安装与启动
安装(官方文档)
1
2
3
4
|
# 这里以linux为例 wget https: //releases .hashicorp.com /consul/0 .6.4 /consul_0 .6.4_linux_amd64.zip unzip consul_0.6.4_linux_amd64.zip sudo mv consul /bin |
启动(官方文档)
1
2
|
测试环境启动: consul agent -dev |
ui后台 http://localhost:8500/ui
Motan-Consul配置
在server和client中添加motan-registry-consul依赖
1
2
3
4
5
|
< dependency > < groupId >com.weibogroupId> < artifactId >motan-registry-consulartifactId> < version >0.1.1version> < dependency > |
在server和client的配置文件中分别增加consul registry定义。
1
|
< motan:registry regProtocol = "consul" name = "my_consul" address = "127.0.0.1:8500" /> |
在Motan client及server配置改为通过registry服务发现。
client
1
|
< motan:referer id = "remoteService" interface = "quickstart.FooService" registry = "my_consul" /> |
server
1
|
< motan:service interface = "quickstart.FooService" ref = "serviceImpl" registry = "my_consul" export = "8002" /> |
server程序启动后,需要显式调用心跳开关,注册到consul。
1
|
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true ) |
进入ui后台查看服务是否正常提供调用
启动client,调用服务
使用ZOOKEEPER作为注册中心
ZooKeeper安装与启动
单机版安装与启动
1
2
3
4
5
6
7
8
|
wget http: //mirrors .cnnic.cn /apache/zookeeper/zookeeper-3 .4.8 /zookeeper-3 .4.8. tar .gz tar zxvf zookeeper-3.4.8. tar .gz cd zookeeper-3.4.8 /conf/ cp zoo_sample.cfg zoo.cfg cd ../ sh bin /zkServer .sh start |
Motan-ZooKeeper配置
在server和client中添加motan-registry-zookeeper依赖
1
2
3
4
5
|
< dependency > < groupId >com.weibogroupId> < artifactId >motan-registry-zookeeperartifactId> < version >0.1.1version> < dependency > |
在server和client的配置文件中分别增加zookeeper registry定义。
zookeeper为单节点
1
|
< motan:registry regProtocol = "zookeeper" name = "my_zookeeper" address = "127.0.0.1:2181" /> |
zookeeper多节点集群
1
|
< 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
1
|
< motan:referer id = "remoteService" interface = "quickstart.FooService" registry = "my_zookeeper" /> |
server
1
|
< motan:service interface = "quickstart.FooService" ref = "serviceImpl" registry = "my_zookeeper" export = "8002" /> |
server程序启动后,需要显式调用心跳开关,注册到zookeeper。
1
|
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true ) |
启动client,调用服务
以上就是新浪开源轻量级分布式RPC框架motan简单示例解析的详细内容,更多关于新浪开源轻量级分布式RPC框架motan的资料请关注服务器之家其它相关文章!
原文链接:http://www.kailing.pub/article/index/arcid/118.html