当前位置:网站首页>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
边栏推荐
- 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
- Idea new UI usage
- Overview of three core areas of Mathematics: algebra
- 【C语言】qsort函数
- The latest 2022 review of "graph classification research"
- 职场进阶指南:大厂人必看书籍推荐
- 一文揭开,测试外包公司的真 相
- 全链路压测:构建三大模型
- Linux regularly backs up MySQL database
- Significance of unit testing
猜你喜欢
单元测试的意义
GTSAM中李群的运用
Properties file
IP day 16 VLAN MPLS configuration
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
G - Supermarket
Sqlmap tutorial (III) practical skills II
10m25dcf484c8g (FPGA) amy-6m-0002 BGA GPS module
Application du Groupe Li dans gtsam
请求转发与重定向
随机推荐
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协议第18篇:UFS如何配置逻辑单元(LU Management)
【Postman】Collections-运行配置之导入数据文件
Arrays and collections
Web界面元素的测试
Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
ESP32 ESP-IDF看门狗TWDT
Properties file
selenium源码通读·9 |DesiredCapabilities类分析
公司视频加速播放
Luogu p1460 [usaco2.1] healthy Holstein cows
Pat (Grade B) 2022 summer exam
[leetcode] day96 - the first unique character & ransom letter & letter ectopic word
[postman] collections - run the imported data file of the configuration
黑猫带你学UFS协议第8篇:UFS初始化详解(Boot Operation)
(5) Explanation of yolo-v3 core source code (3)
HCIA review
[untitled]
请求转发与重定向
对数据安全的思考(转载)