Spring Cloud Alibaba Sentinel集成Spring Cloud Gateway

背景

Sentinel 支持对 Spring Cloud Gateway、Zuul 等主流的 API Gateway 进行限流。
sentinel-api-gateway-common-arch

Sentinel 1.6.0 引入了 Sentinel API Gateway Adapter Common 模块,此模块中包含网关限流的规则和自定义 API 的实体和管理逻辑:

  • GatewayFlowRule:网关限流规则,针对 API Gateway 的场景定制的限流规则,可以针对不同 route 或自定义的 API 分组进行限流,支持针对请求中的参数、Header、来源 IP 等进行定制化的限流。
  • ApiDefinition:用户自定义的 API 定义分组,可以看做是一些 URL 匹配的组合。比如我们可以定义一个 API 叫 my_api,请求 path 模式为 /foo/**/baz/** 的都归到 my_api 这个 API 分组下面。限流的时候可以针对这个自定义的 API 分组维度进行限流。

其中网关限流规则 GatewayFlowRule 的字段解释如下:

  • resource:资源名称,可以是网关中的 route 名称或者用户自定义的 API 分组名称。
  • resourceMode:规则是针对 API Gateway 的 route(RESOURCE_MODE_ROUTE_ID)还是用户在 Sentinel 中定义的 API 分组(RESOURCE_MODE_CUSTOM_API_NAME),默认是 route。
  • grade:限流指标维度,同限流规则的 grade 字段。
  • count:限流阈值
  • intervalSec:统计时间窗口,单位是秒,默认是 1 秒。
  • controlBehavior:流量整形的控制效果,同限流规则的 controlBehavior 字段,目前支持快速失败和匀速排队两种模式,默认是快速失败。
  • burst:应对突发请求时额外允许的请求数目。
  • maxQueueingTimeoutMs:匀速排队模式下的最长排队时间,单位是毫秒,仅在匀速排队模式下生效。
  • paramItem:参数限流配置。若不提供,则代表不针对参数进行限流,该网关规则将会被转换成普通流控规则;否则会转换成热点规则。其中的字段:
    • parseStrategy:从请求中提取参数的策略,目前支持提取来源 IP(PARAM_PARSE_STRATEGY_CLIENT_IP)、Host(PARAM_PARSE_STRATEGY_HOST)、任意 Header(PARAM_PARSE_STRATEGY_HEADER)和任意 URL 参数(PARAM_PARSE_STRATEGY_URL_PARAM)四种模式。
    • fieldName:若提取策略选择 Header 模式或 URL 参数模式,则需要指定对应的 header 名称或 URL 参数名称。
    • pattern:参数值的匹配模式,只有匹配该模式的请求属性值会纳入统计和流控;若为空则统计该请求属性的所有值。(1.6.2 版本开始支持)
    • matchStrategy:参数值的匹配策略,目前支持精确匹配(PARAM_MATCH_STRATEGY_EXACT)、子串匹配(PARAM_MATCH_STRATEGY_CONTAINS)和正则匹配(PARAM_MATCH_STRATEGY_REGEX)。(1.6.2 版本开始支持)

用户可以通过 GatewayRuleManager.loadRules(rules) 手动加载网关规则,或通过 GatewayRuleManager.register2Property(property) 注册动态规则源动态推送(推荐方式)。

集成Spring Cloud Gateway

从 1.6.0 版本开始,Sentinel 提供了 Spring Cloud Gateway 的适配模块,可以提供两种资源维度的限流:

  • route 维度:即在 Spring 配置文件中配置的路由条目,资源名为对应的 routeId
  • 自定义 API 维度:用户可以利用 Sentinel 提供的 API 来自定义一些 API 分组

关于Spring Cloud Gateway这里就不做过多介绍,可以看我之前的文章springcloud 入门 之网关 springcloud gateway

集成入门

  1. 新建一个springboot项目子模块cloud_gateway

父模块依赖如下:

<properties>
     <java.version>1.8</java.version>
     <spring.cloud.version>2021.0.1</spring.cloud.version>
     <springboot.version>2.6.4</springboot.version>
     <spring.cloud.alibaba.version>2021.0.1.0</spring.cloud.alibaba.version>
 </properties>

 <dependencies>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>

     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-loadbalancer</artifactId>
     </dependency>

 </dependencies>
 <dependencyManagement>
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-dependencies</artifactId>
             <version>${springboot.version}</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-dependencies</artifactId>
             <version>${spring.cloud.version}</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>

         <dependency>
             <groupId>com.alibaba.cloud</groupId>
             <artifactId>spring-cloud-alibaba-dependencies</artifactId>
             <version>${spring.cloud.alibaba.version}</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
     </dependencies>
 </dependencyManagement>

若想跟 Sentinel Starter 配合使用,需要加上 spring-cloud-alibaba-sentinel-gateway 依赖,同时需要添加 spring-cloud-starter-gateway 依赖来让 spring-cloud-alibaba-sentinel-gateway 模块里的 Spring Cloud Gateway 自动化配置类生效,子模块cloud_gateway引入依赖:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

同时请将 spring.cloud.sentinel.filter.enabled 配置项置为 false(若在网关流控控制台上看到了 URL 资源,就是此配置项没有置为 false)。Sentinel 网关流控默认的粒度是 route 维度以及自定义 API 分组维度,默认不支持 URL 粒度。

  1. 修改配置文件
server.port=8086

# 应用名
spring.application.name=SentinelGateway
spring.cloud.gateway.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true

# spring cloud gateway 采用的是netty+webflux
spring.main.web-application-type=reactive

spring.cloud.sentinel.filter.enabled=false

# 路由配置
spring.cloud.gateway.routes[0].id=my_csdn_route
spring.cloud.gateway.routes[0].uri=http://blog.csdn.net/
spring.cloud.gateway.routes[0].predicates[0]=Path=/qq_39654841

如果项目中引入了 spring-boot-starter-web依赖, spring.main.web-application-type=reactive 这个配置一定要加上,因为Spring Cloud Gateway是使用netty+webflux方式实现的,跟spring-boot-starter-web冲突,启动时会报错。

  1. 测试

在配置文件中,我们配置了断言路径qq_39654841,在我们访问http://localhost:8086/qq_39654841路径时,Spring Cloud Gateway会帮我们重新定向到http://blog.csdn.net/qq_39654841

总结

本篇文章我们只是简单的介绍和使用了 Sentinel集成Spring Cloud Gateway,算是对 Sentinel网关限流有了一个简单的认识,想认识的更多可以去官网看看。

参考资料:

https://github.com/alibaba/Sentinel/wiki/%E7%BD%91%E5%85%B3%E9%99%90%E6%B5%81

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Cloud Alibaba是一个基于Spring Cloud的一组开源框架和组件,旨在为微服务架构提供解决方案。Sentinel GatewaySpring Cloud Alibaba中的一部分,是一个高性能的API网关,用于管理和保护微服务的访问权限。 2022年的Sentinel Gateway将在当前的功能基础上进行进一步的优化和增强。首先,它将提供更加灵活和强大的流量控制和熔断降级机制,以便更好地应对高并发和流量高峰情况。这将更好地保护后端微服务的稳定性和可靠性,确保系统的正常运行。 其次,Sentinel Gateway将提供更好的安全防护能力,通过集成常见的安全防护机制,如黑名单、白名单、IP过滤等,保护系统免受恶意攻击和非法访问。这将提升整个微服务架构的安全性,保护敏感数据和业务逻辑的安全性。 此外,Sentinel Gateway还将提供更加强大和灵活的路由配置功能,允许用户根据具体需求和策略进行动态路由转发。这将使得微服务架构在面对复杂的网络环境和多样化的服务调用场景时能够更加灵活和高效地进行消息传递和数据交换。 最后,Sentinel Gateway将进一步完善其监控和统计功能,提供更加全面和准确的系统运行状态和性能指标数据。这将帮助用户更好地理解和掌握系统的运行状况,及时发现和解决潜在的问题,提升整个微服务架构的可管理性和可维护性。 综上所述,2022年的Sentinel Gateway将在流量控制、熔断降级、安全防护、动态路由、监控统计等方面进行进一步的优化和增强,为微服务架构提供更加强大和可靠的API网关解决方案。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

索码理

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值