阅读HystrixGatewayFilterFactory的apply方法百思不得姐 后来自己写了一遍 终于弄明白了 这个是lambda的一种高级应用 特留书于此 做个记录
首先该方法的返回值是GatewayFilter 一个接口 返回值却如下
return (exchange, chain) -> { RouteHystrixCommand command = new RouteHystrixCommand(config.setter, config.fallbackUri, exchange, chain); return Mono.create(s -> { Subscription sub = command.toObservable().subscribe(s::success, s::error, s::success); s.onCancel(sub::unsubscribe); }).onErrorResume((Function<Throwable, Mono<Void>>) throwable -> { if (throwable instanceof HystrixRuntimeException) { HystrixRuntimeException e = (HystrixRuntimeException) throwable; if (e.getFailureType() == TIMEOUT) { //TODO: optionally set status setResponseStatus(exchange, HttpStatus.GATEWAY_TIMEOUT); return exchange.getResponse().setComplete(); } } return Mono.empty(); }).then(); };
怎么看也不是GatewayFilter对象 之后弄明白了
是使用了一个内部类 直接实现了GatewayFilter接口 其中 有个方法如下
Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain);
返回值是Mono 所以在{}中实现了此方法 但是只有接口中单个方法的时候可以用
以后试一下接口中多个方法的时候 如何使用lambda表达式