SpringCloudAlibaba-Sentinel流量监控

本文介绍如何使用Sentinel实现微服务流量控制与管理,包括Sentinel与Nacos的集成、客户端搭建及控制台使用等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开发环境

开发工具:IDEA 2021.3.2

JDK版本:JDK1.8

Maven版本:Maven3.8

SpringCloud版本:Hoxton.SR12

SpringCloudAlibaba版本:2.2.7.RELEASE

SpringBoot版本:2.3.12.RELEASE

Nacos版本:2.0.3

Sentinel版本:1.8.2

下载安装Nacos

根据Nacos官方文档,下载Nacos2.X源码或者压缩包进行安装

使用Nacos2.0.3下载安装教程下载安装Nacos

下载安装Sentinel

根据Sentinel官方文档,下载Sentinel源码或者压缩包(下载速度慢)进行安装

Sentinel简介

Sentinel 是面向分布式、多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性。

下载安装Sentinel-dashboard

①从Gitee克隆Sentinel1.8.2项目代码

# -b 后面是版本号
git clone -b 1.8.2 https://gitee.com/mirrors/Sentinel.git

②进入Sentinel\sentinel-dashboard目录下,打包sentinel-dashboard

cd Sentinel\sentinel-dashboard

mvn clean package

③进入target目录下,打开cmd输入java -jar sentinel-dashboard.jar 启动sentinel控制台

 ④访问http://localhost:8080/,登录sentinel控制台(默认账号密码都是sentinel)

搭建Sentinel客户端

搭建Spring Cloud项目

以Nacos作为注册中心,搭建spring cloud项目

创建Sentinel客户端

①创建maven模块,命名为cloud-provider-sentinel8021

②导入依赖

    <dependencies>
        <!--sentinel-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
        <!--nacos注册-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--springboot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

③创建application.yml配置文件

server:
  port: 8021

spring:
  application:
    name: cloud-provider-sentinel
  cloud:
    #nacos注册中心配置
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    #sentinel配置
    sentinel:
      transport:
        port: 8719
        dashboard: 127.0.0.1:8080

management:
  endpoints:
    web:
      exposure:
        include: "*"

④创建启动类

@EnableDiscoveryClient
@SpringBootApplication
public class ProviderSentinel8021 {
    public static void main(String[] args) {
        SpringApplication.run(ProviderSentinel8021.class, args);
    }
}

⑤创建controller

@RestController
@RequestMapping("/provider")
public class ProviderController {
    @Value("${server.port}")
    private String port;

    @GetMapping("/getUUID")
    public String getUUID() {
        return port + "\t:\t" + UUID.randomUUID();
    }
}

测试

启动Nacos注册中心

启动Sentinel控制台

启动cloud-provider-sentinel8021

测试

①浏览器访问http://localhost:8021/provider/getUUID,向后端发送请求

②浏览器访问http://localhost:8080/,打开Sentinel客户端并登录

 ③再向后端发送几次请求,观察实时数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值