Home > database >  Spring API Gateway java.net.ConnectException: Connection timed out: no further information
Spring API Gateway java.net.ConnectException: Connection timed out: no further information

Time:12-10

I have a spring boot app with simple GET method "sayHello" i have configured API gateway to route calls to my microservice module when uri pattern matches /ms1/**

Both gateway and microservice registered to the Registry service and dashboard displays both registered

enter image description here

API-GATEAY YAML configuration

server:
  port: '8010'
spring:
  application:
    name: MY-GATEWAY
  cloud:
    compatibility-verifier:
      enabled: false
    gateway:
      routes:
        - id: MICRO-SERVICE1
          uri: lb://MICRO-SERVICE1
          predicates:
            - Path=/ms1/**
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-uri: http://localhost:8761/eureka/
  intance:
    hostname: localhost

MICRO-SERVICE1 YAML configuration

server:
  port: '8030'

spring:
  application:
    name: MICRO-SERVICE1
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-uri: http://localhost:8761/eureka/
  intance:
    hostname: localhost

Controller's GET method

@RestController
@RequestMapping("/ms1")
public class UATController {
    @GetMapping(value = "/sayHello")
    public @ResponseBody ResponseEntity<String> sayHello() {
        String message = "Hello World";
        return new ResponseEntity<String>(message, HttpStatus.OK);
    }
}

I am getting following error when i access via gateway, however when i access my microservice directly it works any idea where i am wrong here?

  • http://localhost:8010/ms1/sayHello <- Not working via gateway
  • http://localhost:8030/ms1/sayHello <- Working direct access
Wed Dec 07 23:32:31 EST 2021
[83b324d4-2] There was an unexpected error (type=Internal Server Error, status=500).
Connection timed out: no further information: DESKTOP-1.verizon.net/192.168.1.160:8030
io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection timed out: no further information: DESKTOP-1.verizon.net/192.168.1.160:8030
    Suppressed: The stacktrace has been enhanced by Reactor, refer to additional information below: 
Error has been observed at the following site(s):
    *__checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
    *__checkpoint ⇢ org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain]
    *__checkpoint ⇢ HTTP GET "/ms1/sayHello" [ExceptionHandlingWebHandler]
Original Stack Trace:
Caused by: java.net.ConnectException: Connection timed out: no further information
    at java.base/sun.nio.ch.Net.pollConnect(Native Method)
    at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)
    at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:946)
    at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330)
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:707)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:833)

CodePudding user response:

I have faced the same issue today as well when running the micro services, the gateway and the Eureka discovery all on localhost.

I was able to solve it by adding eureka.instance.preferIpAddress=true to my application.properties of the micro services. That property causes that the services will advertise their IP addresses instead of the host names. More info here under point 2.6: https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-eureka-server.html. My guess is that there was an issue with forwarding the requests to the microservices due to using localhost.

Edit: Here are my settings There is nothing special in my setup. These are the values I am using in my application.properties The microservice is using:

spring.cloud.discovery.enabled=true
spring.application.name=[The name of my app]
eureka.client.register.with-eureka=true
eureka.client.service-url.defaultZone=http://localhost:8999/eureka
eureka.instance.prefer-ip-address=true

The properties for the gateway are similar to the service one. The load balancing part looks like this:

spring.cloud.gateway.routes[0].id=[Same name as my app]
spring.cloud.gateway.routes[0].uri=lb://[name of my app]
spring.cloud.gateway.routes[0].predicates=Path=/test/**

Where test/** is the part of the Rest URL of my application

CodePudding user response:

Adding DESKTOP-1.verizon.net to hosts file worked, following is entry I have added to C:\Windows\System32\drivers\etc\hosts file. Unfortunately eureka.instance.prefer-ip-address=true didn't work for me

127.0.0.1   DESKTOP-1.verizon.net
  • Related