curl from pod is successful but restTemplate call is giving connection TimeOut in spring boot app

8/23/2021

Hi am doing a API call from restTemplate with is giving me 404 , but after doing sh insside the service pod and doing curl I get proper response .

Here is my code

public String emailServiceCall(NotificationDto notificationDto, String url) {
    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);

    return "email send";
}
public UriComponentsBuilder getUriComponentsBuilder(String url) {

    if (null != applicationConfig.emailServiceInstance()) {
        return UriComponentsBuilder.fromHttpUrl(applicationConfig.emailServiceInstance() + url);
    } else {
        throw xxx.internalServerError(ExceptionMessage.EMAIL_SERVICE_NOT_AVAILABLE);
    }
}

my error stack is this

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://email-service:8080/email/sendThroughTemplateUnAuthenticated": Connect to 10.244.0.105:8088 [/10.244.0.105] failed: Connection timed out (Connection timed out); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to 10.244.0.105:8088 [/10.244.0.105] failed: Connection timed out (Connection timed out)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:785)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:602)
at com.telekom.halo.utils.EmailServiceUtils.emailServiceCall(EmailServiceUtils.java:109)
at com.telekom.halo.service.impl.SupportServiceImpl.saveTicket(SupportServiceImpl.java:89)
at com.telekom.halo.service.impl.SupportServiceImpl$FastClassBySpringCGLIB$bf2d3425.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692)
at com.telekom.halo.service.impl.SupportServiceImpl$EnhancerBySpringCGLIB$2ec2f56.saveTicket(<generated>)
at com.telekom.halo.controller.SupportController.saveTicket(SupportController.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method

Here is bootstrap.yaml

server:
  port: 8088
spring:
  application:
    name: email-service
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        instance-id: ${spring.application.name}:${random.value}
        enabled: true
        register: true
        health-check-interval: 20s
        prefer-ip-address: true
      config:
        enabled: true
        prefix: configuration
        format: YAML
        data-key: data
        watch:
          enabled: true

Here is kubectl get svc output

email-service NodePort 10.106.40.110 <none> 8080:32015/TCP

Thanks in advance...

-- Prashant Raghav
curl
kubernetes
resttemplate

0 Answers