Giter VIP home page Giter VIP logo

nettyrpc's Introduction

Build Status NettyRPC

Yet another RPC framework based on Netty.

Features

  • Simple, small code base, easy to learn API
  • Very fast, high performance
  • Totally non-blocking asynchronous call, synchronous call, oneway call.
  • Long lived persistent connection, reconnect to server automatically
  • High availability, load balance and failover
  • Multi thread server and multi thread client
  • Thread safe client, for an remote service you only need to create a singleton client.
  • Service Discovery support, use zookeeper as a service registry.
  • PHP client (Unimplemented yet)

Simple tutorial

####1.Define an obj interface

public interface IHelloWordObj {
	String hello(String msg);
	String test(Integer i, String s, Long l);
	void notifySomeThing(Integer i, String s, Long l);
}

####2.Implements the previous defined interface

public class HelloWorldObj implements IHelloWordObj {
	@Override
	public String hello(String msg) {
		return msg;
	}
	@Override
	public String test(Integer i, String s, Long l) {
		return i+s+l;
	}
	@Override
	void notifySomeThing(Integer i, String s, Long l) {
		System.out.println("notifySomeThing|i="+i+"|s="+s+"l="+l);
	}
}

####3. Update configuration file and start the server "com.lubin.rpc.server.RPCServer"

server {
	port = 9090
	backlog = 1000
	async = false	//handling request in business logic thread pool
	asyncThreadPoolSize = 4
    ioThreadNum = 4
    enableServiceDiscovery = true   
	objects = [
		com.lubin.rpc.example.obj.HelloWorldObj
	]
}
client {
	syncCallTimeOutMillis = 300
	connectTimeoutMillis = 300
	reconnIntervalMillis = 1000	//time interval for reconnecting to server
	asyncThreadPoolSize = 1   //thread pool for excuting Async callback
    ioThreadNum = 1   
    objects = [ 
		{ 
			name = com.lubin.rpc.example.obj.IHelloWordObj
			servers ="127.0.0.1:9090 127.0.0.1:9091"
		}
	]
}

####4.Synchronous call. Create an Obj proxy and call the remote Obj.

    IHelloWordObj client = RPCClient.proxyBuilder(IHelloWordObj.class)
    					            .withServerNode("127.0.0.1", 9090)
    					            .build();
    String result = client.hello("hello world!");

####5. Asynchronous call #####5.1.

    IAsyncObjectProxy asyncClient = RPCClient.proxyBuilder(IHelloWordObj.class)
    					                     .withServerNode
    						                 .buildAsyncObjPrx();
    
    RPCFuture helloFuture = asyncClient.call("hello", "hello world!");
    RPCFuture testFuture = asyncClient.call("test", 1,"hello world!",2L);
    Object res1= helloFuture.get(3000, TimeUnit.MILLISECONDS);
    Object res2= testFuture.get(3000, TimeUnit.MILLISECONDS);

#####5.2. Optionally you can provide a callback which will be automatically called by NettyRPC after received response from server.

public class AsyncHelloWorldCallback implements AsyncRPCCallback {
	@Override
	public void fail(Exception e) {
		System.out.println(e.getMessage());
	}
	@Override
	public void success(Object result) {
		System.out.println(result);
	}
}

    IAsyncObjectProxy asyncClient = RPCClient.proxyBuilder(IHelloWordObj.class)
    						                 .withServerNode("127.0.0.1", 9090)
    					   	                 .buildAsyncObjPrx();
    RPCFuture helloFuture = asyncClient.call("hello", "hello world!")
    						           .addCallback(new AsyncHelloWorldCallback());
    RPCFuture testFuture = asyncClient.call("test", 1,"hello world!",2L)
    						           .addCallback(new AsyncHelloWorldCallback());

####6.Oneway call

    IAsyncObjectProxy asyncClient = RPCClient.proxyBuilder(IHelloWordObj.class)
    						                 .withServerNode("127.0.0.1", 9090)
    						                 .buildAsyncObjPrx();
    asyncClient.notify("notifySomeThing", 1, "hello world!", 2L);

####7 High availability, you can deploy more than one servers to achieve HA, NettyRPC handle load balance and failover automatically.

    ArrayList<InetSocketAddress> serverNodeList = new ArrayList<InetSocketAddress>();
    serverNodeList.add(new InetSocketAddress("127.0.0.1",9090));
    serverNodeList.add(new InetSocketAddress("127.0.0.1",9091));
         
    IHelloWordObj client = RPCClient.proxyBuilder(IHelloWordObj.class)
    					            .withServerNodes(serverNodeList)
    					            .build();
    System.out.println("test server list:"+client.hello("test server list11"));

####8 Service Discovery support Instead of hard coding the server address in config file( or java source code), NettyRPC support service discovery. All Services will automatically register themself to registry(Zookeeper) after services started, and NettyRPC client will automatically query addresses from the registry. If you want to scale out and deploy more server, you just simply start the new services, NettyRPC client will automatically got notified and dispatch requests to that new services.

    IHelloWordObj client = RPCClient.proxyBuilder(IHelloWordObj.class)
    					            .enableRegistry()
    					            .build();
    System.out.println("test server list:"+client.hello("test server list11"));

======== Please feel free to contact me([email protected]) if you have any questions.

nettyrpc's People

Contributors

dawnbreaks avatar

Watchers

wasai avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.