我可以更改 Spring 的 ProxyExchange 中使用的 RestTemplate 吗?
Can I alter the RestTemplate used in Spring's ProxyExchange?
我正在使用 Spring 的 ProxyExchange
,我想更改默认值 RestTemplate
(以便忽略开发环境中的证书)。
有办法吗?
使用 MVC(而不是 flux)
所以基本上我构建了一个RestTemplateCustomizer
并添加了一个bean来自定义默认RestTemplate
我们使用 ProxyExchangeArgumentResolver class 改变了 RestTemplate。下面是代码片段
package com.vikrant.practice.config;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.gateway.mvc.config.ProxyExchangeArgumentResolver;
import org.springframework.cloud.gateway.mvc.config.ProxyProperties;
import org.springframework.cloud.gateway.mvc.config.ProxyResponseAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.util.Optional;
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.vikrant.practice"})
public class XyzConfig {
public RestTemplate restTemplate() {
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(30000).setConnectTimeout(30000).build();
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
poolingHttpClientConnectionManager.setMaxTotal(100);
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(100);
HttpClient httpClient = HttpClientBuilder
.create().setDefaultRequestConfig(requestConfig)
.setConnectionManager(poolingHttpClientConnectionManager)
.disableCookieManagement().build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setConnectionRequestTimeout(1000);
factory.setReadTimeout(30000);
factory.setConnectTimeout(30000);
factory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);
return restTemplate;
}
@Bean
public ProxyExchangeArgumentResolver proxyExchangeArgumentResolver(
Optional<RestTemplateBuilder> optional, ProxyProperties proxy) {
RestTemplate template = restTemplate();
template.getMessageConverters().add(new ByteArrayHttpMessageConverter() {
@Override
public boolean supports(Class<?> clazz) {
return true;
}
});
ProxyExchangeArgumentResolver resolver = new ProxyExchangeArgumentResolver(template);
resolver.setHeaders(proxy.convertHeaders());
resolver.setSensitive(proxy.getSensitive());
return resolver;
}
}
这是为 SpringBoot 应用程序完成的。如果你想在 web 应用程序中使用它,那么创建一个 class 扩展 SpringBootServletInitializer 并实现 WebApplicationInitializer 并在配置方法下连接这个 class.
我正在使用 Spring 的 ProxyExchange
,我想更改默认值 RestTemplate
(以便忽略开发环境中的证书)。
有办法吗?
使用 MVC(而不是 flux)
所以基本上我构建了一个RestTemplateCustomizer
并添加了一个bean来自定义默认RestTemplate
我们使用 ProxyExchangeArgumentResolver class 改变了 RestTemplate。下面是代码片段
package com.vikrant.practice.config;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.gateway.mvc.config.ProxyExchangeArgumentResolver;
import org.springframework.cloud.gateway.mvc.config.ProxyProperties;
import org.springframework.cloud.gateway.mvc.config.ProxyResponseAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.util.Optional;
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.vikrant.practice"})
public class XyzConfig {
public RestTemplate restTemplate() {
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(30000).setConnectTimeout(30000).build();
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
poolingHttpClientConnectionManager.setMaxTotal(100);
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(100);
HttpClient httpClient = HttpClientBuilder
.create().setDefaultRequestConfig(requestConfig)
.setConnectionManager(poolingHttpClientConnectionManager)
.disableCookieManagement().build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setConnectionRequestTimeout(1000);
factory.setReadTimeout(30000);
factory.setConnectTimeout(30000);
factory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);
return restTemplate;
}
@Bean
public ProxyExchangeArgumentResolver proxyExchangeArgumentResolver(
Optional<RestTemplateBuilder> optional, ProxyProperties proxy) {
RestTemplate template = restTemplate();
template.getMessageConverters().add(new ByteArrayHttpMessageConverter() {
@Override
public boolean supports(Class<?> clazz) {
return true;
}
});
ProxyExchangeArgumentResolver resolver = new ProxyExchangeArgumentResolver(template);
resolver.setHeaders(proxy.convertHeaders());
resolver.setSensitive(proxy.getSensitive());
return resolver;
}
}
这是为 SpringBoot 应用程序完成的。如果你想在 web 应用程序中使用它,那么创建一个 class 扩展 SpringBootServletInitializer 并实现 WebApplicationInitializer 并在配置方法下连接这个 class.