基于 Netty、ZooKeeper 和 Spring 的简单 RPC 框架
基于 Netty、ZooKeeper 和 Spring 的 RPC 框架 中文详情:Chinese Details ##### 特性: * 代码和框架简单 * ZooKeeper 支持服务注册/发现 * 高可用性、负载均衡和故障转移 * 支持不同的负载均衡策略 * 支持异步/同步调用 * 支持不同版本的服务 * 支持不同的序列化器/反序列化器 * 使用心跳检测死机的 TCP 连接 ##### 设计: ##### 如何使用 (netty-rpc-test) 1. 定义接口: public interface HelloService { String hello(String name); String hello(Person person); } 2. 使用 @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();
}
}
``` 3. 运行 ZooKeeper 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);
```
2. 使用注解 @RpcAutowired:public class Baz implements Foo { @RpcAutowired(version = "1.0") private HelloService helloService1;
@RpcAutowired(version = "2.0")
private HelloService helloService2;暂无开放 Issues,或尚未同步最近议题。