简体   繁体   English

如何像 Spring Boot 方式一样为 Micronaut (1.1.4) HTTP 客户端配置 HTTP 代理?

[英]How can I configure the HTTP proxy for a Micronaut (1.1.4) HTTP client like the Spring Boot way?

Well after struggling a lot with Micronaut to dompted our proxies, I came to the idea to write a Spring Boot Application doing for the same purpose.好吧,在与 Micronaut 苦苦挣扎以放弃我们的代理之后,我萌生了编写一个用于相同目的的 Spring Boot 应用程序的想法。

For Spring Boot the HTTP proxy configuration is really straight forward and there are a lot examples available.对于 Spring Boot,HTTP 代理配置非常简单,并且有很多示例可用。 I came out with this example:我提出了这个例子:

application.properties应用程序属性

generic.proxyHost = my.corporateproxy.net
generic.proxyPort = 3128

MyController.java我的控制器

@Value("${generic.proxyHost}")
private String proxyHost;

@Value("${generic.proxyPort}")
private Integer proxyPort;

@GetMapping("/proxy")
public HttpStatus getApiWithProxy(){

    SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
    InetSocketAddress address = new InetSocketAddress(proxyHost, proxyPort);
    Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
    factory.setProxy(proxy);

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setRequestFactory(factory);
    ResponseEntity<String> response = restTemplate.getForEntity("https://any.api.returningstring.net/", String.class);
    return response.getStatusCode();
}

This way actually works, I tried to translate this listing to Micronaut extending for example the HttpClientConfiguration.这种方式确实有效,我尝试将此列表转换为 Micronaut 扩展,例如 HttpClientConfiguration。 Without any success.没有任何成功。

Is there any solution to set proxy and passing it programmatically to the HttpClient in Micronaut?是否有任何解决方案可以设置代理并将其以编程方式传递给 Micronaut 中的 HttpClient?

PS: This spring boot application is launched as Docker Container in our corporate Cloud (Kubernetes). PS:这个 Spring Boot 应用程序在我们的企业云 (Kubernetes) 中作为 Docker 容器启动。 The micronaut have to replace it, but we stuck at how to set the proxies. micronaut 必须更换它,但我们坚持如何设置代理。

@James, I spent a couple days trying to get a Service Http Client Configuration but no matter what I tried, I couldn't get it to inject. @James,我花了几天时间尝试获取服务 Http 客户端配置,但无论我尝试什么,都无法注入。 I tried to get the VAT example working from the micronaut aws lambda guide .我试图从micronaut aws lambda 指南中获取增值税示例。 That example needs a proxy in my corporate environment, so I spent nearly two days, give or take, researching to get it to work.这个例子在我的公司环境中需要一个代理,所以我花了将近两天的时间,给予或接受,研究让它工作。 I tried 2 ways:我尝试了两种方法:

  1. ServiceHttpClientConfiguration FAILED ServiceHttpClientConfiguration失败
  2. CustomHttpClientConfiguration SUCCESS CustomHttpClientConfiguration成功

ServiceHttpClientConfiguration服务HttpClient配置

Here are some things I tried:以下是我尝试过的一些事情:

resources/application.yml
...
micronaut:
    http:
        services:
            vat:
                proxy-address: some.proxy.corp.com:8000
                proxy-type: http        
example/micronaut/VatService.groovy
...
    @Client("vat")
    @Inject RxHttpClient client

I would run with the debugger but the client.configuration class would never get populated with the ServiceHttpClientConfiguration.我会使用调试器运行,但 client.configuration 类永远不会填充 ServiceHttpClientConfiguration。 It always got populated with the DefaultHttpClientConfiguration它总是填充有 DefaultHttpClientConfiguration

CustomHttpClientConfiguration自定义HttpClient配置

I finally went for a custom configuration class instead.我最终改为使用自定义配置类。 It looks like this for me and works for a proxy:对我来说看起来像这样并且适用于代理:

    @Inject
    @Client(value = "client-two", configuration = ClientTwoHttpConfiguration.class)
    RxHttpClient client

    @Singleton
    static class ClientTwoHttpConfiguration extends HttpClientConfiguration {

        private final DefaultHttpClientConfiguration.DefaultConnectionPoolConfiguration connectionPoolConfiguration

        @Inject
        ClientTwoHttpConfiguration(ApplicationConfiguration applicationConfiguration, DefaultHttpClientConfiguration.DefaultConnectionPoolConfiguration connectionPoolConfiguration) {
            super(applicationConfiguration)
            this.connectionPoolConfiguration = connectionPoolConfiguration
            setProxyType(Proxy.Type.HTTP)
            setProxyAddress(new InetSocketAddress("some.proxy.corp.com",8000))
        }

        @Override
        HttpClientConfiguration.ConnectionPoolConfiguration getConnectionPoolConfiguration() {
            return this.connectionPoolConfiguration
        }

    }

I took this from one of the micronaut tests我从其中一项micronaut 测试中获取了这个

暂无
暂无

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

相关问题 如何使用 Micronaut 在 HttpClientFilter 中重试 HTTP 客户端请求? - How can I retry HTTP client requests within a HttpClientFilter using Micronaut? 如何为客户端和服务器配置Spring Boot RestTemplate代理 - How to configure spring boot resttemplate proxy for client and server 如何修改 Micronaut Http 过滤器的顺序,以便在 Micronaut Security 之后执行 - How can i modify the order of Micronaut Http Filter so it get executed after Micronaut Security 如何在eclipse中设置http代理? - How can I set the http proxy in eclipse? 如何通过微型Http客户端将Grails应用程序连接到Consul? - How to connect a grails app to Consul through micronaut Http Client? 如果http请求的内容类型为urlencoded,如何让我的spring启动控制器读取我对某个对象的http请求的主体? - How can I get my spring boot controller to read the body of my http request to an object if the http request has content type urlencoded? 我如何使用Spring Boot Controller执行http post junit测试 - How can i do the http post junit test with spring boot controller 如何使用Spring Boot在http请求中创建自定义登录? - How can you create a custom log in http request with Spring boot? 如何在 micronaut 应用程序中禁用 http 服务器 - How do I disable the http server in a micronaut application Spring启动在与http不同的端口上配置websocket - Spring boot configure websocket on a different port than http
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM