新浪開源輕量級分布式RPC框架motan簡單示例解析
前言
好消息,支撐微博千億調(diào)用的輕量級 RPC 框架 Motan 在2016年5月份正式開源了,業(yè)界現(xiàn)在除了Dubbo 和 DubboX典型的分布式RPC服務治理型框架外,又多了一個優(yōu)秀的分布式RPC了。心動了嗎?使用過dubbo的話,so easy的上手,官方實例如下,動起來吧
我的demo地址,參考官方實例的簡單demo,包含zookeeper注冊中心,以及服務監(jiān)控平臺:https://coding.net/u/kailingchen/p/motan_Test/git
概述
Motan是一套高性能、易于使用的分布式遠程服務調(diào)用(RPC)框架。
github項目地址:https://github.com/weibocom/motan
功能
- 支持通過spring配置方式集成,無需額外編寫代碼即可為服務提供分布式調(diào)用能力。
- 支持集成consul、zookeeper等配置服務組件,提供集群環(huán)境的服務發(fā)現(xiàn)及治理能力。
- 支持動態(tài)自定義負載均衡、跨機房流量調(diào)整等高級服務調(diào)度能力。
- 基于高并發(fā)、高負載場景進行優(yōu)化,保障生產(chǎn)環(huán)境下RPC服務高可用。
簡單調(diào)用示例
在pom中添加依賴
<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>
為調(diào)用方和服務方創(chuàng)建公共接口
src/main/java/quickstart/FooService.java
package quickstart; public interface FooService { public String hello(String name); }
編寫業(yè)務接口邏輯、創(chuàng)建并啟動RPC Server
src/main/java/quickstart/FooServiceImpl.java
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
<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
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..."); } }
執(zhí)行Server類中的main函數(shù)將會啟動Motan服務,并監(jiān)聽8002端口.
創(chuàng)建并執(zhí)行RPC Client
src/main/resources/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:referer id="remoteService" interface="quickstart.FooService" directUrl="localhost:8002"/>
src/main/java/quickstart/Client.java
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")); } }
執(zhí)行Client類中的main函數(shù)將執(zhí)行一次遠程調(diào)用,并輸出結(jié)果。
集群調(diào)用示例
在集群環(huán)境下使用Motan需要依賴外部服務發(fā)現(xiàn)組件,目前支持consul或zookeeper。
使用CONSUL作為注冊中心
Consul安裝與啟動
安裝(官方文檔)
# 這里以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
啟動(官方文檔)
測試環(huán)境啟動: consul agent -dev
ui后臺 http://localhost:8500/ui
Motan-Consul配置
在server和client中添加motan-registry-consul依賴
<dependency> <groupId>com.weibogroupId> <artifactId>motan-registry-consulartifactId> <version>0.1.1version> <dependency>
在server和client的配置文件中分別增加consul registry定義。
<motan:registry regProtocol="consul" name="my_consul" address="127.0.0.1:8500"/>
在Motan client及server配置改為通過registry服務發(fā)現(xiàn)。
client
<motan:referer id="remoteService" interface="quickstart.FooService" registry="my_consul"/>
server
<motan:service interface="quickstart.FooService" ref="serviceImpl" registry="my_consul" export="8002" />
server程序啟動后,需要顯式調(diào)用心跳開關(guān),注冊到consul。
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true)
進入ui后臺查看服務是否正常提供調(diào)用
啟動client,調(diào)用服務
使用ZOOKEEPER作為注冊中心
ZooKeeper安裝與啟動
單機版安裝與啟動
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依賴
<dependency> <groupId>com.weibogroupId> <artifactId>motan-registry-zookeeperartifactId> <version>0.1.1version> <dependency>
在server和client的配置文件中分別增加zookeeper registry定義。
zookeeper為單節(jié)點
<motan:registry regProtocol="zookeeper" name="my_zookeeper" address="127.0.0.1:2181"/>
zookeeper多節(jié)點集群
<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服務發(fā)現(xiàn)。
client
<motan:referer id="remoteService" interface="quickstart.FooService" registry="my_zookeeper"/>
server
<motan:service interface="quickstart.FooService" ref="serviceImpl" registry="my_zookeeper" export="8002" />
server程序啟動后,需要顯式調(diào)用心跳開關(guān),注冊到zookeeper。
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true)
啟動client,調(diào)用服務
以上就是新浪開源輕量級分布式RPC框架motan簡單示例解析的詳細內(nèi)容,更多關(guān)于新浪開源輕量級分布式RPC框架motan的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
用3個實例從原理到實戰(zhàn)講清楚Log4j史詩級漏洞
最近應該很多人都在關(guān)注著一個漏洞Apache Log4j 2遠程代碼執(zhí)行,該漏洞一旦被攻擊者利用會造成嚴重危害,這篇文章主要給大家介紹了關(guān)于如何用3個實例從原理到實戰(zhàn)講清楚Log4j史詩級漏洞的相關(guān)資料,需要的朋友可以參考下2021-12-12從Myeclipse 導入到eclipse中無法識別為 web項目 問題的解決步驟
這篇文章主要介紹了從Myeclipse 導入到eclipse中無法識別為 web項目 問題的解決步驟,需要的朋友可以參考下2018-05-05mybatis TypeHandler注入spring的依賴方式
這篇文章主要介紹了mybatis TypeHandler注入spring的依賴方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01Spring MVC獲取參數(shù)和自定義參數(shù)類型轉(zhuǎn)換器及編碼過濾器
這篇文章主要為大家詳細介紹了Spring MVC獲取參數(shù)和自定義參數(shù)類型轉(zhuǎn)換器及編碼過濾器,文中通過代碼示例介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下2023-06-06