1.找規(guī)律
局部過濾器命名規(guī)則 XXXGatewayFilterFactory, 必須以GatewayFilterFactory結(jié)尾。文章來源:http://www.zghlxwxcb.cn/news/detail-861008.html
/* 注意名稱約定
* AddRequestHeaderGatewayFilterFactory 配置的時候?qū)懙氖?AddRequestHeader
* AddRequestParameterGatewayFilterFactory 配置的時候?qū)懙氖?AddRequestParameter
* LogTimeGatewayFilterFactory 配置的時候?qū)懯裁矗?
* */
2.接口耗時過濾器
package com.by.filter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.AddRequestHeaderGatewayFilterFactory;
import org.springframework.cloud.gateway.support.GatewayToStringStyler;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@Component
@Slf4j
public class LogTimeGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
public GatewayFilter apply(final AbstractNameValueGatewayFilterFactory.NameValueConfig config) {
return new GatewayFilter() {
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String value = ServerWebExchangeUtils.expand(exchange, config.getValue());
int times = Integer.parseInt(value);
long start = System.currentTimeMillis();
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
long end = System.currentTimeMillis();
long time = end - start;
if(time>times*1000){
log.info("請求耗時過長,耗時:{}",time);
}
}));
}
public String toString() {
return GatewayToStringStyler.filterToStringCreator(LogTimeGatewayFilterFactory.this).append(config.getName(), config.getValue()).toString();
}
};
}
}
3.如何使用
文章來源地址http://www.zghlxwxcb.cn/news/detail-861008.html
到了這里,關(guān)于GateWay具體的使用之局部過濾器接口耗時的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!