本文小編為大家詳細介紹“輕量級分布式RPC框架motan怎么使用”,內容詳細,步驟清晰,細節處理妥當,希望這篇“輕量級分布式RPC框架motan怎么使用”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
Motan是一套高性能、易于使用的分布式遠程服務調用(RPC)框架。
支持通過spring配置方式集成,無需額外編寫代碼即可為服務提供分布式調用能力。
支持集成consul、zookeeper等配置服務組件,提供集群環境的服務發現及治理能力。
支持動態自定義負載均衡、跨機房流量調整等高級服務調度能力。
基于高并發、高負載場景進行優化,保障生產環境下RPC服務高可用。
<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
package quickstart; public interface FooService { public String hello(String name); }
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..."); } }
執行Server類中的main函數將會啟動Motan服務,并監聽8002端口.
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")); } }
執行Client類中的main函數將執行一次遠程調用,并輸出結果。
在集群環境下使用Motan需要依賴外部服務發現組件,目前支持consul或zookeeper。
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
啟動(官方文檔)
測試環境啟動: consul agent -dev
ui后臺 http://localhost:8500/ui
在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服務發現。
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程序啟動后,需要顯式調用心跳開關,注冊到consul。
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true)
進入ui后臺查看服務是否正常提供調用
啟動client,調用服務
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
在server和client中添加motan-registry-zookeeper依賴
<dependency> <groupId>com.weibogroupId> <artifactId>motan-registry-zookeeperartifactId> <version>0.1.1version> <dependency>
在server和client的配置文件中分別增加zookeeper registry定義。
zookeeper為單節點
<motan:registry regProtocol="zookeeper" name="my_zookeeper" address="127.0.0.1:2181"/>
zookeeper多節點集群
<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
<motan:referer id="remoteService" interface="quickstart.FooService" registry="my_zookeeper"/>
server
<motan:service interface="quickstart.FooService" ref="serviceImpl" registry="my_zookeeper" export="8002" />
server程序啟動后,需要顯式調用心跳開關,注冊到zookeeper。
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true)
啟動client,調用服務
讀到這里,這篇“輕量級分布式RPC框架motan怎么使用”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。