9

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.

3logy
  • 2,384
  • 4
  • 37
  • 86
  • Possible duplicate with https://stackoverflow.com/questions/55813538/micronaut-server-and-httpclient-behind-corporate-proxy ? – saw303 Jul 25 '19 at 13:07
  • 1
    It's not the same. Here is about setting proxy on the fly for Production Environment for example. – 3logy Jul 25 '19 at 18:36

2 Answers2

4

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--

James Kleeh
  • 11,581
  • 5
  • 30
  • 59
  • Thank you for the answer, but some examples with the configuration class? because i tried like https://mrhaki.blogspot.com/2018/08/micronaut-mastery-using-specific.html but in k8s,cloud profile it doesnt work. – 3logy Jul 26 '19 at 05:38
  • I couldn't get the Service per client config to work, but the Default and custom seem to work fine. If different services need different proxies, see my answer below. This is the case when your tests hit your local service and those hit the internet. They both use the DefaultHttpClientConfiguration so both probably don't need a proxy. – Brent Fisher Nov 13 '19 at 23:28
1

@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

Brent Fisher
  • 103
  • 1
  • 10