我遇到了同样的问题。这就是我解决它的方法:
首先,这些是路线:
builder.routes()
.route( r -> r.path("/api/{appid}/users/{userid}")
.and().method(HttpMethod.GET)
.filters(f -> f.hystrix("myHystrixCommandName")
.setFallbackUri("forward:/hystrixfallback"))
.uri("/destination")
.id("route_1"));
路径谓词处理 url 并提取路径变量,这些变量保存到ServerWebExchange.getAttributes()
中,键定义为PathRoutePredicate.URL_PREDICATE_VARS_ATTR
orServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE
您可以在此处找到有关路径谓词的更多信息
其次,我创建了一个@RestController
来处理来自 Hystrix 的前向注入ServerWebExchange
:
@RestController
@RequestMapping("/hystrixfallback")
public class ServiceFallBack {
@RequestMapping(method = {RequestMethod.GET})
public String get(ServerWebExchange serverWebExchange) {
//Get the PathMatchInfo
PathPattern.PathMatchInfo pathMatchInfo = serverWebExchange.getAttribute(ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
//Get the template variables
Map<String, String> urlTemplateVariables = pathMatchInfo.getUriVariables();
//TODO Logic using path variables
return "result";
}
}