一、提前准备
1、分析之前的服务降级熔断Hystrix
- 使用过我们的Hystrix后,就会发现它很是麻烦,很多东西我们要自己去配置表【就像我们之前学习的Eureka一样,我们不用再自己去建什么8001哦,作为Eureka的主机配置,使用了nacos就解决了,而sentinel就是替代料Hystrix】
- sentinel能干什么
2、sentinel下载安装
- 下载地址:https://github.com/alibaba/sentinel/releases
- 运行后端
把Tomcat关掉
- 访问客户端
3、sentinel初始化监控
- 前提是你的nacos启动好。
(1)建项目
- 导入依赖
<dependencies>
<dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<groupId>cn.mldn</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</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>
<!--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>
-
主启动
-
yaml
这个client-ip改为port:8719 -
随便写点逻辑
(2)启动相关8080,8401,8848
- nacos启动
- sentinel启动
- 8401启动
- 访问测试
什么都没有,因为sentinel是采用懒加载机制,只有你的项目访问过了,才会被加载。
- 再次测试
- 我请求不断的来,但是就是不行,CAO哦,烦死了,天天报错。
- 修改版本试一试还是不行,安装个Linux的试一试
- 最后自己才发现是那个client-ip应该是port,这个client-ip后续才会用。
二、流量控制
(一)sentinel流量控制
1、sentinel流控的位置
- 相关概念