定制开发SpringCloud Gateway使用和配置,SpringCloud Gateway predicates详细配置

SpringCloud Gateway定制开发使用和配置,

SpringCloud Gateway routes详细配置,SpringCloud Gateway predicates详细配置SpringCloud Gateway 跨域配置,SpringCloud Gateway 超时配置

================================

©Copyright 蕃薯耀 2021-03-18

https://www.cnblogs.com/fanshuyao/

一、SpringCloud Gateway概述


spring cloud gateway定制开发旨在提供一种简单而有定制开发效的方法来路由到api,定制开发并为它们提供跨领域的关注点,例如:安全性、监视/定制开发度量和恢复能力。

客户端向Spring定制开发云网关发出请求。定制开发如果网关处理程序映射定制开发确定请求与路由匹配,定制开发则将其发送到网关Web处理程序。定制开发此处理程序通过特定于定制开发请求的筛选器链运行请求。定制开发过滤器被虚线分割的原因是,定制开发过滤器可以在代理请求定制开发发送之前和之后运行逻辑。执行所有“预”定制开发定制开发过滤器逻辑。定制开发然后发出代理请求。定制开发在发出代理请求之后,运行“post”过滤器逻辑。

定制开发官方文档地址:

https://docs.spring.io/spring-cloud-gateway/docs/2.2.7.RELEASE/reference/html/

SpringCloud Gateway主要成员:Route(路由):定制开发网关的基本构建块。它由一个ID、一个目标URI、定制开发一组谓词和一组筛选器定义。定制开发如果聚合谓词为true,定制开发则匹配路由。Predicate(断言,定制开发即路由匹配规则):这是一个Java 8 Function 。输入类型是springframework serverwebexchange。这允许您匹配来自HTTP请求的任何内容,例如头或参数。Filter(过滤器):这些是用特定工厂构建的Spring Framework GatewayFilter的实例。在这里,您可以在发送下游请求之前或之后修改请求和响应。

流程图:

 

二、SpringCloud Gateway使用和配置1、pom.xml引入依赖
主要的包是:spring-cloud-starter-gateway

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. <version>2.2.7.RELEASE</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>cn.hutool</groupId>
  8. <artifactId>hutool-all</artifactId>
  9. <version>5.4.4</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-actuator</artifactId>
  14. </dependency>
  15. <!-- Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency. -->
  16. <!--
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. </dependency>
  21. -->
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-starter-gateway</artifactId>
  25. <version>2.2.7.RELEASE</version>
  26. </dependency>

2、application.properties文件配置

