代码拉取完成,页面将自动刷新
同步操作将从 sandyfog/zrpc 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
基于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);
Future<String> f =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);
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。