sentinel流控降级与熔断

简介: sentinel流控降级与熔断

人们因为能忘却,所以自己能渐渐的脱离了受过的苦痛,也因为能忘却,所以照样得再犯前人的错误。——鲁迅

引入依赖

<!--    sentinel 降级熔断    -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--    与sentinel控制台进行通信    -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-transport-simple-http</artifactId>
    <version>1.7.1</version>
</dependency>

下载客户端jar包

然后输入命令运行

java -Dserver.port=9000 -Dcsp.sentinel.dashboard.server=localhost:9000 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar

若您的应用使用了 Spring AOP,您需要通过配置的方式将 SentinelResourceAspect 注册为一个 Spring Bean:

@Configuration
public class SentinelAspectConfiguration {
  @Bean
  public SentinelResourceAspect sentinelResourceAspect() {
            return new SentinelResourceAspect();
  }
}

然后在配置文件配置

sentinel:
  transport:
    port: 9001
    dashboard: localhost:9000
  filter:
    enabled: true
    url-patterns: /**

因为我们可以看到运行后输出的

2021-03-16 23:24:49.986 INFO 18604 — [ main] c.a.c.s.SentinelWebAutoConfiguration : [Sentinel Starter] register SentinelWebInterceptor with urlPatterns: [/*].

所以需要配置url-patterns: /**否则只有一层url会被监控到

/**
     * 登录成功时调用该接口,传入一句话
     *
     * @param word 参数 例子:?word=登录成功!
     * @return 返回json格式的map
     */
    @GetMapping("say")
    public AjaxJson say(@RequestParam String word) {
        return AjaxJson.success().put("data", word);
    }

访问控制台localhost:9000(上面配置的9000端口)

输入默认用户名密码sentinel

然后找到我们需要限流的接口点击流控

然后我们每秒就只能访问一次了

接下来是服务降级

我们可以在配置文件开启

feign:
  sentinel:
    enabled: true

然后实现我们的feign接口

package com.ruben.feign.fallback;
import com.ruben.feign.ConsumerService;
import com.ruben.pojo.dto.PageDTO;
import com.ruben.utils.AjaxJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
 * @ClassName: ConsumerServiceFallback
 * @Description: 我还没有写描述
 * @Date: 2021/3/16 0016 22:14
 * *
 * @author: <achao1441470436@gmail.com>
 * @version: 1.0
 * @since: JDK 1.8
 */
@Slf4j
@Service
public class ConsumerServiceFallback implements ConsumerService {
    @Override
    public AjaxJson list(PageDTO pageDTO) {
        log.error("服务挂了");
        return AjaxJson.error("");
    }
    @Override
    public AjaxJson dropWare() {
        log.error("服务挂了");
        return AjaxJson.error("");
    }
}

最后在feign接口使用@FeignClientfallback参数指定降级实现

package com.ruben.feign;
import com.ruben.feign.fallback.ConsumerServiceFallback;
import com.ruben.pojo.dto.PageDTO;
import com.ruben.utils.AjaxJson;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
@FeignClient(value = "ruben-consumer", fallback = ConsumerServiceFallback.class)
public interface ConsumerService {
    @GetMapping("study/list")
    AjaxJson list(@RequestBody PageDTO pageDTO);
    @GetMapping("ware")
    AjaxJson dropWare();
}

然后访问我们的接口如果出现异常,则会调用我们的降级实现

我们再配置熔断

点击我们远程接口的降级按钮

配置完成后如果我们再远程调用在5秒内异常比例超过百分之八十,则之后都会直接调用我们的降级实现了

自定义受保护的资源

可以在接口上加@SentinelResource注解

也可以在代码中

@Override
    @Transactional
    @GlobalTransactional
    public AjaxJson order() {
        try (Entry entry = SphU.entry("resourceName")) {
            consumerService.dropWare();
//        mpOrderMapper.insert(OrderPO.builder().id(1L).build());
        } catch (BlockException e) {
            return AjaxJson.error("慢点");
        }
        return AjaxJson.success();
    }
}

加上try(Entry entry = SphU.entry("{资源名}")){}catch(BlockException e){}

后即可把这段代码作为一个受保护的资源

我们可以在catch中编写我们的降级方法

然后我们需要在流控规则中新建

然后可以看到我们成功实现流控我们的受保护资源

相关文章
|
11天前
|
缓存 数据安全/隐私保护 Sentinel
断路精灵:探秘Sentinel熔断策略的神奇效果
断路精灵:探秘Sentinel熔断策略的神奇效果
44 0
|
11天前
|
监控 API 开发者
Sentinel之道:流控模式解析与深度探讨
Sentinel之道:流控模式解析与深度探讨
66 0
|
11天前
|
Java 数据库连接 Maven
如何使用Sentinel实现流控和降级
通过以上步骤,你可以使用Sentinel实现应用的流量控制和降级操作,以保护系统在高流量或不稳定情况下的稳定性。欢迎关注威哥爱编程,一起学习成长。
|
11天前
|
SpringCloudAlibaba 监控 Java
SpringCloud Alibaba Sentinel实现熔断与限流--学习笔记
SpringCloud Alibaba Sentinel实现熔断与限流--学习笔记
32 0
|
11天前
|
SQL Go 数据库
【Sentinel Go】新手指南、流量控制、熔断降级和并发隔离控制
【2月更文挑战第12天】随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式、多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性。
73 0
|
11天前
|
监控 Java API
Sentinel熔断限流真的太丝滑了
Sentinel熔断限流真的太丝滑了
111 0
|
11天前
|
监控 Java API
数字护盾:深度探讨Sentinel的三大流控策略
数字护盾:深度探讨Sentinel的三大流控策略
25 0
|
11天前
|
监控 Java Sentinel
springcloud4-服务熔断hystrix及sentinel
springcloud4-服务熔断hystrix及sentinel
28 0
|
11天前
|
监控 测试技术 数据安全/隐私保护
如何集成Sentinel实现流控、降级、热点规则、授权规则总结
如何集成Sentinel实现流控、降级、热点规则、授权规则总结
89 0
|
11天前
|
Sentinel
一文速通Sentinel熔断及降级规则
一文速通Sentinel熔断及降级规则