Sentinel Dashborad实现推送降级则到nacos

原因

其实在网上有很多实现sentinel dashborad推送限流规则到nacos或者其他数据源的博客,比如使用Sentinel Dashboard动态推,拉数据同步到Nacos ,我是按照这篇博客实现的推送,如果有需要实现限流推送的可以看一下。但是发现找了很多都是推送限流规则,而降级规则没有降解如何推送的。我就当在这里进行补充一下吧。

实现

前提是你已经实现了限流规则的推送,限流规则的推送网上有很多,比如上面我放出的链接即可。

1.创建一个降级规则推送类

package com.alibaba.csp.sentinel.dashboard.rule.nacos;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author yuyang
 * @date 2020/6/4 10:17
 * @Description
 */
@Component("degradeRuleNacosPublisher")
public class DegradeRuleNacosPublisher implements DynamicRulePublisher> {

    @Autowired
    private ConfigService configService;
    @Autowired
    private Converter, String> degradeConverter;
    /**
     * 你的降级规则的后缀
     */
    public static final String FLOW_DATA_ID_POSTFIX = "-sentinel-degrade";
    /**
     * 你的降级规则groupId
     */
    public static final String GROUP_ID = "default";

    @Override
    public void publish(String app, List rules) throws Exception {
        AssertUtil.notEmpty(app, "app name cannot be empty");
        if (rules == null) {
            return;
        }
        configService.publishConfig(app + FLOW_DATA_ID_POSTFIX, GROUP_ID, degradeConverter.convert(rules));
    }
}

2.有了这个类之后,我们只需要在修改、创建降级规则的时候调用一次即可。找到类DegradeController,先引用我们创建的类


    @Autowired
    private DegradeRuleNacosPublisher degradeRuleNacosPublisher;

 3.我们来看一下新增、修改降级规则的部分源码发现,最后都会调用一个方法publishRules

//添加降级规则的部分源码
try {
	entity = repository.save(entity);
} catch (Throwable throwable) {
	logger.error("add error:", throwable);
	return Result.ofThrowable(-1, throwable);
}
if (!publishRules(app, ip, port)) {
	logger.info("publish degrade rules fail after rule add");
}
return Result.ofSuccess(entity);

//修改降级规则的部分源码
try {
	entity = repository.save(entity);
} catch (Throwable throwable) {
	logger.error("save error:", throwable);
	return Result.ofThrowable(-1, throwable);
}
if (!publishRules(entity.getApp(), entity.getIp(), entity.getPort())) {
	logger.info("publish degrade rules fail after rule update");
}
return Result.ofSuccess(entity);

4.那我们看一下这个方法并进行修改,try-catch部分是我加的。这样,降级规则就实现了推送到nacos

private boolean publishRules(String app, String ip, Integer port) {
	List rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
	try {
		logger.info("开始推送降级规则到nacos,rules:{}", JSON.toJSONString(rules));
		degradeRuleNacosPublisher.publish(app,rules);
		logger.info("推送降级规则到nacos成功");
	} catch (Exception e) {
		logger.error("推送降级规则到nacos错误:",e);
	}
	return sentinelApiClient.setDegradeRuleOfMachine(app, ip, port, rules);
}

总结

实现降级规则的推送其实很简单,仿照推送限流规则的方式即可实现

你可能感兴趣的:(spring,cloud,Sentinel推送,Sentinel,降级规则推送,降级规则推送nacos,限流降级)