当前位置:网站首页>【微服务|SCG】Filters的33种用法
【微服务|SCG】Filters的33种用法
2022-07-05 02:29:00 【步尔斯特】
文章目录
增加请求头/请求参数/响应头
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: ossa-service-producer
uri: lb://ossa-service-producer
predicates:
- Path=/gw/producer/**
- After=2022-03-03T15:50:30.337+08:00[Asia/Shanghai]
- Before=2024-03-03T15:50:30.337+08:00[Asia/Shanghai]
filters:
- StripPrefix=1
# 增加请求头、请求参数
- AddRequestHeader=origin-header,producer-header
- AddRequestParameter= origin-param,producer-param
- AddResponseHeader=rep-header,rep-header
新增动态header请求头
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: ossa-service-producer
uri: lb://ossa-service-producer
predicates:
- Path=/gw/producer/{
id}
- After=2022-03-03T15:50:30.337+08:00[Asia/Shanghai]
- Before=2024-03-03T15:50:30.337+08:00[Asia/Shanghai]
filters:
- StripPrefix=1
# 增加请求头、请求参数
- AddRequestHeader=origin-header,producer-header-{
id}
设置请求头、过滤器配置,修改移除请求头
filters:
- StripPrefix=1
# 增加请求头、请求参数
- AddRequestHeader=origin-header,producer-header-{
id}
- AddRequestParameter=origin-param,producer-param
- AddResponseHeader=rep-header,rep-header
# 设置请求头,没有则新增,有则修改
- SetRequestHeader=X-Request-id,value
# 移除请求头、请求参数
- RemoveRequestHeader=X-Request-id
- RemoveRequestParameter=X-Request
- RemoveRequestParameter=X-Request-author
- AddResponseHeader=req-param,req-param
# 先从header取from-Header,如果有则赋值给to-Header,如果没from-Header则无效果
- MapRequestHeader=from-Header,to-Header
PreserveHostHeader
默认开启,在gateway转发请求前把原始请求的host头部带上,转发给目的服务。
spring:
cloud:
gateway:
routes:
- id: preserve_host_route
uri: https://example.org
filters:
- PreserveHostHeader
重写response加密密码
#例如: /42?user=ford&passwd=omg!what&flag=true 经过下面配置替换为/42?user=ford&passwd=***&flag=true
spring:
cloud:
gateway:
routes:
- id: rewriteresponseheader_route
uri: https://example.org
filters:
- RewriteResponseHeader=X-Response-Red, , password=[^&]+, password=***
response去重
RETAIN_FIRST保留第一个(default)
RETAIN_LAST保留最后一个
RETAIN_FIRST RETAIN_UNIQUE 保留唯一
spring:
cloud:
gateway:
routes:
- id: dedupe_response_header_route
uri: https://example.org
filters:
- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
路径带前缀
请求/ 其实是/mypath
spring:
cloud:
gateway:
routes:
- id: prefixpath_route
uri: https://example.org
filters:
- PrefixPath=/mypath
配置30几 跳转到指定地址
#300 Multiple多种选择,请求的资源可能包括多个位置,相应可返回一个资源特征与地址的列表用于用户终端选择
#301 Moved Permanently永久移动,请求的资源已被永久移动新位置,返回信息会包含URI,浏览器会自动定向到新地址,以后请求应该用新的URI代替
#302 Found 临时移动,与301类似,资源临时被移动,客户端应该继续使用原URI
#303 See Other查看其它地址,与301类似。使用GET和POST请求查看
#304 未修改,所请求资源未修改,服务器返回此状态吗时,不会返回任何资源
#305 Use proxy 使用代理,所请求资源必须通过代理
#306 Unused 已经废弃的HTTP状态码
#307 Temporary Redirect 临时重定向,与302类似,使用GET请求重定向
- RedirectTo=302,http://www.baidu.com
- RedirectTo=301,http://www.taobao.com
修改状态码
修改状态码,可以org.springframework.http.HttpStatus
枚举,也可以直接写状态码
无论哪种情况,响应的 HTTP 状态都设置为 401。
spring:
cloud:
gateway:
routes:
- id: setstatusstring_route
uri: https://example.org
filters:
- SetStatus=UNAUTHORIZED
- id: setstatusint_route
uri: https://example.org
filters:
- SetStatus=401
转发地址
访问/api/getheader 实际访问:/getheader,转发的目的地址必须写 / 否则会出错
- RewritePath=/consumingServiceEndpoint, /backingServiceEndpoint
spring:
cloud:
gateway:
routes:
- id: rewritepath_route
uri: https://example.org
predicates:
- Path=/red/**
filters:
- RewritePath=/red/?(?<segment>.*), /$\{
segment}
去掉\增加请求路径中部分层级
请求/name/first/123 实际请求/123
spring:
cloud:
gateway:
routes:
- id: nameRoot
uri: https://nameservice
predicates:
- Path=/name/first/**
filters:
# 从前往后去掉一些路径
- StripPrefix=2
请求/name/first/123 实际请求/prefix/name/first/123
spring:
cloud:
gateway:
routes:
- id: nameRoot
uri: https://nameservice
predicates:
- Path=/name/first/**
filters:
# 增加前缀路径
- PrefixPath=/prefix
重试过滤器
spring:
cloud:
gateway:
routes:
- id: retry_test
uri: http://localhost:8080/flakey
predicates:
- Host=*.retry.com
filters:
- name: Retry
args:
retries: 3
statuses: BAD_GATEWAY
methods: GET,POST
backoff:
firstBackoff: 10ms
maxBackoff: 50ms
factor: 2
basedOnPreviousValue: false
过滤器设置请求大小
filters:
- name: RequestSize #过滤器名称
args: #控制请求的大小,超过则返回413.请求最大默认5000000 约5M
maxSize:1000 #1KB
spring-session
只有当集成Spring Session才会将session放到Redis,来实现共享Session功能,如果项目继承了Spring Session中的Spring Security框架,如果希望安全验证信息可以转发到远程应用,那么这个配置是必须的。
filters:
- SaveSession
默认filter
spring:
cloud:
gateway:
default-filters:
- AddResponseHeader=X-Response-Default-Red, Default-Blue
- PrefixPath=/httpbin
边栏推荐
- LeetCode --- 1071. Great common divisor of strings problem solving Report
- Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
- openresty ngx_ Lua execution phase
- Advanced conditional statements of common SQL operations
- Go RPC call
- 【LeetCode】501. Mode in binary search tree (2 wrong questions)
- Marubeni Baidu applet detailed configuration tutorial, approved.
- 【LeetCode】111. Minimum depth of binary tree (2 brushes of wrong questions)
- When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?
- Learn tla+ (XII) -- functions through examples
猜你喜欢
Codeforces Global Round 19 ABC
【LeetCode】501. Mode in binary search tree (2 wrong questions)
[Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation
A label making navigation bar
Application and Optimization Practice of redis in vivo push platform
[download white paper] does your customer relationship management (CRM) really "manage" customers?
【LeetCode】110. Balanced binary tree (2 brushes of wrong questions)
Design and practice of kubernetes cluster and application monitoring scheme
ELFK部署
Learn game model 3D characters, come out to find a job?
随机推荐
Introduce reflow & repaint, and how to optimize it?
【LeetCode】106. Construct binary tree from middle order and post order traversal sequence (wrong question 2)
GFS分布式文件系统
Richview trvunits image display units
[机缘参悟-38]:鬼谷子-第五飞箝篇 - 警示之一:有一种杀称为“捧杀”
d3js小记
如何搭建一支搞垮公司的技術團隊?
RichView TRVStyle MainRVStyle
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
He was laid off.. 39 year old Ali P9, saved 150million
Go RPC call
Yolov5 model training and detection
Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down
[download white paper] does your customer relationship management (CRM) really "manage" customers?
Android advanced interview question record in 2022
openresty ngx_lua变量操作
Practical case of SQL optimization: speed up your database
[illumination du destin - 38]: Ghost Valley - chapitre 5 Flying clamp - one of the Warnings: There is a kind of killing called "hold Kill"
Luo Gu Pardon prisoners of war
Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