客户管理系统开发定制Gateway-路由、过滤器配置

客户管理系统开发定制相关名称介绍

1、Route(路由)
客户管理系统开发定制路由是的基本单元,由ID、URI、一组Predicate、一组Filter组成,根据Predicate客户管理系统开发定制进行匹配转发。

  1. ID:客户管理系统开发定制自定义的路由 ID,保持唯一
  2. URL:客户管理系统开发定制目标服务地址

2、(谓语、断言)
客户管理系统开发定制路由转发的判断条件,目前SpringCloud Gateway支持多种方式,常见如:Path、Query、Method、等,写法必须遵循 key=vlue的形式

3、Filter(过滤器)
过滤器是路由转发请求时所经过的过滤逻辑,可用于修改请求、响应内容

路由规则

路由规则的配置也就是配置Predicate(谓语、断言),下面介绍路由的配置规则:这里以yml配置文件为例,切换成代码时使用对应的api就可以了。

1、通过请求路径匹配
Path Route Predicate 接收一个匹配路径的参数来判断是否走路由。

//通过配置文件配置spring:  cloud:    gateway:      routes:        - id: gate_route          uri: http://localhost:9023          predicates:          ## 当请求的路径为gate、rule开头的时,转发到http://localhost:9023服务器上            - Path=/gate/**,/rule/**        ### 请求路径前加上/app          filters:          - PrefixPath=/app
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2、通过请求参数匹配
Query Route Predicate 支持传入两个参数,一个是属性名一个为属性值,属性值可以是正则表达式。

server:  port: 8080spring:  cloud:    gateway:      routes:      - id: query_route        uri: https://example.org        predicates:        - Query=smile
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

这样配置,只要请求中包含 smile 属性的参数即可匹配路由。
只要请求汇总带有 smile 参数即会匹配路由,不带 smile 参数则不会匹配。
还可以将 Query 的值以键值对的方式进行配置,这样在请求过来时会对属性值和正则进行匹配,匹配上才会走路由,也就是key对应的value的直必须匹配正则表达式才会进行路由。

3、通过Cookie 匹配
Cookie Route Predicate 可以接收两个参数,一个是 Cookie name ,一个是正则表达式,路由规则会通过获取对应的 Cookie name 值和正则表达式去匹配,如果匹配上就会执行路由,如果没有匹配上则不执行。

server:  port: 8080spring:  application:    name: api-gateway  cloud:    gateway:      routes:        -id: gateway-service          uri: https://www.baidu.com          order: 0          predicates:            - Cookie=sessionId, test
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

测试路径:http://localhost:8080 --cookie “sessionId=test”
则会返回页面代码,如果去掉–cookie “sessionId=test”,后台汇报 404 错误。

4、通过Header属性匹配
Header Route Predicate 和 Cookie Route Predicate 一样,也是接收 2 个参数,一个 header 中属性名称和一个正则表达式,这个属性值和正则表达式匹配则执行。

server:  port: 8080spring:  cloud:    gateway:      routes:      - id: query_route        uri: https://example.org        predicates:            - Header=X-Request-Id, \d+
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

测试路径:http://localhost:8080 -H “X-Request-Id:88”
将参数-H "X-Request-Id:88"改为-H "X-Request-Id:spring"再次执行时返回404证明没有匹配。

5、通过Host 匹配
Host Route Predicate 接收一组参数,一组匹配的域名列表,这个模板是一个 ant 分隔的模板,用.号作为分隔符。它通过参数中的主机地址作为匹配规则。

server:  port: 8080spring:  cloud:    gateway:      routes:      - id: query_route        uri: https://example.org        predicates:          - Host=**.baidu.com
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

测试路径:http://localhost:8080 -H “Host: www.baidu.com”
测试路径:http://localhost:8080 -H “Host: md.baidu.com”
经测试以上两种 host 均可匹配到 host_route 路由,去掉 host 参数则会报 404 错误。

6、通过请求方式匹配
可以通过是 POST、GET、PUT、DELETE 等不同的请求方式来进行路由。

server:  port: 8080spring:  cloud:    gateway:      routes:      - id: query_route        uri: https://example.org        predicates:            - Method=GET
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

此配置只能使用GET方法请求。

7、通过请求 ip 地址进行匹配
Predicate 也支持通过设置某个 ip 区间号段的请求才会路由,RemoteAddr Route Predicate 接受 cidr 符号(IPv4 或 IPv6 )字符串的列表(最小大小为1),例如 192.168.0.1/16 (其中 192.168.0.1 是 IP 地址,16 是子网掩码)。

server:  port: 8080spring:  cloud:    gateway:      routes:      - id: query_route        uri: https://example.org        predicates:            - RemoteAddr=192.168.1.1/24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

