部署 Sentinel 控制台:实现流量管理和监控

序言

Sentinel 是阿里巴巴开源的一款流量防护与监控平台,它可以帮助开发者有效地管理微服务的流量,实现流量控制、熔断降级、系统负载保护等功能。本文将介绍如何在项目中部署和配置 Sentinel 控制台,实现微服务的流量防护和监控。

一、Sentinel 控制台部署流程

  1. 官网下载 Sentinel 控制台

  2. 启动 Sentinel 控制台

    bash 复制代码
    java \
    # 指定 Sentinel 控制台的端口为 8080
    -Dserver.port=8080 \
    # 配置 Sentinel 控制台的地址
    -Dcsp.sentinel.dashboard.server=localhost:8080 \
    # 指定 Sentinel 控制台的项目名称
    -Dproject.name=sentinel-dashboard \
    # 指定启动的 Java 应用程序的 JAR 文件
    -jar sentinel-dashboard.jar
  3. 打开浏览器输入网址 localhost:8080 进入 Sentinel 控制台。Sentinel 控制台需要登录,账号和密码默认都是 sentinel

二、微服务整合 Sentinel

  1. 引入依赖

    xml 复制代码
    <dependencyManagement>
      <dependencies>
        <!-- spring-cloud-alibaba 依赖版本 -->
        <dependency>
          <groupId>com.alibaba.cloud</groupId>
          <artifactId>spring-cloud-alibaba-dependencies</artifactId>
          <version>2021.0.5.0</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
    
        <!-- spring-boot 依赖版本 -->
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-dependencies</artifactId>
          <version>2.7.15</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
    
    <!-- 引入 sentinel 依赖 -->
    <dependency>
      <groupId>com.alibaba.cloud</groupId> 
      <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>
  2. application.yml 文件中编写 Sentinel 配置

    yaml 复制代码
    spring:
      cloud: 
        sentinel:
          transport:
          	# 指定 dashboard
            dashboard: localhost:8080
  3. 访问微服务接口

    bash 复制代码
    # 使用 curl 命令访问自定义 user-service 服务的 /test/helo 接口
    curl localhost:1920/test/helo

    多次访问之后的效果:

三、流量监控和管理

3.1 流量监控

在 Sentinel 控制台的实时监控页面,同一个服务下的所有机器的簇点信息会被汇总,并且秒级地展示在 "实时监控" 页面。默认情况下,实时监控仅存储 5 分钟以内的数据,如果需要持久化,需要通过调用实时监控接口来定制。

3.2 流量管理

Sentinel 控制台提供简单的规则管理以及推送的功能。开发人员可以根据具体的业务需求添加相应的规则实现流量的管理(其中流量管理包括流量控制、熔断降级、热点参数限流、来源访问控制等)。

四、FAQ

  1. Sentinel 控制台所在的机器时间需要与应用的机器时间保持一致,否则会导致拉不到实时的监控数据。

  2. Sentinel 控制台定义的规则仅保存在内存中,控制台重启之后,规则将会丢失。

  3. 在使用 Sentinel 时,注意 spring-bootspring-cloud-alibaba 的版本兼容关系。版本兼容关系,可参考下图或官网

推荐阅读

  1. Docker Compose:简化多容器应用部署
  2. cURL:命令行下的网络工具
  3. RabbitMQ(Docker 单机部署)
  4. 深入理解 Java 并发:AbstractQueuedSynchronizer 源码分析
  5. 深入了解 Arthas:Java 应用程序诊断利器
相关推荐
熙客20 小时前
SpringCloud概述
java·spring cloud·微服务
波波烤鸭1 天前
Redis 高可用实战源码解析(Sentinel + Cluster 整合应用)
数据库·redis·sentinel
阿里云云原生2 天前
HiMarket 正式开源,为企业落地开箱即用的 AI 开放平台
微服务
眠りたいです2 天前
基于脚手架微服务的视频点播系统-数据管理与网络通信部分的预备工作
c++·qt·ui·微服务·云原生·架构·媒体
码农小伙2 天前
单体到微服务拆分方案
微服务·架构
mask哥2 天前
详解mcp以及agen架构设计与实现
java·微服务·flink·大模型·ai agent·springai·mcp
好多173 天前
《微服务事务管理》
java·微服务·架构
hzzzzzo03 天前
微服务保护全攻略:从雪崩到 Sentinel 实战
数据库·微服务·sentinel
APItesterCris3 天前
构建分布式京东商品数据采集系统:基于 API 的微服务实现方案
分布式·微服务·架构
hzzzzzo03 天前
微服务网关全解析:从入门到实践
java·开发语言·微服务