前期已实现结合nacos持久化sentinel流控与熔断规则功能,本次网关流控规则持久化依葫芦画瓢。
1、gateway网关sentinel控制台
2、sentinel服务 sidebar.html部分代码
网关流控规则与普通服务流控规则有区别。
<li ui-sref-active="active" ng-if="entry.isGateway">
<a ui-sref="dashboard.gatewayFlow({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控规则</a>
</li>
<li ui-sref-active="active" ng-if="!entry.isGateway">
<a ui-sref="dashboard.flow({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控规则</a>
</li>
3、定位到网关服务的controller类GatewayFlowRuleController.java
4、修改 NacosConfigUtil.java 添加网关流控dataId后缀常量
public static final String GATEWAY_FLOW_DATA_ID_POSTFIX = "-gateway-flow-rules";
5、修改nacosConfig.java 添加GatewayFlowRuleEntity的Converter转换
@Bean
public Converter<List<GatewayFlowRuleEntity>, String> gatewayFlowRuleEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter<String, List<GatewayFlowRuleEntity>> gatewayFlowRuleEntityDecoder() {
return s -> JSON.parseArray(s, GatewayFlowRuleEntity.class);
}
6、新增网关流控规则发布类GatewayFlowRuleNacosPublisher.java
@Component("gatewayFlowRuleNacosPublisher")
public class GatewayFlowRuleNacosPublisher implements DynamicRulePublisher<List<GatewayFlowRuleEntity>> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<List<GatewayFlowRuleEntity>, String> converter;
@Override
public void publish(String app, List<GatewayFlowRuleEntity> rules) throws Exception {
AssertUtil.notEmpty(app, "app name cannot be empty");
if (rules == null) {
return;
}
configService.publishConfig(app + NacosConfigUtil.GATEWAY_FLOW_DATA_ID_POSTFIX,
NacosConfigUtil.GROUP_ID, converter.convert(rules));
}
}
7、新增网关流控规则提供类GatewayFlowRuleNacosProvider.java
@Component("gatewayFlowRuleNacosProvider")
public class GatewayFlowRuleNacosProvider implements DynamicRuleProvider<List<GatewayFlowRuleEntity>> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<String, List<GatewayFlowRuleEntity>> converter;
@Override
public List<GatewayFlowRuleEntity> getRules(String appName) throws Exception {
String rules = configService.getConfig(appName + NacosConfigUtil.GATEWAY_FLOW_DATA_ID_POSTFIX,
NacosConfigUtil.GROUP_ID, 3000);
if (StringUtil.isEmpty(rules)) {
return new ArrayList<>();
}
return converter.convert(rules);
}
}
8、修改GatewayFlowRuleController.java
8.1
// @Autowired
// private SentinelApiClient sentinelApiClient;
@Autowired
@Qualifier("gatewayFlowRuleNacosProvider")
private DynamicRuleProvider<List<GatewayFlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("gatewayFlowRuleNacosPublisher")
private DynamicRulePublisher<List<GatewayFlowRuleEntity>> rulePublisher;
8.2 修改 @GetMapping(“/list.json”)
// List<GatewayFlowRuleEntity> rules = sentinelApiClient.fetchGatewayFlowRules(app, ip, port).get();
List<GatewayFlowRuleEntity> rules = ruleProvider.getRules(app);
if (rules != null && !rules.isEmpty()) {
for (GatewayFlowRuleEntity entity : rules) {
entity.setApp(app);
}
}
8.3 修改方法@PostMapping(“/new.json”)
entity = repository.save(entity);
publishRules(app);
8.4 修改方法@PostMapping(“/save.json”)
entity = repository.save(entity);
publishRules(app);
8.5 修改方法@PostMapping(“/delete.json”)
repository.delete(id);
publishRules(oldEntity.getApp());
8.6 新增方法publishRules(String app)
private void publishRules(/*@NonNull*/ String app) throws Exception {
List<GatewayFlowRuleEntity> rules = repository.findAllByApp(app);
rulePublisher.publish(app, rules);
}
9、验证已实现sentinel网关流控规则持久化,sentinel nacos修改规则可相互同步。
9.1 sentinel控制台新增一条网关流控规则,查看nacos新增了一条dataId为gateway-service-gateway-flow-rules的配置
9.2 修改nacos配置将阈值调整为2,sentinel控制台刷新规则阈值已更新为2。