6666
6767public class HttpClientJavaLib extends GXHttpClient {
6868
69- private static String getGxIpResolverConfig () {
69+ private static final DnsResolver FIRST_IP_DNS_RESOLVER = host -> {
70+ InetAddress [] allIps = SystemDefaultDnsResolver .INSTANCE .resolve (host );
71+ if (allIps != null && allIps .length > 1 ) {
72+ return new InetAddress []{allIps [0 ]};
73+ }
74+ return allIps ;
75+ };
76+
77+ private static boolean isFirstIpDnsEnabled () {
7078 String name = "GX_USE_FIRST_IP_DNS" ;
7179 String gxDns = System .getProperty (name );
7280 if (gxDns == null || gxDns .trim ().isEmpty ()) {
7381 gxDns = System .getenv (name );
7482 }
75- if (gxDns != null && gxDns .trim ().equalsIgnoreCase ("true" )) {
76- return gxDns .trim ();
77- } else {
78- return null ;
79- }
83+ return gxDns != null && gxDns .trim ().equalsIgnoreCase ("true" );
8084 }
8185
82-
8386 public HttpClientJavaLib () {
8487 getPoolInstance ();
8588 ConnectionKeepAliveStrategy myStrategy = generateKeepAliveStrategy ();
8689 HttpClientBuilder builder = HttpClients .custom ()
8790 .setConnectionManager (connManager )
8891 .setConnectionManagerShared (true )
8992 .setKeepAliveStrategy (myStrategy );
93+ if (isFirstIpDnsEnabled ()) {
94+ builder .setDnsResolver (FIRST_IP_DNS_RESOLVER );
95+ }
9096 httpClientBuilder = builder ;
9197 cookies = new BasicCookieStore ();
9298 streamsToClose = new Vector <>();
@@ -98,17 +104,8 @@ private static void getPoolInstance() {
98104 RegistryBuilder .<ConnectionSocketFactory >create ()
99105 .register ("http" , PlainConnectionSocketFactory .INSTANCE ).register ("https" , getSSLSecureInstance ())
100106 .build ();
101-
102- if (getGxIpResolverConfig () != null ) {
103- DnsResolver dnsResolver ;
104- dnsResolver = host -> {
105- InetAddress [] allIps = SystemDefaultDnsResolver .INSTANCE .resolve (host );
106- if (allIps != null && allIps .length > 1 ) {
107- return new InetAddress []{allIps [0 ]};
108- }
109- return allIps ;
110- };
111- connManager = new PoolingHttpClientConnectionManager (socketFactoryRegistry , dnsResolver );
107+ if (isFirstIpDnsEnabled ()) {
108+ connManager = new PoolingHttpClientConnectionManager (socketFactoryRegistry , FIRST_IP_DNS_RESOLVER );
112109 } else {
113110 connManager = new PoolingHttpClientConnectionManager (socketFactoryRegistry );
114111 }
0 commit comments