一 filter作用
1.1 filter搭建流程
1.1.1 网关配置
1.1.2 服务提供者
1.1.3 测试验证
1.启动consul
2.启动zipkin
3.启动应用微服务
4.进行访问: http://localhost:6666/pay/wg/filter
1.2 其他常见API
RemoveRequestHeader=sec-fetch-site # 删除请求头sec-fetch-site #- SetRequestHeader=sec-fetch-mode, Blue-updatebyzzyy # 将请求头sec-fetch-mode对应的值修改为Blue-updatebyzzyy #- AddRequestParameter=customerId,9527001 # 新增请求参数Parameter:k ,v
二 自定义网关统计接口耗时
2.1 编写统计耗时接口类
1.截图
2.具体代码:
package com.jurf.ms.gateway6666.config;import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;/*** @auther zzyy* @create 2023-12-31 21:05*/
@Component
@Slf4j
public class MyGlobalFilter implements GlobalFilter, Ordered
{public static final String BEGIN_VISIT_TIME = "begin_visit_time";//开始调用方法的时间@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain){//1 先记录下访问接口的开始时间exchange.getAttributes().put(BEGIN_VISIT_TIME,System.currentTimeMillis());//2 返回统计的各个结果给后台return chain.filter(exchange).then(Mono.fromRunnable(() -> {Long beginVisitTime = exchange.getAttribute(BEGIN_VISIT_TIME);if(beginVisitTime != null){log.info("访问接口主机:"+exchange.getRequest().getURI().getHost());log.info("访问接口端口:"+exchange.getRequest().getURI().getPort());log.info("访问接口URL:"+exchange.getRequest().getURI().getPath());log.info("访问接口URL后面参数:"+exchange.getRequest().getURI().getRawQuery());log.info("访问接口时长:"+(System.currentTimeMillis() - beginVisitTime) + "毫秒");log.info("============分割线==========================");System.out.println();}}));}/*** 数字越小,优先级越高* @return*/@Overridepublic int getOrder(){return 0;}
}
2.2 启动服务统计耗时
1.启动服务
2.请求地址:
http://localhost:8081/feign/pay/gateway/zdy/1?cityName=sz
3.耗时统计显示:
三 自定义网关过滤指定参数
2.1 编写过滤参数代码
package com.jurf.ms.gateway6666.config;import lombok.Getter;
import lombok.Setter;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;import java.util.Arrays;
import java.util.List;/*** @auther zzyy* @create 2023-12-31 21:41*/
@Component
public class ZdyGatewayFilterFactory extends AbstractGatewayFilterFactory<ZdyGatewayFilterFactory.Config>
{public ZdyGatewayFilterFactory(){super(Config.class);}@Overridepublic GatewayFilter apply(Config config){return new GatewayFilter(){@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain){ServerHttpRequest request = exchange.getRequest();System.out.println("进入了自定义网关过滤器MyGatewayFilterFactory,status:"+config.getStatus());if(request.getQueryParams().containsKey("guangzhou")){return chain.filter(exchange);}else{exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST);return exchange.getResponse().setComplete();}}};}@Overridepublic List<String> shortcutFieldOrder() {return Arrays.asList("status");}public static class Config{@Getter@Setterprivate String status;//设定一个状态值/标志位,它等于多少,匹配和才可以访问}
}
//单一内置过滤器GatewayFilter
2.2 配置
在filters的配置项,如下:
2.3 测试
1.请求地址:http://localhost:6666/pay/wg/filter?guangzhou=234
2.console控制台:status的值为gz,可以和请求参数的值guagnzhou=234 即gz和234 是否相等做一下业务逻辑判断的场景。