赞
踩
目录
官方给出的定义: Sentinel是分布式系统的流量防卫兵,可想而知,Sentinel对于分布式系统的重要性。
Sentinel 是面向云原生微服务的高可用流控防护组件。随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。简单来说,就是之前学习过的 Hystrix的升级版。
由上图可以看到,Sentinel 提供开箱即用的与其它开源框架/库的整合模块 。
Sentinel官网地址:https://github.com/alibaba/Sentinel
Hystrix | Sentinel |
---|---|
需要程序员手工搭建监控平台 | 单独一个组件、可以独立出来 |
没有一套Web界面可以给程序员进行更加细粒度化得配置,比如流控、速率控制、服务熔断、服务降级 | 直接界面化的细粒度统一配置 |
总的来说,Sentinel使用起来比较方便快捷,不需要程序员手动搭建监控平台,耦合度降低。
【a】下载地址:https://github.com/alibaba/Sentinel/releases
这里我们选择下载:sentinel-dashboard-1.7.0.jar作为本次演示所使用Sentinel版本。
【b】安装Sentinel控制台
Sentinel分为两个部分:
【c】运行命令
运行前提:需要最低Java8 JDK环境OK,8080端口不能被占用。我们都知道8080端口是 Tomcat 默认的端口,不知道为什么,Sentinel 选择了8080作为端口号。
在自己下载的 Sentinel Jar包目录中打开CMD命令行窗口,输入如下命令进行启动:
java -jar sentinel-dashboard-1.7.0.jar
启动完成后,可以看到Sentinel监听的端口是8080。
【d】访问Sentinel管理界面
启动成功之后,我们浏览器访问:http://localhost:8080/#/dashboard/home
我们使用默认的用户名和密码(都是sentinel)进行登录,登录成功之后,如下图所示:
如果能看到此界面,说明我们的Sentinel下载安装配置成功。
接下来,我们通过一个示例介绍如何将我们微服务与Sentinel监控平台整合起来,实现对微服务应用的监控、限流等操作。
我们新建一个module【springcloudalibaba-sentinel-service8401】,注意需要结合nacos实现服务注册功能,如果没了解过nacos,可以前往https://blog.csdn.net/Weixiaohuai/article/details/108302673 进行学习。
【a】pom.xml添加sentinel依赖:spring-cloud-starter-alibaba-sentinel
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>springcloud2020</artifactId>
- <groupId>com.wsh.springcloud</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
-
- <artifactId>springcloudalibaba-sentinel-service8401</artifactId>
-
- <dependencies>
- <!--SpringCloud ailibaba sentinel -->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
- </dependency>
- <!--SpringCloud ailibaba nacos -->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- </dependency>
- <!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到-->
- <dependency>
- <groupId>com.alibaba.csp</groupId>
- <artifactId>sentinel-datasource-nacos</artifactId>
- </dependency>
- <dependency>
- <groupId>com.wsh.springcloud</groupId>
- <artifactId>springcloud-api-commons</artifactId>
- <version>${project.version}</version>
- </dependency>
- <!--openfeign-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- </dependency>
- <!-- SpringBoot整合Web组件+actuator -->
- <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>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
-
- </dependencies>
- </project>

【b】aplication.yml:配置sentinel监控平台的地址
- server:
- port: 8401
- spring:
- application:
- name: springcloudalibaba-sentinel-service
- cloud:
- nacos:
- discovery:
- server-addr: localhost:8848 #指定nacos服务器地址
- sentinel:
- transport:
- dashboard: localhost:8080 #指定sentinel控制台的地址t
- port: 8719 #sentinel api端口, 默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
- management:
- endpoints:
- web:
- exposure:
- include: '*' #配置对外暴露端口

【c】启动类
- package com.wsh.springcloud.alibaba;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
-
- @EnableDiscoveryClient
- @SpringBootApplication
- public class SentinelServiceApplicaiton8401 {
- public static void main(String[] args) {
- SpringApplication.run(SentinelServiceApplicaiton8401.class, args);
- }
- }
【d】测试Controller
- package com.wsh.springcloud.alibaba.controller;
-
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- public class SentinelController {
-
- @GetMapping("/sentinel")
- public String sentinel() {
- return "hello, sentinel dashboard....";
- }
-
- }
【e】测试
Step1:启动 Nacos 服务器;
Step2:启动 Sentinel Dashboard 服务器;
Step3:运行【springcloudalibaba-sentinel-service8401】启动类,端口为8401;
Step4:然后访问http://localhost:8848/nacos,找到服务管理下的服务列表,能够看到一个服务名为 "springcloudalibaba-sentinel-service" 的应用程序注册至 Nacos 服务管理页面,如下图所示:
Step5:接着访问http://localhost:8080请求,切换到 Sentinel 登录页面进入到 Dashboard 首页。此时 Sentinel 控制台并没有监控任何微服务。
原因是 Sentinel 是采用了懒加载的机制,微服务【springcloudalibaba-sentinel-service8401】目前还没有流量信息可以监控,我们需要手动触发一个请求:http://localhost:8401/sentinel:
然后再次回到Sentinel Dashboard界面, 就可以看到【springcloudalibaba-sentinel-service8401】服务的监控流量信息等,如下图所示:
通过实时监控,可以看到波峰流量的流动效果,绿线代表通过,蓝线代表拒绝 。
此时 Sentinel 控制台正在监控微服务 【springcloudalibaba-sentinel-service8401】,也就是说Sentinel Dashboard 代替了 Hystrix Dashboard 功能。
本篇文章主要总结了Sentinel的主要特征以及通过示例介绍了如何将Sentinel作为我们微服务的流量监控、限流平台。总的来说,比起我们之前学习的Hystrix,简化了使用方式,至少不需要我们自己手动去搭建Hystrix Dashboard监控仪表盘。以上相关项目的代码我已经放在Gitee上,有需要的小伙伴可以去拉取进行学习:https://gitee.com/weixiaohuai/springcloud_Hoxton,由于笔者水平有限,如有不对之处,还请小伙伴们指正,相互学习,一起进步。
参考资料:
https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
https://sentinelguard.io/zh-cn/docs/quick-start.html
下面是笔者总结的关于Spring Cloud Alibaba教程系列文章目录,有需要的小伙伴可以前往学习:
2. Spring Cloud Alibaba Nacos之服务注册中心
3. Spring Cloud Alibaba Nacos之服务配置中心
4. Spring Cloud Alibaba Nacos集群和持久化配置
5. Spring Cloud Alibaba Sentinel之入门篇
6. Spring Cloud Alibaba Sentinel之流控规则篇
7. Spring Cloud Alibaba Sentinel之服务降级篇
8. Spring Cloud Alibaba Sentinel之热点参数限流篇
9. Spring Cloud Alibaba @SentinelResource配置详解
10. Spring Cloud Alibaba Sentinel之服务熔断篇
11. Spring Cloud Alibaba Sentinel之持久化篇
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。