sentinel console practice

Because the current open source version of the console is not suitable for use in the generation environment, the console has been modified

Control desk address: https://gitee.com/lis1314/sentinel-dashboard

 

1, Transformation of control console

Based on the 1.7.0 console UI, the jar package uses 1.7.1 (upgraded from 1.7.0)
1. Cluster link page UI default view changed from original (tree view) to (list view)
2. Flow control rules, degradation rules, hotspot rules, system rules, authorization rules, all persistent (zookeeper)
3. For the rule persistence ID, from the default, automicinteger is changed to the SnowflakeId generation algorithm (originally 18 bits here, but due to the accuracy of js, the background is changed to 12 bits and the left 6 bits are intercepted)
Specific (SnowflakeIdWorker) class
4. The configuration of the flow control rules of the stand-alone version is deleted, and the flow control rules of the stand-alone version are not supported
5. Deleted the DynamicRuleProvider and DynamicRulePublisher classes, and used (DynamicRuleDrive) to replace the methods of the two interfaces (1. Publish rules, 2. Get rules)

2, Client integrated control console

In the sentinel.properties file, for some reasons, I want to use spring's ognl expression to dynamically configure some properties

As well as problems with dubbo applications and other applications such as monitoring and unified naming, project name wants to write in the configuration file.

The following is the integration method of sentinel client (business application) configuration console

1,properties

# zookeeper address
zkServer=${zookeeperAddress}

#https://github.com/alibaba/Sentinel/wiki/ startup configuration item

#The project.name parameter can only be configured through the jvm-d parameter mode. Other parameters support all configuration modes
project.name=${spring.config.name}
#The maximum effective response time (ms), beyond which it is recorded according to this value, default (4900)
csp.sentinel.statistic.max.rt=100000
#Heartbeat packet sending cycle, in milliseconds
csp.sentinel.heartbeat.interval.ms=60000
#Address of the console. After specifying the console, the client will automatically send heartbeat packets to the address. The address format is: hostIp:port
csp.sentinel.dashboard.server=${sentinel.dashboard.server}

2. spring configuration

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/Start configuration item
 * 
 * @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());
		}
	}

}

In this way, if you need to add new sentinel configuration parameters, it will be well supported

3. Configure zookeeper rule data source

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 Access
 * 
 * @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 corresponds to the data path in 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);
	}

}

Tags: Programming Zookeeper Java Spring github

Posted on Mon, 13 Jan 2020 23:52:41 -0800 by noimad1