文章目录
一、推模式架构图
图片引用自 https://github.com/alibaba/Sentinel/wiki/在生产环境中使用-Sentinel
二、原理简述
2.1. 组件版本关系
Spring Cloud Alibaba Version | Sentinel Version | Nacos Version | RocketMQ Version | Dubbo Version | Seata Version |
---|---|---|---|---|---|
2.2.6.RELEASE | 1.8.1 | 1.4.2 | 4.4.0 | 2.7.8 | 1.3.0 |
2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE | 1.8.0 | 1.4.1 | 4.4.0 | 2.7.8 | 1.3.0 |
Spring Cloud Version | Spring Cloud Alibaba Version | Spring Boot Version | Nacos Version | jdk |
---|---|---|---|---|
Spring Cloud Hoxton.SR9 | 2.2.6.RELEASE | 2.3.2.RELEASE | 1.4.2 | 1.8.202 |
2.2. 控制台推送规则
将规则推送到Nacos或其他远程配置中心
Sentinel客户端链接Nacos,获取规则配置;并监听Nacos配置变化,如发生变化,就更新本地缓存(从而让本地缓存总是和Nacos一致)
控制台监听Nacos配置变化,如发生变化就更新本地缓存(从而让控制台本地缓存总是和Nacos一致)
三、Sentinel控制台改造
控制台改造主要是为规则实现
DynamicRuleProvider:从Nacos上读取配置
DynamicRulePublisher:将规则推送到Nacos上
3.1. 下载源码
这里使用1.8版本演示
https://github.com/alibaba/Sentinel/tags
3.2. 修改pom
将
<!-- for Nacos rule publisher sample -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<scope>test</scope>
</dependency>
改为
<!-- for Nacos rule publisher sample -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
3.3. 重要文件复制
由于sentinel持久化默认不支持持久化,但是官方关于sentinel提供了nacos、zookeeper、apollo这3种方式,但是需要自己集成。
找到 sentinel-dashboard/src/test/java/com/alibaba/csp/sentinel/dashboard/rule/nacos
目录,将整个目录拷贝到 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos
,如图:
3.4. 注册地址修改
3.5. 请求实例需改
修改sentinel页面端保存规则请求的url控制层
修改后的配置
@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
3.6. 菜单新增
修改 sentinel-dashboard/src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html
,找到:
修改后
齐活儿啦!终于把流控规则改造成推模式持久化啦!
四、编译 & 启动
4.1. 先启动nacos
https://nacos.io/zh-cn/docs/quick-start.html
这里一windows环境演示
startup.cmd -m standalone
里面的配置先忽略
4.2. 编译打包
执行 mvn clean package -DskipTests
在项目的 target 目录找到sentinel-dashboard.jar
,执行 java -jar sentinel-dashboard.jar
启动控制台。
登录
4.3. 创建微服务 && 启动
创建微服务模块
添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<!--spring-cloud-alibaba 版本控制-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
配置文件
server:
port: 9000
spring:
cloud:
nacos:
discovery:
service: product-serv
server-addr: localhost:8848
sentinel:
transport:
dashboard: localhost:8080
filter:
enabled: false
management:
endpoints:
web:
exposure:
include: '*'
创建测试方法
package com.gblfy.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProductController {
//http://localhost:9000/product/" + productId
@GetMapping("/product/{productId}")
public String getProductName(@PathVariable Integer productId) {
return "IPhone 12";
}
}
启动应用
请求url
http://localhost:9000/product/1
4.4. 刷新sentinel
4.5. 登录nacos
4.6. sentinel 规则删除
4.7. nacos规则查看
规则也随之删除了
4.8. (企业案例)规则扩展
以上只是将流控规则扩展好了,其他需要其他模块,还需要根据官网文档自行扩展