部署 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 应用程序诊断利器
相关推荐
我就是你毛毛哥23 分钟前
微服务的拆分原则
微服务·云原生·架构
我就是你毛毛哥25 分钟前
微服务的注册中心
微服务·云原生·架构
云技纵横43 分钟前
本地限流与 Redis 分布式限流的无缝切换 技术栈:Sentinel 线程池隔离 + Nginx + Kafka
redis·分布式·sentinel
赵榕1 小时前
RabbitMQ发布订阅模式多实例消费者防止重复消费实现方式
微服务·消息队列·rabbitmq
没有bug.的程序员1 小时前
Ribbon vs LoadBalancer 深度解析
jvm·后端·spring cloud·微服务·ribbon·架构·gc调优
想学后端的前端工程师1 小时前
【分布式系统架构设计实战:从单体到微服务】
微服务·云原生·架构
没有bug.的程序员12 小时前
Nacos vs Eureka 服务发现深度对比
jvm·微服务·云原生·容器·eureka·服务发现
黄俊懿16 小时前
【深入理解SpringCloud微服务】Seata(AT模式)源码解析——全局事务的回滚
java·后端·spring·spring cloud·微服务·架构·架构师
wodet19 小时前
golang实现的批量审核文本服务
微服务·golang
喵了几个咪21 小时前
开箱即用的 GoWind Admin|风行,企业级前后端一体中后台框架:分层设计的取舍之道(从 “简单粗暴” 到依赖倒置)
微服务·golang