Sentinel熔断机制的学习与运用

本文详细介绍了Sentinel熔断机制在微服务架构中的应用,包括限流的作用、指标控制、算法原理,以及Guava、Semaphore等工具的限流实现。通过Sentinel的实例演示,展示了如何在Spring Boot项目中配置和使用Sentinel进行流量控制,包括设置限流规则、处理限流和熔断情况,并提供了测试验证。Sentinel作为阿里巴巴的高可用流量控制组件,能有效保护服务稳定性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@Sentinel熔断机制的学习与运用

Sentinel熔断机制的学习与运用

微服务架构现状图及未来图

在这里插入图片描述

限流的作用

1、保护系统避免被瞬时流量冲垮

2、预防恶意请求与


  • 针对请求限制

    1、应用(接口的处理能力(QPS/TPS)). RT -> Jmeter (压测)

    2、资源限制( cpu (线程池),内存(),网络资源)

    3、服务器

  • 如何控制流量。

1、限流的指标(可以容纳的流量,已经容纳的流量,可以接收的流量)阈值

2、限流的过程(通过算法来实现)

3、限流的结果(处理策略)

限流的算法


  • 计数器(ZK: RequestThrottle)

  • 滑动窗口
    在这里插入图片描述

  • 漏桶算法(用来控制传输速率)
    1、水的流出速度是固定的

  • 令牌桶算法
    在这里插入图片描述

令牌桶算法的使用:

1、创建一个Demo类用来写令牌桶算法的Demo类
在这里插入图片描述
2、导入guava依赖
阿里云官方地址:https://developer.aliyun.com/mvn/search

      <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>22.0</version>
        </dependency>

3、编写测试类

package com.chaoxing.springboot_sentinel;

import com.google.common.util.concurrent.RateLimiter;

/**
 * 单机版限流使用方法
 * @Author :Kun
 * @Date 2022/6/27 10:21
 */
public class RateLimiterDemo {
    /**
     * 令牌桶算法
     * Tps = 10
     * 表示每秒可以允许10个令牌
     */
    RateLimiter rateLimiter  = RateLimiter.create(10);
    /**
     *请求方法
     *如果令牌大于10会显示失败,小于10会显示成功
     * 测试需要开许多的线程进行测试
     */
    public  void  doRequest(){
        if(rateLimiter.tryAcquire()){
            System.out.println("success");
        }else {
            System.out.println("failed");
        }
    }
    public static void main(String[] args) {

    }

}

限流的实现


  • Guava
  • Semphore
  • 分布式限流

Sentinel


1、Sentinel流程图
在这里插入图片描述
2、Sentinel的定义
Sentinel是阿里中间件团队开源的,面向分布式服务架构的轻量级高可用流量控制组件,主要以流量为切入点,从流量控制,熔断降级,系统负载保护等多个维度来帮助用户保护服务的稳定性
3、Sentinel的技术图
在这里插入图片描述

服务熔断


  • 通过并发线程数进行限制
  • 针对响应时间

Sentinel的使用及案例


1.建立项目

1、建立一个自己的项目并添加Sentinal依赖
在这里插入图片描述
2、导入Sentinel依赖

   <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
            <version>1.8.0</version>
        </dependency>

3、建立Sentinel测试Demo
在这里插入图片描述
4、使用单机版测试是否可以限流(resource_Name为自己设置的资源名称)

package com.chaoxing.springboot_sentinel;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 单机版本
 *
 * @Author :Kun
 * @Date 2022/6/27 10:54
 */
public class SentinelDemo {
    public static void main(String[] args) {
        initFlowRule();
        while (true) {
            try {
                //ResourceName 表示资源,控制访问流量的点
                Entry entry = SphU.entry("resource_Name");
                System.out.println("执行成功");
            } catch (BlockException e) {
                e.printStackTrace();
                System.out.println("执行被拒绝");
            }
        }
    }

    /**
     *定义限流规则,初始化规则方便使用
     */
    private static void initFlowRule(){
        List<FlowRule> rules = new ArrayList<>();
        FlowRule flowRule = new FlowRule();
        //绑定规则名称,针对那个规则设置规则
        flowRule.setResource("resource_Name");
        //QPS或者并发数
        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        //QPS=5
        flowRule.setCount(5);
        //添加规则
        rules.add(flowRule);
        FlowRuleManager.loadRules(rules);
    }
}


5、定义Sentinel的控制器SentinelController
在这里插入图片描述

package com.chaoxing.springboot_sentinel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author :Kun
 * @Date 2022/6/27 11:32
 */
@RestController
public class SentinelController {

    @Autowired
    TestService testService;

   @GetMapping("/hello/{name}")
    public String sayHello(@PathVariable("name") String name){
       return  testService.doTest(name);
   }

}

6、定义TestService服务
在这里插入图片描述

package com.chaoxing.springboot_sentinel;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @Author :Kun
 * @Date 2022/6/27 11:36
 */
@Service
public class TestService {
    @SentinelResource(value = "doTest")//声明限流的资源
    public String doTest(@PathVariable String name){
        return  "hello,"+name;
    }
}

7、在SpringbootSentinelApplication中定义并使用规则

package com.chaoxing.springboot_sentinel;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
public class SpringBootSentinelApplication {

    public static void main(String[] args) {
        initFlowRule();
        SpringApplication.run(SpringBootSentinelApplication.class, args);
    }
    /**
     *定义限流规则,初始化规则方便使用
     */
    private static void initFlowRule(){
        List<FlowRule> rules = new ArrayList<>();
        FlowRule flowRule = new FlowRule();
        //绑定规则名称,针对那个规则设置规则
        flowRule.setResource("doTest");
        //QPS或者并发数
        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        //QPS=5
        flowRule.setCount(5);
        //添加规则
        rules.add(flowRule);
        FlowRuleManager.loadRules(rules);
    }
}

8、测试是否可以限流,打开http://localhost:8080/hello/jack
在这里插入图片描述
快速刷新看是否存在限流服务
在这里插入图片描述
成功显示,由此可看限流服务已经加载到微服务上,此时的限流服务是可以自己设置页面等操作的
比如在配置文件中使用spring.cloud.sentinel.block-page色湖之阻塞页面的跳转

#被阻塞页面的跳转
spring.cloud.sentinel.block-page=

9、修改TestService做限流处理

package com.chaoxing.springboot_sentinel;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @Author :Kun
 * @Date 2022/6/27 11:36
 */
@Service
public class TestService {
    @SentinelResource(value = "doTest",blockHandler ="blockHandler",fallback = "fallback")//声明限流的资源
    public String doTest(@PathVariable String name){
        return  "hello,"+name;
    }

    public String blockHandler(String name, BlockException e)//降级,限流触发的
    {
        return "被限流了";
    }

    public String fallback(String name){//降级,熔断触发的
        return "被降级了";
    }
}


此时如果限流的话会显示限流 处理的事务,如图所示:
在这里插入图片描述
详解:如果有blockHandler 触发blockHandler ,有fallback 触发fallback 两者都有触发blockHandler

Sentinel中的流量控制

  • 正常,则通过
  • 被限制,抛出FlowException(FlowException extends BlockException)
  • 同一个资源创建不同的规则,List
  • 一个规则有什么组成
    1、resource资源
    2、count阈值
    3、grade
    4、limitApp 针对的调用来源
    5、strategy,调用关系限流
    6、controllBehavior(直接拒绝,冷启动,匀速排队)

    并发线程数量


    QPS


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值