【问题标题】:Spring API Gateway java.net.ConnectException: Connection timed out: no further informationSpring API Gateway java.net.ConnectException:连接超时:没有更多信息
【发布时间】:2022-01-13 23:38:53
【问题描述】:

我有一个带有简单 GET 方法“sayHello”的 Spring Boot 应用程序,我已配置 API 网关,以便在 uri 模式匹配 /ms1/** 时将调用路由到我的微服务模块

网关和微服务都注册到 Registry 服务和仪表板显示都注册了

API-GATEAY YAML 配置

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 配置

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

控制器的 GET 方法

@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);
    }
}

当我通过网关访问时出现以下错误,但是当我直接访问我的微服务时,它知道我在哪里错了吗?

  • http://localhost:8010/ms1/sayHello
  • http://localhost:8030/ms1/sayHello
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)

【问题讨论】:

  • 你用 docker 运行它吗?
  • 我的桌面上没有安装 docker

标签: spring-boot spring-cloud netflix-eureka


【解决方案1】:

我今天在 localhost 上运行微服务、网关和 Eureka 发现时也遇到了同样的问题。

我能够通过将eureka.instance.preferIpAddress=true 添加到我的微服务的 application.properties 来解决它。该属性导致服务将公布其 IP 地址而不是主机名。更多信息请参见第 2.6 点:https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-eureka-server.html。我的猜测是,由于使用 localhost,将请求转发到微服务时出现问题。

编辑:这是我的设置 我的设置没有什么特别之处。这些是我在 application.properties 中使用的值 微服务正在使用:

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

网关的属性类似于服务之一。 负载均衡部分如下所示:

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/**

其中 test/** 是我的应用程序的 Rest URL 的一部分

【讨论】:

  • 即使我用 preferIpAddress: true 更新了 application.yml 文件,我还是有同样的问题,你能分享你的网关和微服务 application.yml 吗?
  • 我使用适合我的设置编辑了我的帖子
【解决方案2】:

将 DESKTOP-1.verizon.net 添加到主机文件有效,以下是我添加到 C:\Windows\System32\drivers\etc\hosts 文件的条目。不幸的是 eureka.instance.prefer-ip-address=true 对我不起作用

127.0.0.1   DESKTOP-1.verizon.net

【讨论】:

    猜你喜欢
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 2014-04-18
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    相关资源
    最近更新 更多