当前位置:网站首页>自定义指定路由上的Gateway过滤器工厂
自定义指定路由上的Gateway过滤器工厂
2022-07-06 06:08:00 【雪峰.贵】
前言:核心API
一、需求:拦截请求,打印日志
1.加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
2.创建自定义过滤器 工厂
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
@Slf4j
@Component
//自定义过滤器工厂的名字要以GatewayFilterFactory结尾,只需在前面加个前缀,并把前缀配到配置文件里
public class PreLogGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
@Override
public GatewayFilter apply(NameValueConfig config) {
return ((exchange, chain) -> { //配置上写的参数用config.getName(),config.getValue()来拿 //config.getName()拿到的是a,config.getValue()拿到的是b log.info("请求进来了,参数:{},{}",config.getName(),config.getValue());
//Request.mutate后可以修改请求,需改后build()
ServerHttpRequest modifiedRequest = exchange.getRequest().mutate().build();
//把修改后的Request给Exchange
ServerWebExchange modifiedExchange = exchange.mutate().request(modifiedRequest).build();
//继续下一个过滤器
return chain.filter(modifiedExchange);
});
}
}
3.加配置
spring:
application:
name: gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true #让gateway在nacos上找微服务
routes:
- id: before_route
uri: lb://user-center #lb :nacos上的微服务
predicates:
- MyTimeBetween=上午9:00,下午11:00 #指定一个路由规则,这里的MyTimeBetween是自定义的,可以使用spring-cloud-gateway自带的。
filters:
- PreLog=a,b #写前缀即可,后面是传给工厂的参数
4.验证
通过gateway访问微服务,前提是请求路径需要满足routes的谓词,才会走到这个工厂里。
http://localhost:8040/users/1
边栏推荐
- How Huawei routers configure static routes
- P问题、NP问题、NPC问题、NP-hard问题详解
- J'ai un chaton.
- 请求转发与重定向
- The difference and usage between continue and break
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- 二维码的前世今生 与 六大测试点梳理
- Wib3.0 leapfrogging, in leapfrogging (ง • ̀_•́) ง
- Nodejs realizes the third-party login of Weibo
- Request forwarding and redirection
猜你喜欢
随机推荐
Coordinatorlayout+nestedscrollview+recyclerview pull up the bottom display is incomplete
Introduction to promql of # yyds dry goods inventory # Prometheus
【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
nodejs实现微博第三方登录
一文揭开,测试外包公司的真 相
Hypothesis testing learning notes
A complete collection of necessary learning websites for office programmers
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
黑猫带你学UFS协议第8篇:UFS初始化详解(Boot Operation)
Sqlmap tutorial (III) practical skills II
ICLR 2022 spotlight | analog transformer: time series anomaly detection method based on correlation difference
Huawei BFD configuration specification
数字三角形模型 AcWing 1015. 摘花生
LAN communication process in the same network segment
Function of activation function
Overview of three core areas of Mathematics: algebra
Web界面元素的测试
[untitled]
Seven imperceptible truths in software testing
Investment strategy discussion and market scale prediction report of China's solid state high power amplifier industry from 2022 to 2028