package fw.config;
import com.alibaba.csp.sentinel.command.handler.ModifyParamFlowRulesCommandHandler;
import com.alibaba.csp.sentinel.datasource.*;
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 org.springframework.beans.factory.annotation.Value;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
* Sentinel持久化配置类
*/
public class FilePersistence implements InitFunc {
// 服务名称
@Value("spring.application.name")
private String appcationName;
@Override
public void init() throws Exception {
// 指定存放持久化规则的目录(在指定目录下新建一个当前“服务名称”的文件夹)
String ruleDir = System.getProperty("user.home") + "/sentinel-rules/" + appcationName;
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,
flowRuleListParser
);
FlowRuleManager.register2Property(flowRuleRDS.getProperty());
WritableDataSource<List<FlowRule>> flowRuleWDS = new FileWritableDataSource<>(
flowRulePath,
this::encodeJson
);
WritableDataSourceRegistry.registerFlowDataSource(flowRuleWDS);
// 降级规则
ReadableDataSource<String, List<DegradeRule>> degradeRuleRDS = new FileRefreshableDataSource<>(
degradeRulePath,
degradeRuleListParser
);
DegradeRuleManager.register2Property(degradeRuleRDS.getProperty());
WritableDataSource<List<DegradeRule>> degradeRuleWDS = new FileWritableDataSource<>(
degradeRulePath,
this::encodeJson
);
WritableDataSourceRegistry.registerDegradeDataSource(degradeRuleWDS);
// 系统规则
ReadableDataSource<String, List<SystemRule>> systemRuleRDS = new FileRefreshableDataSource<>(
systemRulePath,
systemRuleListParser
);
SystemRuleManager.register2Property(systemRuleRDS.getProperty());
WritableDataSource<List<SystemRule>> systemRuleWDS = new FileWritableDataSource<>(
systemRulePath,
this::encodeJson
);
WritableDataSourceRegistry.registerSystemDataSource(systemRuleWDS);
// 授权规则
ReadableDataSource<String, List<AuthorityRule>> authorityRuleRDS = new FileRefreshableDataSource<>(
authorityRulePath,
authorityRuleListParser
);
AuthorityRuleManager.register2Property(authorityRuleRDS.getProperty());
WritableDataSource<List<AuthorityRule>> authorityRuleWDS = new FileWritableDataSource<>(
authorityRulePath,
this::encodeJson
);
WritableDataSourceRegistry.registerAuthorityDataSource(authorityRuleWDS);
// 热点参数规则
ReadableDataSource<String, List<ParamFlowRule>> paramFlowRuleRDS = new FileRefreshableDataSource<>(
paramFlowRulePath,
paramFlowRuleListParser
);
ParamFlowRuleManager.register2Property(paramFlowRuleRDS.getProperty());
WritableDataSource<List<ParamFlowRule>> paramFlowRuleWDS = new FileWritableDataSource<>(
paramFlowRulePath,
this::encodeJson
);
ModifyParamFlowRulesCommandHandler.setWritableDataSource(paramFlowRuleWDS);
}
private Converter<String, List<FlowRule>> flowRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<FlowRule>>() {
}
);
private Converter<String, List<DegradeRule>> degradeRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<DegradeRule>>() {
}
);
private Converter<String, List<SystemRule>> systemRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<SystemRule>>() {
}
);
private Converter<String, List<AuthorityRule>> authorityRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<AuthorityRule>>() {
}
);
private Converter<String, List<ParamFlowRule>> paramFlowRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<ParamFlowRule>>() {
}
);
private void mkdirIfNotExits(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
}
private void createFileIfNotExits(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
}
private <T> String encodeJson(T t) {
return JSON.toJSONString(t);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
(十一)SpringCloud-Nacos Config(服务配置中心&动态更新配置&共享配置)本章代码 (272个子文件)
FilePersistence.class 8KB
GatewayConfiguration.class 5KB
Order.class 4KB
AuthGlobalFilter.class 4KB
Product.class 3KB
User.class 3KB
OrderController.class 2KB
GatewayConfiguration$3.class 2KB
AgeRoutePredicateFactory$1.class 2KB
ExceptionHandlerPage.class 2KB
LogSwitchGatewayFilterFactory$1.class 2KB
ProductController.class 2KB
ResponseData.class 2KB
LogSwitchGatewayFilterFactory$Config.class 2KB
AgeRoutePredicateFactory$Config.class 2KB
AgeRoutePredicateFactory.class 2KB
LogSwitchGatewayFilterFactory.class 1KB
OrderServiceImpl3BlockHandler.class 1KB
ProductServiceFallbackFactory$1.class 1KB
ProductServiceFallbackFactory.class 1KB
OrderServiceImpl3.class 1KB
FilterContextConfig.class 1KB
GatewayConfiguration$1.class 1KB
RequestOriginParserDefinition.class 1KB
ProductServiceImpl.class 1KB
NacosConfigController.class 993B
GatewayConfiguration$2.class 966B
ProductServiceFallback.class 950B
OrderServiceImpl.class 950B
OrderApplication.class 839B
OrderServiceImpl3Fallback.class 825B
ApiGatewayApplication.class 791B
ProductApplication.class 782B
UserApplication.class 781B
FilePersistence$5.class 708B
FilePersistence$4.class 707B
FilePersistence$2.class 703B
FilePersistence$1.class 697B
FilePersistence$3.class 695B
UserServiceImpl.class 624B
ProductService.class 601B
UserController.class 402B
ProductService.class 365B
ProductDao.class 359B
UserDao.class 354B
OrderDao.class 353B
OrderService.class 341B
UserService.class 293B
shop-order.iml 16KB
api-gateway.iml 13KB
shop-product.iml 13KB
shop-user.iml 13KB
shop-common.iml 5KB
springcloud-alibaba.iml 830B
com.alibaba.csp.sentinel.init.InitFunc 25B
com.alibaba.csp.sentinel.init.InitFunc 25B
FilePersistence.java 6KB
GatewayConfiguration.java 6KB
AuthGlobalFilter.java 3KB
ExceptionHandlerPage.java 3KB
AgeRoutePredicateFactory.java 2KB
LogSwitchGatewayFilterFactory.java 2KB
ProductController.java 2KB
OrderController.java 2KB
OrderController3.java 2KB
OrderServiceImpl3BlockHandler.java 1KB
OrderServiceImpl3.java 1KB
ProductServiceFallbackFactory.java 1KB
NacosConfigController.java 947B
FilterContextConfig.java 872B
RequestOriginParserDefinition.java 871B
ProductService.java 870B
ProductServiceFallback.java 783B
Order.java 652B
ProductServiceImpl.java 580B
OrderServiceImpl.java 550B
OrderApplication.java 529B
OrderServiceImpl3Fallback.java 529B
Product.java 458B
User.java 443B
ApiGatewayApplication.java 434B
UserApplication.java 433B
ProductApplication.java 433B
UserServiceImpl.java 344B
ProductDao.java 224B
UserDao.java 219B
OrderDao.java 216B
ProductService.java 214B
OrderService.java 207B
UserService.java 176B
UserController.java 145B
workspace.xml 63KB
uiDesigner.xml 9KB
pom.xml 2KB
pom.xml 1KB
pom.xml 1KB
pom.xml 1KB
pom.xml 1KB
pom.xml 1KB
compiler.xml 1KB
共 272 条
- 1
- 2
- 3
资源评论
钗头风
- 粉丝: 259
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功