部署 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 应用程序诊断利器
相关推荐
曾经的三心草5 小时前
微服务的编程测评系统11-jmeter-redis-竞赛列表
redis·jmeter·微服务
k↑10 小时前
微服务之注册中心与ShardingSphere关于分库分表的那些事
数据库·微服务·架构·shardingsphere
sniper_fandc10 小时前
Spring Cloud系列—Alibaba Sentinel授权与规则管理及推送
spring cloud·sentinel
Tadas-Gao12 小时前
Java设计模式全景解析:从演进历程到创新实践
java·开发语言·微服务·设计模式·云原生·架构·系统架构
xiao-xiang1 天前
redis-sentinel基础概念及部署
数据库·redis·sentinel
SirLancelot11 天前
K8s-kubernetes(二)资源限制-详细介绍
微服务·云原生·容器·kubernetes·k8s·devops·kubelet
为什么要内卷,摆烂不香吗1 天前
Docker容器技术全面解析(一):入门
docker·微服务·容器
孤狼程序员1 天前
【Spring Cloud 微服务】1.Hystrix断路器
java·spring boot·spring·微服务
为什么要内卷,摆烂不香吗2 天前
kubernetes(4) 微服务
linux·运维·微服务·容器·kubernetes
鼠鼠我捏,要死了捏3 天前
基于Spring Boot与gRPC的高性能微服务架构设计分享
spring boot·微服务·grpc