• SpringCloudGateway--过滤器(内置filter)


    目录

    一、概览

    二、内置过滤器

    1、StripPrefix

    2、AddRequestHeader

    3、AddResponseHeader

    4、DedupeResponseHeader

    5、AddRequestParameter

    6、CircuitBreaker

    7、FallbackHeaders

    8、RequestRateLimiter

    9、RedirectTo

    10、RemoveRequestHeader

    11、RemoveResponseHeader

    12、RemoveRequestParameter

    13、RewritePath 

    14、RewriteResponseHeader 

    15、SaveSession

    16、SecureHeaders

    17、SetPath

    18、SetRequestHeader

    19、SetResponseHeader

    20、SetStatus

    21、PrefixPath

    22、Retry

    23、RequestSize

    24、ModifyRequestBody

    25、ModifyResponseBody

    26、MapRequestHeader

    27、PreserveHostHeader

    28、RewriteLocationResponseHeader

    29、SetRequestHostHeader


    一、概览

            SpringGateway的过滤器分为内置过滤器Filter与自定义过滤器GlobalFilter。

            内置Filter都实现GatewayFilter接口。使用时filter

    s属性中过滤器名为XXXGatewayFilterFactory的类对应的名称为XXX内置Filter都实现GatewayFilter接口。使用时filters属性中过滤器名为XXXGatewayFilterFactory的类对应的名称为XXX。其中内置过滤器实现类如下:

            同理在SpringCloudGateway中可以找到加载这些实现类的工厂方法: 

    二、内置过滤器

             内置Filter是使用工厂模式加匿名内部类实现的。 

       

             所有的Filter最终一定要调用chain.filter()方法,代表向下执行,在这句话之前调用的逻辑,是微服务的前置过滤,在这之后的都是远程微服务调用的后置过滤。

            所有内置过滤器中,常用的四种已经标红,重点演示前四种。SpringCloudGateWay项目参考:SpringCloudGateway--自动路由映射与手动路由映射_雨欲语的博客-CSDN博客

    1、StripPrefix

             StripPrefix是最常用的内置过滤器,含义是:过滤转发地址前缀, 也就是过滤掉url中前几节,然后转发给下游,比如以下配置:

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. metadata:
    9. connect-timeout: 15000 #ms
    10. response-timeout: 15000 #ms

            当我们访问http://localhost:9999/service/nacos/test时,谓词校验service,uri的lb是service-one,此时转发地址是 http://service/service-one/nacos/test,然后后面有过滤器StripPrefix,表示删除第一节service,因此最终转发地址是http://service-one/nacos/test。

            我们看一下源码中的处理方式:

    1. import java.util.Arrays;
    2. import java.util.List;
    3. import org.springframework.cloud.gateway.filter.GatewayFilter;
    4. import org.springframework.cloud.gateway.filter.GatewayFilterChain;
    5. import org.springframework.cloud.gateway.support.GatewayToStringStyler;
    6. import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
    7. import org.springframework.http.server.reactive.ServerHttpRequest;
    8. import org.springframework.util.StringUtils;
    9. import org.springframework.web.server.ServerWebExchange;
    10. import reactor.core.publisher.Mono;
    11. public class StripPrefixGatewayFilterFactory extends AbstractGatewayFilterFactory {
    12. public static final String PARTS_KEY = "parts";
    13. public StripPrefixGatewayFilterFactory() {
    14. super(StripPrefixGatewayFilterFactory.Config.class);
    15. }
    16. public List shortcutFieldOrder() {
    17. return Arrays.asList("parts");
    18. }
    19. public GatewayFilter apply(StripPrefixGatewayFilterFactory.Config config) {
    20. return new GatewayFilter() {
    21. public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    22. ServerHttpRequest request = exchange.getRequest();
    23. ServerWebExchangeUtils.addOriginalRequestUrl(exchange, request.getURI());
    24. // 获取url的path
    25. String path = request.getURI().getRawPath();
    26. // 将url以/进行分割成字符串数组
    27. String[] originalParts = StringUtils.tokenizeToStringArray(path, "/");
    28. StringBuilder newPath = new StringBuilder("/");
    29. for(int i = 0; i < originalParts.length; ++i) {
    30. // 如果当前索引下标大于配置,则添加到newPath中,否则相当于直接跳过
    31. if (i >= config.getParts()) {
    32. if (newPath.length() > 1) {
    33. newPath.append('/');
    34. }
    35. newPath.append(originalParts[i]);
    36. }
    37. }
    38. if (newPath.length() > 1 && path.endsWith("/")) {
    39. newPath.append('/');
    40. }
    41. // 重新buildurl地址
    42. ServerHttpRequest newRequest = request.mutate().path(newPath.toString()).build();
    43. exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR, newRequest.getURI());
    44. return chain.filter(exchange.mutate().request(newRequest).build());
    45. }
    46. public String toString() {
    47. return GatewayToStringStyler.filterToStringCreator(StripPrefixGatewayFilterFactory.this).append("parts", config.getParts()).toString();
    48. }
    49. };
    50. }
    51. public static class Config {
    52. private int parts;
    53. public Config() {
    54. }
    55. public int getParts() {
    56. return this.parts;
    57. }
    58. public void setParts(int parts) {
    59. this.parts = parts;
    60. }
    61. }
    62. }

    2、AddRequestHeader

            添加请求头参数,参数和值之间使用逗号分隔

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - AddRequestHeader=MyHeader,test
    9. metadata:
    10. connect-timeout: 15000 #ms
    11. response-timeout: 15000 #ms

            将之前的服务稍微修改一下,返回MyHeader:

    1. @RestController
    2. @RequestMapping("/nacos")
    3. public class NacosTestController {
    4. @GetMapping("/test")
    5. public String test(@RequestHeader("MyHeader") String myHeader){
    6. return myHeader;
    7. }
    8. }

            启动后访问:http://localhost:9999/service/nacos/test

            可以看到返回内容:

            源码就比较简单,就从配置文件中拿到header,然后添加进去即可:

    1. import org.springframework.cloud.gateway.filter.GatewayFilter;
    2. import org.springframework.cloud.gateway.filter.GatewayFilterChain;
    3. import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory.NameValueConfig;
    4. import org.springframework.cloud.gateway.support.GatewayToStringStyler;
    5. import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
    6. import org.springframework.http.server.reactive.ServerHttpRequest;
    7. import org.springframework.web.server.ServerWebExchange;
    8. import reactor.core.publisher.Mono;
    9. public class AddRequestHeaderGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
    10. public AddRequestHeaderGatewayFilterFactory() {
    11. }
    12. public GatewayFilter apply(NameValueConfig config) {
    13. return new GatewayFilter() {
    14. public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    15. String value = ServerWebExchangeUtils.expand(exchange, config.getValue());
    16. ServerHttpRequest request = exchange.getRequest().mutate().headers((httpHeaders) -> {
    17. httpHeaders.add(config.getName(), value);
    18. }).build();
    19. return chain.filter(exchange.mutate().request(request).build());
    20. }
    21. public String toString() {
    22. return GatewayToStringStyler.filterToStringCreator(AddRequestHeaderGatewayFilterFactory.this).append(config.getName(), config.getValue()).toString();
    23. }
    24. };
    25. }
    26. }

    3、AddResponseHeader

            在响应的header中添加参数

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - AddResponseHeader=addHeader, test
    9. metadata:
    10. connect-timeout: 15000 #ms
    11. response-timeout: 15000 #ms

            打开浏览器,F12显示控制台,访问之后可以看到响应的请求头中出现我们自己添加的数据

            源码也很简单,也是在响应的header中添加参数即可:

    1. import org.springframework.cloud.gateway.filter.GatewayFilter;
    2. import org.springframework.cloud.gateway.filter.GatewayFilterChain;
    3. import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory.NameValueConfig;
    4. import org.springframework.cloud.gateway.support.GatewayToStringStyler;
    5. import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
    6. import org.springframework.web.server.ServerWebExchange;
    7. import reactor.core.publisher.Mono;
    8. public class AddResponseHeaderGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
    9. public AddResponseHeaderGatewayFilterFactory() {
    10. }
    11. public GatewayFilter apply(NameValueConfig config) {
    12. return new GatewayFilter() {
    13. public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    14. String value = ServerWebExchangeUtils.expand(exchange, config.getValue());
    15. exchange.getResponse().getHeaders().add(config.getName(), value);
    16. return chain.filter(exchange);
    17. }
    18. public String toString() {
    19. return GatewayToStringStyler.filterToStringCreator(AddResponseHeaderGatewayFilterFactory.this).append(config.getName(), config.getValue()).toString();
    20. }
    21. };
    22. }
    23. }

    4、DedupeResponseHeader

            对指定响应头去重复,也即某个响应头key的value有多个值,去除一些重复的,有三种策略:

            RETAIN_FIRST 默认值,保留第一个

            RETAIN_LAST 保留最后一个

            RETAIN_UNIQUE 保留唯一的,出现重复的属性值,会保留一个。例如有两个My:bbb的属性,最后会只留一个。

         我们利用上面添加参数,添加两个key一样的,value不一样的header,然后利用DedupeResponseHeader进行去重:

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - AddResponseHeader=addHeader, test
    9. - AddResponseHeader=addHeader,test1
    10. - DedupeResponseHeader=addHeader, RETAIN_LAST
    11. metadata:
    12. connect-timeout: 15000 #ms
    13. response-timeout: 15000 #ms

             测试返回结果:

             这个的源码稍微复杂一点点,需要根据不同的策略进行选择不同的去重规则:

    1. import java.util.ArrayList;
    2. import java.util.Arrays;
    3. import java.util.LinkedHashSet;
    4. import java.util.List;
    5. import org.springframework.cloud.gateway.filter.GatewayFilter;
    6. import org.springframework.cloud.gateway.filter.GatewayFilterChain;
    7. import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory.NameConfig;
    8. import org.springframework.cloud.gateway.support.GatewayToStringStyler;
    9. import org.springframework.http.HttpHeaders;
    10. import org.springframework.web.server.ServerWebExchange;
    11. import reactor.core.publisher.Mono;
    12. public class DedupeResponseHeaderGatewayFilterFactory extends AbstractGatewayFilterFactory {
    13. private static final String STRATEGY_KEY = "strategy";
    14. public DedupeResponseHeaderGatewayFilterFactory() {
    15. super(DedupeResponseHeaderGatewayFilterFactory.Config.class);
    16. }
    17. public List shortcutFieldOrder() {
    18. return Arrays.asList("name", "strategy");
    19. }
    20. public GatewayFilter apply(DedupeResponseHeaderGatewayFilterFactory.Config config) {
    21. return new GatewayFilter() {
    22. public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    23. // return中调用dedupe方法
    24. return chain.filter(exchange).then(Mono.fromRunnable(() -> {
    25. DedupeResponseHeaderGatewayFilterFactory.this.dedupe(exchange.getResponse().getHeaders(), config);
    26. }));
    27. }
    28. public String toString() {
    29. return GatewayToStringStyler.filterToStringCreator(DedupeResponseHeaderGatewayFilterFactory.this).append(config.getName(), config.getStrategy()).toString();
    30. }
    31. };
    32. }
    33. void dedupe(HttpHeaders headers, DedupeResponseHeaderGatewayFilterFactory.Config config) {
    34. String names = config.getName();
    35. DedupeResponseHeaderGatewayFilterFactory.Strategy strategy = config.getStrategy();
    36. if (headers != null && names != null && strategy != null) {
    37. // 根据空格进行分组,可以看出如果添加多个key,只需要使用空格隔开即可
    38. String[] var5 = names.split(" ");
    39. int var6 = var5.length;
    40. // 遍历需要去重的header
    41. for(int var7 = 0; var7 < var6; ++var7) {
    42. String name = var5[var7];
    43. this.dedupe(headers, name.trim(), strategy);
    44. }
    45. }
    46. }
    47. // 根据不同策略进行不同的去重策略
    48. private void dedupe(HttpHeaders headers, String name, DedupeResponseHeaderGatewayFilterFactory.Strategy strategy) {
    49. List values = headers.get(name);
    50. if (values != null && values.size() > 1) {
    51. switch(strategy) {
    52. case RETAIN_FIRST:
    53. headers.set(name, (String)values.get(0));
    54. break;
    55. case RETAIN_LAST:
    56. headers.set(name, (String)values.get(values.size() - 1));
    57. break;
    58. case RETAIN_UNIQUE:
    59. headers.put(name, new ArrayList(new LinkedHashSet(values)));
    60. }
    61. }
    62. }
    63. public static class Config extends NameConfig {
    64. private DedupeResponseHeaderGatewayFilterFactory.Strategy strategy;
    65. public Config() {
    66. this.strategy = DedupeResponseHeaderGatewayFilterFactory.Strategy.RETAIN_FIRST;
    67. }
    68. public DedupeResponseHeaderGatewayFilterFactory.Strategy getStrategy() {
    69. return this.strategy;
    70. }
    71. public DedupeResponseHeaderGatewayFilterFactory.Config setStrategy(DedupeResponseHeaderGatewayFilterFactory.Strategy strategy) {
    72. this.strategy = strategy;
    73. return this;
    74. }
    75. }
    76. // 定义的三种策略
    77. public static enum Strategy {
    78. RETAIN_FIRST,
    79. RETAIN_LAST,
    80. RETAIN_UNIQUE;
    81. private Strategy() {
    82. }
    83. }
    84. }

    5、AddRequestParameter

            添加请求参数

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - AddRequestParameter=name,test
    9. metadata:
    10. connect-timeout: 15000 #ms
    11. response-timeout: 15000 #ms

    6、CircuitBreaker

            实现熔断时使用,支持CircuitBreaker和Hystrix两种,这个功能会单开一篇文章阐述。

    7、FallbackHeaders

            添加降级时的异常信息,一般与CircuitBreaker配合使用。

    8、RequestRateLimiter

             限流,会单开文章阐述。地址:SpringCloudGateway--基于redis实现令牌桶算法_雨欲语的博客-CSDN博客

    9、RedirectTo

            重定向,连个参数,status和url,status是重定向300系列状态码

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - RedirectTo=302, https://www.baidu.com
    9. metadata:
    10. connect-timeout: 15000 #ms
    11. response-timeout: 15000 #ms

    10、RemoveRequestHeader

            删除请求头

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - AddRequestHeader=MyHeader, test
    9. - RemoveRequestHeader=MyHeader
    10. metadata:
    11. connect-timeout: 15000 #ms
    12. response-timeout: 15000 #ms

    11、RemoveResponseHeader

            删除响应头

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - AddResponseHeader=addHeader, test
    9. - RemoveResponseHeader=addHeader
    10. metadata:
    11. connect-timeout: 15000 #ms
    12. response-timeout: 15000 #ms

    12、RemoveRequestParameter

            删除请求参数

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - RemoveRequestParameter=name
    9. metadata:
    10. connect-timeout: 15000 #ms
    11. response-timeout: 15000 #ms

    13、RewritePath 

            重写请求路径,比如我们之前都是访问http://localhost:9999/service/nacos/test,现在我们重写路径,并访问http://localhost:9999/service/test1/test,gateway会帮助我们将test1换成nacos

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - RewritePath=/test1/?(?<segment>.*), /nacos/$\{segment}
    9. metadata:
    10. connect-timeout: 15000 #ms
    11. response-timeout: 15000 #ms

    14、RewriteResponseHeader 

            修改响应的header,三个参数,一是key,二是value,可用正则,三是修改value的结果:

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - AddResponseHeader=addHeader, test1
    9. - RewriteResponseHeader=addHeader,test1,test
    10. metadata:
    11. connect-timeout: 15000 #ms
    12. response-timeout: 15000 #ms

    15、SaveSession

            如果项目中使用Spring Security和Spring Session整合时,想确保安全信息都传到下游机器,需要使用此Filter,在转发到后端微服务请求之前,强制执行WebSession::Save操作。用在那种像Spring session延迟数据存储的,并希望在请求转发前确保session状态保存情况。

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - SaveSession
    9. metadata:
    10. connect-timeout: 15000 #ms
    11. response-timeout: 15000 #ms

    16、SecureHeaders

            在响应的头中添加很多安全相关的信息:

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - SecureHeaders
    9. metadata:
    10. connect-timeout: 15000 #ms
    11. response-timeout: 15000 #ms

            也可以让某些信息不显示,需要进行配置,比如我关闭strict-transport-security和x-download-options:

    1. filter:
    2. secure-headers:
    3. disable:
    4. - strict-transport-security
    5. - x-download-options
    6. routes:
    7. - id: service-one
    8. uri: lb://service-one
    9. predicates:
    10. - Path=/service/**
    11. filters:
    12. - StripPrefix=1
    13. - SecureHeaders
    14. metadata:
    15. connect-timeout: 15000 #ms
    16. response-timeout: 15000 #ms

    17、SetPath

            功能和StripPrefix有点类似,语法更贴近restful,是将predicates中的路径进行修改:

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/{segment}
    6. filters:
    7. - SetPath=/{segment}
    8. metadata:
    9. connect-timeout: 15000 #ms
    10. response-timeout: 15000 #ms

    18、SetRequestHeader

            设置请求头header,将指定的key的value值修改为指定的value,如果不存在就新建:

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - SetRequestHeader=myHeader, test
    8. metadata:
    9. connect-timeout: 15000 #ms
    10. response-timeout: 15000 #ms

    19、SetResponseHeader

            设置响应的header,将指定的key的value值修改为指定的value,如果不存在就新建:

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - SetResponseHeader=addHeader, test
    8. metadata:
    9. connect-timeout: 15000 #ms
    10. response-timeout: 15000 #ms

    20、SetStatus

            设置返回的code:

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - SetStatus=500
    8. metadata:
    9. connect-timeout: 15000 #ms
    10. response-timeout: 15000 #ms

    21、PrefixPath

            给请求路径path添加前缀:

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - PrefixPath=/nacos
    9. metadata:
    10. connect-timeout: 15000 #ms
    11. response-timeout: 15000 #ms

    22、Retry

            设置重试次数:

            ①retries:重试次数
            ②statuses:遇到什么样的返回状态才重试,取值参考:org.springframework.http.HttpStatus
            ③methods:那些类型的方法会才重试(GET、POST等),取值参考:org.springframework.http.HttpMethod
            ④series:遇到什么样的series值才重试,取值参考:org.springframework.http.HttpStatus.Series
            ⑤exceptions:遇到什么样的异常才重试
            ⑥backoff:重试策略,由多个参数构成,例如firstBackoff

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - name: Retry
    9. args:
    10. retries: 3
    11. statuses: BAD_GATEWAY
    12. methods: GET,POST
    13. backoff:
    14. firstBackoff: 10ms
    15. maxBackoff: 50ms
    16. factor: 2
    17. basedOnPreviousValue: false
    18. metadata:
    19. connect-timeout: 15000 #ms
    20. response-timeout: 15000 #ms

    23、RequestSize

            控制请求大小,单位有KB、MB、B,默认是B,如果没有设置,默认上限是5MB:

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - RequestSize=200
    9. metadata:
    10. connect-timeout: 15000 #ms
    11. response-timeout: 15000 #ms

    24、ModifyRequestBody

            修改请求体body内容,官方推荐使用代码完成。

    25、ModifyResponseBody

            修改响应body的内容,也是推荐使用代码完成。

    26、MapRequestHeader

            用于键值对的赋值,以下意思是如果请求的header中有myHeader,就新增myHeader1,值和myHeader一样:

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - MapRequestHeader=myHeader, myHeader1
    9. metadata:
    10. connect-timeout: 15000 #ms
    11. response-timeout: 15000 #ms

            如果原有的header中已经有myHeader1,同时也有myheader,那么会新增一个myHeader1,值和myHeader一样。 

    27、PreserveHostHeader

            在转发请求到服务提供者时,保留host信息:

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - PreserveHostHeader
    9. metadata:
    10. connect-timeout: 15000 #ms
    11. response-timeout: 15000 #ms

    28、RewriteLocationResponseHeader

            用于改写reponse中的location信息,一共四个参数:stripVersionMode、locationHeaderName、hostValue、protocolsRegex,其中stripVersionMode策略一共三种:        

            NEVER_STRIP:不执行
            AS_IN_REQUEST :原始请求没有vesion,就执行
            ALWAYS_STRIP :固定执行

            Location用于替换host:port部分,如果没有就是用Request中的host;protocolsRegex用于匹配协议,如果匹配不上,name过滤器啥都不做

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - RewriteLocationResponseHeader=AS_IN_REQUEST, Location, ,
    9. metadata:
    10. connect-timeout: 15000 #ms
    11. response-timeout: 15000 #ms

    29、SetRequestHostHeader

            修改请求header中的host值:

    1. routes:
    2. - id: service-one
    3. uri: lb://service-one
    4. predicates:
    5. - Path=/service/**
    6. filters:
    7. - StripPrefix=1
    8. - SetRequestHostHeader=name,test
    9. metadata:
    10. connect-timeout: 15000 #ms
    11. response-timeout: 15000 #ms

  • 相关阅读:
    swagger3+Apifox生成接口文档
    6.1数值积分问题&6.2插值型求积公式
    flatbuffer使用
    现代 ERP 系统,如何使中小企业智能制造商受益?
    Discord将切换到临时文件链接以阻止恶意软件传播
    python元类(type)
    Vue:第一个Vue程序和vue-router路由
    QThread安全退出
    2020年计算机能力挑战赛C/C++初赛题解
    科大讯飞--C++开发工程师一面
  • 原文地址:https://blog.csdn.net/qq_41061437/article/details/128069622