首页 > 解决方案 > Spring Cloud Gateway 无法进行负载平衡并给出错误 500

问题描述

我在这里学习教程:https ://www.javainuse.com/spring/cloud-gateway-eureka 。

我有 2 个应用程序,可通过 localhost(localhost:8080/employee/messages,localhost:8082/consumer/messages)访问。我还有一个服务发现(Spring Cloud Eureka)和用于负载平衡的 Spring Cloud Gateway。我已经用尤里卡注册了所有这三个。在此处输入图像描述

另外,我从本地机器上运行所有东西,这里的 host.docker.internal 是什么?

网关应用程序的 application.properties 文件如下所示:

server:
  port: 9090

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8083/eureka  

spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
      - id: employeeModule
        uri: lb://FIRST-SERVICE
        predicates:
        - Path=/employee/**
      - id: consumerModule
        uri: lb://SECOND-SERVICE
        predicates:
        - Path=/consumer/**

但是,每当我尝试访问 localhost:9090/employee/message 时,都会收到错误代码 500。网关服务中的错误日志如下所示:

io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection timed out: no further information: host.docker.internal/10.7.250.57:8080
Caused by: java.net.ConnectException: Connection timed out: no further information
        at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
        at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:579) ~[na:na]
        at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:820) ~[na:na]
        at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:327) ~[netty-transport-4.1.38.Final.jar!/:4.1.38.Final]
        at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:336) ~[netty-transport-4.1.38.Final.jar!/:4.1.38.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:685) ~[netty-transport-4.1.38.Final.jar!/:4.1.38.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:632) ~[netty-transport-4.1.38.Final.jar!/:4.1.38.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:549) ~[netty-transport-4.1.38.Final.jar!/:4.1.38.Final]
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:511) ~[netty-transport-4.1.38.Final.jar!/:4.1.38.Final]
        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:918) ~[netty-common-4.1.38.Final.jar!/:4.1.38.Final]
        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.38.Final.jar!/:4.1.38.Final]
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.38.Final.jar!/:4.1.38.Final]
        at java.base/java.lang.Thread.run(Thread.java:830) ~[na:na]

有人可以告诉我这里的错误是什么。

编辑:我支持公司代理,无法执行以下操作: curl host.docker.internal:8080/employee/message

标签: springspring-bootspring-cloudspring-cloud-netflixspring-cloud-gateway

解决方案


该消息告诉您您没有从网关连接到微服务 REST 端点。您的 URI 似乎没有正确指定;建议使用 URL,例如 uri: http://localhost:8000/employee或类似的东西。(在我的应用程序中工作......)

至于负载均衡,这篇文章的任何地方都没有负载均衡。Spring Cloud Gateway 匹配请求属性上的路由。这就是您试图在 yml 文件中促进的内容。Spring Cloud Gateway 不等于负载均衡;它是进入微服务架构的一种方式。

负载平衡可以使用 Netflix Ribbon(显然已被弃用)或新的 Spring Cloud Load Balancer 来完成。您没有使用任何一个,因此没有负载平衡。参见例如:

https://spring.io/blog/2020/03/25/spring-tips-spring-cloud-loadbalancer

https://spring.io/guides/gs/spring-cloud-loadbalancer/

此外,您需要设置可以在本地或跨网络发现的并行应用程序。然后实现 Netflix Ribbon 或者最好是新的 Spring Load Balancer。

我相信可以为非常高流量的应用程序设置并行 Spring Cloud Gateway 和负载平衡。我希望一个网关不是高流量的瓶颈,即使它是异步的,就像这里的情况一样。

我还没有找到任何跨网关负载平衡的例子,希望在这里得到反馈。具体来说,是否有任何特定于跨网关的负载平衡与 Spring 文档中的 Hello 应用程序(正在发展)不同?


推荐阅读