SpringCloud Sentinel如何实现微服务流量控制?

2026-06-10 05:422阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计531个文字,预计阅读时间需要3分钟。

SpringCloud Sentinel如何实现微服务流量控制?

Sentinel 是什么?

1.概念:分布式服务架构的流量治理组件。

2.作用:

2.1 流量控制:QPS、线程数。 2.2 熔断降级:降级策略、时长、请求数等。 2.3 授权:黑白名单。 2.4 系统:熔断、限流、降级、授权等功能。

一、 sentinel是什么?

1.概念:

分布式服务架构的流量治理组件。

2.sentinel有什么作用?

2.1 流控:QPS、线程数

2.2 熔断降级:降级-->熔断策略、时长、请求数等

2.3 授权:黑白名单

2.4 系统自适应过载保护

提供保护机制让系统入口流量与负载达到平衡,使得系统在尽可能处理最多的请求

2.5 热点流量防护

二、 sentinel如何应用于项目中?

1.引入sentinel及持久化到nacos依赖

<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!-- Sentinel规则持久化至Nacos配置 --> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency>

2.配置

2.1 sentinel及nacos

spring: cloud: sentinel: enabled: true eager: true # 取消控制台懒加载,项目启动即连接Sentinel transport: client-ip: localhost dashboard: localhost:8080 datasource: ds: nacos: server-addr: cloud.lebao.site:8848 dataId: ams-admin-degrade-rules groupId: DEFAULT_GROUP data-type: json rule-type: flow

2.2 在nacos创建限流规则配置文件:

ams-admin-degrade-rules

[ { "resource": "/hello", "limitApp": "default", "grade": 1, "count": 5, "strategy": 0, "controlBehavior": 0, "clusterMode": false } ]

3.添加sentinel公共模块

3.1 创建网关过滤器PortalFilter

package com.ams.gateway.security; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.core.Ordered; import org.springframework.zhuanlan.zhihu.com/p/64786381

<hr style=" border:solid; width:100px; height:1px;" color=#000000 size=1">

随心所往,看见未来。Follow your heart,see night!<br/>

欢迎点赞、关注、留言,收藏及转发,一起学习、交流!

SpringCloud Sentinel如何实现微服务流量控制?

本文共计531个文字,预计阅读时间需要3分钟。

SpringCloud Sentinel如何实现微服务流量控制?

Sentinel 是什么?

1.概念:分布式服务架构的流量治理组件。

2.作用:

2.1 流量控制:QPS、线程数。 2.2 熔断降级:降级策略、时长、请求数等。 2.3 授权:黑白名单。 2.4 系统:熔断、限流、降级、授权等功能。

一、 sentinel是什么?

1.概念:

分布式服务架构的流量治理组件。

2.sentinel有什么作用?

2.1 流控:QPS、线程数

2.2 熔断降级:降级-->熔断策略、时长、请求数等

2.3 授权:黑白名单

2.4 系统自适应过载保护

提供保护机制让系统入口流量与负载达到平衡,使得系统在尽可能处理最多的请求

2.5 热点流量防护

二、 sentinel如何应用于项目中?

1.引入sentinel及持久化到nacos依赖

<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!-- Sentinel规则持久化至Nacos配置 --> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency>

2.配置

2.1 sentinel及nacos

spring: cloud: sentinel: enabled: true eager: true # 取消控制台懒加载,项目启动即连接Sentinel transport: client-ip: localhost dashboard: localhost:8080 datasource: ds: nacos: server-addr: cloud.lebao.site:8848 dataId: ams-admin-degrade-rules groupId: DEFAULT_GROUP data-type: json rule-type: flow

2.2 在nacos创建限流规则配置文件:

ams-admin-degrade-rules

[ { "resource": "/hello", "limitApp": "default", "grade": 1, "count": 5, "strategy": 0, "controlBehavior": 0, "clusterMode": false } ]

3.添加sentinel公共模块

3.1 创建网关过滤器PortalFilter

package com.ams.gateway.security; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.core.Ordered; import org.springframework.zhuanlan.zhihu.com/p/64786381

<hr style=" border:solid; width:100px; height:1px;" color=#000000 size=1">

随心所往,看见未来。Follow your heart,see night!<br/>

欢迎点赞、关注、留言,收藏及转发,一起学习、交流!

SpringCloud Sentinel如何实现微服务流量控制?