聊聊JerseyEurekaHttpClient的参数

本文详细解析了Eureka客户端通过JerseyEurekaHttpClient进行HTTP连接配置的方法,包括连接超时、读取超时、最大连接数等关键参数的设置方式及其在源码中的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要研究一下JerseyEurekaHttpClient的参数

JerseyEurekaHttpClientFactory

eureka-client-1.8.8-sources.jar!/com/netflix/discovery/shared/transport/jersey/JerseyEurekaHttpClientFactory.java

        private JerseyEurekaHttpClientFactory buildLegacy(Map<String, String> additionalHeaders, boolean systemSSL) {
            EurekaJerseyClientBuilder clientBuilder = new EurekaJerseyClientBuilder()
                    .withClientName(clientName)
                    .withUserAgent("Java-EurekaClient")
                    .withConnectionTimeout(connectionTimeout)
                    .withReadTimeout(readTimeout)
                    .withMaxConnectionsPerHost(maxConnectionsPerHost)
                    .withMaxTotalConnections(maxTotalConnections)
                    .withConnectionIdleTimeout((int) connectionIdleTimeout)
                    .withEncoderWrapper(encoderWrapper)
                    .withDecoderWrapper(decoderWrapper);

            if (systemSSL) {
                clientBuilder.withSystemSSLConfiguration();
            } else if (sslContext != null) {
                clientBuilder.withCustomSSL(sslContext);
            }
            
            if (hostnameVerifier != null) {
                clientBuilder.withHostnameVerifier(hostnameVerifier);
            }

            EurekaJerseyClient jerseyClient = clientBuilder.build();
            ApacheHttpClient4 discoveryApacheClient = jerseyClient.getClient();
            addFilters(discoveryApacheClient);

            return new JerseyEurekaHttpClientFactory(jerseyClient, additionalHeaders);
        }
这里开业看到build的时候,指定了connectionTimeout、readTimeout、maxConnectionsPerHost、maxTotalConnections、connectionIdleTimeout参数。

EurekaJerseyClientImpl

eureka-client-1.8.8-sources.jar!/com/netflix/discovery/shared/transport/jersey/EurekaJerseyClientImpl.java

    public EurekaJerseyClientImpl(int connectionTimeout, int readTimeout, final int connectionIdleTimeout,
                                  ClientConfig clientConfig) {
        try {
            jerseyClientConfig = clientConfig;
            apacheHttpClient = ApacheHttpClient4.create(jerseyClientConfig);
            HttpParams params = apacheHttpClient.getClientHandler().getHttpClient().getParams();

            HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
            HttpConnectionParams.setSoTimeout(params, readTimeout);

            this.apacheHttpClientConnectionCleaner = new ApacheHttpClientConnectionCleaner(apacheHttpClient, connectionIdleTimeout);
        } catch (Throwable e) {
            throw new RuntimeException("Cannot create Jersey client", e);
        }
    }
    
    public EurekaJerseyClient build() {
            MyDefaultApacheHttpClient4Config config = new MyDefaultApacheHttpClient4Config();
            try {
                return new EurekaJerseyClientImpl(connectionTimeout, readTimeout, connectionIdleTimeout, config);
            } catch (Throwable e) {
                throw new RuntimeException("Cannot create Jersey client ", e);
            }
        }

        class MyDefaultApacheHttpClient4Config extends DefaultApacheHttpClient4Config {
            MyDefaultApacheHttpClient4Config() {
                MonitoredConnectionManager cm;

                if (systemSSL) {
                    cm = createSystemSslCM();
                } else if (sslContext != null || hostnameVerifier != null || trustStoreFileName != null) {
                    cm = createCustomSslCM();
                } else {
                    cm = createDefaultSslCM();
                }

                if (proxyHost != null) {
                    addProxyConfiguration(cm);
                }

                DiscoveryJerseyProvider discoveryJerseyProvider = new DiscoveryJerseyProvider(encoderWrapper, decoderWrapper);
                getSingletons().add(discoveryJerseyProvider);

                // Common properties to all clients
                cm.setDefaultMaxPerRoute(maxConnectionsPerHost);
                cm.setMaxTotal(maxTotalConnections);
                getProperties().put(ApacheHttpClient4Config.PROPERTY_CONNECTION_MANAGER, cm);

                String fullUserAgentName = (userAgent == null ? clientName : userAgent) + "/v" + buildVersion();
                getProperties().put(CoreProtocolPNames.USER_AGENT, fullUserAgentName);

                // To pin a client to specific server in case redirect happens, we handle redirects directly
                // (see DiscoveryClient.makeRemoteCall methods).
                getProperties().put(PROPERTY_FOLLOW_REDIRECTS, Boolean.FALSE);
                getProperties().put(ClientPNames.HANDLE_REDIRECTS, Boolean.FALSE);
            }
            //......
        }            
其中可以看到maxConnectionsPerHost以及maxTotalConnections是配置在MonitoredConnectionManager;而connectionTimeout、readTimeout、connectionIdleTimeout则是配置在apache http client的HttpConnectionParams上。

参数配置

eureka-client-1.8.8-sources.jar!/com/netflix/discovery/shared/transport/EurekaClientFactoryBuilder.java

    public B withClientConfig(EurekaClientConfig clientConfig) {
        withClientAccept(EurekaAccept.fromString(clientConfig.getClientDataAccept()));
        withAllowRedirect(clientConfig.allowRedirects());
        withConnectionTimeout(clientConfig.getEurekaServerConnectTimeoutSeconds() * 1000);
        withReadTimeout(clientConfig.getEurekaServerReadTimeoutSeconds() * 1000);
        withMaxConnectionsPerHost(clientConfig.getEurekaServerTotalConnectionsPerHost());
        withMaxTotalConnections(clientConfig.getEurekaServerTotalConnections());
        withConnectionIdleTimeout(clientConfig.getEurekaConnectionIdleTimeoutSeconds() * 1000);
        withEncoder(clientConfig.getEncoderName());
        return withDecoder(clientConfig.getDecoderName(), clientConfig.getClientDataAccept());
    }
可以看到这里从EurekaClientConfig读取,其映射关系如下:
属性配置默认值说明
connectionTimeouteureka.client.eureka-server-connect-timeout-seconds5Indicates how long to wait (in seconds) before a connection to eureka server needs to timeout. Note that the connections in the client are pooled by org.apache.http.client.HttpClient and this setting affects the actual connection creation and also the wait time to get the connection from the pool.
readTimeouteureka.client.eureka-server-read-timeout-seconds8Indicates how long to wait (in seconds) before a read from eureka server needs to timeout.
maxConnectionsPerHosteureka.client.eureka-server-total-connections-per-host50Gets the total number of connections that is allowed from eureka client to a eureka server host.
maxTotalConnectionseureka.client.eureka-server-total-connections200Gets the total number of connections that is allowed from eureka client to all eureka servers.
connectionIdleTimeouteureka.client.eureka-connection-idle-timeout-seconds30Indicates how much time (in seconds) that the HTTP connections to eureka server can stay idle before it can be closed. In the AWS environment, it is recommended that the values is 30 seconds or less, since the firewall cleans up the connection information after a few mins leaving the connection hanging in limbo

小结

可以通过eureka-server-connect-timeout-seconds、eureka-server-read-timeout-seconds、eureka-server-total-connections-per-host、eureka-server-total-connections、eureka-connection-idle-timeout-seconds参数来配置及管理eureka client请求eureka server的http连接。

doc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值