Nacos和GateWay路由转发NotFoundException: 503 SERVICE_UNAVAILABLE “Unable to find

本文涉及的产品
传统型负载均衡 CLB,每月750个小时 15LCU
应用型负载均衡 ALB,每月750个小时 15LCU
网络型负载均衡 NLB,每月750个小时 15LCU
简介: Nacos和GateWay路由转发NotFoundException: 503 SERVICE_UNAVAILABLE “Unable to find

问题再现:

2023-08-15 16:51:16,151 DEBUG [reactor-http-nio-2][CompositeLog.java:147] - [dc73b32c-1] Encoding [{timestamp=Tue Aug 15 16:51:16 CST 2023, path=/content/course/list, status=503, error=Service Unavai (truncated)...]

2023-08-15 16:51:16,175 TRACE [reactor-http-nio-2][GatewayMetricsFilter.java:115] - gateway.requests tags: [tag(httpMethod=POST),tag(httpStatusCode=503),tag(outcome=SERVER_ERROR),tag(routeId=content-api),tag(routeUri=lb://content-api),tag(status=SERVICE_UNAVAILABLE)]

2023-08-15 16:51:16,187 DEBUG [reactor-http-nio-2][Loggers.java:254] - [id: 0xdc73b32c, L:/127.0.0.1:63010 - R:/127.0.0.1:53113] Decreasing pending responses, now 0

2023-08-15 16:51:16,190 DEBUG [reactor-http-nio-2][Loggers.java:249] - [id: 0xdc73b32c, L:/127.0.0.1:63010 - R:/127.0.0.1:53113] Last HTTP packet was sent, terminating the channel

2023-08-15 16:51:16,192 DEBUG [reactor-http-nio-2][LogFormatUtils.java:91] - [dc73b32c-1] Completed 503 SERVICE_UNAVAILABLE

2023-08-15 16:51:16,195 DEBUG [reactor-http-nio-2][Loggers.java:249] - [id: 0xdc73b32c, L:/127.0.0.1:63010 - R:/127.0.0.1:53113] Last HTTP response frame

2023-08-15 16:51:16,203 DEBUG [reactor-http-nio-2][Loggers.java:254] - [id: 0xdc73b32c, L:/127.0.0.1:63010 - R:/127.0.0.1:53113] FluxReceive{pending=0, cancelled=true, inboundDone=false, inboundError=null}: dropping frame DefaultLastHttpContent(data: PooledSlicedByteBuf(ridx: 0, widx: 53, cap: 53/53, unwrapped: PooledUnsafeDirectByteBuf(ridx: 296, widx: 296, cap: 2048)), decoderResult: success)

2023-08-15 16:51:16,205 DEBUG [reactor-http-nio-2][Loggers.java:259] - [id: 0xdc73b32c, L:/127.0.0.1:63010 - R:/127.0.0.1:53113] onUncaughtException(SimpleConnection{channel=[id: 0xdc73b32c, L:/127.0.0.1:63010 - R:/127.0.0.1:53113]})

java.io.IOException: 远程主机强迫关闭了一个现有的连接。

   at sun.nio.ch.SocketDispatcher.read0(Native Method)

   at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)

   at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)

   at sun.nio.ch.IOUtil.read(IOUtil.java:192)

   at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:379)

   at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:253)

   at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1134)

   at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:350)

   at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:151)

   at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719)

   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:989)

   at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)

   at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)

   at java.lang.Thread.run(Thread.java:750)

2023-08-15 16:51:16,432 DEBUG [com.alibaba.nacos.naming.failover][FailoverReactor.java:147] - failover switch is not found, failover00-00---000-VIPSRV_FAILOVER_SWITCH-000---00-00

2023-08-15 16:51:17,002 DEBUG [PollingServerListUpdater-0][PropertySourcesPropertyResolver.java:115] - Found key 'spring.profiles.active' in PropertySource 'configurationProperties' with value of type String

2023-08-15 16:51:17,003 DEBUG [PollingServerListUpdater-0][HostReactor.java:306] - failover-mode: false

2023-08-15 16:51:17,003 DEBUG [PollingServerListUpdater-0][DynamicServerListLoadBalancer.java:241] - List of Servers for content-api obtained from Discovery client: []

2023-08-15 16:51:17,003 DEBUG [PollingServerListUpdater-0][DynamicServerListLoadBalancer.java:246] - Filtered List of Servers for content-api obtained from Discovery client: []

问题前提的环境配置:

首先我的nacos上都已经成功注册上了两个服务

其中Gateway的nacos上的配置和本地的配置分别为

server:
  port: 63010 # 网关端口
