- 本次引用版本 1.7.2 目前最新版本为1.8.0 git地址
从 Sentinel 1.4.0 开始,sentinel抽取出了接口用于向远程配置中心推送规则以及拉取规则:1.
DynamicRuleProvider<T>
: 拉取规则 2.DynamicRulePublisher<T>
: 推送规则用户需实现
DynamicRuleProvider
和DynamicRulePublisher
接口,即可实现应用维度推送
第一步,启动redis 从redis官网下载redis
第二步:sentinel-dashboard 配置修改(或者直接下载已经改好的jar )
在: com.alibaba.csp.sentinel.dashboard.rule 目录下添加新的包redis用于存放以下几个类
DegradeRuleRedisProvider.java
package com.alibaba.csp.sentinel.dashboard.rule.redis; import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity; import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider; import com.alibaba.csp.sentinel.util.StringUtil; import com.alibaba.fastjson.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; /** * @package: com.alibaba.csp.sentinel.dashboard.rule.redis * @title: FlowRuleRedisProvider * @projectName: sentinel-parent * @author zhuhongqiang3 * @date: 2020-05-18 10:30 * @version: V1.0 * @retrun com.alibaba.csp.sentinel.dashboard.rule.redis.sentinel-parent */ @Component("degradeRuleRedisProvider") public class DegradeRuleRedisProvider implements DynamicRuleProvider<List<DegradeRuleEntity>> { @Autowired private RedisUtil redisConfigUtil; @Override public List<DegradeRuleEntity> getRules(String appName) throws Exception { String rules = redisConfigUtil.getString(RuleConsts.RULE_DEGRADE + appName); if (StringUtil.isEmpty(rules)) { return new ArrayList<>(); } return JSONObject.parseArray(rules,DegradeRuleEntity.class); } }
package com.alibaba.csp.sentinel.dashboard.rule.redis;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @package: com.alibaba.csp.sentinel.dashboard.rule.redis
* @title: FlowRuleRedisPublisher
* @projectName: sentinel-parent
* @author zhuhongqiang3
* @date: 2020-05-18 10:30
* @version: V1.0
* @retrun com.alibaba.csp.sentinel.dashboard.rule.redis.sentinel-parent
*/
@Component("degradeRuleRedisPublisher")
public class DegradeRuleRedisPublisher implements DynamicRulePublisher<List<DegradeRuleEntity>> {
@Autowired
private RedisUtil redisConfigUtil;
@Override
public void publish(String app, List<DegradeRuleEntity> rules) throws Exception {
AssertUtil.notEmpty(app, "app name cannot be empty");
if (rules == null) {
return;
}
String strs = JSON.toJSONString(rules);
redisConfigUtil.setString(RuleConsts.RULE_DEGRADE + app, strs);
}
}
package com.alibaba.csp.sentinel.dashboard.rule.redis;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* @package: com.alibaba.csp.sentinel.dashboard.rule.redis
* @title: FlowRuleRedisProvider
* @projectName: sentinel-parent
* @description: //TODO
* @author zhuhongqiang3
* @date: 2020-05-18 10:30
* @version: V1.0
* @retrun com.alibaba.csp.sentinel.dashboard.rule.redis.sentinel-parent
*/
@Component("flowRuleRedisProvider")
public class FlowRuleRedisProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {
@Autowired
private RedisUtil redisConfigUtil;
@Override
public List<FlowRuleEntity> getRules(String appName) throws Exception {
String rules = redisConfigUtil.getString(RuleConsts.RULE_FLOW + appName);
if (StringUtil.isEmpty(rules)) {
return new ArrayList<>();
}
return JSONObject.parseArray(rules,FlowRuleEntity.class);
}
}
package com.alibaba.csp.sentinel.dashboard.rule.redis;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @package: com.alibaba.csp.sentinel.dashboard.rule.redis
* @title: FlowRuleRedisPublisher
* @projectName: sentinel-parent
* @description: //TODO
* @author zhuhongqiang3
* @date: 2020-05-18 10:30
* @version: V1.0
* @retrun com.alibaba.csp.sentinel.dashboard.rule.redis.sentinel-parent
*/
@Component("flowRuleRedisPublisher")
public class FlowRuleRedisPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {
@Autowired
private RedisUtil redisConfigUtil;
@Override
public void publish(String app, List<FlowRuleEntity> rules) throws Exception {
AssertUtil.notEmpty(app, "app name cannot be empty");
if (rules == null) {
return;
}
String strs = JSON.toJSONString(rules);
redisConfigUtil.setString(RuleConsts.RULE_FLOW + app, strs);
}
}
package com.alibaba.csp.sentinel.dashboard.rule.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* @author zhuhongqiang3
* @package: com.alibaba.csp.sentinel.dashboard.rule.redis
* @title: RedisUtil
* @projectName: sentinel-parent
* @date: 2020-05-18 10:33
* @version: V1.0
* @retrun com.alibaba.csp.sentinel.dashboard.rule.redis.sentinel-parent
*/
@Component
public class RedisUtil {
@Autowired
private StringRedisTemplate stringRedisTemplate;
public StringRedisTemplate getStringRedisTemplate() {
return stringRedisTemplate;
}
/**
* 存放string类型
*
* @param key
* @param data
* @param timeout
*/
public void setString(String key, String data, Long timeout) {
try {
stringRedisTemplate.opsForValue().set(key, data, 7, TimeUnit.DAYS);
if (timeout != null) {
stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 存放string类型
*
* @param key
* @param data
*/
public void setString(String key, String data) {
setString(key, data, null);
}
/**
* 根据key查询string类型
*
* @param key
* @return
*/
public String getString(String key) {
String value = stringRedisTemplate.opsForValue().get(key);
return value;
}
/**
* 根据对应的key删除key
*
* @param key
*/
public Boolean delKey(String key) {
return stringRedisTemplate.delete(key);
}
}