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();
}

在控制台设置限流方法等

相关推荐
曹朋羽21 小时前
spring cloud sentinel 动态规则配置
spring·spring cloud·sentinel
java叶新东老师4 天前
四、搭建springCloudAlibaba2021.1版本分布式微服务-加入openFeign远程调用和sentinel流量控制
分布式·微服务·sentinel
曹朋羽6 天前
Spring Cloud Alibaba Sentinel 基本工作原理源码阅读
sentinel
Zfox_9 天前
Redis:哨兵(Sentinel)
服务器·数据库·redis·缓存·sentinel
Bug改不动了11 天前
微服务雪崩防护最佳实践之sentinel
java·sentinel
朱杰jjj11 天前
Sentinel配置Nacos持久化
sentinel
AI_RSER12 天前
基于GEE与哨兵2号的土地覆盖分类方法及实现
机器学习·信息可视化·分类·sentinel·可视化·遥感·gee
@淡 定15 天前
Sentinel热点参数限流完整示例实现
windows·sentinel
okok__TXF23 天前
Sentinel入门篇【流量治理】
java·sentinel
述雾学java25 天前
Spring Cloud Feign 整合 Sentinel 实现服务降级与熔断保护
java·spring cloud·sentinel