当前位置:网站首页>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
边栏推荐
- [wechat applet] build a development tool environment
- RestTemplate、Feign实现Token传递
- Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
- Function of activation function
- GTSAM中李群的运用
- 单元测试的意义
- Huawei BFD configuration specification
- 【无App Push 通用测试方案
- CoordinatorLayout+NestedScrollView+RecyclerView 上拉底部显示不全
- Thoughts on data security (Reprint)
猜你喜欢

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

How Huawei routers configure static routes

Significance of unit testing

Digital triangle model acwing 1015 Picking flowers

全程实现单点登录功能和请求被取消报错“cancelToken“ of undefined的解决方法

单元测试的意义

数据库隔离级别

【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能

Fault, error, failure of functional safety

win10无法操作(删除、剪切)文件
随机推荐
Web界面元素的测试
IDEA 新UI使用
Arrays and collections
[API interface tool] Introduction to postman interface
黑猫带你学UFS协议第8篇:UFS初始化详解(Boot Operation)
win10无法操作(删除、剪切)文件
GTSAM中李群的運用
单元测试的意义
[postman] test script writing and assertion details
Accélération de la lecture vidéo de l'entreprise
ESP32 ESP-IDF看门狗TWDT
Manhattan distance and Manhattan rectangle - print back font matrix
数据库-当前读与快照读
Cognitive introspection
LeetCode 731. 我的日程安排表 II
数据库隔离级别
使用Nacos管理配置
【eolink】PC客户端安装
[course notes] Compilation Principle
Properties file