SCA:二、Sentinel熔断、降级、限流

目录

[一、nacos-consumer pom.xml 新增 Sentinel 依赖](#一、nacos-consumer pom.xml 新增 Sentinel 依赖)

二、application.yml(nacos-consumer)核心配置

[三、降级兜底类 HelloFeignFallback(重点!)(实现HelloFeign接口)](#三、降级兜底类 HelloFeignFallback(重点!)(实现HelloFeign接口))

[四、Feign 接口改造 HelloFeign(增加 fallback)](#四、Feign 接口改造 HelloFeign(增加 fallback))

[五、下载 sentinel-dashboard-1.8.10.jar](#五、下载 sentinel-dashboard-1.8.10.jar)

六、测试步骤

七、高频坑点


接上篇:

eclipse中搭建Spring Boot4 + SCA 2025.1.0.0 + Nacos + OpenFeign 完整 Demo-CSDN博客

一、nacos-consumer pom.xml 新增 Sentinel 依赖

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

二、application.yml(nacos-consumer)核心配置

XML 复制代码
server:
  port: 8082

spring:
  application:
    name: nacos-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        username: nacos
        password: 111111
        #分组名
        group: test
    sentinel:
      transport:
        dashboard: 127.0.0.1:8079   # sentinel控制台地址
        port: 8719                  # 客户端和控制台通信端口,默认即可
feign:
  sentinel:
    enabled: true  # ✅ 关键开关,开启Feign对Sentinel支持,不打开降级不生效

三、降级兜底类 HelloFeignFallback(重点!)(实现HelloFeign接口)

HelloFeignFallback.java

java 复制代码
package com.demo.consumer;

import org.springframework.stereotype.Component;

@Component
public class HelloFeignFallback implements HelloFeign{

	@Override
	public String hello(String name) {
		// 降级逻辑:provider宕机、超时、sentinel熔断限流都会走到这里
        return "【Sentinel降级兜底】provider服务暂时不可用,请稍后重试!入参:" + name;
	}
	
}

要点:

  1. 必须 @Component 交给 Spring 管理
  2. 方法签名和 Feign 接口完全一致
  3. 降级里面不要查库、不要远程调用,尽量返回静态兜底,防止雪崩

四、Feign 接口改造 HelloFeign(增加 fallback)

HelloFeign.java (fallback = HelloFeignFallback.class)

java 复制代码
package com.demo.consumer;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

// name 和提供者 spring.application.name 保持一致
@FeignClient(name = "nacos-provider", fallback = HelloFeignFallback.class)
public interface HelloFeign {

    @GetMapping("/hello/{name}")
    String hello(@PathVariable("name") String name);
}

五、下载 sentinel-dashboard-1.8.10.jar

https://download.csdn.net/download/Bof_jangle/93817612

java 复制代码
java -Dserver.port=8079 -jar sentinel-dashboard-1.8.10.jar

访问http://127.0.0.1:8079 账号密码:sentinel / sentinel

六、测试步骤

  1. 启动 Nacos → provider-app → consumer-app
  2. 访问:http://127.0.0.1:8082/call/jangle 正常返回 provider 的结果
  3. 停掉 provider-app ,再次访问接口 → 直接返回兜底文字,降级生效 ✅
  4. 打开 Sentinel 控制台,簇点链路里能看到 Feign 资源:GET:http://nacos-provider/hello/{name}
  5. 给这个资源配置 熔断规则 / 限流规则 :
    • 熔断:慢调用比例、异常比例、异常数
    • 限流:QPS 阈值

七、高频坑点

  1. feign.sentinel.enabled=true 忘记配置 → fallback 完全不触发
  2. Fallback 类没加 @Component → 报错找不到 bean
  3. @PathVariable 不写参数名 → 遇到的 -parameters 报错
  4. Sentinel 控制台看不到链路:第一次访问接口之后才会注册资源

注:

  • 熔断(circuit break) :停止调用下游,保护系统,是动作
  • 降级(fallback):熔断之后,返回兜底内容的方案,是熔断之后的处理手段
  • 限流:请求太多了,新来的请求不让进来(挡外面)

一句话:熔断是 "不访问下游",降级是 "给个兜底返回"

相关推荐
露天赏雪2 天前
第3篇:Sentinel熔断降级实战:大模型接口抖动时的服务兜底方案
java·开发语言·人工智能·spring boot·spring·servlet·sentinel
凯基迪科技12 天前
圣天诺Sentinel LDK加密锁复制难点逆向分析
sentinel
Promising_GEO20 天前
GEE实战02期:从卫星波段开始:利用 Sentinel-2 自己计算 NDVI
java·sentinel·遥感·空间分析
我不会起名字32222 天前
Redis 从 0 到 1(九):主库宕机 30 秒自动切主——1 主 2 从 3 哨兵 + Go 客户端
redis·docker·sentinel·高可用·哨兵
Msshu12322 天前
PD快充诱骗取电芯片工作原理,XSP17诱骗取电5V9V12V15V20V功能测试
spring boot·spring cloud·sentinel·java-rocketmq·java-zookeeper·java-consul·java-activemq
流烟默1 个月前
Spring Cloud 微服务 Sentinel 热点参数与授权规则集成指南(附架构建议)
spring cloud·微服务·架构·sentinel
Thomas.Sir1 个月前
第23课:Sentinel熔断降级、系统自适应保护、集群限流
spring cloud·sentinel
jyOverQ1 个月前
Redis 主从复制、Sentinel 和 Cluster 到底有什么区别?
java·redis·sentinel
流烟默1 个月前
网关 Sentinel 限流实战:API 分组、规则加载与全量匹配踩坑记
nacos·gateway·sentinel·流控规则