1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ZRPC基于 Netty 實(shí)現(xiàn)的 RPC 框架

        聯(lián)合創(chuàng)作 · 2023-09-29 12:32

        ZRPC

        基于Netty實(shí)現(xiàn)的RPC框架

        服務(wù)端

          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);
          //同步調(diào)用
          System.out.println(service.hello("test rpc"));

        異步調(diào)用

        默認(rèn)的遠(yuǎn)程調(diào)用都是同步的,發(fā)起異步調(diào)用需要設(shè)置RpcContext.setAsync(true),異步調(diào)用有兩種方式:Future方式、callback方式,可以單獨(dú)使用也可以混合使用

        • 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);

        單向調(diào)用

        單向調(diào)用是一種特殊類型的異步調(diào)用,意味著客戶端對本次調(diào)用不期待服務(wù)端的響應(yīng)結(jié)果。實(shí)際上服務(wù)端對于單向調(diào)用請求也不會(huì)作出響應(yīng)。對于特定場景單向調(diào)用性能更好,但并不那么可靠。

        //單向調(diào)用
          RpcContext ctx = RpcContext.getContext();
          ctx.setOneway(true);
          System.out.println(service.hello("test rpc")==null);
        瀏覽 18
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        編輯 分享
        舉報(bào)
        評論
        圖片
        表情
        推薦
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        編輯 分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            久久久岛国 | 美女黄网站免费观看 | 美女黄可图片 | 大香蕉凹凸视频 | 欧美有码视频 | 国产 日韩 欧美 综合 | 丁香综合 | 成人精品动漫H无码 | 91偷拍网 | www.人人cao |