部署 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 应用程序诊断利器
相关推荐
kkkkk0211066 小时前
微服务学习笔记(黑马商城)
java·spring boot·spring·spring cloud·sentinel·mybatis·java-rabbitmq
阿里云云原生7 小时前
阿里云微服务引擎 MSE 及 API 网关 2025 年 9 月产品动态
微服务
二宝1529 小时前
黑马商城day3-微服务01
微服务·云原生·架构
roykingw9 小时前
【终极面试集锦】如何设计微服务熔断体系?
java·微服务·面试
我命由我123459 小时前
Spring Cloud - Spring Cloud 微服务概述 (微服务的产生与特点、微服务的优缺点、微服务设计原则、微服务架构的核心组件)
java·运维·spring·spring cloud·微服务·架构·java-ee
我命由我1234510 小时前
Spring Cloud - Spring Cloud 注册中心与服务提供者(Spring Cloud Eureka 概述、微服务快速入门、微服务应用实例)
java·spring boot·spring·spring cloud·微服务·eureka·java-ee
鼠鼠我捏,要死了捏10 小时前
微服务熔断降级方案对比:Hystrix、Resilience4j与Sentinel实践
微服务·熔断·resilience4j
没有bug.的程序员12 小时前
金融支付分布式架构实战:从理论到生产级实现
java·分布式·微服务·金融·架构·分布式调度系统
没有bug.的程序员16 小时前
分布式架构未来趋势:从云原生到智能边缘的演进之路
java·分布式·微服务·云原生·架构·分布式系统
007php0071 天前
百度面试题解析:微服务架构、Dubbo、Redis及其一致性问题(一)
redis·百度·docker·微服务·容器·职场和发展·架构