spring-cloud-gateway ||需要配置全局和应用程序级别以及api级别超时

时间:2018-06-05 12:46:26

标签: spring-boot spring-cloud spring-cloud-netflix spring-cloud-gateway

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

@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));
    }
}

此apis作为下游服务运行。以下是我gateway-application中所有这些api的路线配置:

# ============ 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

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

0 个答案:

没有答案