Skip to content

A cross-language remote procedure call(RPC) framework for rapid development of high performance distributed services.

License

Notifications You must be signed in to change notification settings

weibocom/motan

Repository files navigation

Motan

LicenseMaven CentralcodecovOpenTracing-1.0 BadgeSkywalking Tracing

Overview

Motan is a cross-language remote procedure call(RPC) framework for rapid development of high performance distributed services.

Related projects in Motan ecosystem:

Features

  • Create distributed services without writing extra code.
  • Provides cluster support and integrate with popular service discovery services like Consul or Zookeeper.
  • Supports advanced scheduling features like weighted load-balance, scheduling cross IDCs, etc.
  • Optimization for high load scenarios, provides high availability in production environment.
  • Supports both synchronous and asynchronous calls.
  • Support cross-language interactive with Golang, PHP, Lua(Luajit), etc.

Quick Start

The quick start gives very basic example of running client and server on the same machine. For the detailed information about using and developing Motan, please jump to Documents.

The minimum requirements to run the quick start are:

  • JDK 1.8 or above
  • A java-based project management software like Maven or Gradle

Synchronous calls

  1. Add dependencies to pom.
<properties> <motan.version>1.1.12</motan.version> <!--use the latest version from maven central--> </properties> <dependencies> <dependency> <groupId>com.weibo</groupId> <artifactId>motan-core</artifactId> <version>${motan.version}</version> </dependency> <dependency> <groupId>com.weibo</groupId> <artifactId>motan-transport-netty</artifactId> <version>${motan.version}</version> </dependency> <!-- dependencies below were only needed for spring-based features --> <dependency> <groupId>com.weibo</groupId> <artifactId>motan-springsupport</artifactId> <version>${motan.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> </dependency> </dependencies>
  1. Create an interface for both service provider and consumer.

    src/main/java/quickstart/FooService.java

    packagequickstart; publicinterfaceFooService{publicStringhello(Stringname)}
  2. Write an implementation, create and start RPC Server.

    src/main/java/quickstart/FooServiceImpl.java

    packagequickstart; publicclassFooServiceImplimplementsFooService{publicStringhello(Stringname){System.out.println(name + " invoked rpc service"); return"hello " + name} }

    src/main/resources/motan_server.xml

    <?xml version="1.0" encoding="UTF-8"?> <beansxmlns="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 implementation bean --> <beanid="serviceImpl"class="quickstart.FooServiceImpl" /> <!-- exporting service by motan --> <motan:serviceinterface="quickstart.FooService"ref="serviceImpl"export="8002" /> </beans>

    src/main/java/quickstart/Server.java

    packagequickstart; importorg.springframework.context.ApplicationContext; importorg.springframework.context.support.ClassPathXmlApplicationContext; publicclassServer{publicstaticvoidmain(String[] args) throwsInterruptedException{ApplicationContextapplicationContext = newClassPathXmlApplicationContext("classpath:motan_server.xml"); System.out.println("server start...")} }

    Execute main function in Server will start a motan server listening on port 8002.

  3. Create and start RPC Client.

    src/main/resources/motan_client.xml

    <?xml version="1.0" encoding="UTF-8"?> <beansxmlns="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:refererid="remoteService"interface="quickstart.FooService"directUrl="localhost:8002"/> </beans>

    src/main/java/quickstart/Client.java

    packagequickstart; importorg.springframework.context.ApplicationContext; importorg.springframework.context.support.ClassPathXmlApplicationContext; publicclassClient{publicstaticvoidmain(String[] args) throwsInterruptedException{ApplicationContextctx = newClassPathXmlApplicationContext("classpath:motan_client.xml"); FooServiceservice = (FooService) ctx.getBean("remoteService"); System.out.println(service.hello("motan"))} }

    Execute main function in Client will invoke the remote service and print response.

Asynchronous calls

  1. Based on the Synchronous calls example, add @MotanAsync annotation to interface FooService.

    packagequickstart; importcom.weibo.api.motan.transport.async.MotanAsync; @MotanAsyncpublicinterfaceFooService{publicStringhello(Stringname)}
  2. Include the plugin into the POM file to set target/generated-sources/annotations/ as source folder.

    <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.10</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>${project.build.directory}/generated-sources/annotations</source> </sources> </configuration> </execution> </executions> </plugin>
  3. Modify the attribute interface of referer in motan_client.xml from FooService to FooServiceAsync.

    <motan:refererid="remoteService"interface="quickstart.FooServiceAsync"directUrl="localhost:8002"/>
  4. Start asynchronous calls.

    publicstaticvoidmain(String[] args){ApplicationContextctx = newClassPathXmlApplicationContext(newString[]{"classpath:motan_client.xml"}); FooServiceAsyncservice = (FooServiceAsync) ctx.getBean("remoteService"); // sync callSystem.out.println(service.hello("motan")); // async callResponseFuturefuture = service.helloAsync("motan async "); System.out.println(future.getValue()); // multi callResponseFuturefuture1 = service.helloAsync("motan async multi-1"); ResponseFuturefuture2 = service.helloAsync("motan async multi-2"); System.out.println(future1.getValue() + ", " + future2.getValue()); // async with listenerFutureListenerlistener = newFutureListener(){@OverridepublicvoidoperationComplete(Futurefuture) throwsException{System.out.println("async call " + (future.isSuccess() ? "success! value:" + future.getValue() : "fail! exception:" + future.getException().getMessage()))} }; ResponseFuturefuture3 = service.helloAsync("motan async multi-1"); ResponseFuturefuture4 = service.helloAsync("motan async multi-2"); future3.addListener(listener); future4.addListener(listener)}

Documents

Contributors

License

Motan is released under the Apache License 2.0.

About

A cross-language remote procedure call(RPC) framework for rapid development of high performance distributed services.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 38