实例关闭时的 Spring Cloud Gateway 500

2024-03-14

我有一个使用Spring Cloud Load Balancer(Spring Cloud版本:Hoxton.SR6)的Spring Cloud Gateway(eureka客户端)应用程序,并且我有一个Spring Boot应用程序的实例(启用了优雅关闭的Spring Boot 2.3,(eureka客户端)。

当我关闭 Spring Boot 服务并通过网关执行请求时,网关会抛出 500 错误(连接被拒绝),而不是 503。1-2 分钟后出现 503。

谁能澄清这是否是预期的行为?

看来问题来自 eureka-client (我的例子是 1.9.21 版本)AtomicReference<Applications> localRegionApps不经常更新

Thanks!

更新: 我决定更深入地检查这个 500 错误。结果是,如果不使用端口,我的系统(ubuntu)会出现此错误:

curl -v localhost:9722
 Rebuilt URL to: localhost:9722/
   Trying 127.0.0.1...
 TCP_NODELAY set
 connect to 127.0.0.1 port 9722 failed: Connection refused
 Failed to connect to localhost port 9722: Connection refused
 Closing connection 0

所以我输入了 application.yml:

spring:
  cloud:
    gateway:
      routes:
        - id: my_route
          uri: http://localhost:9722/

然后,当我的请求路由到 my_route 并且没有应用程序使用 9722 时,我收到错误:

io.netty.channel.AbstractChannel$AnnotatedConnectException: finishConnect(..) failed: Connection refused: localhost/127.0.0.1:9722
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
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 "/internal/mail/internal/health-check" [ExceptionHandlingWebHandler]
Stack trace:
Caused by: java.net.ConnectException: finishConnect(..) failed: Connection refused
    at io.netty.channel.unix.Errors.throwConnectException(Errors.java:124)
    at io.netty.channel.unix.Socket.finishConnect(Socket.java:251)
    at io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.doFinishConnect(AbstractEpollChannel.java:672)
    at io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.finishConnect(AbstractEpollChannel.java:649)
    at io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.epollOutReady(AbstractEpollChannel.java:529)
    at io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:465)
    at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:378)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
    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:834)

这似乎是一个意外的异常,因为无法使用断路器或任何网关过滤器来处理它。

是否可以正确处理这个错误?在这种情况下我想返回 503


将特定异常映射到特定 HTTP 状态代码的最简单方法之一是提供类型的自定义 beanorg.springframework.boot.web.reactive.error.ErrorAttributes。这是一个例子:

@Bean
public ErrorAttributes errorAttributes() {
    return new CustomErrorAttributes(httpStatusExceptionTypeMapper);
}

