Dubbo服务端、消费端网络连接数控制

我们知道,在Dubbo中,默认底层是通过Netty来进行网络通信的,而我们知道单台计算机能够支持的网络连接的数量是有限制的,那么在Dubbo中是怎么来设置这些连接的数量呢 ?

消费端

通过之前的分析(Dubbo消费端启动流程、处理逻辑,方法调用实现(基于Dubbo3))[https://blog.csdn.net/LeoHan163/article/details/121333133]
我们知道在DubboProtocol中会生成对应的服务端的连接,通过getClients方法:

private ExchangeClient[] getClients(URL url) {
    
    
        // whether to share connection

        boolean useShareConnect = false;

        int connections = url.getParameter(CONNECTIONS_KEY, 0);
        List<ReferenceCountExchangeClient> shareClients = null;
        // if not configured, connection is shared, otherwise, one connection for one service
        if (connections == 0) {
    
    
            useShareConnect = true;

            /*
             * The xml configuration should have a higher priority than properties.
             */
            String shareConnectionsStr = url.getParameter(SHARE_CONNECTIONS_KEY, (String) null);
            connections = Integer.parseInt(StringUtils.isBlank(shareConnectionsStr) ? ConfigUtils.getProperty(SHARE_CONNECTIONS_KEY,
                    DEFAULT_SHARE_CONNECTIONS) : shareConnectionsStr);
            shareClients = getSharedClient(url, connections);
        }

        ExchangeClient[] clients = new ExchangeClient[connections];
        for (int i = 0; i < clients.length; i++) {
    
    
            if (useShareConnect) {
    
    
                clients[i] = shareClients.get(i);

            } else {
    
    
                clients[i] = initClient(url);
            }
        }

        return clients;
    }

可以看到,这里会从url中获取connections这个参数,这个配置在DubboReference -> connections,如果没有配置的话,这里返回的就是0,而如果为0的话,会获取shareconnections配置,默认为1,这时候就是通过共享方式来使用连接,共享方式连接,默认情况下只有一个连接。另外,这里判断是否是同一个连接并不是通过服务,而是通过ip+port方式
如果我们制定了connections数量,那么就会生成对应数量的连接。

服务端

我们知道,服务端默认是使用Netty网络框架,对应的是NettyServer,在 其父类AbstractServer的连接事件处理时,会判断当前已经建立的连接的数量:

public void connected(Channel ch) throws RemotingException {
    
    
        // If the server has entered the shutdown process, reject any new connection
        if (this.isClosing() || this.isClosed()) {
    
    
            logger.warn("Close new channel " + ch + ", cause: server is closing or has been closed. For example, receive a new connect request while in shutdown process.");
            ch.close();
            return;
        }

        if (accepts > 0 && getChannels().size() > accepts) {
    
    
            logger.error("Close channel " + ch + ", cause: The server " + ch.getLocalAddress() + " connections greater than max config " + accepts);
            ch.close();
            return;
        }
        super.connected(ch);
    }

可以看到,这里会判断已经建立的连接数量,如果 满足accepts > 0 && getChannels().size() > accepts则会将连接关闭,而默认accepts=0,也就是服务端不会对连接进行限制。
而我们可通过DubboService -> connections进行限制。
另外之前也说过,可以看到消费端建立连接的时候也是通过ip+port来进行连接的唯一识别,服务端也是一样

おすすめ

転載: blog.csdn.net/LeoHan163/article/details/121344411