繁体   English   中英

spring boot api 不时返回 404 Not Found

[英]spring boot api return 404 Not Found time to time

大部分时间使用是正常的,偶尔会出现404。 我不知道如何定位问题。

控制器文件:

@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final AuthService authService;

    @GetMapping("info")
    public Result info(@RequestParam("token") String token) {
        Map<String, Object> stringObjectMap = authService.getInfo(token);

        return ResultGenerator.success(stringObjectMap);
    }
}

GET: localhost:9000/v1/auth/info?token=gNGLJLLZsluDsIQw 此错误消息不时显示:

{
    "timestamp": "2021-06-29T06:46:35.477+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/auth/info"
}

版本信息:

  • 弹簧靴:2.2.6.RELEASE
  • 春云:Hoxton.SR1

附加:

spring 云网关 yml 配置:

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      default-filters:
        - DedupeResponseHeader=Access-Control-Allow-Origin
      globalcors:
        cors-configurations:
          "[/**]":
            allowCredentials: true
            allowedOrigins: "*"
            allowedHeaders: "Origin, X-Requested-With, Content-Type, Accept, Content-Length, TOKEN, Authorization"
            allowedMethods: "GET, POST, PATCH, PUT, DELETE, OPTIONS"
            maxAge: 3628800
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
        - id: auth-service
          uri: lb://auth-service
          predicates:
            - Path=/v1/auth/**
          filters:
            - StripPrefix=1
        - id: bus-service
          uri: lb://bus-service
          predicates:
            - Path=/v1/**
          filters:
            - StripPrefix=1

待验证解决方案

我使用了 zipkin,发现路由 /v1/auth/info 与bus-service(/v1/**)匹配,因此返回 404 not found。

由此得出结论:路由的写入顺序并不能保证其匹配优先级。 所以必须添加order配置。

尝试删除@RequestMapping("/auth")并替换@GetMapping("info")如下所示:

@GetMapping("auth/info")
public Result info(@RequestParam("token") String token) {
    Map<String, Object> stringObjectMap = authService.getInfo(token);

请在这样的信息之前添加 /

 @GetMapping("/info")
    public Result info(@RequestParam("token") String token) {
        Map<String, Object> stringObjectMap = authService.getInfo(token);

        return ResultGenerator.success(stringObjectMap);
    }

由于您使用的是@RequestParam,您需要像这样传递值

本地主机:9000/auth/info?token="token_value"

请检查这个:

如果 url 具有 lb 方案(即 lb://myservice),它将使用 Spring Cloud LoadBalancerClient 将名称(上例中的 myservice)解析为实际主机和端口,并替换同一属性中的 URI。

默认情况下,当在 LoadBalancer 中找不到服务实例时,将返回 503。 您可以通过设置 spring.cloud.gateway.loadbalancer.use404=true 将网关配置为返回 404

您也可以尝试在路由列表中指定一个顺序

routes:
  - id: auth-service
    uri: lb://auth-service
    order: 0
    predicates:
      - Path=/v1/auth/**
    filters:
      - StripPrefix=1
  - id: bus-service
    uri: lb://bus-service
    order: 1
    predicates:
      - Path=/v1/**
    filters:
      - StripPrefix=1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM