問題再現:?
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: trueroutes: - id: content-api # 路由id,自定義,只要唯一即可uri: lb://content-api # 路由的目標地址 lb就是負載均衡,后面跟服務名稱predicates: # 路由斷言,也就是判斷請求是否符合路由規則的條件- Path=/content/** # 這個是按照路徑匹配,只要以/content/開頭就符合要求- id: system-apiuri: lb://system-apipredicates:- Path=/system/**
#微服務配置
spring:application:name: gatewaycloud:nacos:server-addr: 192.168.101.65:8848discovery:namespace: ${spring.profiles.active}group: EducationPlatform-projectconfig:namespace: ${spring.profiles.active}group: EducationPlatform-projectfile-extension: yamlrefresh-enabled: trueshared-configs:- data-id: logging-${spring.profiles.active}.yamlgroup: EducationPlatform-commonrefresh: trueprofiles:active: dev11
而content-api的nacos上的配置和本地的配置分別為
server:servlet:context-path: /contentport: 63040test_config:a: 3ab: 3b#配置本地優先
spring:cloud:config:override-none: true
#微服務配置
spring:application:name: content-apicloud:nacos:server-addr: 192.168.101.65:8848discovery:namespace: ${spring.profiles.active}group: EducationPlatformusername: nacospassword: nacosconfig:namespace: ${spring.profiles.active}group: EducationPlatformfile-extension: yamlrefresh-enabled: trueextension-configs:- data-id: content-service-${spring.profiles.active}.yamlgroup: EducationPlatformrefresh: trueshared-configs:- data-id: swagger-${spring.profiles.active}.yamlgroup: EducationPlatform-commonrefresh: true- data-id: logging-${spring.profiles.active}.yamlgroup: EducationPlatform-commonrefresh: trueprofiles: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
重啟服務即可
?