安装Sentinel控制台与初始化演示工程 分手后的思念是犯贱 2023-10-11 20:44 1阅读 0赞 **目录** 一、Sentinel 二、安装Sentinel控制台 (一)sentinel组件由2部分构成 (二)安装步骤 三、初始化演示工程 四、流控模式 (一)快速失败 (二)关联资源 (三)链路 (四)预热(Warm Up) (五)排队等待 -------------------- ## 一、Sentinel ## 官网:[GitHub - alibaba/Sentinel: A powerful flow control component enabling reliability, resilience and monitoring for microservices. (面向云原生微服务的高可用流控防护组件)][GitHub - alibaba_Sentinel_ A powerful flow control component enabling reliability_ resilience and monitoring for microservices.] 中文文档:[https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D][https_github.com_alibaba_Sentinel_wiki_E4_BB_8B_E7_BB_8D] ![7093c9f126e745999e12199838c49541.png][] 理解为Hystrix的加强版即可 下载地址:[https://github.com/alibaba/Sentinel/releases][https_github.com_alibaba_Sentinel_releases] ![9ce3795d2558429fbbb7447a9ea623ac.png][] [Spring Cloud Alibaba Reference Documentation (spring-cloud-alibaba-group.github.io)][Spring Cloud Alibaba Reference Documentation _spring-cloud-alibaba-group.github.io] > **解决服务使用中的各种问题:** > > **服务雪崩、服务降级、服务熔断、服务限流** ## 二、安装Sentinel控制台 ## ### (一)sentinel组件由2部分构成 ### ![01e60b2717214e4caebb74c1e9c6d4e0.png][] 后台以及前台8080 ### (二)安装步骤 ### 下载地址:[https://github.com/alibaba/Sentinel/releases][https_github.com_alibaba_Sentinel_releases] 下载到本地sentinel-dashboard-1.7.0.jar **前提**:java8环境OK 8080端口不能被占用 以jar包方式运行 > **java -jar sentinel-dashboard-1.7.0.jar** **访问sentinel管理界面** 登录账号密码均为sentinel [http://localhost:8080][http_localhost_8080] ![0d11cc13fb754cc29cce148a1406c8ab.png][] ## 三、初始化演示工程 ## nacos要先启动成功 新建module cloudalibaba-sentinel-service8401 POM <dependencies> <!--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> <!--SpringCloud ailibaba sentinel --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </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> <!--日常通用jar包配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.6.3</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> YML server: port: 8401 spring: application: name: cloudalibaba-sentinel-service cloud: nacos: discovery: #Nacos服务注册中心地址 server-addr: localhost:8848 sentinel: transport: #配置Sentinel dashboard地址 dashboard: localhost:8080 #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口 port: 8719 management: endpoints: web: exposure: include: '*' 主启动 @EnableDiscoveryClient @SpringBootApplication public class MainApp8401 { public static void main(String[] args) { SpringApplication.run(MainApp8401.class, args); } } 业务类FlowLimitController @RestController public class FlowLimitController { @GetMapping("/testA") public String testA() { return "------testA"; } @GetMapping("/testB") public String testB() { return "------testB"; } } 启动Sentinel8080后启动该微服务 因为Sentinel采用的是懒加载方式,所以我们需要先访问微服务的方法,Sentinel控制台才会出现监控 ![a38365ea67fe478c8262024948911447.png][] ## 四、流控模式 ## 基本介绍 ![d82aa6b2449e4e4891fd15351249c8a4.png][] ### (一)快速失败 ### **快速失败(QPS)** ![a1a74bebf19144858ee34f1694e1174d.png][] **快速失败(线程)** 这里使线程睡眠模仿业务 @GetMapping("/testA") public String testA() { try { TimeUnit.MILLISECONDS.sleep(1000); } catch (Exception e) { System.out.println(e); } return "------testA"; } 开两个页面访问,当一个页面业务未解决完毕,别的线程被限流 ![c3b12bc9c5c24f25ad57f813c54b3738.png][] ### **(二)关联资源** ### 当关联的资源达到阈值时,就限流自己。比如,当与订单接口关联的资源支付接口达到阀值后,就限流订单接口自己 **设置效果** 当关联资源/testB的qps阀值超过1时,就限流/testA的Rest访问地址,当关联资源到阈值后限制配置好的资源名 ![70841d5bde8b4e5da7c50b20a052f5b3.png][] 使用postman模拟并发密集访问testB 先测试访问testB成功,这里要点击一下save保存请求内容 ![a469c3a0290943eebb437c1b1e7806ea.png][] postman里新建多线程集合组 ![3efd4ffcf12d4532b273e938cc533a9e.png][] 将访问地址添加进新新线程组 ![93caaa58ae2e4111977af909760f20eb.png][] Run ![cd1c38dd54b54e02b54ffc0d437ce5ee.png][] A被限流了 ![e584a87b176a439b82f4b4b9422e2bc8.png][] ### (三)链路 ### 链路:**阈值统计时,只统计从指定资源进入当前资源的请求,是对请求来源的限流** [sentinel流控规则\_机智的爆爆哥的博客-CSDN博客][sentinel_-CSDN] [Sentinel 链路流控模式失效 · Issue \#1213 · alibaba/Sentinel (github.com)][Sentinel _ _ Issue _1213 _ alibaba_Sentinel _github.com] ### **(四)预热(Warm Up)** ### 官网:[流量控制 · alibaba/Sentinel Wiki (github.com)][_ alibaba_Sentinel Wiki _github.com] ![acece857ca2540cea7b3694b11f7a6da.png][] 解释:默认coldFactor为3,即请求 QPS 从 threshold / 3 开始,经预热时长逐渐升至设定的 QPS 阈值。 **案例**,阀值为10+预热时长设置5秒。 系统初始化的阀值为10 / 3 约等于3,即阀值刚开始为3;然后过了5秒后阀值才慢慢升高恢复到10 ![0c9c47d6936b45eb87d5e04fac93201c.png][] **应用场景** 秒杀系统在开启的瞬间,会有很多流量上来,很有可能把系统打死,预热方式就是把为了保护系统,可慢慢的把流量放进来,慢慢的把阀值增长到设置的阀值。 ### (五)排队等待 ### **匀速排队,阈值必须设置为QPS** 官网 ![6e92f24476f14778981a0234074db5ec.png][] 设置含义:/testA每秒1次请求,超过的话就排队等待,等待的超时时间为20000毫秒。 ![d452e28dd73d4bc7b9896cb451899214.png][] 测试 ![1cccaa70b5e04244a661041fcd9a23d3.png][] [GitHub - alibaba_Sentinel_ A powerful flow control component enabling reliability_ resilience and monitoring for microservices.]: https://github.com/alibaba/Sentinel [https_github.com_alibaba_Sentinel_wiki_E4_BB_8B_E7_BB_8D]: https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D [7093c9f126e745999e12199838c49541.png]: https://img-blog.csdnimg.cn/7093c9f126e745999e12199838c49541.png [https_github.com_alibaba_Sentinel_releases]: https://github.com/alibaba/Sentinel/releases [9ce3795d2558429fbbb7447a9ea623ac.png]: https://img-blog.csdnimg.cn/9ce3795d2558429fbbb7447a9ea623ac.png [Spring Cloud Alibaba Reference Documentation _spring-cloud-alibaba-group.github.io]: https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html#_spring_cloud_alibaba_sentinel [01e60b2717214e4caebb74c1e9c6d4e0.png]: https://img-blog.csdnimg.cn/01e60b2717214e4caebb74c1e9c6d4e0.png [http_localhost_8080]: http://localhost:8080 [0d11cc13fb754cc29cce148a1406c8ab.png]: https://img-blog.csdnimg.cn/0d11cc13fb754cc29cce148a1406c8ab.png [a38365ea67fe478c8262024948911447.png]: https://img-blog.csdnimg.cn/a38365ea67fe478c8262024948911447.png [d82aa6b2449e4e4891fd15351249c8a4.png]: https://img-blog.csdnimg.cn/d82aa6b2449e4e4891fd15351249c8a4.png [a1a74bebf19144858ee34f1694e1174d.png]: https://img-blog.csdnimg.cn/a1a74bebf19144858ee34f1694e1174d.png [c3b12bc9c5c24f25ad57f813c54b3738.png]: https://img-blog.csdnimg.cn/c3b12bc9c5c24f25ad57f813c54b3738.png [70841d5bde8b4e5da7c50b20a052f5b3.png]: https://img-blog.csdnimg.cn/70841d5bde8b4e5da7c50b20a052f5b3.png [a469c3a0290943eebb437c1b1e7806ea.png]: https://img-blog.csdnimg.cn/a469c3a0290943eebb437c1b1e7806ea.png [3efd4ffcf12d4532b273e938cc533a9e.png]: https://img-blog.csdnimg.cn/3efd4ffcf12d4532b273e938cc533a9e.png [93caaa58ae2e4111977af909760f20eb.png]: https://img-blog.csdnimg.cn/93caaa58ae2e4111977af909760f20eb.png [cd1c38dd54b54e02b54ffc0d437ce5ee.png]: https://img-blog.csdnimg.cn/cd1c38dd54b54e02b54ffc0d437ce5ee.png [e584a87b176a439b82f4b4b9422e2bc8.png]: https://img-blog.csdnimg.cn/e584a87b176a439b82f4b4b9422e2bc8.png [sentinel_-CSDN]: https://blog.csdn.net/weixin_44353507/article/details/113815458 [Sentinel _ _ Issue _1213 _ alibaba_Sentinel _github.com]: https://github.com/alibaba/Sentinel/issues/1213 [_ alibaba_Sentinel Wiki _github.com]: https://github.com/alibaba/Sentinel/wiki/%E6%B5%81%E9%87%8F%E6%8E%A7%E5%88%B6 [acece857ca2540cea7b3694b11f7a6da.png]: https://img-blog.csdnimg.cn/acece857ca2540cea7b3694b11f7a6da.png [0c9c47d6936b45eb87d5e04fac93201c.png]: https://img-blog.csdnimg.cn/0c9c47d6936b45eb87d5e04fac93201c.png [6e92f24476f14778981a0234074db5ec.png]: https://img-blog.csdnimg.cn/6e92f24476f14778981a0234074db5ec.png [d452e28dd73d4bc7b9896cb451899214.png]: https://img-blog.csdnimg.cn/d452e28dd73d4bc7b9896cb451899214.png [1cccaa70b5e04244a661041fcd9a23d3.png]: https://img-blog.csdnimg.cn/1cccaa70b5e04244a661041fcd9a23d3.png
还没有评论,来说两句吧...