ZRPC ZRPC服务端客户端异步调用单向调用 介绍
ZRPC
基于Netty实现的RPC框架
服务端
RpcServer server = new RpcServer("127.0.0.1",1234); HelloServiceImpl impl = new HelloServiceImpl(); server.export(HelloService.class, impl);
客户端
Rpcclient client = new Rpcclient("127.0.0.1",1234); HelloService service = client.refer(HelloService.class); //同步调用 System.out.println(service.hello("test rpc"));
异步调用
默认的远程调用都是同步的,发起异步调用需要设置RpcContext.setAsync(true),异步调用有两种方式:Future方式、callback方式,可以单独使用也可以混合使用
-
Future方式
Rpcclient client = new Rpcclient(“127.0.0.1”,1234);
HelloService service = client.refer(HelloService.class);
RpcContext ctx = RpcContext.getContext();
ctx.setAsync(true);
String obj=service.hello(“test rpc”);
System.out.println(obj==null);
Futuref =ctx.getFuture();
System.out.println(f.get()); -
callback方式
Rpcclient client = new Rpcclient(“127.0.0.1”,1234);
HelloService service = client.refer(HelloService.class);
RpcContext ctx = RpcContext.getContext();
ctx.setAsync(true);
ctx.setCallback(new Callback() {@Override
public void onSuccess(Object result) {
System.out.println(“success ”+ result);
}
@Override
public void onError(Throwable thr) {
System.out.println(“error”);
thr.printstacktrace();
}
});
String obj=service.hello(“test rpc”);
System.out.println(obj==null);
单向调用
单向调用是一种特殊类型的异步调用,意味着客户端对本次调用不期待服务端的响应结果。实际上服务端对于单向调用请求也不会作出响应。对于特定场景单向调用性能更好,但并不那么可靠。
//单向调用 RpcContext ctx = RpcContext.getContext(); ctx.setoneway(true); System.out.println(service.hello("test rpc")==null);
ZRPC ZRPC服务端客户端异步调用单向调用 官网
http://git.oschina.net/sandyfog/zrpc
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。