代理设置不适用于 Spring WebClient
Proxy setting not working with Spring WebClient
我的以下 WebClient 在 Internet 连接下工作正常,但通过我们的代理连接时不正常。
WebClient webClient = WebClient.builder()
.baseUrl("https://targetsite.com")
.build();
webClient.post()
.uri("/service/serviceName")
.body(BodyInserters.fromObject(reqData))
.retrieve()
.bodyToMono(WebServiceResponse.class)
尽管如此,同一个客户端正在通过代理工作,如果我按如下所述进行设置,
HttpClient httpClient = HttpClient.create()
.tcpConfiguration(tcpClient -> tcpClient
.proxy(proxy -> proxy
.type(ProxyProvider.Proxy.HTTP)
.host("ourproxy.com")
.port(8080)));
ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
WebClient webClient = WebClient.builder()
.clientConnector(connector)
.baseUrl("https://targetsite.com")
.build();
webClient.post()
.uri("/service/serviceName")
.body(BodyInserters.fromObject(reqData))
.retrieve()
.bodyToMono(WebServiceResponse.class)
但是如果我使用 System.setProperty("http.proxyHost","ourproxy.com");
设置相同的代理详细信息
System.setProperty("http.proxyPort","8080");
要么
JVM 运行-时间参数-Dhttp.proxyHost=ourproxy.com -Dhttp.proxyPort=8080
WebClient webClient = WebClient.builder()
.baseUrl("https://targetsite.com")
.build();
System.setProperty("http.proxyHost", "ourproxy.com");
System.setProperty("http.proxyPort", "8080");
webClient.post()
.uri("/service/serviceName")
.body(BodyInserters.fromObject(reqData))
.retrieve()
.bodyToMono(WebServiceResponse.class)
调用失败,出现 UnknownHostException,例如,
[04/11/2019 12:32:43.031 IST] DEBUG [reactor-http-epoll-3] [PooledConnectionProvider:254] - Creating new client pool [http] for targetsite.com:443
[04/11/2019 12:32:43.033 IST] DEBUG [reactor-http-epoll-3] [PooledConnectionProvider:254] - [id: 0xe4a0dc15] Created new pooled channel, now 0 active connections and 1 inactive connections
[04/11/2019 12:32:43.045 IST] DEBUG [reactor-http-epoll-3] [SslProvider:254] - [id: 0xe4a0dc15] SSL enabled using engine SSLEngineImpl and SNI targetsite.com:443
[04/11/2019 12:32:43.046 IST] DEBUG [reactor-http-epoll-3] [BootstrapHandlers:254] - [id: 0xe4a0dc15] Initialized pipeline DefaultChannelPipeline{(reactor.left.sslHandler = io.netty.handler.ssl.SslHandler), (reactor.left.sslReader = reactor.netty.tcp.SslProvider$SslReadHandler), (BootstrapHandlers$BootstrapInitializerHandler#0 = reactor.netty.channel.BootstrapHandlers$BootstrapInitializerHandler), (SimpleChannelPool#0 = io.netty.channel.pool.SimpleChannelPool), (reactor.left.httpCodec = io.netty.handler.codec.http.HttpClientCodec), (reactor.left.decompressor = io.netty.handler.codec.http.HttpContentDecompressor), (reactor.right.reactiveBridge = reactor.netty.channel.ChannelOperationsHandler)}
[04/11/2019 12:32:43.165 IST] ERROR [reactor-http-epoll-2] [AbstractErrorWebExceptionHandler:117] - [13ebf1eb] 500 Server Error for HTTP POST "/service/serviceName"
java.net.UnknownHostException: targetsite.com: Name or service not known
请帮忙,如果我通过 JVM 运行 时间参数或系统属性设置代理详细信息,为什么我的代码无法运行。
其实我想避免代码级代理设置。所以请指导我更正我的代码或方法,以便我可以使用 JVM 运行-时间参数选项。
后来当我向 reactor-netty 团队(https://github.com/reactor/reactor-netty/issues/887#issuecomment-549439355)提出这个问题时,他们确认,reactor.netty.http.client.HttpClient[=17 不支持系统属性=].唯一的选择是通过 tcpConfiguration 来设置它,我已经在我的问题中提到过;所以我用这句话结束这个问题。
谢谢大家
您需要在创建 HttpClient 实例时设置代理主机和端口。
HttpClient httpClient =
HttpClient.create()
.proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP)
.host(sasConfig.getProxyHost())
.port(Integer.parseInt(sasConfig.getProxyPort())));
ReactorClientHttpConnector conn = new ReactorClientHttpConnector(httpClient);
WebClient webClient = WebClient.builder().clientConnector(conn).build();
我没有足够的声誉来评论其他答案,所以我必须在这里开始我自己的回答。
从 reactor-netty v1.0.8 开始,可以从系统属性配置代理。
示例:
WebClient
.builder()
.clientConnector(
new ReactorClientHttpConnector(
HttpClient.create().proxyWithSystemProperties()))
.build()
我的以下 WebClient 在 Internet 连接下工作正常,但通过我们的代理连接时不正常。
WebClient webClient = WebClient.builder()
.baseUrl("https://targetsite.com")
.build();
webClient.post()
.uri("/service/serviceName")
.body(BodyInserters.fromObject(reqData))
.retrieve()
.bodyToMono(WebServiceResponse.class)
尽管如此,同一个客户端正在通过代理工作,如果我按如下所述进行设置,
HttpClient httpClient = HttpClient.create()
.tcpConfiguration(tcpClient -> tcpClient
.proxy(proxy -> proxy
.type(ProxyProvider.Proxy.HTTP)
.host("ourproxy.com")
.port(8080)));
ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
WebClient webClient = WebClient.builder()
.clientConnector(connector)
.baseUrl("https://targetsite.com")
.build();
webClient.post()
.uri("/service/serviceName")
.body(BodyInserters.fromObject(reqData))
.retrieve()
.bodyToMono(WebServiceResponse.class)
但是如果我使用 System.setProperty("http.proxyHost","ourproxy.com");
设置相同的代理详细信息
System.setProperty("http.proxyPort","8080");
要么
JVM 运行-时间参数-Dhttp.proxyHost=ourproxy.com -Dhttp.proxyPort=8080
WebClient webClient = WebClient.builder()
.baseUrl("https://targetsite.com")
.build();
System.setProperty("http.proxyHost", "ourproxy.com");
System.setProperty("http.proxyPort", "8080");
webClient.post()
.uri("/service/serviceName")
.body(BodyInserters.fromObject(reqData))
.retrieve()
.bodyToMono(WebServiceResponse.class)
调用失败,出现 UnknownHostException,例如,
[04/11/2019 12:32:43.031 IST] DEBUG [reactor-http-epoll-3] [PooledConnectionProvider:254] - Creating new client pool [http] for targetsite.com:443
[04/11/2019 12:32:43.033 IST] DEBUG [reactor-http-epoll-3] [PooledConnectionProvider:254] - [id: 0xe4a0dc15] Created new pooled channel, now 0 active connections and 1 inactive connections
[04/11/2019 12:32:43.045 IST] DEBUG [reactor-http-epoll-3] [SslProvider:254] - [id: 0xe4a0dc15] SSL enabled using engine SSLEngineImpl and SNI targetsite.com:443
[04/11/2019 12:32:43.046 IST] DEBUG [reactor-http-epoll-3] [BootstrapHandlers:254] - [id: 0xe4a0dc15] Initialized pipeline DefaultChannelPipeline{(reactor.left.sslHandler = io.netty.handler.ssl.SslHandler), (reactor.left.sslReader = reactor.netty.tcp.SslProvider$SslReadHandler), (BootstrapHandlers$BootstrapInitializerHandler#0 = reactor.netty.channel.BootstrapHandlers$BootstrapInitializerHandler), (SimpleChannelPool#0 = io.netty.channel.pool.SimpleChannelPool), (reactor.left.httpCodec = io.netty.handler.codec.http.HttpClientCodec), (reactor.left.decompressor = io.netty.handler.codec.http.HttpContentDecompressor), (reactor.right.reactiveBridge = reactor.netty.channel.ChannelOperationsHandler)}
[04/11/2019 12:32:43.165 IST] ERROR [reactor-http-epoll-2] [AbstractErrorWebExceptionHandler:117] - [13ebf1eb] 500 Server Error for HTTP POST "/service/serviceName"
java.net.UnknownHostException: targetsite.com: Name or service not known
请帮忙,如果我通过 JVM 运行 时间参数或系统属性设置代理详细信息,为什么我的代码无法运行。
其实我想避免代码级代理设置。所以请指导我更正我的代码或方法,以便我可以使用 JVM 运行-时间参数选项。
后来当我向 reactor-netty 团队(https://github.com/reactor/reactor-netty/issues/887#issuecomment-549439355)提出这个问题时,他们确认,reactor.netty.http.client.HttpClient[=17 不支持系统属性=].唯一的选择是通过 tcpConfiguration 来设置它,我已经在我的问题中提到过;所以我用这句话结束这个问题。
谢谢大家
您需要在创建 HttpClient 实例时设置代理主机和端口。
HttpClient httpClient =
HttpClient.create()
.proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP)
.host(sasConfig.getProxyHost())
.port(Integer.parseInt(sasConfig.getProxyPort())));
ReactorClientHttpConnector conn = new ReactorClientHttpConnector(httpClient);
WebClient webClient = WebClient.builder().clientConnector(conn).build();
我没有足够的声誉来评论其他答案,所以我必须在这里开始我自己的回答。
从 reactor-netty v1.0.8 开始,可以从系统属性配置代理。
示例:
WebClient
.builder()
.clientConnector(
new ReactorClientHttpConnector(
HttpClient.create().proxyWithSystemProperties()))
.build()