RestTemplate connection error after migrating from boot 2.2.5.RELEASE to 2.4.6

8/18/2021

I have 2 micro-services communicating via simple ResT call. I have a scenario where email needs to be send to user and admin , so there are two calls going from service 1 to service 2 .

 **log.info("sending mail to user {}", ticketDetailsDto.getEmail());

        // Send Email to User
        **emailServiceUtils.emailServiceCall(notificationDtoForUser, ServiceEndPoint.EMAIL_URL_UNAUTHENTICATE);**

        NotificationDto notificationDtoAdmin = new NotificationDto();
        notificationDtoAdmin.setRecipient(applicationConfig.getSupportEmail());
        notificationDtoAdmin.setTemplateId(EmailTemplate.SUPPORT_ADMIN);
        notificationDtoAdmin.setData(dataOfUser);

        log.info("sending mail to {}", applicationConfig.getSupportEmail());
        log.debug("email service url {}", applicationConfig.emailServiceInstance() + ServiceEndPoint.EMAIL_URL_UNAUTHENTICATE);

        //Send Email to Admin
        emailServiceUtils.emailServiceCall(notificationDtoAdmin, ServiceEndPoint.EMAIL_URL_UNAUTHENTICATE);

        return "Your request sent to support team";**

email service call method is like this .

public String emailServiceCall(NotificationDto notificationDto, String url) {
    try {
        UriComponentsBuilder uriBuilder = getUriComponentsBuilder(url);

        log.info("email service url {}", uriBuilder.build().toUri());
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.APPLICATION_JSON);
        requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        requestHeaders.add(Constants.AUTHORIZATION, requestScopedStorageService.getToken());

        restTemplate.setErrorHandler(new ExceptionHandler());
        HttpEntity<?> requestEntity = new HttpEntity<>(notificationDto, requestHeaders);
        restTemplate.exchange(uriBuilder.build().toUri(), HttpMethod.POST, requestEntity, String.class);


    } catch (RestClientException e) {
        e.printStackTrace();

    }
    return "email send";
}

Now calls have started failing.

2021-08-18 10:25:14.262 ERROR 1 --- [nio-8089-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://email-service/email/sendThroughTemplateUnAuthenticated": Connection timed out (Connection timed out); nested exception is java.net.ConnectException: Connection timed out (Connection timed out)] with root cause

java.net.ConnectException: Connection timed out (Connection timed out)
	at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_302]
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_302]
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_302]
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_302]
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_302]
	at java.net.Socket.connect(Socket.java:607) ~[na:1.8.0_302]
	at java.net.Socket.connect(Socket.java:556) ~[na:1.8.0_302]
	at sun.net.NetworkClient.doConnect(NetworkClient.java:180) ~[na:1.8.0_302]
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:463) ~[na:1.8.0_302]
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:558) ~[na:1.8.0_302]
	at sun.net.www.http.HttpClient.<init>(HttpClient.java:242) ~[na:1.8.0_302]
	at sun.net.www.http.HttpClient.New(HttpClient.java:339) ~[na:1.8.0_302]
	at sun.net.www.http.HttpClient.New(HttpClient.java:357) ~[na:1.8.0_302]
	at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1226) ~[na:1.8.0_302]
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1162) ~[na:1.8.0_302]
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1056) ~[na:1.8.0_302]
	at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:990) ~[na:1.8.0_302]

This started happening after I upgraded spring boot from 2.2.5.RELEASE to 2.4.6 , Interesting thing is this works if I downgrade to 2.2.5.RELEASE.

-- Prashant Raghav
java
kubernetes
rest
resttemplate
spring-boot

0 Answers