SpringCloud gateway自定义请求的 httpClient( 二 )

上面 代码中的 gatewayHttpClient 为 spring cloud gateway 使用的 HttpClient 实例,在spring cloud gateway 进行服务请求时,会自动配置使用该 实例 。
如果需要自定义的 HttpClient 实例,如在 httpClient 中自定义 ip 白名单校验,https 请求证书预置,或是添加特殊认证请求头等,这种场景下需要在代码中显示的定义 gatewayHttpClient 实例,代码如下:
@Configurationpublic class GatewayAutoConfiguration {@Bean@ConditionalOnMissingBeanpublic HttpClient gatewayHttpClient(HttpClientProperties properties, List<HttpClientCustomizer> customizers) {Pool pool = properties.getPool();ConnectionProvider connectionProvider;if (pool.getType() == PoolType.DISABLED) {connectionProvider = ConnectionProvider.newConnection();} else if (pool.getType() == PoolType.FIXED) {connectionProvider = ConnectionProvider.fixed(pool.getName(), pool.getMaxConnections(), pool.getAcquireTimeout(), pool.getMaxIdleTime(), pool.getMaxLifeTime());} else {connectionProvider = ConnectionProvider.elastic(pool.getName(), pool.getMaxIdleTime(), pool.getMaxLifeTime());}HttpClient httpClient = HttpClient.create(connectionProvider).httpResponseDecoder((spec) -> {if (properties.getMaxHeaderSize() != null) {spec.maxHeaderSize((int)properties.getMaxHeaderSize().toBytes());}if (properties.getMaxInitialLineLength() != null) {spec.maxInitialLineLength((int)properties.getMaxInitialLineLength().toBytes());}return spec;}).tcpConfiguration((tcpClient) -> {if (properties.getConnectTimeout() != null) {tcpClient = tcpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, properties.getConnectTimeout());}Proxy proxy = properties.getProxy();if (StringUtils.hasText(proxy.getHost())) {tcpClient = tcpClient.proxy((proxySpec) -> {Builder builder = proxySpec.type(reactor.netty.tcp.ProxyProvider.Proxy.HTTP).host(proxy.getHost());PropertyMapper map = PropertyMapper.get();proxy.getClass();map.from(proxy::getPort).whenNonNull().to(builder::port);proxy.getClass();map.from(proxy::getUsername).whenHasText().to(builder::username);proxy.getClass();map.from(proxy::getPassword).whenHasText().to((password) -> {builder.password((s) -> {return password;});});proxy.getClass();map.from(proxy::getNonProxyHostsPattern).whenHasText().to(builder::nonProxyHosts);});}return tcpClient;});Ssl ssl = properties.getSsl();if (ssl.getKeyStore() != null && ssl.getKeyStore().length() > 0 || ssl.getTrustedX509CertificatesForTrustManager().length > 0 || ssl.isUseInsecureTrustManager()) {httpClient = httpClient.secure((sslContextSpec) -> {SslContextBuilder sslContextBuilder = SslContextBuilder.forClient();X509Certificate[] trustedX509Certificates = ssl.getTrustedX509CertificatesForTrustManager();if (trustedX509Certificates.length > 0) {sslContextBuilder = sslContextBuilder.trustManager(trustedX509Certificates);} else if (ssl.isUseInsecureTrustManager()) {sslContextBuilder = sslContextBuilder.trustManager(InsecureTrustManagerFactory.INSTANCE);}try {sslContextBuilder = sslContextBuilder.keyManager(ssl.getKeyManagerFactory());} catch (Exception var6) {this.logger.error(var6);}sslContextSpec.sslContext(sslContextBuilder).defaultConfiguration(ssl.getDefaultConfigurationType()).handshakeTimeout(ssl.getHandshakeTimeout()).closeNotifyFlushTimeout(ssl.getCloseNotifyFlushTimeout()).closeNotifyReadTimeout(ssl.getCloseNotifyReadTimeout());});}if (properties.isWiretap()) {httpClient = httpClient.wiretap(true);}if (!CollectionUtils.isEmpty(customizers)) {customizers.sort(AnnotationAwareOrderComparator.INSTANCE);HttpClientCustomizer customizer;for(Iterator var7 = customizers.iterator(); var7.hasNext(); httpClient = customizer.customize(httpClient)) {customizer = (HttpClientCustomizer)var7.next();}}return httpClient;}}这样服务在启动的时候就会优先加载自定的 httpClient 实例 。

原文链接:
https://www.cnblogs.com/zjdxr-up/p/16530423.html

【SpringCloud gateway自定义请求的 httpClient】


推荐阅读