Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

For Spring Boot the HTTP proxy configuration is really straight forward and there are a lot examples available. 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. Without any success.

Is there any solution to set proxy and passing it programmatically to the HttpClient in Micronaut?

P.S: This spring boot application is launched as Docker Container in our corporate Cloud (Kubernetes). The micronaut have to replace it, but we stuck at how to set the proxies.

like image 535
3logy Avatar asked Jul 25 '19 10:07

3logy


People also ask

What is HTTP client proxy?

What Does HTTP Proxy Mean? An HTTP Proxy serves two intermediary roles as an HTTP Client and an HTTP Server for security, management, and caching functionality. The HTTP Proxy routes HTTP Client requests from a Web browser to the Internet, while supporting the caching of Internet data.


2 Answers

To configure the proxy for all clients: https://docs.micronaut.io/latest/guide/configurationreference.html#io.micronaut.http.client.DefaultHttpClientConfiguration

To configure a proxy for a manually configured client: https://docs.micronaut.io/latest/guide/configurationreference.html#io.micronaut.http.client.ServiceHttpClientConfiguration

For any other clients you can specify the configuration class (which contains the proxy settings) in the annotation https://docs.micronaut.io/latest/api/io/micronaut/http/client/annotation/Client.html#configuration--

like image 167
James Kleeh Avatar answered Oct 13 '22 08:10

James Kleeh


@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. I tried to get the VAT example working from the micronaut aws lambda guide. 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
  2. CustomHttpClientConfiguration SUCCESS

ServiceHttpClientConfiguration

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. It always got populated with the DefaultHttpClientConfiguration

CustomHttpClientConfiguration

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

like image 26
Brent Fisher Avatar answered Oct 13 '22 06:10

Brent Fisher