栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

用netty 实现一个rpc框架

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用netty 实现一个rpc框架

  • CheetRpc
  • 基于netty实现的一个简易rpc 框架,会陆续完成 主要是用来学习netty

  • 使用 服务端
public class RpcImpl{


    public int GetRandom(int a,int b){
      return   a-b;
    }

    public int Add(int a, int b) {
        return a+b;
    }
}

public class Server {
    public static void main(String[] args) {
        CheetRpcServer ser=new CheetRpcServerImpl();
        ser.Rigist(RpcImpl.class);

        ser.ListenServer(8080);
    }
}

客户端
  • 单机模式
CheetRpcClientImpl client=new CheetRpcClientImpl();

     
        client.SetDiscovery(new StandaloneDiscovery("ip",port));


        Object call = client.Call(".RpcImpl","GetRandom", 1, 2);

        System.out.println(call.toString());

         call = client.Call(".RpcImpl","Add", 1, 2);

        System.out.println(call.toString());


        call = client.Call(".RpcImpl","Add", 1, 2);

        System.out.println(call.toString());
  • zookeeper 多主机模式
    • 我们可以启动多个服务端
    • 但是前提需要手动写入节点到zookeeper 中
    • 列如 /rpc_zk/127.0.0.1:8080 、/rpc_zk/127.0.0.1:8081
    • 我们分别启动8080 服务和8081 服务
         CheetRpcClientImpl client=new CheetRpcClientImpl();
    
        //load () 选择负载均衡算法
        ZookeeperConfig rpc_zk = ZookeeperConfig.builder().load(new Pollingbalancing())
                .ServerNode("/rpc_zk")
                .zkAddr("127.0.0.1:9000").build();
    
        client.SetDiscovery(new ZookeeperDisovery(rpc_zk));
    
    
        Object call = client.Call(".RpcImpl","GetRandom", 1, 2);
    
        System.out.println(call.toString());
    
         call = client.Call(".RpcImpl","Add", 1, 2);
    
        System.out.println(call.toString());
    
    
        call = client.Call(".RpcImpl","Add", 1, 2);
    
        System.out.println(call.toString());
    
  • 项目地址
  • github 地址

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1000285.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号