当前位置:网站首页>自定义指定路由上的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
边栏推荐
- 通过修改style设置打印页样式
- [leetcode] day96 - the first unique character & ransom letter & letter ectopic word
- LAN communication process in the same network segment
- As3013 fire endurance test of cable distribution system
- VINS-Mono: A Robust and Versatile Monocular Visual-Inertial State Estimator
- A complete collection of necessary learning websites for office programmers
- Buuctf-[gxyctf2019] no dolls (xiaoyute detailed explanation)
- 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
- Configuring OSPF GR features for Huawei devices
- Luogu p1460 [usaco2.1] healthy Holstein cows
猜你喜欢

LeetCode 731. 我的日程安排表 II

Gtest之TEST宏的用法

Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin

selenium源码通读·9 |DesiredCapabilities类分析

LeetCode 729. 我的日程安排表 I

Hongliao Technology: Liu qiangdong's "heavy hand"

Novice entry SCM must understand those things

MySQL之基础知识

Application of Lie group in gtsam

Usage of test macro of GTEST
随机推荐
Detailed explanation of BF and KMP
「 WEB测试工程师 」岗位一面总结
[API interface tool] Introduction to postman interface
ICLR 2022 spotlight | analog transformer: time series anomaly detection method based on correlation difference
进程和线程的理解
Function of activation function
【Postman】测试(Tests)脚本编写和断言详解
曼哈顿距离和-打印菱形
假设检验学习笔记
Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
异常检测方法总结
Baidu online AI competition - image processing challenge: the 8th program of handwriting erasure
IP day 16 VLAN MPLS configuration
Sqlmap tutorial (III) practical skills II
GTSAM中ISAM2和IncrementalFixedLagSmoother说明
Dynamic programming -- knapsack problem
Web界面元素的测试
[eolink] PC client installation
How Huawei routers configure static routes
Analysis report on development trends and investment planning of China's methanol industry from 2022 to 2028