rpc pigeon 原理

参考
https://blog.csdn.net/ningdunquan/article/details/79910367

image.png

客户端


  • 启动调用过程


    image.png
  • 客户端创建nettyClient, 这里比较耗时
//ClientManager.registerClients()
// 这段还是比较重要的,使用到了线程同步,栅栏。
// 这里会创建nettyClient, 拉取zk地址, 比较耗时。
// 当注册bean上百个时,启动下耗时都要好几分钟。

 final CountDownLatch latch = new CountDownLatch(addresses.size());
            Iterator i$ = addresses.iterator();

            while(i$.hasNext()) {
                final HostInfo hostInfo = (HostInfo)i$.next();
                Runnable r = new Runnable() {
                    public void run() {
                        try {
                            // 里面创建nettyClient
                            RegistryEventListener.providerAdded(url, hostInfo.getHost(), hostInfo.getPort(), hostInfo.getWeight(), address);
                            RegistryEventListener.serverInfoChanged(url, hostInfo.getConnect());
                        } finally {
                            latch.countDown();
                        }

                    }
                };
                registerThreadPool.submit(r);
            }

            try {
                latch.await();
            } catch (InterruptedException var17) {
                logger.info("", var17);
            }
// RegistryEventListener.providerAdded()
listener.providerAdded(event);

//  ClientManager.providerAdded()
ClientManager.this.registerClient(event.getServiceName(), event.getHost(), event.getPort(), event.getWeight(), event.getGroup());

 public void registerClient(String serviceName, String host, int port, int weight, String group) {
        ConnectInfo connectInfo = new ConnectInfo(serviceName, host, port, weight, group);
        this.clusterListenerManager.addConnect(connectInfo);
        RegistryManager.getInstance().addServiceAddress(serviceName, host, port, weight, group);
    }

// ClusterListenerManager.addConnect()
listener.addConnect(cmd);
        
// DefaultClusterListener   public void addConnect(ConnectInfo connectInfo) 
  client = ClientSelector.selectClient(connectInfo);

// ClientSelector
 public static Client selectClient(ConnectInfo connectInfo) {
        List clientFactories = ExtensionLoader.getExtensionList(ClientFactory.class);
        Iterator i$ = clientFactories.iterator();

        ClientFactory clientFactory;
        do {
            if (!i$.hasNext()) {
                throw new ServiceUnavailableException("no available client been created from client factory:" + connectInfo);
            }

            clientFactory = (ClientFactory)i$.next();
        } while(!clientFactory.support(connectInfo));

        return clientFactory.createClient(connectInfo);
    }

// NettyClientFactory, 终于找到了!!!
public Client createClient(ConnectInfo connectInfo) {
        return new NettyClient(connectInfo);
    }

服务端


  • 启动调用过程


    image.png

你可能感兴趣的:(rpc pigeon 原理)