簡體   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