1

我正在开发一个 spring-cloud-gateway 应用程序。我在哪里使用 RewritePath GatewayFilter 来处理一些pathvariable. 以下是我在端口 80上运行的下游 api 。

@GetMapping("/appname/event/{eventId}")
public Mono<ResponseEntity> getEventTimeOutWithPathVariable(
        @RequestHeader(name = "customerId") UUID customerId,
        @PathVariable(name = "eventId") String eventId) {
    Map<String, Object> map = new LinkedHashMap<>();
    map.put("customerId", customerId);
    map.put("eventId", eventId);
    return Mono.just(new ResponseEntity(map, HttpStatus.OK));
}

在我的网关应用程序中,过滤器配置如下:

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

但它不起作用。我做错了什么?我收到以下日志。

Mapping [Exchange: GET http://localhost:8000/withapitimeout/306ac5d0-b6d8-4f78-bde8-c470478ed1b1] 
to Route{id='api_timeout_route', uri=http://localhost:80/appname/event/

主要是路径变量没有被重写。有什么帮助吗?

4

1 回答 1

3

我不是专家,但您可以尝试以下方法:

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

让我知道 ;)

于 2018-06-05T11:12:05.087 回答