若依 ruoyi-cloud [网关异常处理]请求路径:/system/user/getInfo,异常信息:404

文章讲述了作者在使用Nacos时遇到的配置文件编码不一致问题,特别是当配置文件包含中文注释时,需确保IDEA启动项目时设置file.encoding为UTF-8,以避免因编码不同导致的解析错误。

这里遇到的情况是因为nacos中的配置文件与项目启动时的编码不一样,若配置文件中有中文注释,那么用idea启动项目的时候,在参数中加上 -Dfile.encoding=utf-8 ,保持编码一致,(用中文注释的配置文件,在项目启动的时候尽量都加上编码-Dfile.encoding=utf-8 )
在这里插入图片描述
注:每个人遇到的情况可能不一样,这是我遇到的做个记录,以便参考

15:25:40.658 [reactor-http-nio-2] INFO c.r.g.f.AuthFilter - [filter,62] - 请求路径/system/user/getInfo 15:25:41.482 [reactor-http-nio-2] INFO c.r.g.s.i.RemoteLogServiceImpl - [addRequestUrl,25] - {"msg":"操作成功","code":200} 15:25:41.677 [boundedElastic-2] WARN o.s.c.l.c.RoundRobinLoadBalancer - [getInstanceResponse,98] - No servers available for service: ruoyi-system 15:25:41.691 [boundedElastic-2] ERROR c.r.g.h.GatewayExceptionHandler - [handle,52] - [网关异常处理]请求路径:/system/user/getInfo,异常信息:503 SERVICE_UNAVAILABLE "Unable to find instance for ruoyi-system" 15:25:42.064 [reactor-http-nio-3] INFO c.r.g.f.AuthFilter - [filter,62] - 请求路径/system/user/getInfo 15:25:42.065 [reactor-http-nio-4] INFO c.r.g.f.AuthFilter - [filter,62] - 请求路径/auth/logout 15:25:42.171 [boundedElastic-2] WARN o.s.c.l.c.RoundRobinLoadBalancer - [getInstanceResponse,98] - No servers available for service: ruoyi-auth 15:25:42.171 [boundedElastic-2] ERROR c.r.g.h.GatewayExceptionHandler - [handle,52] - [网关异常处理]请求路径:/auth/logout,异常信息:503 SERVICE_UNAVAILABLE "Unable to find instance for ruoyi-auth" 15:25:42.180 [reactor-http-nio-3] INFO c.r.g.s.i.RemoteLogServiceImpl - [addRequestUrl,25] - {"msg":"操作成功","code":200} 15:25:42.182 [boundedElastic-3] WARN o.s.c.l.c.RoundRobinLoadBalancer - [getInstanceResponse,98] - No servers available for service: ruoyi-system 15:25:42.182 [boundedElastic-3] ERROR c.r.g.h.GatewayExceptionHandler - [handle,52] - [网关异常处理]请求路径:/system/user/getInfo,异常信息:503 SERVICE_UNAVAILABLE "Unable to find instance for ruoyi-system" 15:25:42.196 [reactor-http-nio-5] INFO c.r.g.f.AuthFilter - [filter,62] - 请求路径/auth/logout 15:25:42.198 [boundedElastic-3] WARN o.s.c.l.c.RoundRobinLoadBalancer - [getInstanceResponse,98] - No servers available for service: ruoyi-auth 15:25:42.199 [boundedElastic-3] ERROR c.r.g.h.GatewayExceptionHandler - [handle,52] - [网关异常处理]请求路径:/auth/logout,异常信息:503 SERVICE_UNAVAILABLE "Unable to find instance for ruoyi-auth" 15:28:15.993 [reactor-http-nio-6] INFO c.r.g.f.AuthFilter - [filter,62] - 请求路径/system/user/getInfo 15:28:16.054 [reactor-http-nio-6] INFO c.r.g.s.i.RemoteLogServiceImpl - [addRequestUrl,25] - {"msg":"操作成功","code":200} 15:28:16.057 [boundedElastic-2] WARN o.s.c.l.c.RoundRobinLoadBalancer - [getInstanceResponse,98] - No servers available for service: ruoyi-system 15:28:16.058 [boundedElastic-2] ERROR c.r.g.h.GatewayExceptionHandler - [handle,52] - [网关异常处理]请求路径:/system/user/getInfo,异常信息:503 SERVICE_UNAVAILABLE "Unable to find instance for ruoyi-system" 15:28:16.070 [reactor-http-nio-7] INFO c.r.g.f.AuthFilter - [filter,62] - 请求路径/system/user/getInfo 15:28:16.074 [reactor-http-nio-8] INFO c.r.g.f.AuthFilter - [filter,62] - 请求路径/auth/logout 15:28:16.075 [boundedElastic-2] WARN o.s.c.l.c.RoundRobinLoadBalancer - [getInstanceResponse,98] - No servers available for service: ruoyi-auth 15:28:16.076 [boundedElastic-2] ERROR c.r.g.h.GatewayExceptionHandler - [handle,52] - [网关异常处理]请求路径:/auth/logout,异常信息:503 SERVICE_UNAVAILABLE "Unable to find instance for ruoyi-auth" 15:28:16.144 [reactor-http-nio-7] INFO c.r.g.s.i.RemoteLogServiceImpl - [addRequestUrl,25] - {"msg":"操作成功","code":200} 15:28:16.147 [boundedElastic-2] WARN o.s.c.l.c.RoundRobinLoadBalancer - [getInstanceResponse,98] - No servers available for service: ruoyi-system 15:28:16.148 [boundedElastic-2] ERROR c.r.g.h.GatewayExceptionHandler - [handle,52] - [网关异常处理]请求路径:/system/user/getInfo,异常信息:503 SERVICE_UNAVAILABLE "Unable to find instance for ruoyi-system" 15:28:16.159 [reactor-http-nio-9] INFO c.r.g.f.AuthFilter - [filter,62] - 请求路径/auth/logout 15:28:16.161 [boundedElastic-2] WARN o.s.c.l.c.RoundRobinLoadBalancer - [getInstanceResponse,98] - No servers available for service: ruoyi-auth 15:28:16.161 [boundedElastic-2] ERROR c.r.g.h.GatewayExceptionHandler - [handle,52] - [网关异常处理]请求路径:/auth/logout,异常信息:503 SERVICE_UNAVAILABLE "Unable to find instance for ruoyi-auth" 15:29:23.914 [reactor-http-nio-10] ERROR c.r.g.h.GatewayExceptionHandler - [handle,52] - [网关异常处理]请求路径:/,异常信息:404 NOT_FOUND 15:29:23.971 [reactor-http-nio-10] ERROR c.r.g.h.GatewayExceptionHandler - [handle,52] - [网关异常处理]请求路径:/favicon.ico,异常信息:404 NOT_FOUND
最新发布
09-13
### 解决 503 SERVICE_UNAVAILABLE 错误 #### 检查服务注册与发现 确保 `ruoyi-system` 和 `ruoyi-auth` 服务已正确注册到服务注册中心,如 Nacos。可以查看服务注册中心的管理界面,确认这两个服务的实例是否存在且状态正常。同时,检查服务的配置文件,确保服务名称、注册中心地址等配置无误。示例配置如下: ```yaml spring: cloud: nacos: discovery: server-addr: 127.0.0.1:8848 application: name: ruoyi-system ``` #### 检查服务健康状态 确认 `ruoyi-system` 和 `ruoyi-auth` 服务本身处于健康状态,能够正常处理请求。可以通过服务的健康检查接口或日志来判断服务是否正常运行。 #### 检查负载均衡配置 Spring Cloud 2020 版本以后,默认移除了对 Netflix 的依赖,包括 Ribbon,官方推荐使用 Spring Cloud Loadbalancer。确保负载均衡配置正确,没有遗漏必要的依赖。可以在 `pom.xml` 中添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> ``` #### 检查路由配置 确保网关的路由配置中,指向 `ruoyi-system` 和 `ruoyi-auth` 的路由地址无误。示例配置如下: ```yaml spring: cloud: gateway: routes: - id: ruoyi-system-route uri: lb://ruoyi-system predicates: - Path=/system/** - id: ruoyi-auth-route uri: lb://ruoyi-auth predicates: - Path=/auth/** ``` ### 解决 404 NOT_FOUND 错误 #### 检查路由规则 确认网关的路由配置中包含了请求路径的规则,确保请求路径能够被正确匹配到相应的服务。 #### 检查服务接口 确保 `ruoyi-system` 和 `ruoyi-auth` 服务中存在处理请求路径的接口。可以查看服务的代码,确认接口的路径请求方法是否正确。 #### 自定义全局异常处理 可以自定义全局异常处理类,以便更好地处理 404 错误,并返回更友好的错误信息。示例代码如下: ```java import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; @Configuration public class GatewayExceptionHandlerConfig { @Bean public ErrorWebExceptionHandler customExceptionHandler() { return (exchange, ex) -> { DataBufferFactory bufferFactory = exchange.getResponse().bufferFactory(); if (ex instanceof org.springframework.web.server.ResponseStatusException && ((org.springframework.web.server.ResponseStatusException) ex).getStatus() == HttpStatus.NOT_FOUND) { exchange.getResponse().setStatusCode(HttpStatus.NOT_FOUND); exchange.getResponse().getHeaders().setContentType(MediaType.APPLICATION_JSON); String responseBody = "{\"message\": \"Resource not found\"}"; return exchange.getResponse().writeWith(Mono.just(bufferFactory.wrap(responseBody.getBytes()))); } return Mono.error(ex); }; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值