当前位置:网站首页>Customize the gateway filter factory on the specified route
Customize the gateway filter factory on the specified route
2022-07-06 06:14:00 【Snow peak expensive】
List of articles
Preface : The core API
One 、 demand : Intercept request , Print log
1. Plus dependence
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
2. Create custom filters factory
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
// The name of the custom filter factory should start with GatewayFilterFactory ending , Just prefix it , And match the prefix to the configuration file
public class PreLogGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
@Override
public GatewayFilter apply(NameValueConfig config) {
return ((exchange, chain) -> { // The parameters written on the configuration are config.getName(),config.getValue() Come and get it //config.getName() Get is a,config.getValue() Get is b log.info(" The request came in , Parameters :{},{}",config.getName(),config.getValue());
//Request.mutate You can modify the request , After modification build()
ServerHttpRequest modifiedRequest = exchange.getRequest().mutate().build();
// Put the modified Request to Exchange
ServerWebExchange modifiedExchange = exchange.mutate().request(modifiedRequest).build();
// Continue to the next filter
return chain.filter(modifiedExchange);
});
}
}
3. Add configuration
spring:
application:
name: gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true # Give Way gateway stay nacos Find wechat
routes:
- id: before_route
uri: lb://user-center #lb :nacos Microservices on
predicates:
- MyTimeBetween= In the morning 9:00, Afternoon 11:00 # Specify a routing rule , there MyTimeBetween Is custom , have access to spring-cloud-gateway Self contained .
filters:
- PreLog=a,b # Just write a prefix , The following is the parameters passed to the factory
4. verification
adopt gateway Access to microservices , The premise is that the request path needs to meet routes Predicate of , Will walk into this factory .
http://localhost:8040/users/1
边栏推荐
猜你喜欢
随机推荐
2022 software testing workflow to know
isam2运行流程
A complete collection of necessary learning websites for office programmers
Summary of anomaly detection methods
【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
[C language] qsort function
Commodity price visualization
数学三大核心领域概述:代数
LeetCode 739. 每日温度
Web界面元素的测试
曼哈顿距离和-打印菱形
MySQL之基础知识
Nodejs realizes the third-party login of Weibo
B - The Suspects
Linux regularly backs up MySQL database
Understanding of processes and threads
Digital triangle model acwing 1015 Picking flowers
Accélération de la lecture vidéo de l'entreprise
【Postman】Collections-运行配置之导入数据文件
[postman] the monitors monitoring API can run periodically