当前位置:网站首页>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
边栏推荐
- 一文揭开,测试外包公司的真 相
- [postman] collections configuration running process
- How to recover Huawei router's forgotten password
- 【无App Push 通用测试方案
- Linux regularly backs up MySQL database
- [Thesis code] SML part code reading
- F - True Liars (种类并查集+DP)
- GTSAM中李群的運用
- 黑猫带你学UFS协议第8篇:UFS初始化详解(Boot Operation)
- A complete collection of necessary learning websites for office programmers
猜你喜欢

(5) Explanation of yolo-v3 core source code (3)

What are the test sites for tunnel engineering?

使用Nacos管理配置

(中)苹果有开源,但又怎样呢?

Usage of test macro of GTEST

【微信小程序】搭建开发工具环境

数据库-当前读与快照读

【C语言】字符串左旋

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

VINS-Mono: A Robust and Versatile Monocular Visual-Inertial State Estimator
随机推荐
(中)苹果有开源,但又怎样呢?
Thoughts on data security (Reprint)
Software test interview questions - Test Type
【微信小程序】搭建开发工具环境
在线问题与离线问题
win10无法操作(删除、剪切)文件
CoordinatorLayout+NestedScrollView+RecyclerView 上拉底部显示不全
Réflexions sur la sécurité des données (réimpression)
Gtest之TEST宏的用法
[ram IP] introduction and experiment of ram IP core
[Thesis code] SML part code reading
Usage of test macro of GTEST
Luogu p1460 [usaco2.1] healthy Holstein cows
Accélération de la lecture vidéo de l'entreprise
Linux regularly backs up MySQL database
【C语言】qsort函数
B - The Suspects
F - True Liars (种类并查集+DP)
联合索引的左匹配原则
【Postman】Collections配置运行过程