Pom中添加依賴java
<dependency> <groupId>com.weibo</groupId> <artifactId>motan-core</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>com.weibo</groupId> <artifactId>motan-transport-netty</artifactId> <version>RELEASE</version> </dependency> <!-- only needed for spring-based features --> <dependency> <groupId>com.weibo</groupId> <artifactId>motan-springsupport</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> </dependency>
爲調用方和服務方建立公共接口spring
src/main/java/quickstart/FooService.java package quickstart; public interface FooService { public String hello(String name); }
編寫業務接口邏輯,建立並啓動RPC Serverapi
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"> <!-- service implemention bean --> <bean id="serviceImpl" class="quickstart.FooServiceImpl" /> <!-- exporting service by Motan --> <motan:service interface="quickstart.FooService" ref="serviceImpl" export="8002" /> </beans>
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端口。app
建立並執行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"> <!-- reference to the remote service --> <motan:referer id="remoteService" interface="quickstart.FooService" directUrl="localhost:8002"/> </beans> 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函數將執行一次遠程調用,並輸出結果。async
異步調用和同步調用基本配置同樣,只須要在接口類中加@MotanAsync註解,而後Client端稍做修改,server端不須要作任何修改。分佈式
package quickstart; @MotanAsync public interface FooService { public String hello(String name); }
編譯時,Motan會自動生成異步service類,生成的類名爲service名加上Async。 在client配置motan-client.xml時,在同步調用配置的基礎上,只須要修改refer的interface爲Motan自動生成的接口類便可。ide
<motan:referer id="remoteService" interface="quickstart.FooServiceAsync" directUrl="localhost:8002"/>
異步使用方式以下:函數
public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"classpath:motan_client.xml"}); FooServiceAsync service = (FooServiceAsync) ctx.getBean("remoteService"); // sync call System.out.println(service.hello("motan")); // async call ResponseFuture future = service.helloAsync("motan async "); System.out.println(future.getValue()); // multi call ResponseFuture future1 = service.helloAsync("motan async multi-1"); ResponseFuture future2 = service.helloAsync("motan async multi-2"); System.out.println(future1.getValue() + ", " + future2.getValue()); // async with listener FutureListener listener = new FutureListener() { @Override public void operationComplete(Future future) throws Exception { System.out.println("async call " + (future.isSuccess() ? "sucess! value:" + future.getValue() : "fail! exception:" + future.getException().getMessage())); } }; ResponseFuture future3 = service.helloAsync("motan async multi-1"); ResponseFuture future4 = service.helloAsync("motan async multi-2"); future3.addListener(listener); future4.addListener(listener); }
在分佈式環境下,motan的使用須要依賴於外部服務發現組件,目前支持consul或zk。ui
server和client中添加moan-registry-consul依賴
<dependency> <groupId>com.weibo</groupId> <artifactId>motan-registry-consul</artifactId> <version>RELEASE</version> </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)
zookeeper的實現和Consul相似。