![Maven Central](https://img.shields.io/maven-central/v/com.weibo/motan.svg?label=Maven Central)
Motan is a remote procedure call(RPC) framework for rapid development of high performance distributed services.
- 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.
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:
-
Add dependency to pom
<dependency> <groupId>com.weibo</groupId> <artifactId>motan-core</artifactId> <version>0.0.1</version> </dependency> <dependency> <groupId>com.weibo</groupId> <artifactId>motan-transport-netty</artifactId> <version>0.0.1</version> </dependency> <!-- dependencies blow were only needed for spring-based features --> <dependency> <groupId>com.weibo</groupId> <artifactId>motan-springsupport</artifactId> <version>0.0.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> </dependency>
-
Create an interface for both service provider and consumer.
src/main/java/quickstart/FooService.java
package quickstart; public interface FooService { public String hello(String name); }
-
Implement service provider.
src/main/java/quickstart/FooServiceImpl.java
package quickstart; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class FooServiceImpl implements FooService { public String hello(String name) { System.out.println(name + " invoked rpc service"); return "hello " + name; } public static void main(String[] args) throws InterruptedException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:motan_server.xml"); System.out.println("server start..."); } }
src/main/resources/motan_server.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:motan="https://api.weibo.com/schema/motan" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.5.xsd https://api.weibo.com/schema/motan https://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>
Execute main function in FooServiceImpl will start a motan server listening on port 8002.
-
The service consumer
src/main/resources/motan_client.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:motan="https://api.weibo.com/schema/motan" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.5.xsd https://api.weibo.com/schema/motan https://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")); } }
Execute main function in Client will invoke the remote service and print response.
- maijunsheng(@maijunsheng)
- fishermen(@hustfisher)
- TangFulin(@tangfl)
- bodlyzheng(@bodlyzheng)
- wangyu1
- zenglingshu(@zenglingshu)
- Sugar Zouliu(@lamusicoscos)
- tangyang(@tangyang)
- olivererwang(@olivererwang)
- jackael(@jackael9856)
- Ray(@rayzhang0603)
- r2dx(@half-dead)
- axb(@qdaxb)
- wenqisun(@wenqisun)
- fingki(@fingki)
- 午夜(@sumory)
Motan is released under the Apache License 2.0.