0

I have a common Spring Cloud architecture in my project: eureka for service discovery, zuul as a reverse proxy and some microservices. I want to be able to access the microservices from zuul programatically. As zuul is backed by Ribbon I just wanted to autowire a @LoadBalanced RestTemplate, but apparently this does not work. Below is my little test setup.

ZuulGatewayApplication.java:

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ZuulGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZuulGatewayApplication.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

SomeService.java:

@RequiredArgsConstructor
@Service
@Slf4j
public class SomeService {
    private final RestTemplate restTemplate;

    @PostConstruct
    public void init() {
        ResponseEntity<Void> response = restTemplate.getForEntity("http://my-service/notify", Void.class);
    }
}

Here is the relevant part of the stacktrace:

Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://my-service/notify": my-service; nested exception is java.net.UnknownHostException: my-service
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:748) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:342) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at xxx.xxx.xxxx.zuulgateway.service.SomeService.init(SomeService.java:40) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    ... 38 common frames omitted
Caused by: java.net.UnknownHostException: my-service
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:220) ~[na:na]
    at java.base/java.net.Socket.connect(Socket.java:591) ~[na:na]
    at java.base/java.net.Socket.connect(Socket.java:540) ~[na:na]
    at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:182) ~[na:na]
    at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474) ~[na:na]
    at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569) ~[na:na]
    at java.base/sun.net.www.http.HttpClient.<init>(HttpClient.java:242) ~[na:na]
    at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:341) ~[na:na]
    at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:362) ~[na:na]
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1242) ~[na:na]
    at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1181) ~[na:na]
    at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1075) ~[na:na]
    at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1009) ~[na:na]
    at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:739) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]

Do I need to configure something else to get the correct RestTemplate? I also tried to annotate the MainClass with @RibbonClient, but the error message stays the same.

Garuno
  • 1,935
  • 1
  • 9
  • 20
  • 2
    It doesn't work in post construct. You'll need to listen for an event like application ready event – spencergibb Oct 19 '20 at 18:47
  • 1
    Is the service `my-service` already registered in `eureka`? – Aman Oct 19 '20 at 19:25
  • The service is registered in eureka. Okay, it could be that the service discovery is not ready at the time of post construct. I will try the application ready event tomorrow :) – Garuno Oct 19 '20 at 21:31
  • Sometimes I just don't see the obvious stuff... @spencergibb Calling the `RestTemplate` in the application ready event worked! Can you post this comment as an answer? Then I can give you your internet prestige points by accepting it ^^ – Garuno Oct 20 '20 at 09:14
  • I've added an answer – spencergibb Oct 21 '20 at 17:43

1 Answers1

2

It doesn't work in post construct. You'll need to listen for an event like ApplicationReadyEvent.

public class SomeService
        implements ApplicationListener<ApplicationReadyEvent> {
    private final RestTemplate restTemplate;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        ResponseEntity<Void> response = restTemplate.getForEntity("http://my-service/notify", Void.class);
    }
}
spencergibb
  • 24,471
  • 6
  • 69
  • 75