Skip to content

TFdream/mango

Repository files navigation

Mango

License Release Version Build Status

Overview

Mango is a high-performance, open-source java RPC framework.

Features

  • Supports various serialization protocol, like protostuff, Kryo, Hessian, msgpack, Jackson, Fastjson.
  • Supports advanced features like load-balance(random, Round-Robin), HA strategy(Failfast, Failover).
  • Supports service discovery services like ZooKeeper or Consul.
  • Supports oneway, synchronous or asynchronous invoking.
  • Supports SPI extension.
  • Easy integrated with Spring Framework 4.x.

Requirements

The minimum requirements to run the quick start are:

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

Quick Start

1. Synchronous calls

  1. Add dependencies to pom.
    <dependency>
        <groupId>com.mindflow</groupId>
        <artifactId>mango-core</artifactId>
        <version>1.0.1</version>
    </dependency>

    <dependency>
        <groupId>com.mindflow</groupId>
        <artifactId>mango-registry-zk</artifactId>
        <version>1.0.1</version>
    </dependency>
    
    <!-- dependencies blow were only needed for spring integrated -->
    <dependency>
        <groupId>com.mindflow</groupId>
        <artifactId>mango-springsupport</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.6</version>
    </dependency>
  1. Create an interface for both service provider and consumer.
public interface DemoService {

    void hello(String msg);

    String echo(String msg);

    Map<String, String> introduce(String name, List<String> hobbies);
}
  1. Write an implementation, create and start RPC Server.
@Service("demoService")
public class DemoServiceImpl implements DemoService {

    @Override
    public void hello(String msg) {
        System.out.println(msg);
    }

    @Override
    public String echo(String msg) {
        return "hello, "+msg;
    }

    @Override
    public Map<String, String> introduce(String name, List<String> hobbies) {
        System.out.println("name:"+name + ", hobbies:"+hobbies);
        Map<String, String> map = new HashMap<>();
        map.put("name", name);
        return map;
    }

}

mango-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:context="http://www.springframework.org/schema/context"
       xmlns:mango="http://code.mindflow.com/schema/mango"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.mindflow.com/schema/mango http://code.mindflow.com/schema/mango/mango.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="mango.demo"/>

    <mango:application name="mango-server" />
    
    <mango:protocol name="mango" port="21918"/>

    <mango:registry protocol="zookeeper" address="localhost:2181"/>

    <!--export services-->
    <mango:service interface="mango.demo.service.DemoService" ref="demoService" group="group1" version="1.0.0" />
    <mango:service interface="mango.demo.service.UserService" ref="userService" version="1.0.0" />

</beans>

ServerApp.java

public class ServerApp {

    public static void main( String[] args ) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:mango-server.xml");
        System.out.println("server start...");
    }
}
  1. Create and start RPC Client. mango-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:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:mango="http://code.mindflow.com/schema/mango"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.mindflow.com/schema/mango http://code.mindflow.com/schema/mango/mango.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="mango.demo.client"/>

    <mango:application name="mango-client" />
    
    <mango:protocol name="mango" port="21918"/>

    <mango:registry protocol="zookeeper" address="localhost:2181"/>

    <!--refer services-->
    <mango:reference id="demoService" interface="mango.demo.service.DemoService" group="group1" />
    <mango:reference id="userService" interface="mango.demo.service.UserService"/>

</beans>

ClientApp.java

public class ClientApp {

    public static void main( String[] args ) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:mango-client.xml");

        DemoService service = (DemoService) ctx.getBean("demoService");

        service.hello("rpc");
        System.out.println("echo:"+service.echo("rpc"));

        List<String> hobbies = new ArrayList<>();
        hobbies.add("NBA");
        hobbies.add("Reading");
        Map<String, String> map = service.introduce("hh", hobbies);
        System.out.println("map:"+map);
    }
}

2. Asynchronous calls

In developing.