spring:
  cloud:
    gateway:
      discovery:
        locator:
          lowerCaseServiceId: true
          # 让gateway根据注册中心找到其他服务
          enabled: true
      routes: 
        - id: content-api # 路由id,自定义,只要唯一即可
          uri: lb://content-api # 路由的目标地址 lb就是负载均衡,后面跟服务名称
          predicates: # 路由断言,也就是判断请求是否符合路由规则的条件
            - Path=/content/** # 这个是按照路径匹配,只要以/content/开头就符合要求
        - id: system-api
          uri: lb://system-api
          predicates:
            - Path=/system/**
#微服务配置
spring:
  application:
    name: gateway
  cloud:
    nacos:
      server-addr: 192.168.101.65:8848
      discovery:
        namespace: ${spring.profiles.active}
        group: EducationPlatform-project
      config:
        namespace: ${spring.profiles.active}
        group: EducationPlatform-project
        file-extension: yaml
        refresh-enabled: true
        shared-configs:
          - data-id: logging-${spring.profiles.active}.yaml
            group: EducationPlatform-common
            refresh: true
  profiles:
    active: dev11

而content-api的nacos上的配置和本地的配置分别为

server:
  servlet:
    context-path: /content
  port: 63040
test_config:
 a: 3a
 b: 3b
#配置本地优先
spring:
 cloud:
  config:
    override-none: true
#微服务配置
spring:
  application:
    name: content-api
  cloud:
    nacos:
      server-addr: 192.168.101.65:8848
      discovery:
        namespace: ${spring.profiles.active}
        group: EducationPlatform
        username: nacos
        password: nacos
      config:
        namespace: ${spring.profiles.active}
        group: EducationPlatform
        file-extension: yaml
        refresh-enabled: true
        extension-configs:
          - data-id: content-service-${spring.profiles.active}.yaml
            group: EducationPlatform
            refresh: true
        shared-configs:
          - data-id: swagger-${spring.profiles.active}.yaml
            group: EducationPlatform-common
            refresh: true
          - data-id: logging-${spring.profiles.active}.yaml
            group: EducationPlatform-common
            refresh: true
  profiles:
    active: dev11

而发送了一个这个请求

问题解决:

首先可以在idea里快速的按两下shift,然后搜这个方法名,打个断点看看方法能不能送到这里来,可以看到我这的uri已经是有了的,证明配置和服务都没有问题,问题缩小到路由转发出了问题。

其次这两个问题报错也让我们要注意的问题就是转发问题

Zone aware logic disabled or there is only one zone

NotFoundException: 503 SERVICE_UNAVAILABLE "Unable to find instance for content-api

一、pom配置

因为路由转发配置上用到了lb:,这个是负载均衡的标志,而在Spring Cloud 2020版本以后,就移除了Ribbon负载均衡器,官方默认推荐使用Spring Cloud Loadbalancer正式替换Ribbon。因此我们需要把Spring Cloud Loadbalancer加载到pom里面

<!--客户端负载均衡loadbalancer-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
            <optional>true</optional>
        </dependency>

实在不行的再加载这两个

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

二、小细节

我是加上了pom都还不行的,而后面发现我的group是不同的,在nacos中group的作用就是区分不同类型的配置文件,进行分组管理

相同Data Id而不同Group的意义就在相当于相同的目录下不同的文件,不同group直接的服务调用是只能通过额外config里配置

因此解决办法就是把nacos中的group的后缀都改成-project,本地配置中的也改了然后实在不行的话Gateway配置上再添加上lowerCaseServiceId: true

重启服务即可

目录
相关文章
|
21天前
|
存储 Java Nacos
学成在线笔记+踩坑(4)——【媒资管理模块】上传图片,Nacos+Gateway+MinIO
媒资管理模块简介、MinIO构建分布式文件系统、MinIO数据恢复演示 、【媒资模块】上传图片
学成在线笔记+踩坑(4)——【媒资管理模块】上传图片,Nacos+Gateway+MinIO
|
22天前
|
SpringCloudAlibaba JavaScript 前端开发
谷粒商城笔记+踩坑(2)——分布式组件、前端基础,nacos+feign+gateway+ES6+vue脚手架
分布式组件、nacos注册配置中心、openfegin远程调用、网关gateway、ES6脚本语言规范、vue、elementUI
谷粒商城笔记+踩坑(2)——分布式组件、前端基础,nacos+feign+gateway+ES6+vue脚手架
|
22天前
|
负载均衡 Java Nacos
SpringCloud基础2——Nacos配置、Feign、Gateway
nacos配置管理、Feign远程调用、Gateway服务网关
SpringCloud基础2——Nacos配置、Feign、Gateway
|
2月前
|
JSON Nacos 开发工具
微服务通过nacos实现动态路由
微服务通过nacos实现动态路由
58 7
|
3月前
|
负载均衡 Java Spring
Spring cloud gateway 如何在路由时进行负载均衡
Spring cloud gateway 如何在路由时进行负载均衡
328 15
|
3月前
|
Kubernetes 监控 Java
有了k8s还需要gateway网关,nacos配置中心吗
在Kubernetes环境中,服务网关(如Spring Cloud Gateway)和Nacos配置中心补充了k8s的不足。Nacos提供灵活服务路由和动态配置更新,超越k8s基础服务发现。它还支持更复杂的配置管理和实时推送,以及环境隔离和版本控制。作为服务注册中心,Nacos增强k8s服务治理能力,保持技术一致性,并提供额外的安全层及监控功能。
127 0
|
4月前
|
Java Nacos 网络架构
Spring Cloud gateway 网关四 动态路由
Spring Cloud gateway 网关四 动态路由
|
4月前
|
微服务
springCloud之路由网关gateway
springCloud之路由网关gateway
|
4月前
|
Java Nacos Maven
Sentinel1.8.6更改配置同步到nacos(项目是Gateway)
Sentinel1.8.6 nacos springcloud springcloud-alibaba gateway
182 0
|
5月前
|
Java Nacos 网络架构
SpringCloud Gateway的使用 + Nacos动态路由
SpringCloud Gateway的使用 + Nacos动态路由

热门文章

最新文章

下一篇
无影云桌面