public class CustomErrorAttributes extends DefaultErrorAttributes {
    @Override
    public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
        Map<String, Object> attributes = super.getErrorAttributes(request, options);
        Throwable error = getError(request);
        MergedAnnotation<ResponseStatus> responseStatusAnnotation = MergedAnnotations
            .from(error.getClass(), MergedAnnotations.SearchStrategy.TYPE_HIERARCHY).get(ResponseStatus.class);
        HttpStatus errorStatus = determineHttpStatus(error, responseStatusAnnotation);
        attributes.put("status", errorStatus.value());
        return attributes;
    }

    private HttpStatus determineHttpStatus(Throwable error, MergedAnnotation<ResponseStatus> responseStatusAnnotation) {
        if (error instanceof ResponseStatusException) {
            return ((ResponseStatusException) error).getStatus();
        }
        return responseStatusAnnotation.getValue("code", HttpStatus.class).orElseGet(() -> {
           if (error instanceof java.net.ConnectException) {
               return HttpStatus.SERVICE_UNAVAILABLE;
           }
           return HttpStatus.INTERNAL_SERVER_ERROR;
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

实例关闭时的 Spring Cloud Gateway 500 的相关文章

随机推荐

  • DrawerLayout 必须使用 MeasureSpec.EXACTLY 错误进行测量

    我正在尝试实现导航抽屉 但我不断收到此错误 我看到了类似的问题 但对我不起作用 我有以下布局activity main2 xml
  • TensorFlow“模块”对象没有属性“global_variables_initializer”

    我是张量流新手 我正在 iPython 笔记本上运行 Udacity 的深度学习作业 link https github com tensorflow tensorflow blob master tensorflow examples u
  • pandas加入DataFrame强制后缀?

    如何在合并或连接上强制使用后缀 我知道如果发生冲突 可以提供一个 但在我的情况下 我将 df1 与 df2 合并 这不会导致任何冲突 但然后在使用后缀的 df2 上再次合并 但我希望每次合并都有一个后缀 因为如果我按照你的想象进行不同的组合
  • Spark错误:parquet.column.values.dictionary.PlainValuesDictionary$PlainIntegerDictionary

    我在从配置单元表读取 Spark 数据帧时遇到问题 我将数据框存储为 dataframe coalesce n files write option mergeSchema true mode overwrite parquet table
  • 获取时、分、秒、毫秒、微秒的时间

    我有这个代码 SimpleDateFormat sDate new SimpleDateFormat yyyy MM dd HH mm ss 我知道这段代码返回时间中的小时 分钟 秒 我怎样才能得到毫秒和微秒 您不会有微秒 因为 Date
  • R 在 HPC MPIcluster 上运行 foreach dopar 循环

    我可以访问带有 MPI 分区的 HPC 集群 我的问题是 无论我尝试什么 我的代码 在我的 PC 上运行良好 都无法在 HPC 集群上运行 代码如下所示 图书馆 TM 图书馆 qdap 图书馆 雪 图书馆 doSNOW 库 foreach
  • 创建 24 位 WAV 文件需要做什么特别的事情吗?

    我可以成功创建 16 位 wav 文件 但创建 24 位文件时 我听到的只是白噪声 我正在设置 24 位有符号整数数据块 我是否必须在 wav 文件头的第 20 字节处设置一些特殊的音频格式 我目前使用的是格式1 Edit 1 The wB
  • 如何在 Swift 中在地图图钉周围添加圆圈?

    我一直在试图解决这个问题 但没有运气 我可以做什么 显示用户当前位置 在我想要的任何地方显示图钉 取决于纬度和经度 我不明白的是 如何围绕该位置创建地理围栏 func setupData 1 check if system can moni
  • Object.Create() 在幕后做什么?

    我正在深入研究 JavaScript 的原型继承 当使用 Object Create 创建对象时 有人可以展示幕后发生了什么吗 Object Create 是否依赖于幕后的 new 和构造函数 When Object create 用于创建
  • 为什么 C++ 隐式转换有效,但显式转换无效?

    以下代码在 C 11 中编译成功 include json hpp using json nlohmann json using namespace std int main json js asd string s1 js lt comp
  • 如何获取 firestore 文档中的字段?

    我正在开发一些与 Firestore 配合使用的云功能 我正在尝试获取特定文档的字段列表 例如 我有一个文档参考even data ref 但我不确定该文档是否包含我正在查看的字段 我想获取字段名称的列表 但我不知道该怎么做 我试图使用Ob
  • 使用目录树和过滤填充 TTreeView

    在 Lazarus 0 9 28 2 项目上我有一个TTreeView 与名字DirTree在我的表格上 frmConvert 但我想用所有目录树填充它 因为C 像这样 C 目录树 http i imagehost org 0185 cdi
  • 卷曲 IP 地址

    我需要发送一个带有用户 IP 地址而不是服务器 IP 地址的卷曲请求 我尝试了这个但没有运气 curl setopt ch CURLOPT INTERFACE ip 有任何想法吗 好吧 所以没有办法安全地欺骗curl请求的ip地址 但我发现
  • 2 的幂公式帮助

    我知道 Java 中的 2 i i i 1 1 可以让我找到一个数字是否是 2 的幂 但是有人可以解释为什么这样做吗 2 i i i 1 1 基本上 如果i是 2 的幂 它将有一个1在其位模式中 如果从中减去 1 则该值的所有低位1有点变成
  • LinkedIn 验证用户身份验证令牌服务器端

    经过一天的研究 我以以下问题结束 我正在使用 ember js 构建一个 Web 应用程序 目前我正在使用 linkedin javascript sdk 实现 LinkedIn 登录 我遇到的问题是 在收到用户信息 令牌 电子邮件 名字等
  • 为什么 SSLSocketFactory 缺少 setEnabledCipherSuites?

    SSLSocketFactory提供getDefaultCipherSuites 默认情况下在套接字上启用的密码 和getSupportedCipherSuites 如果需要 可以启用密码 然而 SSLSocketFactory不提供set
  • 迭代 MultiDict 中的键和所有值

    我有一本字典 params ImmutableMultiDict dataStore tardis symbol 1 symbol 2 我希望能够迭代字典并获取所有值及其键的列表 但是 当我尝试这样做时 它只获取第一个符号键值对并忽略另一个
  • 选择更改事件 - Html.DropDownListFor

    我有两个下拉列表 从第一个值中选择的值加载另一个值 当控制器中有辅助方法时 我该如何做到这一点 using Html BeginForm div table width 100 cellpadding 0 cellspacing 0 tr
  • 添加 BouncyCastle 提供程序会破坏 KeyStore.load()

    我使用以下命令生成了密钥库 keytool genkeypair keystore test ks storetype pkcs12 然后我运行以下测试 base64 代表我创建的密钥库 private static final Strin
  • 实例关闭时的 Spring Cloud Gateway 500

    我有一个使用Spring Cloud Load Balancer Spring Cloud版本 Hoxton SR6 的Spring Cloud Gateway eureka客户端 应用程序 并且我有一个Spring Boot应用程序的实例