gateway java,SpringCloud Gateway 的使用(一)

Spring Cloud Gateway 的使用

内容概览

e4862935f379a71168f3a2a4b28186a1.png

一. Springboot 对应的 Springcloud 版本如下~

官网地址 :https://spring.io/projects/spring-cloud

各位大佬 请注意下~  ,最后一句英文还说 不再支持 Dalston, Edgware, Finchley,

Greenwich 这四个版本啦

752438bec823a99569d18257fcd47c6e.png

image-20210215210801486

二. 使用

pom 文件

Hoxton.SR8

org.springframework.cloud

spring-cloud-dependencies

${spring.cloud-version}

pom

import

yaml 文件

spring:

cloud:

gateway:

routes:

- id: weight_high

uri: http://localhost:8080/

predicates:

- Path=/gateway/a

filters:

#           去掉请求路径中的 第一个  如 /gateway/a 变成/a

- StripPrefix=1

#           重新设置这个路由地址

- SetPath=/debug

三. 组成

官方文档

Route

The basic building block of the gateway. It is defined by an ID, a destination URI, a collection of predicates, and a collection of filters. A route is matched if the aggregate predicate is true.

对应该类(可对比上面 yaml 文件):

aed51c0da04da3fa1b504862d26abd2f.png

image-20210118161805469

Predicate

This is a Java 8 Function Predicate. The input type is a Spring Framework ServerWebExchange. This lets you match on anything from the HTTP request, such as headers or parameters.

当匹配时会拦截该请求

Filter

These are instances of Spring Framework GatewayFilter that have been constructed with a specific factory. Here, you can modify requests and responses before or after sending the downstream request.

可以配置添加更多过滤器~

四. 工作原理

由于 gateway 包中 使用了 WEBFLUX , 不能和这个 WEBMVC 同时存在,所以要移除 spring-boot-starter-web 包

37ad8617ea82dae6268d5e6066acb46e.png

image-20210118164727955

工作原理图(来自官网):就一堆过滤器

2c061cbf0c3b79d78361bfebdaeacccb.png

image-20210118162444635

五. Route Predicate Factories(★★★★★)

Route Predicate Factories:https://docs.spring.io/spring-cloud-gateway/docs/3.0.1/reference/html/#gateway-request-predicates-factories

可以看到这里有 11 种匹配模式~

3258e55b6df6570a0b6da4dc3e349f0a.png

image-20210216185916746

匹配模式大全(重点!)

这里的匹配满足正则表达式

spring:

cloud:

gateway:

routes:

- id: cookie_route

uri: https://example.org

predicates:

#           匹配在这个时间之后的

- After=2017-01-20T17:42:47.789-07:00[America/Denver]

#           匹配在这个时间之后的

- Before=2017-01-20T17:42:47.789-07:00[America/Denver]

#           匹配这个时间中间的

- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]

#           匹配 cookie 为 chocolate=ch.p 时拦截该请求

- Cookie=chocolate, ch.p

#           当 header 中有 X-Request-Id 该请求头时,且它的值匹配该正则时 拦截

- Header=X-Request-Id, \d+

#           匹配该 host 地址~

- Host=**.somehost.org,**.anotherhost.org

#           匹配该 请求方法

- Method=GET,POST

#           匹配该路径

- Path=/gateway/a

#           匹配该请求参数~  当请求参数为red 且值为 gree开头的~

- Query=red, gree.

#           匹配 网址为  192.168.1.1 - 192.168.1.254

- RemoteAddr=192.168.1.1/24

根据权重匹配

spring:

cloud:

gateway:

routes:

- id: weight_high

uri: https://weighthigh.org

predicates:

- Weight=group1, 8

- id: weight_low

uri: https://weightlow.org

predicates:

- Weight=group1, 2

六. Gateway Filter Factories(★★★★★)

Gateway Filter Factories : https://docs.spring.io/spring-cloud-gateway/docs/3.0.1/reference/html/#gatewayfilter-factories

数了下~ 官网居然多达 30 个 filter 给你使用

c3ff48036b996f3a01584c2704371fff.png

image-20210216194958056

afbea135eef652a7a5f976949e909483.png

image-20210216195335403

另外一篇博文中再写写~  ,  这里可以先简单参考下 4ye 在上面的 yaml 文件中的配置

七. Global Filters(★★★★★)

Global Filters: https://docs.spring.io/spring-cloud-gateway/docs/3.0.1/reference/html/#global-filters

全局过滤器~😄

77c6568a663f989f40c6f33771e37b00.png

image-20210216202020532

自定义过滤器

@Component

public class CustomGlobalFilter implements GlobalFilter, Ordered{

@Override

public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain){

log.info("custom global filter");

return chain.filter(exchange);

}

@Override

public int getOrder(){

return -1;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值