基于Eureka为服务注册中心

  1. server.port=8701
  2. spring.application.name=SPRINGCLOUD-EUREKA-GATEWAY
  3. #gateway路由配置
  4. #使用服务发现路由
  5. spring.cloud.gateway.discovery.locator.enabled=true
  6. #服务路由名小写
  7. spring.cloud.gateway.discovery.locator.lower-case-service-id=true
  8. #设置路由id,没有固定规则,要求唯一,建设配合服务名
  9. spring.cloud.gateway.routes[0].id=GATEWAY-SERVICE
  10. #设置路由的uri,可以是调用的服务名,也可以请求的地址,当predicates匹配成功后,使用该路由的uri进行服务调用
  11. #设置为服务名:lb://SPRINGCLOUD-EUREKA-SERVER
  12. #设置为请求的地址:http://127.0.0.1:8601
  13. #使用lb,有2个微服务,先启动一个,再启动gateway,然后再启动第二个微服务,未自动实现负载均衡;要先启动2个微服务后,再启动gateway,这样才能实现负载均衡
  14. spring.cloud.gateway.routes[0].uri=lb://SPRINGCLOUD-EUREKA-SERVER
  15. #设置路由断言,即调用的地址匹配的规则
  16. #断言predicates的属性可以有:
  17. #Path:Path=/**
  18. #Cookie:Cookie=chocolate, ch.p,前面的为name,逗号后面的为值
  19. #Header:Header=X-Request-Id, \d+,前面的为name,逗号后面的为值
  20. #Host:Host=**.somehost.org,**.anotherhost.org
  21. #MethodMethod=GET
  22. #Query:Query=aaa,请求参数必须有name为aaa的参数;Query=aaa, 111:请求参数必须有name为aaa的参数,且aaa参数的值为111
  23. #AfterAfter=2021-03-17T15:47:51.534+08:00[Asia/Shanghai],日期时间,在该日期以后请求才被匹配,时间可以使用java.time.ZonedDateTime中的ZonedDateTime.now()获取当前时间
  24. #BeforeBefore=2022-03-17T15:47:51.534+08:00[Asia/Shanghai],日期时间,在该日期之前才被匹配
  25. #Between:Between=2021-03-17T15:47:51.534+08:00[Asia/Shanghai],2022-03-17T15:47:51.534+08:00[Asia/Shanghai],使用两个参数用逗号分隔,在两个时间范围内的请求才被匹配
  26. #RemoteAddr:RemoteAddr=192.168.1.1/24
  27. #断言方式一:
  28. #/gateway/**:表示/gateway/路径下所有请求
  29. #spring.cloud.gateway.routes[0].predicates[0].args.pattern=/**
  30. #这个不能少,少了会报错:reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.IllegalArgumentException: Unable to find RoutePredicateFactory with name gatewayTest
  31. #spring.cloud.gateway.routes[0].predicates[0].name=Path
  32. #断言方式二:请求微服务上的服务,方式一和方式二是2种不同的写法,方式一将规则分开,方式二写在一起
  33. spring.cloud.gateway.routes[0].predicates[0]=Path=/test
  34. #断言方式三:跳转到百度
  35. spring.cloud.gateway.routes[1].id=GATEWAY-REDIRECT
  36. spring.cloud.gateway.routes[1].uri=https://www.baidu.com
  37. spring.cloud.gateway.routes[1].predicates[0]=Path=/redirect/**
  38. spring.cloud.gateway.routes[1].filters[0].name=RedirectTo
  39. spring.cloud.gateway.routes[1].filters[0].args.status=301
  40. spring.cloud.gateway.routes[1].filters[0].args.url=https://www.baidu.com
  41. #eureka实例名称
  42. #eureka.instance.hostname=eureka8601.com
  43. eureka.instance.instance-id=GATEWAY-8701
  44. #路径显示IP地址
  45. eureka.instance.prefer-ip-address=true
  46. #eureka客户端向服务端发送心跳的时间间隔,单元为秒,默认为30
  47. eureka.instance.lease-renewal-interval-in-seconds=2
  48. #eureka服务端收到最后一次心跳等待的时间上限,超时将移除服务,单元为秒,默认为90
  49. eureka.instance.lease-expiration-duration-in-seconds=5
  50. #false表示向注册中心注册自己
  51. eureka.client.register-with-eureka=true
  52. #是否从Eureka抓取已有的注册信息,默认为true。单节点不用设置,集群必须设置为true,才能配置ribbon使用负载均衡
  53. eureka.client.fetch-registry=true
  54. #设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
  55. eureka.client.service-url.defaultZone=http://eureka8501.com:8501/eureka
  56. #集群配置
  57. #eureka.client.service-url.defaultZone=http://eureka8501.com:8501/eureka,http://eureka8502.com:8501/eureka

3、启动类

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  4. @SpringBootApplication
  5. @EnableEurekaClient
  6. public class SpringCloud8701GatewayApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(SpringCloud8701GatewayApplication.class, args);
  9. }
  10. }

三、springCloud Gateway routes predicates 配置方式

配置的方式有2种,1种是键值对的方式,一种是完全展开参数的方式

1、通过键值对的方式

使用两个参数定义Cookie路由,即Cookie名称mycookie和匹配mycookievalue的值。

application.yml文件的配置方式

快捷方式配置由筛选器名称、等号(=)和用逗号(,)分隔的参数值识别。

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: cookie_route
  6. uri: https://example.org
  7. predicates:
  8. - Cookie=mycookie,mycookievalue

application.properties文件的配置方式:

  1. #设置路由id,没有固定规则,要求唯一,建设配合服务名
  2. spring.cloud.gateway.routes[0].id=cookie_route
  3. #设置路由的uri,可以是调用的服务名,也可以请求的地址,当predicates匹配成功后,使用该路由的uri进行服务调用
  4. ##设置为服务名:lb://SPRINGCLOUD-EUREKA-SERVER
  5. #设置为请求的地址:http://127.0.0.1:8601
  6. spring.cloud.gateway.routes[0].uri=https://example.org
  7. spring.cloud.gateway.routes[0].predicates[0]=Cookie=mycookie,mycookievalue

2、完全展开的参数
完全展开的参数更像是带有名称/值对的标准yaml配置。通常,会有一个name键和一个args键。args键是用于配置谓词或筛选器的键值对的映射。

application.yml文件的配置方式

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: cookie_route
  6. uri: https://example.org
  7. predicates:
  8. - name: Cookie
  9. args:
  10. name: mycookie
  11. regexp: mycookievalue

application.properties文件的配置方式:

  1. spring.cloud.gateway.routes[0].id=cookie_route
  2. spring.cloud.gateway.routes[0].uri=https://example.org
  3. spring.cloud.gateway.routes[0].predicates[0].name=Cookie
  4. spring.cloud.gateway.routes[0].predicates[0].args.name=mycookie
  5. spring.cloud.gateway.routes[0].predicates[0].args.regexp=mycookievalue

四、springCloud Gateway predicates 详细配置

1、After route predicate
After route路由接受一个参数datetime(这是一个java分区日期时间)。此路由匹配在指定日期时间之后发生的请求,即在此时间后的请求才能正常访问。

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: after_route
  6. uri: https://example.org
  7. predicates:
  8. - After=2017-01-20T17:42:47.789-07:00[America/Denver]

After=2017-01-20T17:42:47.789-07:00[America/Denver]中的时间可以使用java.time.ZonedDateTime中的ZonedDateTime.now()获取当前时间
具体示例:

  1. import java.time.ZonedDateTime;
  2. public class DatetimeUtil {
  3. public static void main(String[] args) {
  4. ZonedDateTime t = ZonedDateTime.now();
  5. //2021-03-17T15:47:51.534+08:00[Asia/Shanghai]
  6. System.out.println(t);
  7. }
  8. }

2、Before route predicate
Before route路由接受一个参数datetime(它是java分区的日期时间)。此路由匹配在指定日期时间之前发生的请求。

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: before_route
  6. uri: https://example.org
  7. predicates:
  8. - Before=2017-01-20T17:42:47.789-07:00[America/Denver]

3、Between route predicate
路由间接受两个参数,datetime1和datetime2,它们是java分区的日期时间对象。此谓词匹配发生在datetime1之后和datetime2之前的请求。datetime2参数必须在datetime1之后。

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: between_route
  6. uri: https://example.org
  7. predicates:
  8. - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]

4、Cookie route predicate
Cookie路由工厂接受两个参数,Cookie名称和regexp(这是一个Java正则表达式)。此路由匹配具有给定名称且其值与正则表达式匹配的cookie。

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: cookie_route
  6. uri: https://example.org
  7. predicates:
  8. - Cookie=chocolate, ch.p

5、Header route predicate
请求头路由有两个参数,头名称和regexp(这是一个Java正则表达式)。此路由与具有给定名称的标头匹配,该标头的值与正则表达式匹配。

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: header_route
  6. uri: https://example.org
  7. predicates:
  8. - Header=X-Request-Id, \d+

6、Host route predicate
主机路由采用一个参数:主机名模式列表。这个模式是一个蚂蚁风格的模式。作为分隔符。这个路由匹配与模式匹配的主机头。

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: host_route
  6. uri: https://example.org
  7. predicates:
  8. - Host=**.somehost.org,**.anotherhost.org

7、Method Route Predicate
方法路由接受一个methods参数,该参数是一个或多个参数:要匹配的HTTP方法。

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: method_route
  6. uri: https://example.org
  7. predicates:
  8. - Method=GET,POST

如果请求方法是GET或POST,则此路由匹配。

8、Path Route Predicate
路径路由工厂有两个参数:一个Spring路径匹配器模式列表和一个名为matchOptionalTrailingSeparator的可选标志。

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: path_route
  6. uri: https://example.org
  7. predicates:
  8. - Path=/red/{segment},/blue/{segment}

如果请求路径是:/red/1或/red/blue或/blue/green,则此路由匹配。

示例:

  1. spring.cloud.gateway.routes[0].id=GATEWAY-SERVICE
  2. spring.cloud.gateway.routes[0].uri=lb://SPRINGCLOUD-EUREKA-SERVER
  3. spring.cloud.gateway.routes[0].predicates[0]=Path=/test
  4. 或者
  5. spring.cloud.gateway.routes[0].predicates[0]=Path=/gateway/**

9、Query route predicate
查询路由有两个参数:一个必需的参数和一个可选的regexp(这是一个Java正则表达式)。

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: query_route
  6. uri: https://example.org
  7. predicates:
  8. - Query=green

如果请求参数中包含名称为green的参数,则路由匹配。

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: query_route
  6. uri: https://example.org
  7. predicates:
  8. - Query=red, gree.

如果请求参数中包含名称为green的参数,且值为red,则路由匹配。

10、RemoteAddr route predicate
远程Addr route获取源的列表(最小大小1),这些源是CIDR表示法(IPv4或IPv6)字符串,例如192.168.0.1/16(其中192.168.0.1是IP地址,16是子网掩码)。

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: remoteaddr_route
  6. uri: https://example.org
  7. predicates:
  8. - RemoteAddr=192.168.1.1/24

11、Weight route predicate
权重路由工厂有两个参数:group和Weight(int)。每组计算重量。

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: weight_high
  6. uri: https://weighthigh.org
  7. predicates:
  8. - Weight=group1, 8
  9. - id: weight_low
  10. uri: https://weightlow.org
  11. predicates:
  12. - Weight=group1, 2

四、springCloud Gateway GatewayFilter 详细配置

1、The AddRequestHeader GatewayFilter Factory

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: add_request_header_route
  6. uri: https://example.org
  7. filters:
  8. - AddRequestHeader=X-Request-red, blue

2、The AddRequestParameter GatewayFilter Factory

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: add_request_parameter_route
  6. uri: https://example.org
  7. filters:
  8. - AddRequestParameter=red, blue

3、The AddResponseHeader GatewayFilter Factory

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: add_response_header_route
  6. uri: https://example.org
  7. filters:
  8. - AddResponseHeader=X-Response-Red, Blue

4、The DedupeResponseHeader GatewayFilter Factory

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: dedupe_response_header_route
  6. uri: https://example.org
  7. filters:
  8. - DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin

5、The RedirectTo GatewayFilter Factory
重定向到网关过滤器工厂需要两个参数,status和url。status参数应该是300系列重定向HTTP代码,例如301。url参数应该是有效的url。这是位置标头的值。对于相对重定向,应该使用uri:no://op作为路由定义的uri。

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: prefixpath_route
  6. uri: https://example.org
  7. filters:
  8. - RedirectTo=302, https://acme.org

示例:

  1. #断言方式三:跳转到百度
  2. spring.cloud.gateway.routes[1].id=GATEWAY-REDIRECT
  3. spring.cloud.gateway.routes[1].uri=https://www.baidu.com
  4. spring.cloud.gateway.routes[1].predicates[0]=Path=/redirect/**
  5. spring.cloud.gateway.routes[1].filters[0].name=RedirectTo
  6. spring.cloud.gateway.routes[1].filters[0].args.status=302
  7. spring.cloud.gateway.routes[1].filters[0].args.url=https://www.baidu.com

其它的过滤器见官方文档:

https://docs.spring.io/spring-cloud-gateway/docs/2.2.7.RELEASE/reference/html/#gatewayfilter-factories

五、SpringCloud Gateway 自定义全局过滤器

  1. @Bean
  2. public GlobalFilter customFilter() {
  3. return new CustomGlobalFilter();
  4. }
  5. public class CustomGlobalFilter implements GlobalFilter, Ordered {
  6. @Override
  7. public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
  8. log.info("custom global filter");
  9. return chain.filter(exchange);
  10. }
  11. @Override
  12. public int getOrder() {
  13. return -1;
  14. }
  15. }

六、SpringCloud Gateway 超时配置
connect-timeout:必须以毫秒为单位指定连接超时。
response-timeout:响应超时必须指定为java.time.Duration文件

Http超时(响应和连接)可以为所有路由配置,并为每个特定路由重写。

1、全局超时配置

  1. spring:
  2. cloud:
  3. gateway:
  4. httpclient:
  5. connect-timeout: 1000
  6. response-timeout: 5s

2、单个路由超时配置

  1. - id: per_route_timeouts
  2. uri: https://example.org
  3. predicates:
  4. - name: Path
  5. args:
  6. pattern: /delay/{timeout}
  7. metadata:
  8. response-timeout: 200
  9. connect-timeout: 200

七、SpringCloud Gateway 跨域配置
可以配置网关来控制CORS行为。“全局”CORS配置是URL模式到Spring框架CORS配置的映射

  1. spring:
  2. cloud:
  3. gateway:
  4. globalcors:
  5. cors-configurations:
  6. '[/**]':
  7. allowedOrigins: "https://docs.spring.io"
  8. allowedMethods:
  9. - GET

在示例中,允许来自docs.spring.io对于所有GET请求的路径。

 

(时间宝贵,分享不易,捐赠回馈,^_^)

================================

©Copyright 蕃薯耀 2021-03-18

https://www.cnblogs.com/fanshuyao/

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发