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.
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.
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, 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:
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With