tulip notes
首页
  • 学习笔记

    • 《Vue》
  • 踩坑日记

    • JavaScript
  • MQ
  • Nginx
  • IdentityServer
  • Redis
  • Linux
  • Java
  • SpringBoot
  • SpringCloud
  • MySql
  • docker
  • 算法与设计模式
  • 踩坑与提升
  • Git
  • GitHub技巧
  • Mac
  • 网络
  • 项目构建合集
  • 一些技巧
  • 面试
  • 一些杂货
  • 友情链接
  • 项目发布
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Star-Lord

希望一天成为大师的学徒
首页
  • 学习笔记

    • 《Vue》
  • 踩坑日记

    • JavaScript
  • MQ
  • Nginx
  • IdentityServer
  • Redis
  • Linux
  • Java
  • SpringBoot
  • SpringCloud
  • MySql
  • docker
  • 算法与设计模式
  • 踩坑与提升
  • Git
  • GitHub技巧
  • Mac
  • 网络
  • 项目构建合集
  • 一些技巧
  • 面试
  • 一些杂货
  • 友情链接
  • 项目发布
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • SCAlibaba-Nacos

  • SCAlibaba-Sentinel

  • 负载均衡与服务调用

  • 服务熔断与降级

    • Circuit Breaker的使用相关
    • Sentinel-使用相关
      • 流量治理组件
        • 能做啥
      • 相关资料
      • 安装与使用
      • 整合到SpringCloud
        • 原理简介
  • 服务链路追踪与网关

  • 分布式实战与细节

  • 其他

  • 《SpringCloud》笔记
  • 服务熔断与降级
EffectTang
2024-11-05
目录

Sentinel-使用相关

# Sentinel-使用相关

# 流量治理组件

它是一个流量治理组件,或者说它是一个用于实现服务熔断与降级,处理事故发生的组件。

它等价于 Spring Cloud Circuit Breaker。

# 能做啥

丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。

完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。

广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Apache Dubbo、gRPC、Quarkus 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。同时 Sentinel 提供 Java/Go/C++ 等多语言的原生实现。

完善的 SPI 扩展机制:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。

# 相关资料

sentinel-官网 (opens new window)

  • https://sentinelguard.io/zh-cn/

sentinel-github源码 (opens new window)

  • https://github.com/alibaba/Sentinel

sentinel-相关介绍-wiki (opens new window)

  • https://github.com/alibaba/Sentinel/wiki/%E4%B8%BB%E9%A1%B5

springcloud 整合sentinel文档 (opens new window)

  • https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinel

# 安装与使用

Sentinel 的使用可以分为两个部分:

  • 核心库(Java 客户端):不依赖任何框架/库,能够运行于 Java 8 及以上的版本的运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持(见 主流框架适配 (opens new window))。
  • 控制台(Dashboard):控制台主要负责管理推送规则、监控、集群限流分配管理、机器发现等。

日常将对应pom坐标引入项目中进行实现的即为客户端实现,但这样展示的数据不方便查看,因此强烈建议再运行一个控制台,而控制台对应的安装包,可以在源码中的release中进行下载,接着运行jar文件即可。

如:sentinel-release-Assets-Dashboard (opens new window)

  • https://github.com/alibaba/Sentinel/releases

运行成功后,访问对应地址,如 http://localhost:8099/#/login,即可访问sentinel,其中

从 Sentinel 1.6.0 起,Sentinel 控制台引入基本的登录功能,默认用户名和密码都是 sentinel。可以参考 鉴权模块文档 (opens new window) 配置用户名和密码。

# 整合到SpringCloud

先启动服务注册中心,nacos或者其他组件。

为项目引入sentinel pom坐标,

 <!--SpringCloud alibaba sentinel -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--nacos-discovery-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
10

引入sentinel时,还要引入nacos。

sentinel是一个分布式流量治理组件,Sentinel 不仅可以用于微服务架构中的多个服务,也可以用于单个项目或单个服务的流量控制和监控。

但这里是为了演示整合到SpringCloud,故要添加服务注册组件nacos。但单个项目添加的pom坐标跟cloud添加的并非相同。

修改application.yml或者application.properties文件:

spring:
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8099 #配置Sentinel dashboard控制台服务地址
        #port   Sentinel 客户端与控制台通信的本地端口
        port: 8719 #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
1
2
3
4
5
6
7

以下是 **port**更详细的说明:

  • 含义:指定 Sentinel 客户端在本地启动的 HTTP 服务的端口。
  • 作用:这个端口用于接收来自 Sentinel 控制台的请求和指令,例如动态规则的推送、实时监控数据的查询等。
  • 示例:8719 表示 Sentinel 客户端在本地启动一个 HTTP 服务,监听 8719 端口。

启动类的修改

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

@SpringBootApplication
@EnableDiscoveryClient
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class,args);
    }
}
1
2
3
4
5
6
7
8
9
10
11

编写测试的controller类

@RestController
public class FlowLimitController {

    @GetMapping("/sentinel/infoA")
    public String getTestA(){
        return "this is sentinel AAA";
    }

    @GetMapping("/sentinel/infoB")
    public String getTestB(){
        return "this is sentinel BBB";
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

之后,访问对应接口,如“/sentinel/infoA”,再在sentinel的dashboard,http://localhost:8099/#/dashboard 查看即可(若没数据刷新后再查看即可)

到此,SpringCloud整合sentinel完毕。

# 原理简介

  1. 注册与心跳:
    • 当 Sentinel 客户端启动时,会通过 dashboard 配置的地址向 Sentinel 控制台注册自己,并定期发送心跳包,告知控制台自己的状态。
  2. 规则推送:
    • Sentinel 控制台可以通过 port 配置的端口向 Sentinel 客户端推送动态规则,例如限流规则、熔断规则等。
  3. 监控数据上报:
    • Sentinel 客户端会通过 dashboard 配置的地址向控制台上报实时监控数据,例如 QPS、响应时间等。
上次更新: 2025/04/23, 16:23:16
Circuit Breaker的使用相关
Sleuth(Micrometer)

← Circuit Breaker的使用相关 Sleuth(Micrometer)→

最近更新
01
面向切面跟自定义注解的结合
05-22
02
时间跟其他数据的序列化
05-19
03
数据加密与安全
05-17
更多文章>
Theme by Vdoing | Copyright © 2023-2025 EffectTang
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式