motan

by weibocom

weibocom / motan

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

5.6K Stars 1.8K Forks Last release: 7 months ago (1.1.8) Other 557 Commits 20 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

Motan

License Maven Central Build Status OpenTracing-1.0 Badge Skywalking 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.7 or above
  • A java-based project management software like Maven or Gradle

Synchronous calls

  1. Add dependencies to pom.
    com.weibo
    motan-core
       1.0.0


    com.weibo
    motan-transport-netty
    1.0.0




    com.weibo
    motan-springsupport
    1.0.0


    org.springframework
    spring-context
    4.2.4.RELEASE

  1. 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); }

  2. Write an implementation, create and start RPC Server.

    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
    <!-- service implementation bean -->
    <bean id="serviceImpl" class="quickstart.FooServiceImpl"></bean>
    <!-- exporting service by motan -->
    <service interface="quickstart.FooService" ref="serviceImpl" export="8002"></service>

    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...");
    }

    }

    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
    <!-- reference to the remote service -->
    <referer id="remoteService" interface="quickstart.FooService" directurl="localhost:8002"></referer>

    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.

Asynchronous calls

  1. Based on the

    Synchronous calls
    example, add
    @MotanAsync
    annotation to interface
    FooService
    .
    package quickstart;
    import com.weibo.api.motan.transport.async.MotanAsync;
    
    

    @MotanAsync public interface FooService { public String hello(String name); }

  2. Include the plugin into the POM file to set

    target/generated-sources/annotations/
    as source folder.
        org.codehaus.mojo
        build-helper-maven-plugin
        1.10
        
            
                generate-sources
                
                    add-source
                
                
                    
                        ${project.build.directory}/generated-sources/annotations
                    
                
            
        
    
    
  3. Modify the attribute

    interface
    of referer in
    motan_client.xml
    from
    FooService
    to
    FooServiceAsync
    .
    
    
  4. Start asynchronous calls.

    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() ? "success! 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);

    }

Documents

Contributors

License

Motan is released under the Apache License 2.0.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.