An RPC framework based on Netty, ZooKeeper and Spring 中文详情:Chinese Details
Define an interface:
public interface HelloService {
String hello(String name);
String hello(Person person);
}
Implement the interface with annotation @NettyRpcService:
@NettyRpcService(HelloService.class, version = "1.0")
public class HelloServiceImpl implements HelloService {
public HelloServiceImpl(){}
@Override
public String hello(String name) {
return "Hello " + name;
}
@Override
public String hello(Person person) {
return "Hello " + person.getFirstName() + " " + person.getLastName();
}
}
Run zookeeper
For example: zookeeper is running on 127.0.0.1:2181
Start server:
Call the service:
final RpcClient rpcClient = new RpcClient("127.0.0.1:2181");
// Sync call
HelloService helloService = rpcClient.createService(HelloService.class, "1.0");
String result = helloService.hello("World");
// Async call
RpcService client = rpcClient.createAsyncService(HelloService.class, "2.0");
RPCFuture helloFuture = client.call("hello", "World");
String result = (String) helloFuture.get(3000, TimeUnit.MILLISECONDS);
public class Baz implements Foo {
@RpcAutowired(version = "1.0")
private HelloService helloService1;
@RpcAutowired(version = "2.0")
private HelloService helloService2;
@Override
public String say(String s) {
return helloService1.hello(s);
}
}