前言
springcloud-alibaba-sentinel(1)sentinel流量卫兵介绍
springcloud-alibaba-sentinel(2)sentinel下载安装运行
在前边篇章中,已经讲解并下载运行了sentinel
,接下啦,咱们就来进行实操使用!!!
我们既然是微服务组件,自然也就结合着springcloud微服务项目进行演练!
一、启动sentinel-dashboard
sentinel-dashboard
即sentinel
服务端以及web管理平台
二、微服务项目引入依赖
要使用sentinel
的功能必须引入核心依赖
<!--引入sentinel依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
如果您是首次springcloud
与springcloud-alibaba
组件混合使用,您可参考我下方的配置
1.要注意springboot版本号
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2.指定cloud与cloud-alibaba版本号
<properties>
<java.version>1.8</java.version>
<springcloud.alibaba.version>2.2.3.RELEASE</springcloud.alibaba.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
3.指定依赖管理
<dependencyManagement>
<dependencies>
<!--指定spring-cloud-alibaba版本-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${springcloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--指定spring-cloud版本-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
4.引入相关组件依赖
例如我这里同时使用了nacos与sentinel
<!--引入Nacos配置中心客户端依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--open-feign依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--Nacos配置中心客户端依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- sentinel客户端依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
三、项目配置
我们需要在我们的项目配置文件中指明sentinel-dashboard
的地址以及与dashboard
的通信地址
spring:
cloud:
sentinel:
transport:
# sentinel-dashboard 地址
dashboard: 192.168.125.11:8819
# dashboard 通信端口 默认为8719
port: 8719
# 发送心跳间隔时间
heartbeat-interval-ms: 5000
四、启动项目查看setinel控制台
刚登陆sentinel控制台,sentinel界面为空白!
原因:默认情况下sentiel为延迟加载,不会在启动之后立即创建服务监控,需要对服务进行调用时才会初始化
我们需要调用下我们的某一接口,sentinel控制台才会显示当前连接到的服务列表
@GetMapping("/test/sentinel")
public String get() {
return "赵钱孙李"+new Random().nextInt(20);
}
请求几次后,发现左侧应用列表已经我们启动的服务
应用列表名,是读取项目中spring.application.name
配置进行获取的
五、sentinel初体验
展开我们的服务app-login
实时监控页面已有了信息
QPS:请求并发数,白话就是当前的请求频率(此刻一秒内有多少个请求)
此监控仍有一定的延时性,并非真正实时
,延迟大约两秒左右!
图表的数据都是sentinel控制台定时调用我们的sentinel服务端接口进行获取的!所有存在一定延迟性
数据来源流程:
我们在服务的配置文件中配置好sentinel服务端地址以及通信地址(默认8719)后,当我们请求服务时会被sentinel收集,并将数据交给sentine-dashboard…页面访问sentinel-dashboard后端接口获取数据进行展示