1

我正在一个spring-cloud-gateway项目中工作。我需要配置全局超时/应用程序级超时和特定于 api 的超时。以下是我的下游 API:

@RestController
public class TestController {

    // Should have global time out
    @GetMapping("/global")
    public Mono<ResponseEntity> testGlobalTimeOut(
            @RequestHeader(name = "cId") UUID cId,
            @RequestParam(name = "someNumber", required = false) Number someNumber) {

        // Map<String, Object> map = populate Some Map Logic
        return Mono.just(new ResponseEntity(map, HttpStatus.OK));
    }

    // Should have application level time out
    @GetMapping("/appname/count")
    public Mono<ResponseEntity> testApplicationTimeOut_1(
            @RequestHeader(name = "cId") UUID cId,
            @RequestParam(name = "someNumber", required = false) Number someNumber) {

        // Map<String, Object> map = populate Some Map Logic
        return Mono.just(new ResponseEntity(map, HttpStatus.OK));
    }

    // Should have application level time out
    @GetMapping("/appname/posts")
    public Mono<ResponseEntity> testApplicationTimeOut_2(
            @RequestHeader(name = "cId") UUID cId,
            @RequestParam(name = "someNumber", required = false) Number someNumber) {

        // Map<String, Object> map = populate Some Map Logic
        return Mono.just(new ResponseEntity(map, HttpStatus.OK));
    }

    // Should have api level time out
    @GetMapping("/appname/posts/{postId}")
    public Mono<ResponseEntity> getAPITimeOutWithPathVariable(
            @RequestHeader(name = "cId") UUID cId,
            @PathVariable(name = "postId") String postId) {
        // Map<String, Object> map = populate Some Map Logic
        return Mono.just(new ResponseEntity(map, HttpStatus.OK));
    }
}

此 API 作为下游服务运行。现在以下是我在我的所有这些 API 的路由配置gateway-application

# ============ Application Timeout =============      
- id: application_timeout_route_1
  uri: http://localhost/appname/count
  predicates:
  - Path=/withapplicationtimeout1**
  filters:
  - Hystrix=appTimeOut

- id: application_timeout_route_2
  uri: http://localhost/appname/posts
  predicates:
  - Path=/withapplicationtimeout2**
  filters:
  - Hystrix=appTimeOut

# ============ API Level Timeout ===========
- id: api_timeout_route
  uri: http://localhost
  predicates:
  - Path=/withapitimeout/**
  filters:
  - Hystrix=apiTimeOut 
  - RewritePath=/withapitimeout/(?<segment>.*), /appname/posts/$\{segment}


# Global Timeout Configuration    
#hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 30000 
# Application Level Timeout Configuration 
hystrix.command.appTimeOut.execution.isolation.thread.timeoutInMilliseconds: 30000
# API Level Timeout Configuration 
hystrix.command.apiTimeOut.execution.isolation.thread.timeoutInMilliseconds: 15000

现在应用程序级别超时和 api 级别超时工作正常,但我没有任何方法来定义全局超时过滤器。相同的文档尚不可用:

https://github.com/spring-cloud/spring-cloud-gateway/blob/master/docs/src/main/asciidoc/spring-cloud-gateway.adoc#combined-global-filter-and-gatewayfilter-ordering

任何想法如何做到这一点?

4

0 回答 0