SpringCloudAlibaba学习-04-Sentinel入门

sentinel 基本概念:
参考: https://www.cnblogs.com/crazymakercircle/p/14285001.html
参考: https://cloud.tencent.com/developer/news/50215
参考: https://www.cnblogs.com/molrang/p/11132380.html
参考: https://github.com/alibaba/Sentinel/wiki/%E4%B8%BB%E9%A1%B5

名词解释:
RT: Response Time 响应时间 指的是从请求端发起请求开始,到请求端接收到服务器端的返回结束。 越小越好
QPS: Query Per Second 每秒请求数,就是说服务器在一秒的时间内处理了多少个请求。
TPS:Transaction Per second 每秒钟系统能够处理事务或交易的数量,它是衡量系统处理能力的重要指标。 越大越好
吞吐量: 对于并发系统,通常需要用吞吐量作为性能指标。
并发用户数: 并发用户数是指系统可以同时承载的正常使用系统功能的用户的数量。与吞吐量相比,并发用户数是一个更直观但也更笼统的性能指标。

Sentinel解释:
Sentinel 是面向分布式服务架构的轻量级流量控制产品,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护、热点防护等多个维度来帮助开发者保障微服务的稳定性。
流量控制 参考: https://github.com/alibaba/Sentinel/wiki/%E6%B5%81%E9%87%8F%E6%8E%A7%E5%88%B6
流量控制(flow control),其原理是监控应用流量的 QPS 或并发线程数等指标,当达到指定的阈值时对流量进行控制, 以避免被瞬时的流量高峰冲垮,从而保障应用的高可用性。
熔断降级: 参考: https://github.com/alibaba/Sentinel/wiki/%E7%86%94%E6%96%AD%E9%99%8D%E7%BA%A7
现代微服务架构都是分布式的,由非常多的服务组成。不同服务之间相互调用,组成复杂的调用链路。
复杂链路上的某一环不稳定,就可能会层层级联,最终导致整个链路都不可用。因此我们需要对不稳定的弱依赖服务调用进行熔断降级,
暂时切断不稳定调用,避免局部不稳定因素导致整体的雪崩。

Sentinel 的使用:
控制台(Dashboard):控制台主要负责管理推送规则、监控、集群限流分配管理、机器发现等。
docker 安装 账号/密码 sentinelsentinel
‘’’
version: “3.7”
services:
sentinel_server:
image: bladex/sentinel-dashboard
container_name: sentinel
ports:
- 8858:8858
‘’’

项目引用pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.demo.springcloud_03_sentinel</groupId>
    <artifactId>sentinel-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sentinel-provider</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <spring.cloud.version>Hoxton.SR9</spring.cloud.version>
        <spring.boot.version>2.3.2.RELEASE</spring.boot.version>
        <spring.cloud.alibaba.version>2.2.6.RELEASE</spring.cloud.alibaba.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.0</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>${spring.cloud.alibaba.version}</version>
        </dependency>
        <!--nacos注册中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${spring.cloud.alibaba.version}</version>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <!--spring-boot-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- spring-cloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- spring-cloud-alibaba -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring.cloud.alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

项目配置文件 bootstrap.yml

spring:
  application:
    name: sentinel-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    sentinel:
      transport:
        dashboard:  localhost:8858   #sentinel
        port: 8720  # 默认是8719 同一台机器上8719会被占用
server:
  port: 8381

nacos可以用也可以不用,项目中随便写个controller类,使浏览器能访问就行
sentinel-dashboard和项目启动后,并不能在 sentinel-dashboard看到监控的项目,
要使用浏览器访问项目之后才能在sentinel-dashboard中看到项目
在这里插入图片描述

使用Sentinel进行限流,
Sentinel限流可以使用Sentinel核心库和 Sentinel-Dashboard。核心库不依赖 Sentinel-Dashboard
Sentinel-Dashboard采用配置的方式即可实现限流功能:
打开项目配置,找到<流控规则>, 新增流控规则
在这里插入图片描述
其中资源名填入访问路径 单机阈值 是指QPS每秒最多2个,多的会被限制

快速访问资源路径,就会出现不同的访问结果
一种使正常的结果,
一种是被Sentinel限流返回的结果

 Blocked by Sentinel (flow limiting)

同时在 实时监控中也能看到相关数据

在这里插入图片描述
其他配置可以参考: https://www.cnblogs.com/crazymakercircle/p/14285001.html
快速简单的配置参考这个: https://blog.csdn.net/jiangxiulilinux/article/details/102960668

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值