SpringCloud Alibaba Sentinel实现熔断与限流–学习笔记

有目标就不怕路远。年轻人.无论你现在身在何方.重要的是你将要向何处去。只有明确的目标才能助你成功。没有目标的航船.任何方向的风对他来说都是逆风。因此,再遥远的旅程,只要有目标.就不怕路远。没有目标,哪来的劲头?一车尔尼雷夫斯基

导读:本篇文章讲解 SpringCloud Alibaba Sentinel实现熔断与限流–学习笔记,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

1,Sentinel 是什么?

Sentinel: 分布式系统的流量防卫兵

sentinel与hystrix的比较:底层原理采用sentinel的设计思想
在这里插入图片描述
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、流量路由、熔断降级、系统自适应过载保护、热点流量防护等多个维度保护服务的稳定性。

Sentinel 具有以下特征:

丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Apache Dubbo、gRPC、Quarkus 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。同时 Sentinel 提供 Java/Go/C++ 等多语言的原生实现。
完善的 SPI 扩展机制:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。

2,Sentinel 的主要特性:

在这里插入图片描述

3,Sentinel能解决的问题:

				服务血崩
				服务降级
				服务熔断
				服务限流

4,Sentinel控制台和下载安装启动

Sentinel 分为两个部分:

核心库(Java 客户端)不依赖任何框架/库,能够运行于所有 Java 运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持。
控制台(Dashboard)基于 Spring Boot 开发,打包后可以直接运行,不需要额外的 Tomcat 等应用容器。

下载地址:https://github.com/alibaba/Sentinel/releases/tag/1.8.4
启动jar包:java- jar XXXXXXXXXXX.jar

5,访问sentinel管理界面

http://localhost:8080/#/login (登录名和密码均为sentinel)

在这里插入图片描述

Sentinel 控制台包含如下功能:

查看机器列表以及健康情况:收集 Sentinel 客户端发送的心跳包,用于判断机器是否在线。
监控 (单机和集群聚合):通过 Sentinel 客户端暴露的监控 API,定期拉取并且聚合应用监控信息,最终可以实现秒级的实时监控。
规则管理和推送:统一管理推送规则。
鉴权:生产环境中鉴权非常重要。这里每个开发者需要根据自己的实际情况进行定制。
在这里插入图片描述

5,初始化案例demo

首先启动nacos,端口号8848

然后启动sentinel,端口号8080

新建微服务 cloudalibaba-sentinel-service8401

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud-tigerhhzz</artifactId>
        <groupId>com.tigerhhzz.springcloud-tigerhhzz</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-sentinel-service8401</artifactId>

    <dependencies>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!--        后续做持久化用到-->
    <dependency>
        <groupId>com.alibaba.csp</groupId>
        <artifactId>sentinel-datasource-nacos</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--        <dependency>-->
    <!--            <groupId>org.mybatis.spring.boot</groupId>-->
    <!--            <artifactId>mybatis-spring-boot-starter</artifactId>-->
    <!--        </dependency>-->
    <!--        <dependency>-->
    <!--            <groupId>com.alibaba</groupId>-->
    <!--            <artifactId>druid-spring-boot-starter</artifactId>-->
    <!--            <version>1.1.10</version>-->
    <!--        </dependency>-->
    <!--        <dependency>-->
    <!--            <groupId>mysql</groupId>-->
    <!--            <artifactId>mysql-connector-java</artifactId>-->
    <!--        </dependency>-->
    <!--        <dependency>-->
    <!--            <groupId>org.springframework.boot</groupId>-->
    <!--            <artifactId>spring-boot-starter-jdbc</artifactId>-->
    <!--        </dependency>-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.tigerhhzz.springcloud-tigerhhzz</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    </dependencies>
</project>

application.yml

server:
  port: 8401
spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    sentinel:
      transport:
        dashboard: localhost:8080
        port: 8719
#      datasource:
#        ds1:
#          nacos:
#            server-addr: localhost:8848
#            dataId: ${spring.application.name}
#            groupId: DEFAULT_GROUP
#            data_type: json
#            rule_type: flow

management:
  endpoints:
    web:
      exposure:
        include: "*"

SentinelMain8401启动类

package com.tigerhhzz.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author tigerhhzz
 * @date 2022/6/20 11:00
 */
@SpringBootApplication
@EnableDiscoveryClient
public class SentinelMain8401 {
    public static void main(String[] args) {
        SpringApplication.run(SentinelMain8401.class,args);
    }
}

业务类FlowLimitController

package com.tigerhhzz.springcloud.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author tigerhhzz
 * @date 2022/6/20 11:05
 */
@RestController
public class FlowLimitController {

    @RequestMapping("testA")
    public String testA(){
        return "testA-------";
    }

    @RequestMapping("testB")
    public String testB(){
        return "testB-------";
    }

}

启动8401
8401服务注册到nacos8848,并且入住8080sentinel,8080sentinel开始监控8401微服务

SentinelMain8401微服务cloudalibaba-sentinel-service成功注册到nacos注册中心
在这里插入图片描述
实时监控界面
在这里插入图片描述

6,流控规则

在这里插入图片描述

  • QPS+直接失败: 表示1秒钟查询1次就是ok,若超过次数1,就直接-快速失败,报默认的兜底错误(Blocked by Sentinel (flow limiting))!思考:是否有自己定义的兜底方法? 永远有默认,永远有自定义!!!!
    在这里插入图片描述
    在这里插入图片描述

  • 线程数流控

  • 在这里插入图片描述

  • 流控模式—关联
    当关联的资源达到阙值时,就限流自己
    当与A关联的资源B达到阙值后,就限流A自己 B惹事,A挂了
    在这里插入图片描述

  • 流控效果—warmup

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/135760.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!