简单调用示例

同步调用

  • 在pom中添加依赖
  1. <dependency>
  2. <groupId>com.weibo</groupId>
  3. <artifactId>motan-core</artifactId>
  4. <version>RELEASE</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.weibo</groupId>
  8. <artifactId>motan-transport-netty</artifactId>
  9. <version>RELEASE</version>
  10. </dependency>
  11.  
  12. <!-- only needed for spring-based features -->
  13. <dependency>
  14. <groupId>com.weibo</groupId>
  15. <artifactId>motan-springsupport</artifactId>
  16. <version>RELEASE</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-context</artifactId>
  21. <version>4.2.4.RELEASE</version>
  22. </dependency>
  • 为调用方和服务方创建公共接口。

src/main/java/quickstart/FooService.java

  1. package quickstart;
  2.  
  3. public interface FooService {
  4. public String hello(String name);
  5. }
  • 编写业务接口逻辑、创建并启动RPC Server。

src/main/java/quickstart/FooServiceImpl.java

  1. package quickstart;
  2.  
  3. public class FooServiceImpl implements FooService {
  4.  
  5. public String hello(String name) {
  6. System.out.println(name + " invoked rpc service");
  7. return "hello " + name;
  8. }
  9. }

src/main/resources/motan_server.xml

  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"
  4. xmlns:motan="http://api.weibo.com/schema/motan"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6. http://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd">
  7.  
  8. <!-- service implemention bean -->
  9. <bean id="serviceImpl" class="quickstart.FooServiceImpl" />
  10. <!-- exporting service by Motan -->
  11. <motan:service interface="quickstart.FooService" ref="serviceImpl" export="8002" />
  12. </beans>
  1. `src/main/java/quickstart/Server.java`
  1. package quickstart;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class Server {
  7.  
  8. public static void main(String[] args) throws InterruptedException {
  9. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:motan_server.xml");
  10. System.out.println("server start...");
  11. }
  12. }

执行Server类中的main函数将会启动Motan服务,并监听8002端口.

  • 创建并执行RPC Client。

src/main/resources/motan_client.xml

  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"
  4. xmlns:motan="http://api.weibo.com/schema/motan"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6. http://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd">
  7.  
  8. <!-- reference to the remote service -->
  9. <motan:referer id="remoteService" interface="quickstart.FooService" directUrl="localhost:8002"/>
  10. </beans>

src/main/java/quickstart/Client.java

  1. package quickstart;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6.  
  7. public class Client {
  8.  
  9. public static void main(String[] args) throws InterruptedException {
  10. ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:motan_client.xml");
  11. FooService service = (FooService) ctx.getBean("remoteService");
  12. System.out.println(service.hello("motan"));
  13. }
  14. }

执行Client类中的main函数将执行一次远程调用,并输出结果。

异步调用

异步调用与同步调用基本配置完全一样,只需要在接口类中加上@MotanAsync注解,然后client端稍作修改。server端不需要做任何修改。具体步骤如下:

  • 在接口类上加@MotanAsync注解
  1. package quickstart;
  2.  
  3. @MotanAsync
  4. public interface FooService {
  5. public String hello(String name);
  6. }
  • 编译时,Motan自动生成异步service类,生成路径为target/generated-sources/annotations/,生成的类名为service名加上Async,例如service类名为FooService.java,则自动生成的类名为FooServiceAsync.java。另外,需要将motan自动生产类文件的路径配置为项目source path,可以使用maven plugin或手动配置。pom.xml配置如下:
  1. <plugin>
  2. <groupId>org.codehaus.mojo</groupId>
  3. <artifactId>build-helper-maven-plugin</artifactId>
  4. <version>RELEASE</version>
  5. <executions>
  6. <execution>
  7. <phase>generate-sources</phase>
  8. <goals>
  9. <goal>add-source</goal>
  10. </goals>
  11. <configuration>
  12. <sources>
  13. <source>${project.build.directory}/generated-sources/annotations</source>
  14. </sources>
  15. </configuration>
  16. </execution>
  17. </executions>
  18. </plugin>
  • 在client端配置motan_client.xml时,在同步调用配置的基础上,只需要修改referer的interface为Motan自动生成的接口类即可。
  1. <motan:referer id="remoteService" interface="quickstart.FooServiceAsync" directUrl="localhost:8002"/>
  • 异步使用方式如下:
  1. public static void main(String[] args) {
  2. ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"classpath:motan_client.xml"});
  3.  
  4. FooServiceAsync service = (FooServiceAsync) ctx.getBean("remoteService");
  5.  
  6. // sync call
  7. System.out.println(service.hello("motan"));
  8.  
  9. // async call
  10. ResponseFuture future = service.helloAsync("motan async ");
  11. System.out.println(future.getValue());
  12.  
  13. // multi call
  14. ResponseFuture future1 = service.helloAsync("motan async multi-1");
  15. ResponseFuture future2 = service.helloAsync("motan async multi-2");
  16. System.out.println(future1.getValue() + ", " + future2.getValue());
  17.  
  18. // async with listener
  19. FutureListener listener = new FutureListener() {
  20. @Override
  21. public void operationComplete(Future future) throws Exception {
  22. System.out.println("async call "
  23. + (future.isSuccess() ? "sucess! value:" + future.getValue() : "fail! exception:"
  24. + future.getException().getMessage()));
  25. }
  26. };
  27. ResponseFuture future3 = service.helloAsync("motan async multi-1");
  28. ResponseFuture future4 = service.helloAsync("motan async multi-2");
  29. future3.addListener(listener);
  30. future4.addListener(listener);
  31. }

具体代码可以参考demo模块