简体   繁体   English

如何使用Netty4ClientHttpRequestFactory为Spring AsyncRestTemplate设置代理?

[英]How to set proxy for Spring AsyncRestTemplate using Netty4ClientHttpRequestFactory?

When I use a SimpleRequestFactory with my AsyncRestTemplate I can easily configure an HTTP proxy server. 当我将SimpleRequestFactory与AsyncRestTemplate一起使用时,可以轻松配置HTTP代理服务器。

I can either do (sample code in Kotlin): 我可以做(Kotlin中的示例代码):

@Bean
open fun asyncRestTemplate(): AsyncRestTemplate {
    val proxy = Proxy(Proxy.Type.HTTP, InetSocketAddress("127.0.0.1", 8008))

    val requestFactory = SimpleClientHttpRequestFactory().apply {
        this.setConnectTimeout(TimeUnit.SECONDS.toMillis(10).toInt())
        this.setReadTimeout(TimeUnit.SECONDS.toMillis(10).toInt())
        this.setProxy(proxy)
        this.setTaskExecutor(taskExecutor())
    }

    return AsyncRestTemplate(requestFactory)
}

Or I can simply set the corresponding system properties: -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8008 . 或者,我可以简单地设置相应的系统属性: -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8008

However, in the moment that I switch from the SimpleClientHttpRequestFactory to a Netty4ClientHttpRequestFactory there is no evident way to configure the proxy directly and it seems this client does not respect the system properties either. 但是,从我从SimpleClientHttpRequestFactory切换到Netty4ClientHttpRequestFactory的那一刻,没有明显的方法可以直接配置代理,并且此客户端似乎也不尊重系统属性。

val requestFactory = Netty4ClientHttpRequestFactory().apply {
    this.setConnectTimeout(TimeUnit.SECONDS.toMillis(10).toInt())
    this.setReadTimeout(TimeUnit.SECONDS.toMillis(10).toInt())
    //this.setProxy(proxy) //???
}

Once I change for the netty client, I have no clue on how to make it go through the proxy. 一旦更改了netty客户端,我就不知道如何使其通过代理。

My interest in using the netty client was that I not only wanted to make async requests, but also I wanted this to be non-blocking. 我对使用netty客户端的兴趣在于,我不仅想发出异步请求,而且希望它是非阻塞的。 I hope I'm not making a wrong assumption here. 我希望我不会在这里做出错误的假设。

Does anyone know how can I use a proxy server when using the Netty4ClientHttpRequestFactory or perhaps know of an alternative non-blocking client supported by Spring that I could use? 有谁知道在使用Netty4ClientHttpRequestFactory时如何使用代理服务器,或者知道我可以使用的Spring支持的另一种非阻塞客户端?

The Netty4ClientHttpRequestFactory ( source ) and related classes such as Netty4ClientHttpRequest ( source ) use SimpleChannelInboundHandler for the channel and do not use the proxy handler. Netty4ClientHttpRequestFactory )和相关类(例如Netty4ClientHttpRequest ))将SimpleChannelInboundHandler用于通道,而不使用代理处理程序。 Everything is private and unable to be overridden within the source, so there is no way to change it to support Proxies. 一切都是私有的,无法在源代码中被覆盖,因此无法更改它以支持代理。 You would have to almost rewrite the whole thing. 您将不得不几乎重写整个过程。

You have other async client options that will work very well and allow you more configuration options. 您还有其他异步客户端选项可以很好地工作,并允许您使用更多配置选项。 The included Netty one is fairly basic anyway. 包含的Netty还是相当基本的。 OkHttpClientHttpRequestFactory and HttpComponentsAsyncClientHttpRequestFactory both let you pass in your own configured client. OkHttpClientHttpRequestFactoryHttpComponentsAsyncClientHttpRequestFactory都允许您传入自己配置的客户端。

To your interest, AsyncRestTemplate's different implementation: 为了您的兴趣,AsyncRestTemplate的不同实现:

SimpleClientHttpRequestFactory -> simple thread pool, blocking api, proxy supported SimpleClientHttpRequestFactory->简单线程池,阻止api,支持代理

OkHttpClient (OkHttp3) -> blocking api, proxy supported OkHttpClient(OkHttp3)->阻止api,支持代理

CloseableHttpAsyncClient -> non-blocking nio api, proxy supported CloseableHttpAsyncClient->非阻塞Nio API,支持代理

Netty4ClientHttpRequestFactory -> non-blocking nio api, proxy not supported Netty4ClientHttpRequestFactory->非阻塞NIO API,不支持代理

you can visit https://github.com/wuxudong/VariousAsyncHttpClientPerformance for more details 您可以访问https://github.com/wuxudong/VariousAsyncHttpClientPerformance了解更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM