Code Monkey home page Code Monkey logo

nettyrpc's Introduction

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 Object you only need to create a singleton client.
  • Service Discovery, use zookeeper as a service registry. (unimplemented yet)
  • 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;
	}
	public String notifySomeThing(Integer i, String s, Long l) {
	}
}

####3. Edit application.conf to add the previous obj 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 = 1   
	objects = [
		com.lubin.rpc.example.obj.HelloWorldObj
	]
}
client {
	reconnInterval = 1000	//time interval(million second) 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.Make an Obj proxy and call the remote Obj.

    IHelloWordObj client = RPCClient.createObjectProxy("127.0.0.1", 9090, IHelloWordObj.class);
    
    String result = client.hello("hello world!");
    if(!result.equals("hello world!"))
           System.out.println("error="+result);

####5. Asynchronous call #####5.1. Create an asynchronous Obj proxy and call the remote Obj.

    IAsyncObjectProxy asyncClient = RPCClient.createAsyncObjPrx("127.0.0.1", 9090, IHelloWordObj.class);
    
    RPCFuture helloFuture = client.call("hello", new Object[]{"hello world!"});
    RPCFuture testFuture = client.call("test", new Object[]{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 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.createAsyncObjPrx("127.0.0.1", 9090, IHelloWordObj.class);
    RPCFuture helloFuture = client.call("hello", new Object[]{"hello world!"},new AsyncHelloWorldCallback());
    RPCFuture testFuture = client.call("test", new Object[]{1,"hello world!",2L}, new AsyncHelloWorldCallback());

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

    ArrayList<InetSocketAddress> serverList = new ArrayList<InetSocketAddress>();
    serverList.add(new InetSocketAddress("127.0.0.1",9090));
    serverList.add(new InetSocketAddress("127.0.0.1",9091));
         
    IHelloWordObj client = RPCClient.createObjectProxy(serverList, IHelloWordObj.class);
    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

James Cloos avatar  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.