6°

sentinel 管控台实践

前言:因为目前开源版本的管控台不适合在生成环境使用,这里自己对管控台进行了改造

管控台地址:https://gitee.com/lis1314/sentinel-dashboard

 

一、管控台改造

基于1.7.0的管控台UI,jar包使用1.7.1(从1.7.0升级)
1、簇点链路页面UI默认视图从原(树状视图)改成(列表视图)
2、流控规则、降级规则、热点规则、系统规则、授权规则,全部持久化(zookeeper)
3、对规则持久化的ID从默认的,automicinteger改成使用SnowflakeId生成算法(这里原是18位,但是由于js的精度问题,后台改成了12位,截取了左侧的6位)
具体(SnowflakeIdWorker)类
4、删除了单机版的流控规则配置,不支持单机版的流控规则
5、删除了DynamicRuleProvider、DynamicRulePublisher类,使用(DynamicRuleDrive)替代了这两个接口的方法(1.发布规则、2.获取规则)

二、客户端集成管控台

在sentinel.properties文件中由于一些原因,想去使用spring的ognl表达式去动态配置一些属性

以及和dubbo应用以及其他比如监控统一命名,project name 想写在配置文件中的种种问题吧。

下面是sentinel客户端(业务应用)配置管控台的集成方式

1、properties

# zookeeper address
zkServer=${zookeeperAddress}

#https://github.com/alibaba/Sentinel/wiki/启动配置项

#project.name 参数只能通过 JVM -D 参数方式配置,其它参数支持所有的配置方式 project.name=${spring.config.name} #最大的有效响应时长(ms),超出此值则按照此值记录,default(4900) csp.sentinel.statistic.max.rt=100000 #心跳包发送周期,单位毫秒 csp.sentinel.heartbeat.interval.ms=60000 #控制台的地址,指定控制台后客户端会自动向该地址发送心跳包。地址格式为:hostIp:port csp.sentinel.dashboard.server=${sentinel.dashboard.server}

2、spring 配置

package com.gomeplus.bs.sentinel;

import java.util.HashMap; import java.util.Map;

import org.springframework.beans.factory.InitializingBean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component;

import lombok.Data;

/**

  • https://github.com/alibaba/Sentinel/wiki/启动配置项
  • @author lisuo

*/ @PropertySource("classpath:sentinel.properties") @ConfigurationProperties @Component @Data public class SentinelConfig implements InitializingBean {

private String zkServer;

private Map<String, String> project = new HashMap<>();

private Map<String, String> csp = new HashMap<>();

@Override
public void afterPropertiesSet() throws Exception {
	for (Map.Entry<String, String> e : csp.entrySet()) {
		System.setProperty("csp." + e.getKey(), e.getValue());
	}
	for (Map.Entry<String, String> e : project.entrySet()) {
		System.setProperty("project." + e.getKey(), e.getValue());
	}
}

}

这样一来,如果需要增加新的sentinel配置参数,都会进行很好的支持了

3、配置zookeeper规则数据源

package com.gomeplus.bs.sentinel;

import java.util.ArrayList; import java.util.List;

import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Lazy;

import com.alibaba.csp.sentinel.datasource.Converter; import com.alibaba.csp.sentinel.datasource.ReadableDataSource; import com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource; 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.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject;

/**

  • zookeeper接入
  • @author lisuo

*/ @Configuration public class ZkDataSourceConfig implements InitializingBean {

@Autowired
@Lazy
private SentinelConfig sentinelConfig;

@Override
public void afterPropertiesSet() throws Exception {
	FlowRuleManager.register2Property(getDataSource(FlowRule.class).getProperty());
	AuthorityRuleManager.register2Property(getDataSource(AuthorityRule.class).getProperty());
	DegradeRuleManager.register2Property(getDataSource(DegradeRule.class).getProperty());
	SystemRuleManager.register2Property(getDataSource(SystemRule.class).getProperty());
	ParamFlowRuleManager.register2Property(getDataSource(ParamFlowRule.class).getProperty());
}

private <T> ReadableDataSource<String, List<T>> getDataSource(Class<T> clazz){
	final String path = "/sentinel_rule_config/" + sentinelConfig.getProject().get("name")+"/"+clazz.getSimpleName();
	// path 对应 ZK 中的数据路径
	Converter<String, List<T>> converter = new Converter<String, List<T>>() {
		@Override
		public List<T> convert(String source) {
			if(source!=null && !"".equals(source.trim()) && !"[]".equals(source)){
				List<T> list = new ArrayList<>();
				JSONArray array = JSON.parseArray(source);
				for (int i=0;i<array.size();i++) {
					JSONObject obj = array.getJSONObject(i);
					JSONObject rule = obj.getJSONObject("rule");
					T r = rule.toJavaObject(clazz);
					list.add(r);
				}
				return list;
			}
			return null;
		}
	};
	return new ZookeeperDataSource<>(sentinelConfig.getZkServer(), path,converter);
}

}

 

本文由【lis1314】发布于开源中国,原文链接:https://my.oschina.net/lis1314/blog/3158131

全部评论: 0

    我有话说: