9.Sentinel哨兵

1.Sentinel

Sentinel(哨兵)是由阿里开源的一款流量控制和熔断降级框架,用于保护分布式系统中的应用免受流量涌入、超载和故障的影响。它可以作为微服务架构中的一部分,用于保护服务不被异常流量冲垮,从而提高系统的稳定性和可靠性。

1.1 Sentinel下载

https://github.com/alibaba/Sentinel/releaseshttps://github.com/alibaba/Sentinel/releases 命令行启动

java -Dserver.port=8858 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.2.jar

1.2 Sentinel使用

导入依赖

XML 复制代码
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>2.2.9.RELEASE</version>
</dependency>

配置主配置类

java 复制代码
spring:
  application:
    name: cart
  cloud:
    sentinel:
      transport:
        dashboard: 192.168.5.224:8858

网页端访问http://192.168.5.224:8858 (登录账号和密码默认都是sentinel)

1.3 Sentinel实现服务降级、限流

服务降级是一种在系统遇到异常情况或高负载时,为了保证核心功能的稳定性和可用性,暂时关闭某些非核心功能或者提供简化的服务。通过服务降级,系统可以在异常情况下继续正常运行,而不至于因为异常情况导致整个系统不可用。

方法上添加注解SentinelResource

falback:降级;addFallBack:降级方法的名字;blockHandler:限流;

java 复制代码
@SentinelResource(fallback = "addFallBack", blockHandler = "addBlockHandler")
@GetMapping("/test")
public String test(){
    return "cart";
}
public String addFallBack(){
    return "fallback";
}
//限流方法
public String addBlockHandler(BlockException e){
    return e.getMessage();
}

在控制台设置限流方法等

相关推荐
我是小妖怪,潇洒又自在18 小时前
springcloud alibaba(六)Sentinel 配置
spring·spring cloud·sentinel
X***07885 天前
使用bitnamiredis-sentinel部署Redis 哨兵模式
数据库·redis·sentinel
8***a8156 天前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel
咖啡不甜不好喝7 天前
sentinel踩坑记录
sentinel·springcloud
b***62957 天前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel
3***68848 天前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel
我是小妖怪,潇洒又自在8 天前
springcloud alibaba(五)Sentinel实现流控与降级
spring·spring cloud·sentinel
0***v7779 天前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel
m***66739 天前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel
小坏讲微服务10 天前
Spring Cloud Alibaba 整合 Scala 教程完整使用
java·开发语言·分布式·spring cloud·sentinel·scala·后端开发