本文已参与「新人创作礼」活动,一起开启掘金创作之路。
参考文档:在生产环境中使用 Sentinel · alibaba/Sentinel Wiki · GitHub
一,背景
sentinel 官方规则管理 提供三种模式
推送模式
说明
优点
缺点
API 将规则推送至客户端并直接更新到内存中,扩展写数据源(WritableDataSource)
简单,无任何依赖
不保证一致性;规则保存在内存中,重启即消失。严重不建议用于生产环境
扩展写数据源(WritableDataSource), 客户端主动向某个规则管理中心定期轮询拉取规则,这个规则中心可以是 RDBMS、文件 等
简单,无任何依赖;规则持久化
不保证一致性;实时性不保证,拉取过于频繁也可能会有性能问题。
扩展读数据源(ReadableDataSource),规则中心统一推送,客户端通过注册监听器的方式时刻监听变化,比如使用 Nacos、Zookeeper 等配置中心。这种方式有更好的实时性和一致性保证。生产环境下一般采用 push 模式的数据源。
规则持久化;一致性;快速
引入第三方依赖
默认我们引入
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
各种规则是默认保存到内存中的,每次项目重启之后规则都会消失不见。在这我们提供两种解决方案。Pull 模式 和 Push 模式
二,方案
1,Pull 模式 (将各种规则添加进不同的文件中)
编辑
package com.example.demo.config;
import com.alibaba.csp.sentinel.command.handler.ModifyParamFlowRulesCommandHandler;
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
import com.alibaba.csp.sentinel.datasource.FileWritableDataSource;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.WritableDataSource;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.alibaba.csp.sentinel.transport.util.WritableDataSourceRegistry;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
* 拉模式规则持久化
*
* @author lwc
*/
public class FileDataSourceInit implements InitFunc {
@Override
public void init() throws Exception {
String ruleDir = System.getProperty("user.dir") + "/src/main/resources/sentinel/rules";
String flowRulePath = ruleDir + "/flow-rule.json";
String degradeRulePath = ruleDir + "/degrade-rule.json";
String systemRulePath = ruleDir + "/system-rule.json";
String authorityRulePath = ruleDir + "/authority-rule.json";
String paramFlowRulePath = ruleDir + "/param-flow-rule.json";
this.mkdirIfNotExits(ruleDir);
this.createFileIfNotExits(flowRulePath);
this.createFileIfNotExits(degradeRulePath);
this.createFileIfNotExits(systemRulePath);
this.createFileIfNotExits(authorityRulePath);
this.createFileIfNotExits(paramFlowRulePath);
//流控规则
ReadableDataSource<String, List<FlowRule>> flowRuleRds = new FileRefreshableDataSource<>(flowRulePath, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
})
);
//将可读数据源注册至FlowRuleManager 这样当规则文件发生变化时,就会更新规则到内存
FlowRuleManager.register2Property(flowRuleRds.getProperty());
WritableDataSource<List<FlowRule>> flowRuleWds = new FileWritableDataSource<>(flowRulePath, JSON::toJSONString);
//将可写数据源注册至transport模块的WritableDataSourceRegistry中 这样收到控制台推送的规则时,Sentinel会先更新到内存,然后将规则写入到文件中
WritableDataSourceRegistry.registerFlowDataSource(flowRuleWds);
//降级规则
ReadableDataSource<String, List<DegradeRule>> degradeRuleRds = new FileRefreshableDataSource<>(degradeRulePath, source -> JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {
}));
DegradeRuleManager.register2Property(degradeRuleRds.getProperty());
WritableDataSource<List<DegradeRule>> degradeRuleWds = new FileWritableDataSource<>(degradeRulePath, JSON::toJSONString);
WritableDataSourceRegistry.registerDegradeDataSource(degradeRuleWds);
//系统规则
ReadableDataSource<String, List<SystemRule>> systemRuleRds = new FileRefreshableDataSource<>(systemRulePath, source -> JSON.parseObject(source, new TypeReference<List<SystemRule>>() {
}));
SystemRuleManager.register2Property(systemRuleRds.getProperty());
WritableDataSource<List<SystemRule>> systemRuleWds = new FileWritableDataSource<>(systemRulePath, JSON::toJSONString);
WritableDataSourceRegistry.registerSystemDataSource(systemRuleWds);
//授权规则
ReadableDataSource<String, List<AuthorityRule>> authorityRuleRds = new FileRefreshableDataSource<>(authorityRulePath, source -> JSON.parseObject(source, new TypeReference<List<AuthorityRule>>() {
}));
AuthorityRuleManager.register2Property(authorityRuleRds.getProperty());
WritableDataSource<List<AuthorityRule>> authorityRuleWds = new FileWritableDataSource<>(authorityRulePath, JSON::toJSONString);
WritableDataSourceRegistry.registerAuthorityDataSource(authorityRuleWds);
//热点参数规则
ReadableDataSource<String, List<ParamFlowRule>> paramFlowRuleRds = new FileRefreshableDataSource<>(paramFlowRulePath, source -> JSON.parseObject(source, new TypeReference<List<ParamFlowRule>>() {
}));
ParamFlowRuleManager.register2Property(paramFlowRuleRds.getProperty());
WritableDataSource<List<ParamFlowRule>> paramFlowRuleWds = new FileWritableDataSource<>(paramFlowRulePath, JSON::toJSONString);
ModifyParamFlowRulesCommandHandler.setWritableDataSource(paramFlowRuleWds);
}
private void mkdirIfNotExits(String filePath) {
File file = new File(filePath);
if (!file.exists()) {
boolean mkdirs = file.mkdirs();
}
}
private void createFileIfNotExits(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
boolean newFile = file.createNewFile();
}
}
}
配置加载,这里使用的是 SPI 加载, SPI全称Service Provider Interface,是Java提供的一套用来被第三方实现或者扩展的接口,它可以用来启用框架扩展和替换组件。 SPI的作用就是为这些被扩展的API寻找服务实现。感兴趣的可以去学习学习。
在resources 目录创建 META-INF\services,创建加载文件,文件名为接口名 com.alibaba.csp.sentinel.init.InitFunc ,内容为实现类路径 com.example.demo.config.FileDataSourceInit
编辑
重启项目,添加规则
编辑
我们发现文件已经创建
编辑
2,Push 模式 (将各种规则添加进nacos)
编辑
引入依赖
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
package com.example.demo.config;
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory;
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import java.util.Properties;
import java.util.concurrent.*;
import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy;
/**
* @author lwc
*/
public class CustomNacosDataSource<T> extends AbstractDataSource<String, T> {
private static final int DEFAULT_TIMEOUT = 3000;
private final ExecutorService pool;
private final Listener configListener;
protected final String groupId;
protected final String dataId;
private final Properties properties;
protected ConfigService configService;
public CustomNacosDataSource(String serverAddr, String groupId, String dataId, Converter<String, T> parser) {
this(buildProperties(serverAddr), groupId, dataId, parser);
}
public CustomNacosDataSource(final Properties properties, final String groupId, final String dataId, Converter<String, T> parser) {
super(parser);
this.pool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1), new NamedThreadFactory("sentinel-nacos-ds-update"), new DiscardOldestPolicy());
this.configService = null;
if (!StringUtil.isBlank(groupId) && !StringUtil.isBlank(dataId)) {
AssertUtil.notNull(properties, "Nacos properties must not be null, you could put some keys from PropertyKeyConst");
this.groupId = groupId;
this.dataId = dataId;
this.properties = properties;
this.configListener = new Listener() {
@Override
public Executor getExecutor() {
return CustomNacosDataSource.this.pool;
}
@Override
public void receiveConfigInfo(String configInfo) {
RecordLog.info(String.format("[CustomNacosDataSource] New property value received for (properties: %s) (dataId: %s, groupId: %s): %s", properties, dataId, groupId, configInfo), new Object[0]);
T newValue = CustomNacosDataSource.this.parser.convert(configInfo);
CustomNacosDataSource.this.getProperty().updateValue(newValue);
}
};
this.initNacosListener();
this.loadInitialConfig();
} else {
throw new IllegalArgumentException(String.format("Bad argument: groupId=[%s], dataId=[%s]", groupId, dataId));
}
}
private void loadInitialConfig() {
try {
T newValue = this.loadConfig();
if (newValue == null) {
RecordLog.warn("[CustomNacosDataSource] WARN: initial config is null, you may have to check your data source", new Object[0]);
}
this.getProperty().updateValue(newValue);
} catch (Exception var2) {
RecordLog.warn("[CustomNacosDataSource] Error when loading initial config", var2);
}
}
private void initNacosListener() {
try {
this.configService = NacosFactory.createConfigService(this.properties);
this.configService.addListener(this.dataId, this.groupId, this.configListener);
} catch (Exception var2) {
RecordLog.warn("[CustomNacosDataSource] Error occurred when initializing Nacos data source", var2);
var2.printStackTrace();
}
}
@Override
public String readSource() throws Exception {
if (this.configService == null) {
throw new IllegalStateException("Nacos config service has not been initialized or error occurred");
} else {
return this.configService.getConfig(this.dataId, this.groupId, 3000L);
}
}
@Override
public void close() {
if (this.configService != null) {
this.configService.removeListener(this.dataId, this.groupId, this.configListener);
}
this.pool.shutdownNow();
}
private static Properties buildProperties(String serverAddr) {
Properties properties = new Properties();
properties.setProperty("serverAddr", serverAddr);
return properties;
}
}
package com.example.demo.config;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.datasource.WritableDataSource;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author lwc
*/
public class NacosWritableDataSource<T> extends CustomNacosDataSource implements WritableDataSource<T> {
private final Lock lock = new ReentrantLock(true);
private final Converter<T, String> configEncoder;
public NacosWritableDataSource(String serverAddr, String groupId, String dataId, Converter<T, String> configEncoder) {
super(serverAddr, groupId, dataId, configEncoder);
this.configEncoder = configEncoder;
}
@Override
public void write(T value) throws Exception {
lock.lock();
try {
String convertResult = configEncoder.convert(value);
configService.publishConfig(dataId, groupId, convertResult);
} finally {
lock.unlock();
}
}
@Override
public void close() {
super.close();
}
}
package com.example.demo.config;
import com.alibaba.csp.sentinel.command.handler.ModifyParamFlowRulesCommandHandler;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.WritableDataSource;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.alibaba.csp.sentinel.transport.util.WritableDataSourceRegistry;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import java.util.List;
/**
* @author lwc
*/
public class NacosDataSourceInit implements InitFunc {
private static final String SERVER_ADDR = "localhost:8848";
private static final String GROUP_ID = "RENREN_CLOUD_GROUP";
private static final String FLOW_DATA_ID = "sentinel-flow-rule.json";
private static final String AUTHORITY_DATA_ID = "sentinel-authority-rule.json";
private static final String DEGRADE_DATA_ID = "sentinel-degrade-rule.json";
private static final String PARAM_DATA_ID = "sentinel-param-flow-rule.json";
private static final String SYSTEM_DATA_ID = "sentinel-system-rule.json";
@Override
public void init() {
//流控规则
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new CustomNacosDataSource<>(SERVER_ADDR, GROUP_ID, FLOW_DATA_ID, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
WritableDataSource<List<FlowRule>> writableFlowRuleDataSource = new NacosWritableDataSource<>(SERVER_ADDR, GROUP_ID, FLOW_DATA_ID, JSON::toJSONString);
WritableDataSourceRegistry.registerFlowDataSource(writableFlowRuleDataSource);
//降级规则
ReadableDataSource<String, List<DegradeRule>> degradeRuleDataSource = new CustomNacosDataSource<>(SERVER_ADDR, GROUP_ID, DEGRADE_DATA_ID, source -> JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {
}));
DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty());
WritableDataSource<List<DegradeRule>> writableDegradeRuleDataSource = new NacosWritableDataSource<>(SERVER_ADDR, GROUP_ID, DEGRADE_DATA_ID, JSON::toJSONString);
WritableDataSourceRegistry.registerDegradeDataSource(writableDegradeRuleDataSource);
//系统规则
ReadableDataSource<String, List<SystemRule>> systemRuleDataSource = new CustomNacosDataSource<>(SERVER_ADDR, GROUP_ID, SYSTEM_DATA_ID, source -> JSON.parseObject(source, new TypeReference<List<SystemRule>>() {
}));
SystemRuleManager.register2Property(systemRuleDataSource.getProperty());
WritableDataSource<List<SystemRule>> writableSystemRuleDataSource = new NacosWritableDataSource<>(SERVER_ADDR, GROUP_ID, SYSTEM_DATA_ID, JSON::toJSONString);
WritableDataSourceRegistry.registerSystemDataSource(writableSystemRuleDataSource);
//授权规则
ReadableDataSource<String, List<AuthorityRule>> authorityRuleDataSource = new CustomNacosDataSource<>(SERVER_ADDR, GROUP_ID, AUTHORITY_DATA_ID, source -> JSON.parseObject(source, new TypeReference<List<AuthorityRule>>() {
}));
AuthorityRuleManager.register2Property(authorityRuleDataSource.getProperty());
WritableDataSource<List<AuthorityRule>> writableAuthorityRuleDataSource = new NacosWritableDataSource<>(SERVER_ADDR, GROUP_ID, FLOW_DATA_ID, JSON::toJSONString);
WritableDataSourceRegistry.registerAuthorityDataSource(writableAuthorityRuleDataSource);
//热点参数规则
ReadableDataSource<String, List<ParamFlowRule>> paramFlowRuleDataSource = new CustomNacosDataSource<>(SERVER_ADDR, GROUP_ID, PARAM_DATA_ID, source -> JSON.parseObject(source, new TypeReference<List<ParamFlowRule>>() {
}));
ParamFlowRuleManager.register2Property(paramFlowRuleDataSource.getProperty());
WritableDataSource<List<ParamFlowRule>> writableParamFlowRuleDataSource = new NacosWritableDataSource<>(SERVER_ADDR, GROUP_ID, PARAM_DATA_ID, JSON::toJSONString);
ModifyParamFlowRulesCommandHandler.setWritableDataSource(writableParamFlowRuleDataSource);
}
}
指定加载
编辑
去nacos 创建文件
编辑
去sentinel 添加规则
编辑
去nacos查看,发现数据已经更新
编辑
3,直接修改sentinel 面板端源码,添加按按钮或者直接修改保存按钮,把数据同步到nacos等地方 源码重新打包
这种方法网上解决方案较多,这里不再赘述。