可以将此地址设置为本机的 ip 地址进行测试。
如果请求的远程地址是 192.168.1.10,则此路由将匹配。

8、权重谓词

- id: weight_high  uri: http://www.kaico.com/kaicostudy  predicates:    - Weight=group1, 2- id: weight_low  uri: http://www.kaico.com  predicates:    - Weight=group1, 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

根据权重比例实现转发,注意分组一定要相同。

9、组合使用

server:  port: 8080spring:  application:    name: api-gateway  cloud:    gateway:      routes:        - id: gateway-service          uri: https://www.baidu.com          order: 0          predicates:            - Host=**.foo.org            - Path=/headers            - Method=GET            - Header=X-Request-Id, \d+            - Query=foo, ba.            - Query=baz            - Cookie=chocolate, ch.p
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

注意: 各种 Predicates 同时存在于同一个路由时,请求必须同时满足所有的条件才被这个路由匹配。
一个请求满足多个路由的断言条件时,请求只会被首个成功匹配的路由转发。

过滤器规则

1、PrefixPath
对所有的请求路径添加前缀:

spring:  cloud:    gateway:      routes:      - id: prefixpath_route        uri: https://example.org        filters:        - PrefixPath=/mypath #添加请求前缀
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

访问/hello的请求被发送到https://example.org/mypath/hello。

2、RedirectTo
,配置包含重定向的返回码和地址:

spring:  cloud:    gateway:      routes:      - id: prefixpath_route        uri: https://example.org        predicates:        - Path=/say/**        filters:        - RedirectTo=302, https://acme.org
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

如果当前请求匹配此路由,将重定向到指定地址

3、RemoveRequestHeader
去掉某个请求头信息:

spring:  cloud:    gateway:      routes:      - id: removerequestheader_route        uri: https://example.org        filters:        - RemoveRequestHeader=X-Request-Foo
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

去掉请求头信息 X-Request-Foo

4、RemoveResponseHeader
去掉某个回执头信息:

spring:  cloud:    gateway:      routes:      - id: removerequestheader_route        uri: https://example.org        filters:        - RemoveResponseHeader=X-Request-Foo
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

5、RemoveRequestParameter
去掉某个请求参数信息:

spring:  cloud:    gateway:      routes:      - id: removerequestparameter_route        uri: https://example.org        filters:        - RemoveRequestParameter=red
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

6、RewritePath
改写路径:

spring:  cloud:    gateway:      routes:      - id: rewrite_filter        uri: http://localhost:8081        predicates:        - Path=/test/**        filters:        - RewritePath=/where(?<segment>/?.*), /test(?<segment>/?.*)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

/where/… 改成 test/…

7、SetPath
设置请求路径,与RewritePath类似。

spring:  cloud:    gateway:      routes:      - id: setpath_route        uri: https://example.org        predicates:        - Path=/red/{segment}        filters:        - SetPath=/blue/{segment}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

如/red/blue的请求被转发到/blue。

8、SetRequestHeader
设置请求头信息。

spring:  cloud:    gateway:      routes:      - id: setrequestheader_route        uri: https://example.org        filters:        - SetRequestHeader=X-Request-Red, Blue
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

9、SetStatus
设置回执状态码。

spring:  cloud:    gateway:      routes:      - id: setstatusint_route        uri: https://example.org        filters:        - SetStatus=401
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

10、StripPrefix
跳过指定路径。

spring:  cloud:    gateway:      routes:      - id: nameRoot        uri: https://nameservice        predicates:        - Path=/name/**        filters:        - StripPrefix=2 #去除请求路径中第二个
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

请求/name/blue/red会转发到/red。去除第二个 blue。

11、RequestSize
设置请求大小。

spring:  cloud:    gateway:      routes:      - id: request_size_route        uri: http://localhost:8080/upload        predicates:        - Path=/upload        filters:        - name: RequestSize          args:            maxSize: 5000000
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

超过5M的请求会返回413错误。

12、Default-filters
对所有请求添加过滤器。

spring:  cloud:    gateway:      default-filters:      - AddResponseHeader=X-Response-Default-Red, Default-Blue      - PrefixPath=/httpbin
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

使用代码实现配置路由规则和过滤器

路由配置类:RouteDefinition

yml配置:每个路由策略有路由id、一组过滤器、一组断言组成。对应的Java类也是如此。

Predicate(谓语、断言)配置类:PredicateDefinition

在yml配置中,断言是由一个名称和一系列的参数组成的。
参数总结:

过滤器配置类:FilterDefinition

在yml配置中,过滤器是由一个名称和一系列的参数组成的。
参数总结:

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