博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
聊聊JerseyEurekaHttpClient的参数
阅读量:6539 次
发布时间:2019-06-24

本文共 7257 字,大约阅读时间需要 24 分钟。

  hot3.png

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

JerseyEurekaHttpClientFactory

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

private JerseyEurekaHttpClientFactory buildLegacy(Map
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读取,其映射关系如下:

属性 配置 默认值 说明
connectionTimeout eureka.client.eureka-server-connect-timeout-seconds 5 Indicates 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.
readTimeout eureka.client.eureka-server-read-timeout-seconds 8 Indicates how long to wait (in seconds) before a read from eureka server needs to timeout.
maxConnectionsPerHost eureka.client.eureka-server-total-connections-per-host 50 Gets the total number of connections that is allowed from eureka client to a eureka server host.
maxTotalConnections eureka.client.eureka-server-total-connections 200 Gets the total number of connections that is allowed from eureka client to all eureka servers.
connectionIdleTimeout eureka.client.eureka-connection-idle-timeout-seconds 30 Indicates 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

转载于:https://my.oschina.net/go4it/blog/1814500

你可能感兴趣的文章
ubuntu samba服务器多用户配置【转】
查看>>
母线的种类与作用是什么(转)
查看>>
【Xamarin 挖墙脚系列:IOS 开发界面的3种方式】
查看>>
Atitit.工作流系统的本质是dsl 图形化的dsl 4gl
查看>>
I.MX6 Android USB Touch eGTouchA.ini文件存放
查看>>
4-5-创建索引表-串-第4章-《数据结构》课本源码-严蔚敏吴伟民版
查看>>
java 操作 RabbitMQ 发送、接受消息
查看>>
go run main.go undefined? golang main包那点事
查看>>
从零开始写一个npm包,一键生成react组件(偷懒==提高效率)
查看>>
Volley(二)—— 基本Request对象 & RequestQueue&请求取消
查看>>
2017中国系统架构师大会“盛装”来袭
查看>>
中国最强的人工智能学术会议来了
查看>>
Metasploit的射频收发器功能 | Metasploit’s RF Transceiver Capabilities
查看>>
主库 归档 删除策略
查看>>
《Linux从入门到精通(第2版)》——导读
查看>>
路过下载攻击利用旧版 Android 漏洞安装勒索软件
查看>>
ThinkSNS 六大子版本体验及源码下载
查看>>
《算法基础》——1.5实际因素
查看>>
《Java数字图像处理:编程技巧与应用实践》——第3章 基本Swing UI组件与图像显示 3.1 JPanel组件与BufferedImage对象的显示...
查看>>
为什么有人讨厌 Google 的新 Logo?
查看>>