核心内容摘要
解锁3DMAX视觉奇迹:进入成品网站的终极指南
前言Sentinel 原生版本的规则管理通过API 将规则推送至客户端并直接更新到内存中并不能直接用于生产环境。
不过官方也提供了一种 Push模式扩展读数据源ReadableDataSource规则中心统一推送客户端通过注册监听器的方式时刻监听变化比如使用 Nacos、Zookeeper 等配置中心。
这种方式有更好的实时性和一致性保证。
这里我们通过配置 Nacos 来实现流控规则的统一存储配置。
架构控制台推送规则至配置中心客户端通过监听事件从配置中心获取流控规则。
客户端配置pom.xml 引入dependency groupIdcom.alibaba.csp/groupId artifactIdsentinel-datasource-nacos/artifactId version
1.
3/version /dependency配置文件# nacos的访问地址配置参考 https://blog.52itstyle.vip/archives/4174/ spring.cloud.sentinel.datasource.ds.nacos.server-addr
47.
104.
1
19:8848 #nacos中存储规则的dataId,对于dataId使用了${spring.application.name}变量这样可以根据应用名来区分不同的规则配置 spring.cloud.sentinel.datasource.ds.nacos.dataId${spring.application.name}-flow-rules #nacos中存储规则的groupId spring.cloud.sentinel.datasource.ds.nacos.groupIdSENTINEL_GROUP #定义存储的规则类型 spring.cloud.sentinel.datasource.ds.nacos.rule-typeflow控制台配置修改 pom.xml原来的scopetest/scope去掉dependency groupIdcom.alibaba.csp/groupId artifactIdsentinel-datasource-nacos/artifactId /dependency把src/test下面的包com.alibaba.csp.sentinel.dashboard.rule.nacos拷贝到src/main/java下面。
修改 NacosConfig/** * author Eric Zhao * since
1.
0 */ Configuration public class NacosConfig { Value(${nacos.address}) private String address; Bean public ConverterListFlowRuleEntity, String flowRuleEntityEncoder() { return JSON::toJSONString; } Bean public ConverterString, ListFlowRuleEntity flowRuleEntityDecoder() { return s - JSON.parseArray(s, FlowRuleEntity.class); } Bean public ConfigService nacosConfigService() throws Exception { Properties properties new Properties(); properties.put(serverAddr,address); return ConfigFactory.createConfigService(properties); } }application.properties 配置引入 Nacos# nacos的访问地址配置参考 https://blog.52itstyle.vip/archives/4174/ nacos.address
47.
104.
1
19:8848FlowControllerV2指定对应的 Bean 开启 Nacos 适配。
Autowired Qualifier(flowRuleNacosProvider) private DynamicRuleProviderListFlowRuleEntity ruleProvider; Autowired Qualifier(flowRuleNacosPublisher) private DynamicRulePublisherListFlowRuleEntity rulePublisher;修改sidebar.html页面 流控规则路由从dashboard.flowV1改成dashboard.flow-- nacos 动态规则配置-- li ui-sref-activeactive ng-if!entry.isGateway a ui-srefdashboard.flow({app: entry.app}) i classglyphicon glyphicon-filter/inbsp;nbsp;流控规则/a /li如图所示界面会多了一个回到单机页面的按钮这里我们新增一个流控规则。
登录 Nacos 后台配置管理-配置列表点击进入配置详情配置内容如下[{ app: blog, clusterConfig: { fallbackToLocalWhenFail: true, sampleCount: 10, strategy: 0, thresholdType: 0, windowIntervalMs: 1000 }, clusterMode: false, controlBehavior: 0, count:
0, gmtCreate: 1568617671812, gmtModified: 1568622253889, grade: 1, id: 6, ip:
10.
136.
1
88, limitApp: default, port: 8720, resource: blogView, strategy: 0 }]小结生产环境下推送规则正确做法应该是 配置中心控制台/Sentinel 控制台 → 配置中心 → Sentinel 数据源 → Sentinel。
案例https://gitee.com/52itstyle/spring-boot-blog参考https://cloud.tencent.com/developer/article/1549863https://github.com/alibaba/Sentinelhttps://github.com/alibaba/Sentinel/tree/master/sentinel-dashboard