上面 代码中的 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】
推荐阅读
- 索尼|索尼PS5重大更新即将到来!支持2K分辨率、自定义游戏列表等
- Android 添加系统自定义接口
- 502 Bad Gatewaynginx 502 Bad Gateway是什么意思
- SpringCloud OpenFeign 服务调用传递 token
- 华为|华为提前揭秘HarmonyOS 3升级亮点:流畅、安全、布局自定义
- windows10怎么自定义鼠标指针 win10怎么自定义鼠标光标图形
- App|iPhone用户等到了!魅蓝lifeme上架App Store:支持耳机自定义
- 地下城与勇士|DNF:跨一卧虎藏龙!打桩比赛成绩揭晓,自定义战士稀有职业崛起
- win10自定义开机动画 开机画面
- qq撤回消息怎么自定义?