Spring Cloud Gateway 配置信息

Spring Cloud Gateway 配置信息

了解Gateway的配置才可以理解使用Gateway可以做什么事情,才能更好地应用在产品开发中。


一、Predicates

Predicates主要起的作用是:配置路由匹配请求的规则

Http 相关
Path

配置对于请求路径的匹配规则

  1. yml配置,多个参数用逗号隔开
- Path = /aa/**,/bb/**
  1. json配置
{"name":"Path","args":{"pattern":"/aa/**","pattern1":"/bb/**"}}


Cookie

配置对Cookie中值的匹配,第一个为key,第二个为value。下例匹配cookie设置chocolate:ch.p的请求

  1. yml配置
- Cookie = chocolate,ch.p
  1. json配置
{"name":"Cookie","args":{"_genkey_0":"chocolate","_genkey_1":"ch.p"}}


Header

匹配Http请求中设置的内容,http-header设置X-Request-Id:\d+可以匹配,第二个参数第二个参数是正则表达式

  1. yml配置
- Header = X-Request-Id,\d+

  1. json配置
{"name":"Header","args":{"_genkey_0":"X-Request-Id","_genkey_1":"\d+"}}



Host

匹配Http请求Host,匹配所有host为**.somehost.com的请求

  1. yml配置
- Host = **.somehost.com


  1. json配置
{"name":"Host","args":{"_genkey_0":"**.somehost.com"}}



Method

匹配Http请求头

  1. yml配置
- Method = GET


  1. json配置
{"name":"Method","args":{"_genkey_0":"GET"}}


Query

匹配Http请求中的查询参数,请求中携带param1=value的请求可以匹配

  1. yml配置
- Query = param1,value

  1. json配置
{"name":"Query","args":{"_genkey_0":"param1","_genkey_1":"value"}}


RemoteAddr

匹配请求中的RemoteAddr

  1. yml配置
- RemoteAddr = 192.168.1.1/24


  1. json配置
{"name":"RemoteAddr","args":{"_genkey_0":"192.168.1.1/24"}}


RemoteAddr

匹配请求中的RemoteAddr

  1. yml配置
- RemoteAddr = 192.168.1.1/24


  1. json配置
{"name":"RemoteAddr","args":{"_genkey_0":"192.168.1.1/24"}}

时间 相关

After

设置时间之后可以访问

  1. yml配置
- After = 2017-01-20T17:42:47.789-07:00[America/Denver]

  1. json配置
{"name":"After","args":{"_genkey_0":"2017-01-20T17:42:47.789-07:00[America/Denver]"}}



Before

设置时间之前可以访问

  1. yml配置
- Before = 2017-01-20T17:42:47.789-07:00[America/Denver]

  1. json配置
{"name":"Before","args":{"_genkey_0":"2017-01-20T17:42:47.789-07:00[America/Denver]"}}


权重相关

权重相关

至少两组以上路由可以配置权重路由,配置后会根据权重随机访问几个路由

  1. yml配置
- Weight = service1,80

  1. json配置
{"name":"Weight","args":{"_genkey_0":"service1","_genkey_1":"80"}}


二、Filters 过滤

路径重写
  1. yml配置
- RewritePath = /path/(?<segment>.*), /$\{segment}

  1. json配置
{"name":"RewritePath","args":{"_genkey_0":"/foo/(?<segment>.*)","_genkey_1":"/$\\{segment}"}}


修改请求头
  1. yml配置
- AddRequestHeader = X-Request-Foo,Bar


  1. json配置
{"name":"AddRequestHeader","args":{"_genkey_0":"X-Request-Foo","_genkey_1":"Bar"}}



修改请求参数
  1. yml配置
- AddRequestParameter = foo,bar

  1. json配置
{"name":"AddRequestParameter","args":{"_genkey_0":"foo","_genkey_1":"bar"}}


修改响应参数
  1. yml配置
- AddResponseHeader = X-Request-Foo,Bar

  1. json配置
{"name":"AddResponseHeader","args":{"_genkey_0":"X-Request-Foo","_genkey_1":"Bar"}}


路径前缀增强

请求路径/hello, 将会被替换为 /mypath/hello

  1. yml配置
- PrefixPath = /mypath

  1. json配置
{"name":"PrefixPath","args":{"_genkey_0":"/mypath"}}


路径前缀删除

请求/name/bar/foo,去除掉前面两个前缀之后,最后转发到目标服务的路径为/foo

  1. yml配置
- StripPrefix = 2

  1. json配置
{"name":"StripPrefix","args":{"_genkey_0":"2"}}


重定向
  1. yml配置
- PreserveHostHeader

  1. json配置
{"name":"PreserveHostHeader","args":{}}


重定向
  1. yml配置
- RedirectTo = 302,http://acme.org

  1. json配置
{"name":"RedirectTo","args":{"_genkey_0":"302","_genkey_1":"http://acme.org"}}


断路器
  1. yml配置
- name: Hystrix
  args:
	  # 断路后跳转地址
	  name: fallbackcmd
      fallbackUri: forward:/incaseoffailureusethis

  1. json配置
{"name":"Hystrix","args":{"name":"fallbackcmd","fallbackUri":"forward:/incaseoffailureusethis"}}



集成Redis原生支持请求限流
  1. yml配置
 - name: RequestRateLimiter
   args:
     redis-rate-limiter.replenishRate: 10  
     redis-rate-limiter.burstCapacity: 20

  1. json配置
{"name":"RequestRateLimiter","args":{"redis-rate-limiter.replenishRate":"10","redis-rate-limiter.burstCapacity":"20"}}



删除请求头属性
  1. yml配置
- RemoveRequestHeader = X-Request-Foo

  1. json配置
{"name":"RemoveRequestHeader","args":{"_genkey_0":"X-Request-Foo"}}



删除响应头属性
  1. yml配置
- RemoveResponseHeader = X-Request-Foo

  1. json配置
{"name":"RemoveResponseHeader","args":{"_genkey_0":"X-Request-Foo"}}


重写响应头

将请求 /42?user=ford&password=omg!what&flag=true, 改为 /42?user=ford&password=***&flag=true

  1. yml配置
- RewriteResponseHeader = X-Response-Foo,password=[^&]+,password=***


  1. json配置
{"name":"RewriteResponseHeader","args":{"_genkey_0":"X-Response-Foo","_genkey_1":"password=[^&]+","_genkey_2":"password=***"}}



重设请求路径

请求/foo/bar,在接下来的处理中被改为/bar

  1. yml配置
- SetPath =/{segment}

  1. json配置
{"name":"SetPath","args":{"_genkey_0":"/{segment}"}}


设置响应头

在接下来的处理中修改响应头X-Response-Foo为Bar

  1. yml配置
- SetResponseHeader =X-Request-Foo,Bar

  1. json配置
{"name":"SetResponseHeader","args":{"_genkey_0":"X-Response-Foo","_genkey_1":"Bar"}}


设置Http状态
  1. yml配置
- name: SetStatus
  args:
	  status: 401



  1. json配置
{"name":"SetStatus","args":{"_genkey_0":"302"}}


设置文件传输大小
  1. yml配置
 - name: RequestSize
   args:
	   maxSize: 5000000

  1. json配置
{"name":"RequestSize","args":{"_genkey_0":"5000000"}}



失败重试
  1. yml配置
- name: Retry
  args:
	  retries: 3
      statuses: BAD_GATEWAY
      
  1. json配置
{"name":"Retry","args":{"retries":3,"statuses":"BAD_GATEWAY"}}


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 Spring Cloud Gateway配置跨域,可以使用 Spring WebFlux 提供的 CorsConfigurationSource 类。具体步骤如下: 1. 创建一个 CorsConfiguration 对象,配置跨域的属性,比如允许的域名、允许的请求方法等。 ``` CorsConfiguration corsConfig = new CorsConfiguration(); corsConfig.addAllowedOrigin("*"); corsConfig.addAllowedMethod("*"); corsConfig.addAllowedHeader("*"); ``` 2. 创建一个 CorsConfigurationSource 对象,将 CorsConfiguration 对象传入其中。 ``` UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", corsConfig); ``` 3. 在 Spring Cloud Gateway配置类中将 CorsConfigurationSource 对象添加到路由过滤器中。 ``` @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route(r -> r.path("/api/**") .filters(f -> f.cors(corsConfigSource)) // 添加跨域过滤器 .uri("lb://service")) .build(); } @Bean public CorsConfigurationSource corsConfigSource() { CorsConfiguration corsConfig = new CorsConfiguration(); corsConfig.addAllowedOrigin("*"); corsConfig.addAllowedMethod("*"); corsConfig.addAllowedHeader("*"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", corsConfig); return source; } ``` 这样就可以在 Spring Cloud Gateway配置跨域了。需要注意的是,由于 Spring Cloud Gateway 是基于 Spring WebFlux 开发的,因此跨域的配置方式与传统的 Spring MVC 不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值