当前位置:网站首页>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
边栏推荐
猜你喜欢
Buuctf-[[gwctf 2019] I have a database (xiaoyute detailed explanation)
Overview of three core areas of Mathematics: algebra
自定义指定路由上的Gateway过滤器工厂
误差的基本知识
Coordinatorlayout+nestedscrollview+recyclerview pull up the bottom display is incomplete
ContentType的作用
[untitled]
LeetCode 729. 我的日程安排表 I
How Huawei routers configure static routes
[course notes] Compilation Principle
随机推荐
JWT-JSON WEB TOKEN
误差的基本知识
假设检验学习笔记
Application du Groupe Li dans gtsam
Leaflet map
Usage of test macro of GTEST
LeetCode 732. 我的日程安排表 III
[web security] nodejs prototype chain pollution analysis
Arrays and collections
公司視頻加速播放
Understanding of processes and threads
(5) Explanation of yolo-v3 core source code (3)
F - True Liars (种类并查集+DP)
MySQL之基础知识
Luogu p1460 [usaco2.1] healthy Holstein cows
nodejs实现微博第三方登录
单元测试的意义
Function of activation function
Investment strategy discussion and market scale prediction report of China's solid state high power amplifier industry from 2022 to 2028
【C语言】qsort函数