当前位置:网站首页>【微服务|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
边栏推荐
- Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
- 低度酒赛道进入洗牌期,新品牌如何破局三大难题?
- Data guard -- theoretical explanation (III)
- . Net starts again happy 20th birthday
- Prometheus monitors the correct posture of redis cluster
- Application and Optimization Practice of redis in vivo push platform
- 【LeetCode】106. Construct binary tree from middle order and post order traversal sequence (wrong question 2)
- Visual explanation of Newton iteration method
- 【LeetCode】110. Balanced binary tree (2 brushes of wrong questions)
- ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
猜你喜欢
【LeetCode】222. The number of nodes of a complete binary tree (2 mistakes)
Unpool(nn.MaxUnpool2d)
Binary tree traversal - middle order traversal (golang)
Runc hang causes the kubernetes node notready
Practical case of SQL optimization: speed up your database
The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns
Zabbix
spoon插入更新oracle数据库,插了一部分提示报错Assertion botch: negative time
Three properties that a good homomorphic encryption should satisfy
A tab Sina navigation bar
随机推荐
[uc/os-iii] chapter 1.2.3.4 understanding RTOS
Open source SPL optimized report application coping endlessly
A label colorful navigation bar
Visual explanation of Newton iteration method
The MySQL team development specifications used by various factories are too detailed. It is recommended to collect them!
【附源码】基于知识图谱的智能推荐系统-Sylvie小兔
85.4% mIOU! NVIDIA: using multi-scale attention for semantic segmentation, the code is open source!
Practical case of SQL optimization: speed up your database
Use the difference between "Chmod a + X" and "Chmod 755" [closed] - difference between using "Chmod a + X" and "Chmod 755" [closed]
Yuan universe also "real estate"? Multiple second-hand trading websites block metauniverse keywords
Uniapp navigateto jump failure
How to make a cool ink screen electronic clock?
The phenomenology of crypto world: Pioneer entropy
Chinese natural language processing, medical, legal and other public data sets, sorting and sharing
Design and practice of kubernetes cluster and application monitoring scheme
Naacl 2021 | contrastive learning sweeping text clustering task
100 basic multiple choice questions of C language (with answers) 04
runc hang 导致 Kubernetes 节点 NotReady
【LeetCode】111. Minimum depth of binary tree (2 brushes of wrong questions)
172. Zero after factorial