2583 lines
117 KiB
Java
2583 lines
117 KiB
Java
package com.nis.web.service.restful;
|
||
|
||
import java.text.SimpleDateFormat;
|
||
import java.util.ArrayList;
|
||
import java.util.Date;
|
||
import java.util.HashMap;
|
||
import java.util.HashSet;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.Set;
|
||
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.util.StringUtils;
|
||
|
||
import com.nis.domain.MaatXmlConfig;
|
||
import com.nis.domain.MaatXmlExpr;
|
||
import com.nis.domain.MaatXmlSeq;
|
||
import com.nis.domain.restful.MaatConfig;
|
||
import com.nis.restful.RestBusinessCode;
|
||
import com.nis.restful.ServiceRuntimeException;
|
||
import com.nis.util.Configurations;
|
||
import com.nis.util.Constants;
|
||
import com.nis.util.ExceptionUtil;
|
||
import com.nis.util.File2Redis;
|
||
import com.nis.util.JedisUtils;
|
||
import com.nis.util.ReadMaatXmlUtil;
|
||
import com.nis.util.ServiceAndRDBIndexReal;
|
||
|
||
import redis.clients.jedis.Jedis;
|
||
import redis.clients.jedis.ScanParams;
|
||
import redis.clients.jedis.ScanResult;
|
||
import redis.clients.jedis.Transaction;
|
||
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||
|
||
@Service()
|
||
public class ConfigJedisServiceimpl implements ConfigRedisService {
|
||
private static Logger logger = LoggerFactory.getLogger(ConfigJedisServiceimpl.class);
|
||
// Constants.COMMONGROUPDBARR
|
||
// 用于在实时统计配置后面添加时间,方便读取入库时间
|
||
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
||
|
||
public boolean saveUnMaatConfig(Map<Integer, List<Map<String, String>>> configMap) {
|
||
if (configMap != null && configMap.size() > 0) {
|
||
int count = 0;
|
||
Jedis resource = JedisUtils.getResource(0);
|
||
Transaction transaction = resource.multi();
|
||
try {
|
||
for (Integer redisDBIndex : configMap.keySet()) {
|
||
if (redisDBIndex >= 0 && redisDBIndex < Configurations.getIntProperty("maxRedisDBIndex", 6)) {
|
||
transaction.select(redisDBIndex);
|
||
List<Map<String, String>> listMap = configMap.get(redisDBIndex);
|
||
if (listMap != null && listMap.size() > 0) {
|
||
String maatVersionStr = JedisUtils.get("MAAT_VERSION", redisDBIndex);
|
||
if (maatVersionStr == null) {
|
||
maatVersionStr = "0";
|
||
}
|
||
if (maatVersionStr != null) {
|
||
Double maatVersion = Double.valueOf(maatVersionStr) + 1D;
|
||
for (Map<String, String> map : listMap) {
|
||
String serviceStr = map.get("service");
|
||
int service = Integer.parseInt(serviceStr.trim());
|
||
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service);
|
||
if (maatXmlConfig != null) {
|
||
List<MaatXmlExpr> expressionList = maatXmlConfig.getExpressionList();
|
||
String maatKey = null;
|
||
for (MaatXmlExpr maatXmlExpr : expressionList) {
|
||
if (0 == maatXmlExpr.getType().intValue()) {
|
||
StringBuffer keyBF = new StringBuffer();
|
||
String[] keySplit = maatXmlExpr.getKeyExpression().split(";");
|
||
for (String keyStr : keySplit) {
|
||
if (!StringUtils.isEmpty(keyStr) && keyStr.trim().startsWith("[")) {
|
||
keyStr = keyStr.trim().replace("[", "").replace("]", "");
|
||
String keyVal = map.get(keyStr);
|
||
if (keyVal != null && !keyVal.equals("")) {
|
||
keyBF.append(keyVal);
|
||
} else {
|
||
throw new ServiceRuntimeException(
|
||
"未从map中获取到" + keyStr + "的值,请检查数据或配置文件是否正确",
|
||
RestBusinessCode.NotFoundValueByKey.getValue());
|
||
}
|
||
|
||
} else if (!StringUtils.isEmpty(keyStr)
|
||
&& keyStr.trim().startsWith("{")) {
|
||
keyStr = keyStr.trim().replace("{", "").replace("}", "");
|
||
if (keyStr.toLowerCase().contains("table_name")) {
|
||
String maatTableName = ServiceAndRDBIndexReal
|
||
.getUnMaatTableName(service);
|
||
if (maatTableName == null
|
||
|| maatTableName.trim().equals("")) {
|
||
throw new ServiceRuntimeException(
|
||
"无法从applicationConfig-rule.properties配置文件中,找到回调类配置service为"
|
||
+ service + "对应的真实表名",
|
||
RestBusinessCode.NotFoundTableName.getValue());
|
||
} else {
|
||
keyBF.append(maatTableName);
|
||
}
|
||
}
|
||
} else {
|
||
keyBF.append(keyStr.trim());
|
||
}
|
||
}
|
||
StringBuffer valBF = new StringBuffer();
|
||
String[] valSplit = maatXmlExpr.getValueExpression().split(";");
|
||
for (String valStr : valSplit) {
|
||
if (!StringUtils.isEmpty(valStr) && valStr.trim().startsWith("[")) {
|
||
valStr = valStr.trim().replace("[", "").replace("]", "");
|
||
if (valStr.contains("redisFile_")) {
|
||
String key = File2Redis.file2Redis(map.get(valStr),
|
||
transaction);
|
||
logger.info("向{}号redis数据库添加了一条文件配置,key是{}", redisDBIndex,
|
||
key);
|
||
valBF.append(key);
|
||
} else {
|
||
valBF.append(map.get(valStr));
|
||
}
|
||
} else if (valStr.equals(" ")) {
|
||
valBF.append(" ");
|
||
} else if (valStr.equals("\\t")) {// xml中是字符串的\t这里判断的时候需要转义为\\t,但是添加的时候需要添加\t不是\\t
|
||
valBF.append("\t");
|
||
} else if (valStr.equals("\\n")) {
|
||
valBF.append("\n");
|
||
} else {
|
||
valBF.append(valStr);
|
||
}
|
||
}
|
||
maatKey = keyBF.toString();
|
||
transaction.set(keyBF.toString().toUpperCase(), valBF.toString());
|
||
logger.info("向{}号redis数据库添加了一条配置,key是{},value是{}", redisDBIndex,
|
||
keyBF.toString().toUpperCase(), valBF.toString());
|
||
break;
|
||
}
|
||
}
|
||
updateMaatInfo(expressionList, maatKey, transaction, maatVersion, redisDBIndex,
|
||
false);
|
||
} else {
|
||
throw new ServiceRuntimeException(
|
||
"无法从applicationConfig-maatRedis.xml配置文件中获取service为" + service
|
||
+ "对应的规则,请检查业务类型是否正确",
|
||
RestBusinessCode.NotFoundRedisRule.getValue());
|
||
}
|
||
}
|
||
transaction.incrBy("MAAT_VERSION", 1l);
|
||
logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDBIndex,
|
||
Integer.valueOf(maatVersionStr) + 1);
|
||
count++;
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException(
|
||
"向" + redisDBIndex + "号redis库中添加配置时,未发现对应的配置信息,请检查配置参数是否正确",
|
||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException("redis数据库编号:" + redisDBIndex + "不正确,请检查数据库编号",
|
||
RestBusinessCode.DbIndexNotInRange.getValue());
|
||
}
|
||
}
|
||
if (count == configMap.size()) {
|
||
transaction.exec();
|
||
return true;
|
||
} else {
|
||
transaction.discard();
|
||
}
|
||
} catch (JedisConnectionException e) {
|
||
String error = "连接redis异常,保存非maat类配置失败," + e.getMessage();
|
||
logger.error(error + " " + ExceptionUtil.getExceptionMsg(e));
|
||
throw new ServiceRuntimeException(error, RestBusinessCode.CannotConnectionRedis.getValue());
|
||
|
||
} catch (Exception e) {
|
||
String error = "保存非maat类配置发生了异常," + e.getMessage();
|
||
logger.error(error + " " + ExceptionUtil.getExceptionMsg(e));
|
||
transaction.discard();
|
||
throw new ServiceRuntimeException(error, RestBusinessCode.SaveDataInError.getValue());
|
||
} finally {
|
||
// 释放连接到连接池
|
||
JedisUtils.returnResource(resource);
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException("向redis库添加配置时,未发现对应的配置信息,请检查配置参数是否正确",
|
||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 下发配置时,需要记录编译,组,域等配置id的对应关系,以便于在取消配置时可以根据配置id查找对应的编译,分组,域的关系
|
||
*/
|
||
private void addMaatRelation(Map<Integer, List<MaatConfig>> configMap, Transaction transaction,
|
||
List<Long> commonGroupIdList) {
|
||
if (configMap != null && configMap.size() > 0) {
|
||
int idRelaRedisDBIndex = Configurations.getIntProperty("idRelaRedisDBIndex", 15);
|
||
transaction.select(idRelaRedisDBIndex);
|
||
Set<Integer> redisDBSet = configMap.keySet();// 所有的redisdb
|
||
String redisDBSetStr = "-" + redisDBSet.toString().replace("[", "").replace("]", "");
|
||
for (Integer redisDBIndex : redisDBSet) {
|
||
if (redisDBIndex >= 0 && redisDBIndex < Configurations.getIntProperty("maxRedisDBIndex", 6)) {
|
||
List<MaatConfig> maatConfigList = configMap.get(redisDBIndex);
|
||
if (maatConfigList != null && maatConfigList.size() > 0) {
|
||
Map<String, List<String>> compileAndGroupMap = new HashMap<String, List<String>>();// 记录编译下面有哪些组,在哪些redisdb中
|
||
Map<String, List<String>> groupAndCompileMap = new HashMap<String, List<String>>();// 记录每个组属于哪些编译,公共组可能会属于很多的编译redisdb中
|
||
for (MaatConfig maatConfig : maatConfigList) {
|
||
List<Map<String, String>> groupMapList = maatConfig.getGroupMapList();
|
||
if (groupMapList != null && groupMapList.size() > 0) {
|
||
for (Map<String, String> map : groupMapList) {
|
||
String cfgIdStr = "COMPILEGROUP:" + map.get("compile_id");
|
||
String groupIdStr = "GROUPCOMPILE:" + map.get("group_id");
|
||
if (cfgIdStr != null && groupIdStr != null && !cfgIdStr.equals("")
|
||
&& !groupIdStr.equals("")) {
|
||
if (compileAndGroupMap.containsKey(cfgIdStr.toUpperCase())) {
|
||
compileAndGroupMap.get(cfgIdStr.toUpperCase())
|
||
.add(groupIdStr.toUpperCase() + redisDBSetStr);
|
||
} else {
|
||
List<String> list = new ArrayList<String>();
|
||
list.add(groupIdStr.toUpperCase() + redisDBSetStr);
|
||
compileAndGroupMap.put(cfgIdStr.toUpperCase(), list);
|
||
}
|
||
if (groupAndCompileMap.containsKey(groupIdStr.toUpperCase())) {
|
||
groupAndCompileMap.get(groupIdStr.toUpperCase())
|
||
.add(cfgIdStr.toUpperCase() + redisDBSetStr);
|
||
} else {
|
||
List<String> list = new ArrayList<String>();
|
||
list.add(cfgIdStr.toUpperCase() + redisDBSetStr);
|
||
groupAndCompileMap.put(groupIdStr.toUpperCase(), list);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
/**
|
||
* 添加组和域的关联关系信息
|
||
*/
|
||
addGroupRegionReal(maatConfig, transaction, redisDBSetStr, idRelaRedisDBIndex,
|
||
commonGroupIdList);
|
||
}
|
||
for (String compile : compileAndGroupMap.keySet()) {
|
||
List<String> list = compileAndGroupMap.get(compile);
|
||
StringBuffer sb = new StringBuffer();
|
||
if (list != null && list.size() > 0) {
|
||
for (String groupIdStr : list) {
|
||
sb.append(groupIdStr);
|
||
sb.append(";");
|
||
}
|
||
}
|
||
transaction.set(compile, sb.substring(0, sb.length() - 1));// 保存编译id和分组id的关系(每个域配置,只会属于一个组)
|
||
}
|
||
for (String group : groupAndCompileMap.keySet()) {
|
||
List<String> list = groupAndCompileMap.get(group);
|
||
StringBuffer sb = new StringBuffer();
|
||
if (list != null && list.size() > 0) {
|
||
for (String compile : list) {
|
||
sb.append(compile);
|
||
sb.append(";");
|
||
}
|
||
}
|
||
transaction.append(group, ";" + sb.substring(0, sb.length() - 1));// 保存分组id和编译id的关系(每个域配置,只会属于一个组)
|
||
}
|
||
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException("redis数据库编号:" + redisDBIndex + "不正确,请检查数据库编号",
|
||
RestBusinessCode.DbIndexNotInRange.getValue());
|
||
}
|
||
break;// 只循环一次,map中的value都是一模一样的
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException("向redis库中配置时,未发现对应的配置信息,请检查配置参数是否正确",
|
||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* 单独找一个redis-db记录配置信息,方便实时统计程序获取所有配置的分类性质,标签等内容,(为什么实时统计不从配置库redisdb获取呢,
|
||
* 因为配置有多分发的情况,会发送到不同的redisdb,每个redisdb的配置条数不一定相同,有的配置所有的redisdb都有,有的只在某一个redisdb中有,实时统计程序不好获取配置这些配置内容,
|
||
* 最重要的是,前端机不需要这些属性,所以在配置库有可能不带分类性质,标签等属性,maat.xml如果配置了则有,没有配置则就没有这些属性.所以单独找一个reidsdb存放带分类性质,标签等属性的配置)
|
||
*
|
||
* @param configMap
|
||
*/
|
||
private void addStatisticsReal(Map<Integer, List<MaatConfig>> configMap, Transaction transaction) {
|
||
if (configMap != null && configMap.size() > 0) {
|
||
for (Integer redisDBIndex : configMap.keySet()) {
|
||
int redisStatisticsRealDBIndex = Configurations.getIntProperty("redisStatisticsRealDBIndex", 14);
|
||
if (redisStatisticsRealDBIndex >= 0
|
||
&& redisStatisticsRealDBIndex < Configurations.getIntProperty("maxRedisDBIndex", 16)) {
|
||
List<MaatConfig> list = configMap.get(redisDBIndex);
|
||
if (list != null && list.size() > 0) {
|
||
transaction.select(redisStatisticsRealDBIndex);// 选择实时统计库
|
||
String redisStatisticsReal = Configurations.getStringProperty("redis-statisticsReal",
|
||
"[COMPILE_ID];\t;[SERVICE];\t;[ACTION];\t;[CONT_TYPE];\t;[ATTR_TYPE];\t;[CONT_LABEL];\t;[TASK_ID];\t;[AFFAIR_ID];\t;[DO_BLACKLIST];\t;[DO_LOG];\t;[EFFECTIVE_RANGE];\t;[START_TIME];\t;[END_TIME];\t;[USER_REGION];\t;[IS_VALID];\t;[GROUP_NUM];\t;[FATHER_CFG_ID];\t;[OP_TIME]");
|
||
String[] redisStatisticsRealArr = redisStatisticsReal.split(";");
|
||
|
||
String maatVersionStr = JedisUtils.get("MAAT_VERSION", redisStatisticsRealDBIndex);
|
||
if (maatVersionStr == null) {
|
||
maatVersionStr = "0";
|
||
}
|
||
if (maatVersionStr != null) {
|
||
Double maatVersion = Double.valueOf(maatVersionStr) + 1D;
|
||
|
||
for (MaatConfig maatConfig : list) {
|
||
int service = maatConfig.getService();
|
||
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service);
|
||
Map<String, String> compileMap = maatConfig.getCompileMap();
|
||
for (MaatXmlExpr maatXmlExpr : maatXmlConfig.getExpressionList()) {
|
||
if (10 == maatXmlExpr.getType().intValue()) {
|
||
StringBuffer keyBF = new StringBuffer();
|
||
String[] keySplit = maatXmlExpr.getKeyExpression().split(";");
|
||
for (String keyStr : keySplit) {
|
||
if (!StringUtils.isEmpty(keyStr) && keyStr.trim().startsWith("[")) {
|
||
keyStr = keyStr.trim().replace("[", "").replace("]", "");
|
||
String keyVal = compileMap.get(keyStr);
|
||
if (keyVal != null && !keyVal.equals("")) {
|
||
keyBF.append(keyVal);
|
||
} else {
|
||
throw new ServiceRuntimeException(
|
||
"未从map中获取到" + keyStr + "的值,请检查数据或配置文件是否正确",
|
||
RestBusinessCode.NotFoundValueByKey.getValue());
|
||
}
|
||
} else if (!StringUtils.isEmpty(keyStr) && keyStr.trim().startsWith("{")) {
|
||
keyStr = keyStr.trim().replace("{", "").replace("}", "");
|
||
if (keyStr.toLowerCase().contains("table_name")) {
|
||
String argTableName = compileMap.get("table_name");
|
||
String maatTableName = ServiceAndRDBIndexReal.getMaatTableName(
|
||
service, 10, argTableName == null ? null : argTableName);
|
||
if (maatTableName == null) {
|
||
throw new ServiceRuntimeException(
|
||
"无法从applicationConfig-rule.properties配置文件中,找到回调类配置service为"
|
||
+ service + ",配置类型:10,对应的真实表名",
|
||
RestBusinessCode.NotFoundTableName.getValue());
|
||
} else {
|
||
keyBF.append(maatTableName);
|
||
}
|
||
|
||
}
|
||
|
||
} else {
|
||
keyBF.append(keyStr.trim());
|
||
}
|
||
}
|
||
StringBuffer valBF = new StringBuffer();
|
||
for (String valStr : redisStatisticsRealArr) {
|
||
if (!StringUtils.isEmpty(valStr) && valStr.trim().startsWith("[")) {
|
||
valStr = valStr.trim().replace("[", "").replace("]", "");
|
||
String val = compileMap.get(valStr.toLowerCase());
|
||
if (val != null && !val.trim().equals("")) {
|
||
valBF.append(val);
|
||
} else {
|
||
// 编译配置或分组配置 所有在maat.xml中配置的属性都不可以为空
|
||
throw new ServiceRuntimeException(
|
||
"未从map中获取到" + valStr + "的值,请检查数据或配置文件是否正确",
|
||
RestBusinessCode.NotFoundValueByKey.getValue());
|
||
|
||
}
|
||
} else if (valStr.equals("\t")) {// xml中是字符串的\t这里判断的时候需要转义为\\t,但是添加的时候需要添加\t不是\\t
|
||
valBF.append("\t");
|
||
} else {
|
||
valBF.append(valStr.trim());
|
||
}
|
||
}
|
||
// 向实时统计库中添加时间标识,方便人眼直观识别入库时间
|
||
valBF.append("\t");
|
||
valBF.append(sdf.format(new Date()));
|
||
String maatKey = keyBF.toString();
|
||
transaction.set(maatKey.toUpperCase(), valBF.toString());
|
||
String zset = maatKey.replace("EFFECTIVE_RULE:", "ADD,");
|
||
transaction.zadd("MAAT_UPDATE_STATUS", maatVersion, zset);
|
||
|
||
logger.info("向{}号redis数据库添加了一条配置,key是{},value是{}", redisDBIndex,
|
||
maatKey.toUpperCase(), valBF.toString());
|
||
break;
|
||
}
|
||
|
||
}
|
||
}
|
||
transaction.incrBy("MAAT_VERSION", 1l);
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException(
|
||
"向" + redisStatisticsRealDBIndex + "号redis库中添加配置时,未发现对应的配置信息,请检查配置参数是否正确",
|
||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
}
|
||
|
||
} else {
|
||
throw new ServiceRuntimeException("redis数据库编号:" + redisStatisticsRealDBIndex + "不正确,请检查数据库编号",
|
||
RestBusinessCode.DbIndexNotInRange.getValue());
|
||
}
|
||
break;// configMap中所有的value都是相同的,在记录配置新增或者取消时只记录一次即可
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 封装组id与域id对应关系
|
||
*
|
||
* @param maatXmlConfig
|
||
* @param service
|
||
* @param type
|
||
* @param regionMapList
|
||
* @param groupAndRegionMap
|
||
* @param redisDBSetStr 当前配置下发到哪些redisdb中
|
||
* @param commonGroupIdList 公共组id集合
|
||
* @return
|
||
*/
|
||
private Map<String, Set<String>> addGroupAndRegionRelations(MaatXmlConfig maatXmlConfig, int service, int type,
|
||
List<Map<String, String>> regionMapList, Map<String, Set<String>> groupAndRegionMap, String redisDBSetStr,
|
||
List<Long> commonGroupIdList) {
|
||
if (regionMapList != null && regionMapList.size() > 0) {
|
||
for (Map<String, String> map : regionMapList) {
|
||
List<MaatXmlExpr> expressionList = maatXmlConfig.getExpressionList();
|
||
String maatKey = null;
|
||
for (MaatXmlExpr maatXmlExpr : expressionList) {
|
||
if (type == maatXmlExpr.getType().intValue()) {
|
||
StringBuffer keyBF = new StringBuffer();
|
||
String[] keySplit = maatXmlExpr.getKeyExpression().split(";");
|
||
for (String keyStr : keySplit) {
|
||
if (!StringUtils.isEmpty(keyStr) && keyStr.trim().startsWith("[")) {
|
||
keyStr = keyStr.trim().replace("[", "").replace("]", "");
|
||
keyBF.append(map.get(keyStr));
|
||
} else if (!StringUtils.isEmpty(keyStr) && keyStr.trim().startsWith("{")) {
|
||
keyStr = keyStr.trim().replace("{", "").replace("}", "");
|
||
if (keyStr.toLowerCase().contains("table_name")) {
|
||
String argTableName = map.get("table_name");
|
||
String maatTableName = ServiceAndRDBIndexReal.getMaatTableName(service, type,
|
||
argTableName == null ? null : argTableName);
|
||
if (maatTableName == null) {
|
||
throw new ServiceRuntimeException(
|
||
"无法从applicationConfig-rule.properties配置文件中,找到回调类配置service为" + service
|
||
+ ",配置类型:" + type + ",对应的真实表名",
|
||
RestBusinessCode.NotFoundTableName.getValue());
|
||
} else {
|
||
keyBF.append(maatTableName);
|
||
}
|
||
}
|
||
|
||
} else {
|
||
keyBF.append(keyStr.trim());
|
||
}
|
||
}
|
||
maatKey = keyBF.toString();
|
||
break;
|
||
}
|
||
}
|
||
// commonGroupIdList
|
||
Long groupId = Long.parseLong(map.get("group_id"));
|
||
|
||
String groupIdStr = "GROUPREGION:" + groupId;// groupregion里面value是region的信息,key是group的信息
|
||
if (commonGroupIdList.contains(groupId)) {
|
||
groupIdStr = "COMMONGROUPREGION:" + groupId;// 公共组以COMMONGROUPREGION开头和普通的组标识开
|
||
}
|
||
|
||
if (groupAndRegionMap.containsKey(groupIdStr.toUpperCase())) {
|
||
groupAndRegionMap.get(groupIdStr.toUpperCase()).add(maatKey.toUpperCase() + redisDBSetStr);
|
||
} else {
|
||
Set<String> list = new HashSet<String>();
|
||
list.add(maatKey.toUpperCase() + redisDBSetStr);
|
||
groupAndRegionMap.put(groupIdStr.toUpperCase(), list);
|
||
}
|
||
|
||
}
|
||
}
|
||
return groupAndRegionMap;
|
||
}
|
||
|
||
@Override
|
||
public boolean saveMaatConfig(Map<Integer, List<MaatConfig>> configMap, List<Long> commonGroupIdList) {
|
||
if (configMap != null && configMap.size() > 0) {
|
||
int count = 0;
|
||
Jedis resource = JedisUtils.getResource(0);
|
||
Transaction transaction = resource.multi();
|
||
try {
|
||
Set<Integer> keySet = configMap.keySet();
|
||
for (Integer redisDBIndex : configMap.keySet()) {
|
||
if (redisDBIndex >= 0 && redisDBIndex < Configurations.getIntProperty("maxRedisDBIndex", 6)) {
|
||
transaction.select(redisDBIndex);
|
||
String maatVersionStr = JedisUtils.get("MAAT_VERSION", redisDBIndex);
|
||
if (maatVersionStr == null) {
|
||
maatVersionStr = "0";
|
||
}
|
||
if (maatVersionStr != null) {
|
||
Double maatVersion = Double.valueOf(maatVersionStr) + 1D;
|
||
List<MaatConfig> maatConfigList = configMap.get(redisDBIndex);
|
||
if (maatConfigList != null && maatConfigList.size() > 0) {
|
||
for (MaatConfig maatConfig : maatConfigList) {
|
||
int service = maatConfig.getService();
|
||
if (ServiceAndRDBIndexReal.isAddASU(service)) {
|
||
if (!keySet.contains(Constants.TAPREDISDB)) {
|
||
throw new ServiceRuntimeException(
|
||
"业务类型:" + service + ",需要向阀门" + Constants.TAPREDISDB + "号库分发,但是当前只往"
|
||
+ keySet + "库下发,请检查阀门编号或者当前业务对应的配置文件是否正确",
|
||
RestBusinessCode.PropertiesIsError.getValue());
|
||
}
|
||
}
|
||
|
||
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service);
|
||
setConfig(maatConfig, maatXmlConfig, maatVersion, service, transaction,
|
||
redisDBIndex, commonGroupIdList);
|
||
}
|
||
if (commonGroupIdList != null && commonGroupIdList.size() > 0) {// 如果有公共组则将每个redisdb的版本都加1
|
||
for (String db : Constants.COMMONGROUPDBARR) {
|
||
transaction.select(Integer.parseInt(db));
|
||
transaction.incrBy("MAAT_VERSION", 1l);
|
||
logger.info("向{}号redis数据库更新了MAAT_VERSION", Integer.parseInt(db));
|
||
}
|
||
} else {// 没有公共组则只更新当前的redisdb
|
||
transaction.select(redisDBIndex);
|
||
transaction.incrBy("MAAT_VERSION", 1l);
|
||
logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDBIndex,
|
||
Integer.valueOf(maatVersionStr) + 1);
|
||
}
|
||
|
||
count++;
|
||
} else {
|
||
throw new ServiceRuntimeException(
|
||
"向" + redisDBIndex + "号redis库中添加配置时,未发现对应的配置信息,请检查配置参数是否正确",
|
||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
}
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException("redis数据库编号:" + redisDBIndex + "不正确,请检查数据库编号",
|
||
RestBusinessCode.DbIndexNotInRange.getValue());
|
||
}
|
||
}
|
||
if (count == configMap.size()) {
|
||
addMaatRelation(configMap, transaction, commonGroupIdList);
|
||
addStatisticsReal(configMap, transaction);
|
||
transaction.exec();
|
||
return true;
|
||
} else {
|
||
transaction.discard();
|
||
}
|
||
} catch (JedisConnectionException e) {
|
||
String error = "连接redis异常,保存maat类配置失败" + e.getMessage();
|
||
logger.error(error + " " + ExceptionUtil.getExceptionMsg(e));
|
||
throw new ServiceRuntimeException(error, RestBusinessCode.CannotConnectionRedis.getValue());
|
||
} catch (Exception e) {
|
||
String error = "保存maat类配置发生了异常" + e.getMessage();
|
||
logger.error(error + " " + ExceptionUtil.getExceptionMsg(e));
|
||
transaction.discard();
|
||
throw new ServiceRuntimeException(error, RestBusinessCode.SaveDataInError.getValue());
|
||
} finally {
|
||
// 释放连接到连接池
|
||
JedisUtils.returnResource(resource);
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException("写入Redis数据库中的配置信息不能为空", RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
}
|
||
return false;
|
||
|
||
}
|
||
|
||
/**
|
||
* 将编译,分组,域等配置拆开分别添加到redis中
|
||
*
|
||
* @param maatConfig
|
||
* @param maatXmlConfig
|
||
* @param maatVersion
|
||
* @param service
|
||
* @param transaction
|
||
* @param redisDBIndex
|
||
*/
|
||
public void setConfig(MaatConfig maatConfig, MaatXmlConfig maatXmlConfig, Double maatVersion, int service,
|
||
Transaction transaction, Integer redisDBIndex, List<Long> commonGroupIdList) {
|
||
Map<String, String> compileMap = maatConfig.getCompileMap();
|
||
String compileId = compileMap.get("compile_id");
|
||
if (compileMap != null && compileMap.size() > 0) {
|
||
setCommonConfig(maatXmlConfig, compileMap, 10, maatVersion, service, transaction, redisDBIndex, null);// 10代表是编译配置
|
||
} else {
|
||
throw new ServiceRuntimeException(
|
||
"无法从参数中获取" + redisDBIndex + "号redis库,业务类型为:" + service + "的编译配置信息,请检查配置参数是否正确",
|
||
RestBusinessCode.NotFoundCompileInfo.getValue());
|
||
}
|
||
/**
|
||
* 保存域配置信息
|
||
*/
|
||
addRegion(maatConfig, maatXmlConfig, maatVersion, service, transaction, redisDBIndex, commonGroupIdList);
|
||
|
||
List<Map<String, String>> groupMapList = maatConfig.getGroupMapList();
|
||
if (groupMapList != null && groupMapList.size() > 0) {
|
||
for (Map<String, String> map : groupMapList) {
|
||
setCommonConfig(maatXmlConfig, map, 11, maatVersion, service, transaction, redisDBIndex, compileId);// 11代表是分组配置
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException(
|
||
"无法从参数中获取" + redisDBIndex + "号redis库,业务类型为:" + service + "的分组配置信息,请检查配置参数是否正确",
|
||
RestBusinessCode.NotFoundGroupInfo.getValue());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 将整理好的数据添加到redis中
|
||
*
|
||
* @param maatXmlConfig
|
||
* @param map 数据
|
||
* @param type 标识是编译,组还是ip域,字符串域,数值域等
|
||
* @param maatVersion maatversion
|
||
* @param service 业务类型
|
||
* @param transaction
|
||
* @param redisDBIndex
|
||
* @param compileId
|
||
*/
|
||
private void setCommonConfig(MaatXmlConfig maatXmlConfig, Map<String, String> map, int type, Double maatVersion,
|
||
int service, Transaction transaction, Integer redisDBIndex, String compileId) {
|
||
if (maatXmlConfig != null && map != null && map.size() > 0) {
|
||
List<MaatXmlExpr> expressionList = maatXmlConfig.getExpressionList();
|
||
String maatKey = null;
|
||
transaction.select(redisDBIndex);
|
||
for (MaatXmlExpr maatXmlExpr : expressionList) {
|
||
if (type == maatXmlExpr.getType().intValue()) {
|
||
StringBuffer keyBF = new StringBuffer();
|
||
String[] keySplit = maatXmlExpr.getKeyExpression().split(";");
|
||
for (String keyStr : keySplit) {
|
||
if (!StringUtils.isEmpty(keyStr) && keyStr.trim().startsWith("[")) {
|
||
keyStr = keyStr.trim().replace("[", "").replace("]", "");
|
||
String keyVal = map.get(keyStr);
|
||
if (keyVal != null && !keyVal.equals("")) {
|
||
keyBF.append(keyVal);
|
||
} else {
|
||
throw new ServiceRuntimeException("未从map中获取到" + keyStr + "的值,请检查数据或配置文件是否正确",
|
||
RestBusinessCode.NotFoundValueByKey.getValue());
|
||
}
|
||
if (type == 11 && keyStr.toLowerCase().equals("group_id")) {
|
||
keyBF.append(compileId);
|
||
}
|
||
} else if (!StringUtils.isEmpty(keyStr) && keyStr.trim().startsWith("{")) {
|
||
keyStr = keyStr.trim().replace("{", "").replace("}", "");
|
||
if (keyStr.toLowerCase().contains("table_name")) {
|
||
String argTableName = map.get("table_name");
|
||
String maatTableName = ServiceAndRDBIndexReal.getMaatTableName(service, type,
|
||
argTableName == null ? null : argTableName);
|
||
if (maatTableName == null) {
|
||
throw new ServiceRuntimeException(
|
||
"无法从applicationConfig-rule.properties配置文件中,找到回调类配置service为" + service
|
||
+ ",配置类型:" + type + ",对应的真实表名",
|
||
RestBusinessCode.NotFoundTableName.getValue());
|
||
} else {
|
||
keyBF.append(maatTableName);
|
||
}
|
||
|
||
}
|
||
|
||
} else {
|
||
keyBF.append(keyStr.trim());
|
||
}
|
||
}
|
||
StringBuffer valBF = new StringBuffer();
|
||
String[] valSplit = maatXmlExpr.getValueExpression().split(";");
|
||
for (String valStr : valSplit) {
|
||
if (!StringUtils.isEmpty(valStr) && valStr.trim().startsWith("[")) {
|
||
valStr = valStr.trim().replace("[", "").replace("]", "");
|
||
String val = map.get(valStr);
|
||
if (val != null && !val.trim().equals("")) {
|
||
if ((Constants.TAPREDISDB == redisDBIndex || Constants.WEBFOCUSREDISDB == redisDBIndex)
|
||
&& type != 10 && type != 11 && valStr.toLowerCase().equals("op_time")) {// 域配置并且是op_time时在op_time前面添加如下内容
|
||
Map<String, String[]> map2 = ServiceAndRDBIndexReal.getMaatToValveMap()
|
||
.get(service);
|
||
if (map2 != null && map2.size() > 0) {
|
||
String[] arr = null;
|
||
if (type == 12) {// IP类
|
||
arr = map2.get("ipRegion");
|
||
} else if (type == 13) {// 数值类
|
||
arr = map2.get("numRegion");
|
||
} else if (type == 14) {// 字符串类
|
||
arr = map2.get("strRegion");
|
||
} else if (type == 15) {// 增强字符串类
|
||
arr = map2.get("strStrRegion");
|
||
} else if (type == 16) {// 文件摘要类
|
||
arr = map2.get("fileDigestRegion");
|
||
} else if (type == 17) {// 文件摘要类
|
||
arr = map2.get("fileLikeRegion");
|
||
} else if (type == 18) {// 文件摘要类
|
||
arr = map2.get("ipClientRange");
|
||
}
|
||
if (arr != null && arr.length > 0) {
|
||
for (String str : arr) {
|
||
valBF.append(map.get(str.toLowerCase()) + "\t");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
valBF.append(val);
|
||
} else {
|
||
// 所有在maat.xml中配置的属性都不可以为空
|
||
throw new ServiceRuntimeException(
|
||
"未从map中获取到" + valStr + "的值,无法拼接redisValue,请检查数据或配置文件是否正确",
|
||
RestBusinessCode.NotFoundValueByKey.getValue());
|
||
}
|
||
} else if (valStr.equals(" ")) {
|
||
valBF.append(" ");
|
||
} else if (valStr.equals("\\t")) {// xml中是字符串的\t这里判断的时候需要转义为\\t,但是添加的时候需要添加\t不是\\t
|
||
valBF.append("\t");
|
||
} else if (valStr.equals("\\n")) {
|
||
valBF.append("\n");
|
||
} else {
|
||
valBF.append(valStr.trim());
|
||
}
|
||
}
|
||
maatKey = keyBF.toString();
|
||
transaction.set(maatKey.toUpperCase(), valBF.toString());
|
||
logger.info("向{}号redis数据库添加了一条配置,key是{},value是{}", redisDBIndex, maatKey.toUpperCase(),
|
||
valBF.toString());
|
||
break;
|
||
}
|
||
}
|
||
updateMaatInfo(expressionList, maatKey, transaction, maatVersion, redisDBIndex, false);
|
||
} else {
|
||
if (maatXmlConfig == null) {
|
||
throw new ServiceRuntimeException(
|
||
"无法从applicationConfig-maatRedis.xml配置文件中,获取service为" + service + "对应的规则,请检查业务类型是否正确",
|
||
RestBusinessCode.NotFoundRedisRule.getValue());
|
||
} else {
|
||
throw new ServiceRuntimeException("向redis库中添加配置时,未发现对应的配置信息,请检查配置参数是否正确",
|
||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
}
|
||
}
|
||
}
|
||
|
||
public Long getIncrId(String key) {
|
||
Long id = JedisUtils.incrBy(key, 1l, 0);
|
||
logger.info("从0号redis数据库获取{}成功,自增后的值是{}", key, id);
|
||
return id;
|
||
}
|
||
|
||
public Long getIncrById(String key, int num) {
|
||
Long id = JedisUtils.incrBy(key, num, 0);
|
||
logger.info("从0号redis数据库获取{}成功,自增后的值是{}", key, id);
|
||
return id;
|
||
}
|
||
|
||
@Override
|
||
public boolean delUnMaatConfig(Map<Integer, Map<Integer, List<Long>>> idMap, boolean isInvalid) {
|
||
if (idMap != null && idMap.size() > 0) {
|
||
int count = 0;
|
||
Jedis resource = JedisUtils.getResource(0);
|
||
Transaction transaction = resource.multi();
|
||
try {
|
||
for (Integer redisDBIndex : idMap.keySet()) {
|
||
transaction.select(redisDBIndex);
|
||
Map<Integer, List<Long>> serviceConfigMap = idMap.get(redisDBIndex);
|
||
if (serviceConfigMap != null && serviceConfigMap.size() > 0) {
|
||
|
||
String maatVersionStr = JedisUtils.get("MAAT_VERSION", redisDBIndex);
|
||
if (maatVersionStr == null) {
|
||
throw new ServiceRuntimeException("从" + redisDBIndex
|
||
+ "号redis库中获取MAAT_VERSION的值为null,redis中不存在该值,请联系开发人员检查删除逻辑是否正确或redis数据是否出现了异常",
|
||
RestBusinessCode.GetMaatVersionFailure.getValue());
|
||
}
|
||
if (maatVersionStr != null) {
|
||
Double maatVersion = Double.valueOf(maatVersionStr) + 1D;
|
||
for (Integer service : serviceConfigMap.keySet()) {
|
||
List<Long> compileIdList = serviceConfigMap.get(service);
|
||
if (compileIdList != null && compileIdList.size() > 0) {
|
||
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service);
|
||
for (Long id : compileIdList) {
|
||
if (maatXmlConfig != null) {
|
||
List<MaatXmlExpr> expressionList = maatXmlConfig.getExpressionList();
|
||
String maatKey = null;
|
||
for (MaatXmlExpr maatXmlExpr : expressionList) {
|
||
if (0 == maatXmlExpr.getType().intValue()) {
|
||
StringBuffer keyBF = new StringBuffer();
|
||
String[] keySplit = maatXmlExpr.getKeyExpression().split(";");
|
||
for (String keyStr : keySplit) {
|
||
if (!StringUtils.isEmpty(keyStr)
|
||
&& keyStr.toUpperCase().equals("EFFECTIVE_RULE")) {
|
||
|
||
if (isInvalid) {
|
||
keyStr = "OBSOLETE_RULE";
|
||
} else {
|
||
keyStr = "EFFECTIVE_RULE";
|
||
}
|
||
|
||
keyBF.append(keyStr.trim());
|
||
} else if (!StringUtils.isEmpty(keyStr)
|
||
&& keyStr.trim().startsWith("[")) {
|
||
keyBF.append(id);
|
||
} else if (!StringUtils.isEmpty(keyStr)
|
||
&& keyStr.trim().startsWith("{")) {
|
||
keyStr = keyStr.trim().replace("{", "").replace("}", "");
|
||
if (keyStr.toLowerCase().contains("table_name")) {
|
||
String maatTableName = ServiceAndRDBIndexReal
|
||
.getUnMaatTableName(service);
|
||
if (maatTableName == null) {
|
||
throw new ServiceRuntimeException(
|
||
"无法从applicationConfig-rule.properties配置文件中,找到回调类配置service为"
|
||
+ service + "对应的真实表名",
|
||
RestBusinessCode.NotFoundTableName
|
||
.getValue());
|
||
} else {
|
||
keyBF.append(maatTableName);
|
||
}
|
||
}
|
||
} else {
|
||
keyBF.append(keyStr.trim());
|
||
}
|
||
}
|
||
String oldKey = null;
|
||
maatKey = keyBF.toString();
|
||
if (isInvalid) {
|
||
oldKey = maatKey.replace("OBSOLETE_RULE", "EFFECTIVE_RULE");
|
||
} else {
|
||
oldKey = maatKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
|
||
}
|
||
|
||
transaction.rename(oldKey.toString().toUpperCase(),
|
||
maatKey.toString().toUpperCase());
|
||
logger.info("向{}号redis数据库修改了一条配置,修改前key是{},修改后key是{}", redisDBIndex,
|
||
oldKey.toString().toUpperCase(),
|
||
maatKey.toString().toUpperCase());
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (isInvalid) {// 删除
|
||
updateMaatInfo(expressionList, maatKey, transaction, maatVersion,
|
||
redisDBIndex, true);
|
||
} else {// 新增
|
||
updateMaatInfo(expressionList, maatKey, transaction, maatVersion,
|
||
redisDBIndex, false);
|
||
}
|
||
|
||
List<MaatXmlSeq> seqList = maatXmlConfig.getSequenceList();
|
||
for (MaatXmlSeq maatXmlSeq : seqList) {
|
||
String seqKey = maatXmlSeq.getSequenceKey();
|
||
if (!seqKey.toUpperCase().equals("MAAT_VERSION")) {
|
||
Integer operation = maatXmlSeq.getOperation();
|
||
if (operation == 1) {
|
||
transaction.incrBy(seqKey.toUpperCase(), 1l);
|
||
}
|
||
}
|
||
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException(
|
||
"无法从applicationConfig-maatRedis.xml配置文件中获取service为" + service
|
||
+ "对应的规则,请检查业务类型是否正确",
|
||
RestBusinessCode.NotFoundRedisRule.getValue());
|
||
}
|
||
}
|
||
transaction.incrBy("MAAT_VERSION", 1l);
|
||
logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDBIndex,
|
||
Integer.valueOf(maatVersionStr) + 1);
|
||
count++;
|
||
} else {
|
||
throw new ServiceRuntimeException(
|
||
"无法从参数中获取" + redisDBIndex + "号redis库,业务类型为:" + service
|
||
+ "的配置id信息,请检查配置参数是否正确",
|
||
RestBusinessCode.CompileIdListIsNull.getValue());
|
||
}
|
||
}
|
||
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException("向" + redisDBIndex + "号redis库中添加配置时,未发现对应的配置信息,请检查配置参数是否正确",
|
||
RestBusinessCode.ServiceAndCompileMapIsNull.getValue());
|
||
}
|
||
|
||
}
|
||
if (count == idMap.size()) {
|
||
transaction.exec();
|
||
return true;
|
||
} else {
|
||
transaction.discard();
|
||
}
|
||
} catch (JedisConnectionException e) {
|
||
String error = "连接redis异常,删除非maat类配置失败," + e.getMessage();
|
||
logger.error(error + " " + ExceptionUtil.getExceptionMsg(e));
|
||
throw new ServiceRuntimeException(error, RestBusinessCode.CannotConnectionRedis.getValue());
|
||
} catch (Exception e) {
|
||
String error = "删除非maat类配置发生了异常," + e.getMessage();
|
||
logger.error(error + " " + ExceptionUtil.getExceptionMsg(e));
|
||
transaction.discard();
|
||
throw new ServiceRuntimeException(error, RestBusinessCode.DeleteDataInError.getValue());
|
||
} finally {
|
||
// 释放连接到连接池
|
||
JedisUtils.returnResource(resource);
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException("状态更新操作Map参数信息不能为空,请检查!",
|
||
RestBusinessCode.ConfigInfoMapIsNull.getValue());
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 根据配置id从关联关系db中获取各个配置实际所在的db,key是redisdb,value是配置id
|
||
*
|
||
* @param idList
|
||
* @param service
|
||
* @param idRelaRedisDBIndex
|
||
* @return
|
||
*/
|
||
private Map<Integer, Set<Long>> getCompileIdAndDBReal(List<Long> idList, int service, int idRelaRedisDBIndex) {
|
||
// 记录所有需要删除的配置信息key是相同的redisdb,value是需要删除的id集合
|
||
Map<Integer, Set<Long>> idAndDBMap = new HashMap<Integer, Set<Long>>();
|
||
for (Long configId : idList) {
|
||
// if(JedisUtils.exists("COMPILEGROUP:" + configId, idRelaRedisDBIndex)){
|
||
String compileValStr = JedisUtils.get("COMPILEGROUP:" + configId, idRelaRedisDBIndex);
|
||
if (compileValStr != null && !compileValStr.trim().equals("")) {
|
||
String[] keyAndDBArr =
|
||
|
||
org.apache.commons.lang.StringUtils.split(compileValStr, ";");
|
||
for (String keyAndDB : keyAndDBArr) {
|
||
if (keyAndDB != null && !keyAndDB.trim().equals("")) {
|
||
String[] split = keyAndDB.split("-");
|
||
if (split != null && split.length > 0) {
|
||
String[] dbArr = split[1].split(",");
|
||
for (String db : dbArr) {
|
||
if (db != null && !db.trim().equals("")) {
|
||
int redisDb = Integer.parseInt(db.trim());
|
||
if (idAndDBMap.containsKey(redisDb)) {
|
||
idAndDBMap.get(redisDb).add(configId);
|
||
} else {
|
||
Set<Long> list = new HashSet<Long>();
|
||
list.add(configId);
|
||
idAndDBMap.put(redisDb, list);
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException(
|
||
"未从" + idRelaRedisDBIndex + "号redis库(编译,分组,域关系)中获取配置compileId=" + configId
|
||
+ ",对应的redisdb信息,请联系开发人员检查删除逻辑是否正确或redis数据是否出现了异常",
|
||
RestBusinessCode.KeyNotExistsInRedis.getValue());
|
||
}
|
||
// }
|
||
}
|
||
|
||
return idAndDBMap;
|
||
}
|
||
|
||
/**
|
||
* 根据编译id,组id,域id
|
||
*/
|
||
public void delByCompileGroupRegionId() {
|
||
Jedis resource = JedisUtils.getResource(0);
|
||
double version = 3901d;
|
||
Transaction transaction = resource.multi();
|
||
transaction.select(2);
|
||
for (long i = 3143; i <= 13142; i++) {
|
||
String key = "EFFECTIVE_RULE:NTC_COMPILE," + i;
|
||
String a = "OBSOLETE_RULE:NTC_COMPILE," + i;
|
||
transaction.rename(key, a);
|
||
|
||
transaction.zadd("MAAT_RULE_TIMER", version, a);
|
||
transaction.zadd("MAAT_UPDATE_STATUS", version, "DEL,NTC_COMPILE," + i);
|
||
Long nowTime = new Date().getTime();
|
||
nowTime = nowTime / 1000l;
|
||
Double score = nowTime.doubleValue();// 使用redis自带的time,得到当前时间的秒
|
||
transaction.zadd("MAAT_VERSION_TIMER", score, version + "");
|
||
}
|
||
for (long i = 5312; i <= 15311; i++) {
|
||
String key = "EFFECTIVE_RULE:NTC_IP," + i;
|
||
String a = "OBSOLETE_RULE:NTC_IP," + i;
|
||
transaction.rename(key, a);
|
||
|
||
transaction.zadd("MAAT_RULE_TIMER", version, a);
|
||
transaction.zadd("MAAT_UPDATE_STATUS", version, "DEL,NTC_IP," + i);
|
||
Long nowTime = new Date().getTime();
|
||
nowTime = nowTime / 1000l;
|
||
Double score = nowTime.doubleValue();// 使用redis自带的time,得到当前时间的秒
|
||
transaction.zadd("MAAT_VERSION_TIMER", score, version + "");
|
||
}
|
||
int b = 0;
|
||
for (long i = 3898; i <= 13897; i++) {
|
||
int id = 3143 + b;
|
||
String groupId = i + "" + id;
|
||
String key = "EFFECTIVE_RULE:NTC_GROUP," + groupId;
|
||
String c = "OBSOLETE_RULE:NTC_GROUP," + groupId;
|
||
transaction.rename(key, c);
|
||
b++;
|
||
transaction.zadd("MAAT_RULE_TIMER", version, c);
|
||
transaction.zadd("MAAT_UPDATE_STATUS", version, "DEL,NTC_GROUP," + groupId);
|
||
Long nowTime = new Date().getTime();
|
||
nowTime = nowTime / 1000l;
|
||
Double score = nowTime.doubleValue();// 使用redis自带的time,得到当前时间的秒
|
||
transaction.zadd("MAAT_VERSION_TIMER", score, version + "");
|
||
|
||
}
|
||
|
||
transaction.set("MAAT_VERSION", "3901");
|
||
transaction.exec();
|
||
|
||
}
|
||
|
||
/**
|
||
* 根据配置id删除数据
|
||
*/
|
||
public void delByCompileId() {
|
||
Map<Integer, List<Long>> serviceConfigMap = new HashMap<Integer, List<Long>>();
|
||
List<Long> list = new ArrayList<>();
|
||
for (long i = 13143; i <= 23142; i++) {
|
||
list.add(i);
|
||
}
|
||
serviceConfigMap.put(129, list);
|
||
updateMaatStatus(serviceConfigMap, false, null);
|
||
}
|
||
|
||
@Override
|
||
public boolean updateMaatStatus(Map<Integer, List<Long>> serviceConfigMap, boolean isStart,
|
||
Map<Long, List<Long>> compileGroupMap) {
|
||
if (serviceConfigMap != null && serviceConfigMap.size() > 0) {
|
||
Jedis resource = JedisUtils.getResource(0);
|
||
Transaction transaction = resource.multi();
|
||
try {
|
||
int idRelaRedisDBIndex = Configurations.getIntProperty("idRelaRedisDBIndex", 15);
|
||
if (serviceConfigMap != null && serviceConfigMap.size() > 0) {
|
||
for (Integer service : serviceConfigMap.keySet()) {
|
||
Map<Integer, Set<Long>> compileIdAndDBReal = getCompileIdAndDBReal(
|
||
serviceConfigMap.get(service), service, idRelaRedisDBIndex);
|
||
for (Integer redisDb : compileIdAndDBReal.keySet()) {
|
||
String maatVersionStr = JedisUtils.get("MAAT_VERSION", redisDb);
|
||
if (maatVersionStr != null) {
|
||
Double maatVersion = Double.valueOf(maatVersionStr) + 1D;
|
||
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service);
|
||
Set<Long> list = compileIdAndDBReal.get(redisDb);
|
||
if (list != null && list.size() > 0) {
|
||
for (Long id : list) {
|
||
// 按序号选择Redis数据库
|
||
transaction.select(redisDb);
|
||
if (!isStart && compileGroupMap.size() > 0) {// 失效并且有需要保留的group则将groupid的集合传到下一个方法中
|
||
// 如果某一个配置下包含两个组,一个普通组,一个公共组,界面调用该接口时一定会保留公共组,
|
||
// 该接口不能修改公共组的状态如果想对公共组进行操作,调用公共组的相关方法即可
|
||
removeConfig(id, maatXmlConfig, maatVersion, service, transaction, redisDb,
|
||
idRelaRedisDBIndex, isStart, compileGroupMap.get(id));
|
||
} else {// 将配置置为生效,或者删除时不保留groupid
|
||
removeConfig(id, maatXmlConfig, maatVersion, service, transaction, redisDb,
|
||
idRelaRedisDBIndex, isStart, null);
|
||
}
|
||
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException("删除配置时,未发现对应的配置id信息,请检查配置参数是否正确",
|
||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
}
|
||
transaction.incrBy("MAAT_VERSION", 1l);
|
||
logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDb,
|
||
Integer.valueOf(maatVersionStr) + 1);
|
||
} else {
|
||
throw new ServiceRuntimeException("从" + redisDb
|
||
+ "号redis库中获取MAAT_VERSION的值为null,redis中不存在该值,请联系开发人员检查删除逻辑是否正确或redis数据是否出现了异常",
|
||
RestBusinessCode.GetMaatVersionFailure.getValue());
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException("删除maat类配置时,未发现对应的配置信息,请检查配置参数是否正确",
|
||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
}
|
||
if (removeMaatRelation(serviceConfigMap, transaction, isStart, compileGroupMap)
|
||
&& removeStatisticsReal(serviceConfigMap, transaction, isStart)) {
|
||
transaction.exec();
|
||
return true;
|
||
} else {
|
||
transaction.discard();
|
||
}
|
||
|
||
} catch (JedisConnectionException e) {
|
||
String error = "连接redis异常,删除maat配置失败," + e.getMessage();
|
||
logger.error(error + " " + ExceptionUtil.getExceptionMsg(e));
|
||
throw new ServiceRuntimeException(error, RestBusinessCode.CannotConnectionRedis.getValue());
|
||
|
||
} catch (Exception e) {
|
||
|
||
int businessCode = RestBusinessCode.service_runtime_error.getValue();
|
||
if (e instanceof ServiceRuntimeException) {
|
||
businessCode = ((ServiceRuntimeException) e).getErrorCode();
|
||
}
|
||
logger.error("删除maat配置发生了异常" + ExceptionUtil.getExceptionMsg(e));
|
||
transaction.discard();
|
||
throw new ServiceRuntimeException("删除maat配置发生了异常," + e.getMessage(), businessCode);
|
||
} finally {
|
||
// 释放连接到连接池
|
||
JedisUtils.returnResource(resource);
|
||
}
|
||
} else
|
||
|
||
{
|
||
throw new ServiceRuntimeException("Map参数信息不能为空,请检查!", RestBusinessCode.ConfigInfoMapIsNull.getValue());
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 获取关联关系中每个关系中的-前部分
|
||
*
|
||
* @param compileStrVal
|
||
* @return
|
||
*/
|
||
private String getRegionInfo(String compileStrVal) {
|
||
if (compileStrVal != null && compileStrVal.contains("-")) {
|
||
String[] split = compileStrVal.split("-");
|
||
return split[0];// 去除后面的redisdb信息
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 将配置置为生效或失效
|
||
*
|
||
* @param id 配置id
|
||
* @param maatXmlConfig maat.xml中关于当前业务类型的key和value写法的对象
|
||
* @param maatVersion 版本号
|
||
* @param service 业务类型
|
||
* @param redisTemplate
|
||
* @param maatRelation id对应关系对象
|
||
* @param isStart 是否将配置置为生效
|
||
* @param keepGroupId 将配置置为失效时需要保留的组
|
||
*/
|
||
private void removeConfig(Long id, MaatXmlConfig maatXmlConfig, Double maatVersion, int service,
|
||
Transaction transaction, int redisDBIndex, int idRelaRedisDBIndex, boolean isStart,
|
||
List<Long> keepGroupId) {
|
||
Map<String, Boolean> groupIsCommonMap = new HashMap<>();// 记录公共组是否存在,避免每次都去redis中查询影响性能,,key是COMMONGROUPREGION:groupid,value是该组是否存在的标志
|
||
if (maatXmlConfig != null) {
|
||
Map<String, String> keyMap = new HashMap<>();// 记录每个key对应的value,避免每次去redis中查询,影响性能
|
||
// 删除(重命名)编译配置
|
||
removeCompileAndGroupConfig(maatXmlConfig, id + "", 10, maatVersion, service, transaction, redisDBIndex,
|
||
null, isStart);// 10代表是编译配置
|
||
// 拼接编译与分组关系的Redis Key
|
||
String compileStr = "COMPILEGROUP:" + id;
|
||
// 获取当前编译配置与分组配置的关联关系
|
||
String compileStrVal = JedisUtils.get(compileStr, idRelaRedisDBIndex);// 根据编译id获取该编译下的分组关系,示例:
|
||
// GROUPCOMPILE:25166-2,5;GROUPCOMPILE:25167-2,5;GROUPCOMPILE:25168-2,5
|
||
if (compileStrVal != null && !compileStrVal.trim().equals("")) {
|
||
String[] compileGroupStrArr = org.apache.commons.lang.StringUtils.split(compileStrVal, ";");// 获取当前编译下有哪些组
|
||
for (String compileGroup : compileGroupStrArr) {
|
||
// compileGroup=GROUPCOMPILE:25166-2, 5
|
||
// compileGroup=GROUPCOMPILE:编译id-redisdb1,redisdb2
|
||
if (compileGroup != null && !compileGroup.trim().equals("")) {
|
||
String groupId = getRegionInfo(compileGroup);// 获取编译对应的分组信息,去除后面的redisdb信息,GROUPCOMPILE:25166
|
||
if (groupId != null && !groupId.trim().equals("")) {
|
||
String groupCompileAndDBStrs = null;
|
||
if (!keyMap.containsKey(groupId)) {
|
||
groupCompileAndDBStrs = JedisUtils.get(groupId, idRelaRedisDBIndex);
|
||
keyMap.put(groupId, groupCompileAndDBStrs);
|
||
} else {
|
||
groupCompileAndDBStrs = keyMap.get(groupId);
|
||
}
|
||
String groupIdReal = groupId.replace("GROUPCOMPILE:", "");// 获取真实的groupid
|
||
String[] compileGroupArr = org.apache.commons.lang.StringUtils.split(groupCompileAndDBStrs,
|
||
";");// 获取组对应的编译id,示例:
|
||
// ;COMPILEGROUP:24070-2, 5
|
||
for (String groupAndCompileStr : compileGroupArr) {
|
||
if (groupAndCompileStr != null && !groupAndCompileStr.trim().equals("")) {
|
||
String compileId = getRegionInfo(groupAndCompileStr);// COMPILEGROUP:24070
|
||
if (compileId != null && !compileId.trim().equals("")) {
|
||
if (isStart || keepGroupId == null || keepGroupId.size() == 0
|
||
|| (keepGroupId != null && keepGroupId.size() > 0
|
||
&& !keepGroupId.contains(Long.parseLong(groupIdReal)))) {
|
||
// 如果是生效则根据关联关系将组下的域都改为生效
|
||
// 如果是失效并且当前组不是需要保留的组,则将该组下的域置为失效
|
||
if (compileId.equals(compileStr)) {// 判断编译id是否一致
|
||
String groupRegionKey = groupId.replace("GROUPCOMPILE", "GROUPREGION");// groupregion里面value是region的信息,key是group的信息,所以可以直接将GROUPCOMPILE替换为GROUPREGION
|
||
boolean isCommonGroup = false;
|
||
if (isStart) {// 如果是生效,判断下是否是公共组
|
||
String commonGroupRegionKey = groupId.replace("GROUPCOMPILE",
|
||
"COMMONGROUPREGION");// groupregion里面value是region的信息,key是group的信息,所以可以直接将GROUPCOMPILE替换为GROUPREGION
|
||
if (!groupIsCommonMap.containsKey(commonGroupRegionKey)) {
|
||
boolean exist = JedisUtils.exists(commonGroupRegionKey,
|
||
idRelaRedisDBIndex);
|
||
groupIsCommonMap.put(commonGroupRegionKey, exist);
|
||
if (exist) {
|
||
isCommonGroup = true;
|
||
}
|
||
|
||
} else {
|
||
isCommonGroup = groupIsCommonMap.get(commonGroupRegionKey);
|
||
}
|
||
}
|
||
if (!isCommonGroup) {
|
||
// 如果是改为生效状态并且不是公共组,则将其下的域改为生效
|
||
// 如果是失效(默认不是公共组isCommonGroup=false),则将其下的域改为失效
|
||
String regionStrs = null;
|
||
if (!keyMap.containsKey(groupRegionKey)) {
|
||
regionStrs = JedisUtils.get(groupRegionKey, idRelaRedisDBIndex);// 获取组下面对应的域配置
|
||
keyMap.put(groupRegionKey, regionStrs);
|
||
} else {
|
||
regionStrs = keyMap.get(groupRegionKey);
|
||
}
|
||
if (regionStrs != null && !regionStrs.trim().equals("")) {
|
||
String[] regionStrArr = regionStrs.split(";");
|
||
for (String str : regionStrArr) {
|
||
if (str != null && !str.trim().equals("")) {
|
||
String regionStr = getRegionInfo(str);
|
||
if (regionStr != null && !regionStr.trim().equals("")) {
|
||
String[] regionKeyArr = regionStr.split(";");
|
||
if (regionKeyArr != null
|
||
&& regionKeyArr.length > 0) {
|
||
// 根据分组与域关联关系找到对应域配置,将域配置置为生效或失效(重命名)
|
||
removeRegionConfig(maatXmlConfig, regionKeyArr,
|
||
maatVersion, service, transaction,
|
||
redisDBIndex, isStart);
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException(
|
||
"从" + idRelaRedisDBIndex
|
||
+ "号redis库中无法获取MAAT配置分组与域的关联关系,key为"
|
||
+ groupRegionKey,
|
||
RestBusinessCode.KeyNotExistsInRedis
|
||
.getValue());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
// 根据分组与编译关联关系找到对应分组配置然后置为生效或失效(重命名)
|
||
removeCompileAndGroupConfig(maatXmlConfig, groupId.replace("GROUPCOMPILE:", ""),
|
||
11, maatVersion, service, transaction, redisDBIndex, id + "", isStart);// 11代表是分组配置
|
||
|
||
} else {
|
||
throw new ServiceRuntimeException("从" + idRelaRedisDBIndex
|
||
+ "号redis库中无法获取MAAT配置分组与编译的关联关系,key为" + groupId,
|
||
RestBusinessCode.KeyNotExistsInRedis.getValue());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException(
|
||
"从" + idRelaRedisDBIndex + "号redis库中无法获取MAAT配置编译与分组关联关系,key为" + compileStr,
|
||
RestBusinessCode.KeyNotExistsInRedis.getValue());
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException(
|
||
"无法从applicationConfig-maatRedis.xml配置文件中获取service为" + service + "对应的规则,请检查业务类型是否正确",
|
||
RestBusinessCode.NotFoundRedisRule.getValue());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除maat配置时,对redis进行相关操作,主要是重命名key和记录相关状态
|
||
*
|
||
* @param maatXmlConfig
|
||
* @param idList
|
||
* @param type
|
||
* @param maatVersion
|
||
* @param service
|
||
* @param redisTemplate
|
||
*/
|
||
private void removeCompileAndGroupConfig(MaatXmlConfig maatXmlConfig, String id, int type, Double maatVersion,
|
||
int service, Transaction transaction, int redisDBIndex, String compileId, Boolean isStart) {
|
||
if (maatXmlConfig != null) {
|
||
List<MaatXmlExpr> expressionList = maatXmlConfig.getExpressionList();
|
||
String maatKey = null;
|
||
for (MaatXmlExpr maatXmlExpr : expressionList) {
|
||
if (type == maatXmlExpr.getType().intValue()) {
|
||
StringBuffer keyBF = new StringBuffer();
|
||
String[] keySplit = maatXmlExpr.getKeyExpression().split(";");
|
||
for (String keyStr : keySplit) {
|
||
if (!StringUtils.isEmpty(keyStr) && keyStr.trim().startsWith("[")) {
|
||
keyStr = keyStr.trim().replace("[", "").replace("]", "");
|
||
keyBF.append(id);
|
||
} else if (!StringUtils.isEmpty(keyStr) && keyStr.trim().startsWith("{")) {
|
||
keyStr = keyStr.trim().replace("{", "").replace("}", "");
|
||
if (keyStr.toLowerCase().contains("table_name")) {
|
||
String maatTableName = ServiceAndRDBIndexReal.getMaatTableName(service, type, null);
|
||
if (maatTableName == null) {
|
||
throw new ServiceRuntimeException(
|
||
"无法从applicationConfig-rule.properties配置文件中,找到回调类配置service为" + service
|
||
+ "对应的真实表名",
|
||
RestBusinessCode.NotFoundTableName.getValue());
|
||
} else {
|
||
keyBF.append(maatTableName);
|
||
}
|
||
}
|
||
|
||
} else {
|
||
keyBF.append(keyStr.trim());
|
||
}
|
||
}
|
||
String oldKey = keyBF.toString().toUpperCase();
|
||
if (compileId != null) {
|
||
oldKey += compileId;
|
||
}
|
||
if (isStart == null) {// 置为失效
|
||
maatKey = oldKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
|
||
} else if (isStart) {// 将失效置为生效
|
||
maatKey = oldKey;
|
||
oldKey = oldKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
|
||
} else {// 将生效变为失效
|
||
maatKey = oldKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
|
||
}
|
||
|
||
transaction.rename(oldKey, maatKey.toUpperCase());
|
||
logger.info("向{}号redis数据库修改了一条配置,修改前key是{},修改后key是{}", redisDBIndex, oldKey, maatKey.toUpperCase());
|
||
break;
|
||
|
||
}
|
||
}
|
||
|
||
if (isStart == null) {
|
||
updateMaatInfo(expressionList, maatKey, transaction, maatVersion, redisDBIndex, true);// 变成无效
|
||
} else if (isStart) {
|
||
updateMaatInfo(expressionList, maatKey, transaction, maatVersion, redisDBIndex, false);// 变成有效
|
||
} else {
|
||
updateMaatInfo(expressionList, maatKey, transaction, maatVersion, redisDBIndex, true);// 变成无效
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException(
|
||
"无法从applicationConfig-maatRedis.xml配置文件中获取service为" + service + "对应的规则,请检查业务类型是否正确",
|
||
RestBusinessCode.NotFoundRedisRule.getValue());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除域配置,重名key,记录相关信息MAAT_UPDATE_STATUS,MAAT_RULE_TIMER等
|
||
*
|
||
* @param maatXmlConfig
|
||
* @param regionArr
|
||
* @param maatVersion
|
||
* @param service
|
||
* @param redisTemplate
|
||
* @param redisDBIndex
|
||
*/
|
||
private void removeRegionConfig(MaatXmlConfig maatXmlConfig, String[] regionArr, Double maatVersion, int service,
|
||
Transaction transaction, int redisDBIndex, Boolean isStart) {
|
||
|
||
transaction.select(redisDBIndex);
|
||
if (maatXmlConfig != null && regionArr != null && regionArr.length > 0) {
|
||
List<MaatXmlExpr> expressionList = maatXmlConfig.getExpressionList();
|
||
String maatKey = null;
|
||
for (String oldKey : regionArr) {
|
||
if (oldKey != null && !oldKey.trim().equals("")) {
|
||
if (isStart == null) {// 置为失效
|
||
maatKey = oldKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
|
||
} else if (isStart) {// 将失效置为生效
|
||
maatKey = oldKey.replace("OBSOLETE_RULE", "EFFECTIVE_RULE");
|
||
} else {// 将生效变为失效
|
||
maatKey = oldKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
|
||
}
|
||
transaction.rename(oldKey, maatKey.toUpperCase());
|
||
logger.info("向{}号redis数据库修改了一条配置,修改前key是{},修改后key是{}", redisDBIndex, oldKey, maatKey.toUpperCase());
|
||
if (isStart == null) {
|
||
updateMaatInfo(expressionList, maatKey, transaction, maatVersion, redisDBIndex, true);// 变成无效
|
||
} else if (isStart) {
|
||
updateMaatInfo(expressionList, maatKey, transaction, maatVersion, redisDBIndex, false);// 变成有效
|
||
} else {
|
||
updateMaatInfo(expressionList, maatKey, transaction, maatVersion, redisDBIndex, true);
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
if (maatXmlConfig == null) {
|
||
throw new ServiceRuntimeException(
|
||
"无法从applicationConfig-maatRedis.xml配置文件中获取service为" + service + "对应的规则,请检查业务类型是否正确",
|
||
RestBusinessCode.NotFoundRedisRule.getValue());
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 取消实时统计库中的编译配置
|
||
*
|
||
* @return
|
||
*/
|
||
private boolean removeStatisticsReal(Map<Integer, List<Long>> map, Transaction transaction, Boolean isStart) {
|
||
if (map != null && map.size() > 0) {
|
||
int redisStatisticsRealDBIndex = Configurations.getIntProperty("redisStatisticsRealDBIndex", 14);
|
||
transaction.select(redisStatisticsRealDBIndex);
|
||
String maatVersionStr = JedisUtils.get("MAAT_VERSION", redisStatisticsRealDBIndex);
|
||
if (maatVersionStr == null) {
|
||
throw new ServiceRuntimeException("从" + redisStatisticsRealDBIndex + "号redis库中获取MAAT_VERSION失败",
|
||
RestBusinessCode.KeyNotExistsInRedis.getValue());
|
||
}
|
||
double maatVersion = Double.valueOf(maatVersionStr) + 1d;
|
||
if (redisStatisticsRealDBIndex >= 0
|
||
&& redisStatisticsRealDBIndex < Configurations.getIntProperty("maxRedisDBIndex", 16)) {
|
||
for (Integer service : map.keySet()) {
|
||
String maatTableName = ServiceAndRDBIndexReal.getMaatTableName(service, 10, null);
|
||
List<Long> idList = map.get(service);
|
||
if (idList != null && idList.size() > 0) {
|
||
for (Long compileId : idList) {
|
||
String effectiveRuleKey = "EFFECTIVE_RULE:" + maatTableName + "," + compileId;
|
||
if (isStart) {
|
||
transaction.rename(
|
||
effectiveRuleKey.toUpperCase().replace("EFFECTIVE_RULE", "OBSOLETE_RULE"),
|
||
effectiveRuleKey.toUpperCase());
|
||
} else {
|
||
transaction.rename(effectiveRuleKey.toUpperCase(),
|
||
effectiveRuleKey.toUpperCase().replace("EFFECTIVE_RULE", "OBSOLETE_RULE"));
|
||
}
|
||
logger.info("向{}号redis数据库修改了一条配置,修改前key是{},修改后key是:{}", redisStatisticsRealDBIndex,
|
||
effectiveRuleKey.toUpperCase(),
|
||
effectiveRuleKey.toUpperCase().replace("EFFECTIVE_RULE", "OBSOLETE_RULE"));
|
||
String zset = effectiveRuleKey.replace("EFFECTIVE_RULE:", "DEL,");
|
||
if (isStart == null || isStart == false) {
|
||
} else if (isStart) {
|
||
zset = effectiveRuleKey.replace("EFFECTIVE_RULE:", "ADD,");
|
||
}
|
||
transaction.zadd("MAAT_UPDATE_STATUS", maatVersion, zset);
|
||
logger.info("向{}号redis数据库更新了MAAT_UPDATE_STATUS,内容是{},SCORES是{}", redisStatisticsRealDBIndex,
|
||
zset.toUpperCase(), maatVersion);
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException("取消配置时,未发现配置Id信息,请检查配置参数是否正确",
|
||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
}
|
||
|
||
}
|
||
transaction.incrBy("MAAT_VERSION", 1l);
|
||
logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisStatisticsRealDBIndex,
|
||
Integer.valueOf(maatVersionStr) + 1);
|
||
return true;
|
||
} else {
|
||
throw new ServiceRuntimeException("redis数据库编号:" + redisStatisticsRealDBIndex + "不正确,请检查数据库编号",
|
||
RestBusinessCode.DbIndexNotInRange.getValue());
|
||
}
|
||
|
||
} else {
|
||
throw new ServiceRuntimeException("状态更新操作Map参数信息不能为空,请检查配置参数是否正确",
|
||
RestBusinessCode.ConfigInfoMapIsNull.getValue());
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* 失效或生效配置后,更新编译,组,域等配置id的对应关系
|
||
*
|
||
* @param map redisdb与编译id的对应
|
||
* @param transaction
|
||
* @param isStart 是否将配置置为生效
|
||
* @param compileGroupMap 将配置置为失效时,需要保留哪些组(一般针对公共组的配置,保留当前组,下次可以重复利用改组及其下的域)
|
||
* @return
|
||
*/
|
||
private boolean removeMaatRelation(Map<Integer, List<Long>> map, Transaction transaction, Boolean isStart,
|
||
Map<Long, List<Long>> compileGroupMap) {
|
||
if (map != null && map.size() > 0) {
|
||
int idRelaRedisDBIndex = Configurations.getIntProperty("idRelaRedisDBIndex", 15);
|
||
transaction.select(idRelaRedisDBIndex);
|
||
for (Integer service : map.keySet()) {
|
||
List<Long> idList = map.get(service);
|
||
if (idList != null && idList.size() > 0) {
|
||
for (Long compileId : idList) {
|
||
List<Long> keepGroupIdList = compileGroupMap.get(compileId);
|
||
String compileStr = "COMPILEGROUP:" + compileId;
|
||
String compileStrVal = JedisUtils.get(compileStr, idRelaRedisDBIndex);// 根据编译id获取该编译下的分组关系
|
||
if (compileStrVal != null && !compileStrVal.trim().equals("")) {
|
||
String[] split1 = org.apache.commons.lang.StringUtils.split(compileStrVal, ";");
|
||
for (String str : split1) {// 遍历编译下的所有组
|
||
if (str != null && !str.trim().equals("")) {
|
||
String groupCompile = getRegionInfo(str);// 去除redisdb,获取组的真实groupcompile
|
||
if (groupCompile != null && !groupCompile.equals("")) {
|
||
String compileGroupStr = JedisUtils.get(groupCompile.toUpperCase(),
|
||
idRelaRedisDBIndex); // 获取当前分组关系对应的编译信息
|
||
if (compileGroupStr != null && !compileGroupStr.equals("")) {
|
||
String[] compileGroupStrSplit = org.apache.commons.lang.StringUtils
|
||
.split(compileGroupStr, ";");
|
||
for (String compileGroup : compileGroupStrSplit) {
|
||
compileGroup = getRegionInfo(compileGroup);
|
||
if (compileGroup.toUpperCase().equals(compileStr.toUpperCase())) {// 判断是否是同一个配置
|
||
// 域
|
||
String groupRegion = groupCompile.replace("GROUPCOMPILE",
|
||
"GROUPREGION");// groupregion里面value是region的信息,key是group的信息,所以可以直接将GROUPCOMPILE替换为GROUPREGION
|
||
// String commonGroupRegion = groupCompile.replace("GROUPCOMPILE",
|
||
// "COMMONGROUPREGION");// groupregion里面value是region的信息,key是group的信息,所以可以直接将GROUPCOMPILE替换为GROUPREGION
|
||
|
||
String groupRegionVal = JedisUtils.get(groupRegion,
|
||
idRelaRedisDBIndex);// 获取普通group下面的region
|
||
|
||
if (groupRegionVal == null || groupRegionVal.trim().equals("")) {// 如果从普通的组中无法获,取则尝试下获取公共组
|
||
|
||
groupRegion = groupCompile.replace("GROUPCOMPILE",
|
||
"COMMONGROUPREGION");
|
||
|
||
groupRegionVal = JedisUtils.get(groupRegion,
|
||
idRelaRedisDBIndex);
|
||
}
|
||
if (groupRegionVal == null || groupRegionVal.trim().equals("")) {// 如果普通的组和公共组都无法获取则给出错误提示
|
||
throw new ServiceRuntimeException(
|
||
"从" + idRelaRedisDBIndex + "号redis库中无法获取"
|
||
+ groupCompile.replace(
|
||
"GROUPCOMPILE", "COMMONGROUPREGION")
|
||
+ "和"
|
||
+ groupCompile.replace("GROUPCOMPILE",
|
||
"GROUPREGION")
|
||
+ ",请检查groupid是否正确,或联系开发人员检查删除redis数据是否被人为删除了",
|
||
RestBusinessCode.KeyNotExistsInRedis.getValue());
|
||
}
|
||
try {
|
||
// 修改分组与域的关联关系
|
||
if (isStart) {// 将失效置为生效
|
||
groupRegionVal = groupRegionVal.replace("OBSOLETE_RULE",
|
||
"EFFECTIVE_RULE");
|
||
String[] split = org.apache.commons.lang.StringUtils
|
||
.split(groupRegionVal, ";");
|
||
if (split.length > 1) {
|
||
Set<String> set = new HashSet<>();
|
||
for (String regionKeyStr : split) {
|
||
if (regionKeyStr != null
|
||
&& !regionKeyStr.trim().equals("")) {
|
||
set.add(regionKeyStr);
|
||
}
|
||
}
|
||
StringBuffer sb = new StringBuffer();
|
||
for (String regionKeyStr : set) {
|
||
sb.append(";");
|
||
sb.append(regionKeyStr);
|
||
}
|
||
transaction.set(groupRegion, sb.toString());// 避免有多个重复的region-key
|
||
} else {
|
||
transaction.set(groupRegion.toUpperCase(),
|
||
groupRegionVal);
|
||
}
|
||
} else {// 将生效变为失效
|
||
long groupId = -1l;
|
||
if (groupRegion.contains("COMMONGROUPREGION:")) {
|
||
groupId = Long.parseLong(
|
||
groupRegion.replace("COMMONGROUPREGION:", ""));// 获取真实的groupid
|
||
} else {
|
||
groupId = Long.parseLong(
|
||
groupRegion.replace("GROUPREGION:", ""));// 获取真实的groupid
|
||
}
|
||
if (keepGroupIdList == null || keepGroupIdList.size() == 0
|
||
|| !keepGroupIdList.contains(groupId)) {// 如果不是需要保留的groupid则失效(公共组不失效[如需失效使用公共组失效接口],非公共组则失效)
|
||
groupRegionVal = groupRegionVal
|
||
.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
|
||
String[] split = org.apache.commons.lang.StringUtils
|
||
.split(groupRegionVal, ";");
|
||
if (split.length > 1) {
|
||
Set<String> set = new HashSet<>();
|
||
|
||
for (String regionKeyStr : split) {
|
||
if (regionKeyStr != null
|
||
&& !regionKeyStr.trim().equals("")) {
|
||
set.add(regionKeyStr);
|
||
}
|
||
}
|
||
StringBuffer sb = new StringBuffer();
|
||
for (String regionKeyStr : set) {
|
||
sb.append(";");
|
||
sb.append(regionKeyStr);
|
||
}
|
||
transaction.set(groupRegion, sb.toString());// 避免有多个重复的region-key
|
||
|
||
} else {
|
||
transaction.set(groupRegion.toUpperCase(),
|
||
groupRegionVal);
|
||
}
|
||
}
|
||
}
|
||
|
||
} catch (Exception e) {
|
||
throw new ServiceRuntimeException(
|
||
"从" + idRelaRedisDBIndex + "号redis库中删除" + groupRegion
|
||
+ "失败,失败原因:" + ExceptionUtil.getExceptionMsg(e),
|
||
RestBusinessCode.DeleteDataInError.getValue());
|
||
}
|
||
}
|
||
//
|
||
Set<String> set = new HashSet<>();
|
||
if (compileGroup.toUpperCase().equals(compileStr.toUpperCase())
|
||
&& compileGroupStrSplit.length == 1) {// 判断是否是同一个配置
|
||
try {
|
||
// 为了提高效率,不判断了,如果报错直接捕捉异常处理
|
||
// 删除当前组所对应的编译
|
||
String groupRegionVal = JedisUtils
|
||
.get(groupCompile.toUpperCase(), idRelaRedisDBIndex);
|
||
if (isStart == null) {// 删除
|
||
transaction.del(groupCompile.toUpperCase());// 删除编译与分组的关联关系
|
||
} else if (isStart) {// 将失效置为生效
|
||
groupRegionVal = groupRegionVal.replace("OBSOLETE_RULE",
|
||
"EFFECTIVE_RULE");
|
||
String[] split2 = org.apache.commons.lang.StringUtils
|
||
.split(groupRegionVal, ";");
|
||
if (split2.length > 1) {
|
||
Set<String> set1 = new HashSet<>();
|
||
for (String regionKeyStr : split2) {
|
||
if (regionKeyStr != null
|
||
&& !regionKeyStr.trim().equals("")) {
|
||
set1.add(regionKeyStr);
|
||
}
|
||
}
|
||
StringBuffer sb = new StringBuffer();
|
||
for (String regionKeyStr : set1) {
|
||
sb.append(";");
|
||
sb.append(regionKeyStr);
|
||
}
|
||
transaction.set(groupCompile.toUpperCase(),
|
||
sb.toString());
|
||
|
||
} else {
|
||
transaction.set(groupCompile.toUpperCase(),
|
||
groupRegionVal);
|
||
}
|
||
} else {// 将生效变为失效
|
||
groupRegionVal = groupRegionVal.replace("EFFECTIVE_RULE",
|
||
"OBSOLETE_RULE");
|
||
|
||
String[] split2 = org.apache.commons.lang.StringUtils
|
||
.split(groupRegionVal, ";");
|
||
if (split2.length > 1) {
|
||
|
||
Set<String> set2 = new HashSet<>();
|
||
for (String regionKeyStr : split2) {
|
||
if (regionKeyStr != null
|
||
&& !regionKeyStr.trim().equals("")) {
|
||
set2.add(regionKeyStr);
|
||
}
|
||
}
|
||
StringBuffer sb = new StringBuffer();
|
||
for (String regionKeyStr : set2) {
|
||
sb.append(";");
|
||
sb.append(regionKeyStr);
|
||
}
|
||
transaction.set(groupCompile.toUpperCase(),
|
||
sb.toString());
|
||
} else {
|
||
transaction.set(groupCompile.toUpperCase(),
|
||
groupRegionVal);
|
||
}
|
||
}
|
||
} catch (Exception e) {
|
||
throw new ServiceRuntimeException("从" + idRelaRedisDBIndex
|
||
+ "号redis库中删除" + groupCompile.toUpperCase()
|
||
+ "失败,redis中不存在该值,请联系开发人员检查删除逻辑是否正确或redis数据是否出现了异常",
|
||
RestBusinessCode.KeyNotExistsInRedis.getValue());
|
||
}
|
||
} else {
|
||
set.add(compileGroup);
|
||
}
|
||
|
||
}
|
||
|
||
} else {
|
||
throw new ServiceRuntimeException(
|
||
"从" + idRelaRedisDBIndex + "号redis库中无法获取MAAT配置分组与编译的关联关系,key为"
|
||
+ groupCompile.toUpperCase(),
|
||
RestBusinessCode.KeyNotExistsInRedis.getValue());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
} else {
|
||
throw new ServiceRuntimeException(
|
||
"从" + idRelaRedisDBIndex + "号redis关联关系库中获取" + compileStr
|
||
+ "的值为null,redis中不存在该值,请联系开发人员检查删除逻辑是否正确或redis数据是否出现了异常",
|
||
RestBusinessCode.KeyNotExistsInRedis.getValue());
|
||
}
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException(
|
||
"删除配置时,未从service与配置id的对应关系中找到service=" + service + "的配置id,请检查传入的参数是否正确",
|
||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
}
|
||
}
|
||
return true;
|
||
} else {
|
||
throw new ServiceRuntimeException("状态更新操作Map参数信息不能为空,请检查配置参数是否正确",
|
||
RestBusinessCode.ConfigInfoMapIsNull.getValue());
|
||
}
|
||
}
|
||
|
||
private void addReionToCommonGroup(List<Map<String, String>> regionMapList, MaatXmlExpr maatXmlExpr,
|
||
Transaction transaction, int type, int idRelaRedisDBIndex, List<MaatXmlExpr> expressionList, boolean isAdd)
|
||
throws Exception {
|
||
Set<Long> groupIdSet = new HashSet<>();
|
||
Map<Integer, Double> dbVersionMap = new HashMap<>();// 记录所有redisdb的maatversion,避免每次查询影响性能
|
||
if (regionMapList != null && regionMapList.size() > 0) {
|
||
String[] keySplit = maatXmlExpr.getKeyExpression().split(";");
|
||
|
||
for (Map<String, String> map : regionMapList) {
|
||
String maatKey = null;
|
||
StringBuffer keyBF = new StringBuffer();
|
||
for (String keyStr : keySplit) {
|
||
if (!StringUtils.isEmpty(keyStr) && keyStr.trim().startsWith("[")) {
|
||
keyStr = keyStr.trim().replace("[", "").replace("]", "");
|
||
String keyVal = map.get(keyStr);
|
||
if (keyVal != null && !keyVal.equals("")) {
|
||
keyBF.append(keyVal);
|
||
} else {
|
||
throw new ServiceRuntimeException("未从map中获取到" + keyStr + "的值,请检查数据或配置文件是否正确",
|
||
RestBusinessCode.NotFoundValueByKey.getValue());
|
||
}
|
||
} else if (!StringUtils.isEmpty(keyStr) && keyStr.trim().startsWith("{")) {
|
||
keyStr = keyStr.trim().replace("{", "").replace("}", "");
|
||
if (keyStr.toLowerCase().contains("table_name")) {
|
||
String argTableName = map.get("table_name");
|
||
if (argTableName == null || argTableName.trim().equals("")) {
|
||
throw new ServiceRuntimeException("添加公共组域配置时,必须要传入表名,请检查参数,配置类型:" + type + ",对应的真实表名",
|
||
RestBusinessCode.NotFoundTableName.getValue());
|
||
} else {
|
||
keyBF.append(argTableName);
|
||
}
|
||
}
|
||
} else {
|
||
keyBF.append(keyStr.trim());
|
||
}
|
||
}
|
||
String groupId = null;
|
||
StringBuffer valBF = new StringBuffer();
|
||
String[] valSplit = maatXmlExpr.getValueExpression().split(";");
|
||
for (String valStr : valSplit) {
|
||
if (!StringUtils.isEmpty(valStr) && valStr.trim().startsWith("[")) {
|
||
valStr = valStr.trim().replace("[", "").replace("]", "");
|
||
if (valStr.toLowerCase().equals("op_time") && type == 12) {
|
||
String user_region = map.get("user_region");
|
||
valBF.append(user_region + "\t");
|
||
}
|
||
String val = map.get(valStr);
|
||
if (val != null && !val.trim().equals("")) {
|
||
valBF.append(val);
|
||
if (valStr.equals("group_id")) {
|
||
groupId = val;
|
||
}
|
||
} else {
|
||
// 所有在maat.xml中配置的属性都不可以为空
|
||
throw new ServiceRuntimeException("未从map中获取到" + valStr + "的值,无法拼接redisValue,请检查数据或配置文件是否正确",
|
||
RestBusinessCode.NotFoundValueByKey.getValue());
|
||
}
|
||
} else if (valStr.equals(" ")) {
|
||
valBF.append(" ");
|
||
} else if (valStr.equals("\\t")) {// xml中是字符串的\t这里判断的时候需要转义为\\t,但是添加的时候需要添加\t不是\\t
|
||
valBF.append("\t");
|
||
} else if (valStr.equals("\\n")) {
|
||
valBF.append("\n");
|
||
} else {
|
||
valBF.append(valStr.trim());
|
||
}
|
||
}
|
||
|
||
for (String db : Constants.COMMONGROUPDBARR) {
|
||
// 将域配置下发到每个db中,并更新MAAT_UPDATE_STATUS等信息
|
||
int everyDb = Integer.parseInt(db);
|
||
transaction.select(everyDb);
|
||
maatKey = keyBF.toString();
|
||
transaction.set(maatKey.toUpperCase(), valBF.toString());
|
||
logger.info("向{}号redis数据库添加了一条配置,key是{},value是{}", everyDb, maatKey.toUpperCase(),
|
||
valBF.toString());
|
||
|
||
Double maatVersion = null;
|
||
if (!dbVersionMap.containsKey(everyDb)) {
|
||
String maatVersionStr = JedisUtils.get("MAAT_VERSION", everyDb);
|
||
if (maatVersionStr == null) {
|
||
maatVersionStr = "0";
|
||
}
|
||
maatVersion = Double.valueOf(maatVersionStr) + 1D;
|
||
dbVersionMap.put(everyDb, maatVersion);
|
||
} else {
|
||
maatVersion = dbVersionMap.get(everyDb);
|
||
}
|
||
updateMaatInfo(expressionList, maatKey, transaction, maatVersion, everyDb, false);
|
||
}
|
||
groupIdSet.add(Long.parseLong(groupId));
|
||
String groupRegionStr = "COMMONGROUPREGION:" + groupId;
|
||
transaction.select(idRelaRedisDBIndex);
|
||
if (!isAdd) {
|
||
transaction.del(groupRegionStr);
|
||
}
|
||
// 更新组与域的关系
|
||
transaction.append(groupRegionStr, ";" + maatKey.toUpperCase() + "-" + Constants.COMMONGROUPDBARR[0]);// redisdb随便写一个即可,实际每个库都下发了
|
||
|
||
}
|
||
if (!isAdd) {
|
||
|
||
Map<String, String> keyMap = new HashMap<>();// 记录15号库中的COMPILEGROUP,GROUPCOMPILE,避免每次去redis中查询,影响性能
|
||
for (Long groupId : groupIdSet) {
|
||
String groupRegionStr = "COMMONGROUPREGION:" + groupId;
|
||
String regionVal = null;
|
||
if (!keyMap.containsKey(groupRegionStr)) {
|
||
regionVal = JedisUtils.get(groupRegionStr, idRelaRedisDBIndex);
|
||
keyMap.put(groupRegionStr, regionVal);
|
||
} else {
|
||
regionVal = keyMap.get(groupRegionStr);
|
||
}
|
||
|
||
if (regionVal != null && !regionVal.trim().equals("")) {
|
||
String[] commonsplit = org.apache.commons.lang.StringUtils.split(regionVal, ";");
|
||
for (String regionKey : commonsplit) {
|
||
String maatKey = regionKey.substring(0, regionKey.indexOf("-"));
|
||
for (String db : Constants.COMMONGROUPDBARR) {
|
||
int everyDb = Integer.parseInt(db);
|
||
transaction.select(everyDb);
|
||
String obsoleteKey = maatKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
|
||
transaction.rename(maatKey, obsoleteKey);
|
||
Double maatVersion = dbVersionMap.get(everyDb);
|
||
updateMaatInfo(expressionList, obsoleteKey, transaction, maatVersion, everyDb, true);
|
||
}
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException("修改公共组域配置时,无法获取到groupid=" + groupId + "的公共组,请检查配置参数是否正确",
|
||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
}
|
||
}
|
||
|
||
// for (String db : Constants.COMMONGROUPDBARR) {
|
||
// int everyDb = Integer.parseInt(db);
|
||
// transaction.select(everyDb);
|
||
// for (String commonGroupKey : keyMap.keySet()) {
|
||
// String[] commonsplit = org.apache.commons.lang.StringUtils.split(keyMap.get(commonGroupKey),
|
||
// ";");
|
||
// for (String regionKey : commonsplit) {
|
||
// String maatKey = regionKey.substring(0, regionKey.indexOf("-"));
|
||
// try {
|
||
// transaction.del(maatKey);
|
||
// } catch (Exception e) {
|
||
// logger.error("公共组:{}中的域配置:{}在{}号db中找不到,请联系开发人员检查业务逻辑是否正确,失败原因:{}", commonGroupKey,
|
||
// maatKey, everyDb, ExceptionUtil.getExceptionMsg(e));
|
||
// }
|
||
// Double maatVersion = dbVersionMap.get(everyDb);
|
||
// updateMaatInfo(expressionList, maatKey, transaction, maatVersion, everyDb, false);
|
||
// }
|
||
// }
|
||
// }
|
||
}
|
||
}
|
||
}
|
||
|
||
private void updateCommonGroupRegion(List<Map<String, String>> regionMapList, MaatXmlExpr maatXmlExpr,
|
||
Transaction transaction, int type, int idRelaRedisDBIndex, List<MaatXmlExpr> expressionList)
|
||
throws Exception {
|
||
Map<Integer, Double> dbVersionMap = new HashMap<>();// 记录所有redisdb的maatversion,避免每次查询影响性能
|
||
if (regionMapList != null && regionMapList.size() > 0) {
|
||
String[] keySplit = maatXmlExpr.getKeyExpression().split(";");
|
||
|
||
for (Map<String, String> map : regionMapList) {
|
||
String maatKey = null;
|
||
StringBuffer keyBF = new StringBuffer();
|
||
for (String keyStr : keySplit) {
|
||
if (!StringUtils.isEmpty(keyStr) && keyStr.trim().startsWith("[")) {
|
||
keyStr = keyStr.trim().replace("[", "").replace("]", "");
|
||
String keyVal = map.get(keyStr);
|
||
if (keyVal != null && !keyVal.equals("")) {
|
||
keyBF.append(keyVal);
|
||
} else {
|
||
throw new ServiceRuntimeException("未从map中获取到" + keyStr + "的值,请检查数据或配置文件是否正确",
|
||
RestBusinessCode.NotFoundValueByKey.getValue());
|
||
}
|
||
} else if (!StringUtils.isEmpty(keyStr) && keyStr.trim().startsWith("{")) {
|
||
keyStr = keyStr.trim().replace("{", "").replace("}", "");
|
||
if (keyStr.toLowerCase().contains("table_name")) {
|
||
String argTableName = map.get("table_name");
|
||
if (argTableName == null) {
|
||
throw new ServiceRuntimeException("添加公共组域配置时,必须要传入表名,请检查参数,配置类型:" + type + ",对应的真实表名",
|
||
RestBusinessCode.NotFoundTableName.getValue());
|
||
} else {
|
||
keyBF.append(argTableName);
|
||
}
|
||
}
|
||
} else {
|
||
keyBF.append(keyStr.trim());
|
||
}
|
||
}
|
||
// String groupId = null;
|
||
StringBuffer valBF = new StringBuffer();
|
||
String[] valSplit = maatXmlExpr.getValueExpression().split(";");
|
||
for (String valStr : valSplit) {
|
||
if (!StringUtils.isEmpty(valStr) && valStr.trim().startsWith("[")) {
|
||
valStr = valStr.trim().replace("[", "").replace("]", "");
|
||
if (valStr.toLowerCase().equals("op_time") && type == 12) {
|
||
String user_region = map.get("user_region");
|
||
valBF.append(user_region + "\t");
|
||
}
|
||
String val = map.get(valStr);
|
||
if (val != null && !val.trim().equals("")) {
|
||
valBF.append(val);
|
||
} else {
|
||
// 所有在maat.xml中配置的属性都不可以为空
|
||
throw new ServiceRuntimeException("未从map中获取到" + valStr + "的值,无法拼接redisValue,请检查数据或配置文件是否正确",
|
||
RestBusinessCode.NotFoundValueByKey.getValue());
|
||
}
|
||
} else if (valStr.equals(" ")) {
|
||
valBF.append(" ");
|
||
} else if (valStr.equals("\\t")) {// xml中是字符串的\t这里判断的时候需要转义为\\t,但是添加的时候需要添加\t不是\\t
|
||
valBF.append("\t");
|
||
} else if (valStr.equals("\\n")) {
|
||
valBF.append("\n");
|
||
} else {
|
||
valBF.append(valStr.trim());
|
||
}
|
||
}
|
||
|
||
for (String db : Constants.COMMONGROUPDBARR) {
|
||
// 将域配置下发到每个db中,并更新MAAT_UPDATE_STATUS等信息
|
||
int everyDb = Integer.parseInt(db);
|
||
transaction.select(everyDb);
|
||
maatKey = keyBF.toString();
|
||
transaction.set(maatKey.toUpperCase(), valBF.toString());
|
||
logger.info("向{}号redis数据库添加了一条配置,key是{},value是{}", everyDb, maatKey.toUpperCase(),
|
||
valBF.toString());
|
||
|
||
Double maatVersion = null;
|
||
if (!dbVersionMap.containsKey(everyDb)) {
|
||
String maatVersionStr = JedisUtils.get("MAAT_VERSION", everyDb);
|
||
if (maatVersionStr == null) {
|
||
maatVersionStr = "0";
|
||
}
|
||
maatVersion = Double.valueOf(maatVersionStr) + 1D;
|
||
dbVersionMap.put(everyDb, maatVersion);
|
||
} else {
|
||
maatVersion = dbVersionMap.get(everyDb);
|
||
}
|
||
updateMaatInfo(expressionList, maatKey, transaction, maatVersion, everyDb, false);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public boolean updateGroupRegion(List<MaatConfig> configList, boolean isAdd) {
|
||
Jedis resource = JedisUtils.getResource(0);
|
||
Transaction transaction = resource.multi();
|
||
try {
|
||
if (configList != null && configList.size() > 0) {
|
||
int idRelaRedisDBIndex = Configurations.getIntProperty("idRelaRedisDBIndex", 15);
|
||
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(37);// maat类配置的表达式都一样,这里因为没有service所以就随便取了一个
|
||
MaatXmlExpr maatXmlExpr12 = null;
|
||
MaatXmlExpr maatXmlExpr13 = null;
|
||
MaatXmlExpr maatXmlExpr14 = null;
|
||
MaatXmlExpr maatXmlExpr15 = null;
|
||
MaatXmlExpr maatXmlExpr16 = null;
|
||
MaatXmlExpr maatXmlExpr17 = null;
|
||
MaatXmlExpr maatXmlExpr18 = null;
|
||
if (maatXmlConfig != null) {
|
||
List<MaatXmlExpr> expressionList = maatXmlConfig.getExpressionList();
|
||
if (expressionList != null && expressionList.size() > 0) {
|
||
for (MaatXmlExpr maatXmlExpr : expressionList) {
|
||
if (maatXmlExpr.getType().intValue() == 12) {
|
||
maatXmlExpr12 = maatXmlExpr;
|
||
} else if (maatXmlExpr.getType().intValue() == 13) {
|
||
maatXmlExpr13 = maatXmlExpr;
|
||
} else if (maatXmlExpr.getType().intValue() == 14) {
|
||
maatXmlExpr14 = maatXmlExpr;
|
||
} else if (maatXmlExpr.getType().intValue() == 15) {
|
||
maatXmlExpr15 = maatXmlExpr;
|
||
} else if (maatXmlExpr.getType().intValue() == 16) {
|
||
maatXmlExpr16 = maatXmlExpr;
|
||
} else if (maatXmlExpr.getType().intValue() == 17) {
|
||
maatXmlExpr17 = maatXmlExpr;
|
||
} else if (maatXmlExpr.getType().intValue() == 18) {
|
||
maatXmlExpr18 = maatXmlExpr;
|
||
}
|
||
}
|
||
}
|
||
int count = 0;
|
||
for (MaatConfig maatConfig : configList) {
|
||
if (maatConfig.getIpRegionMapList() != null && maatConfig.getIpRegionMapList().size() > 0) {
|
||
count += maatConfig.getIpRegionMapList().size();
|
||
addReionToCommonGroup(maatConfig.getIpRegionMapList(), maatXmlExpr12, transaction, 12,
|
||
idRelaRedisDBIndex, expressionList, isAdd);
|
||
}
|
||
if (maatConfig.getNumRegionMapList() != null && maatConfig.getNumRegionMapList().size() > 0) {
|
||
count += maatConfig.getNumRegionMapList().size();
|
||
addReionToCommonGroup(maatConfig.getNumRegionMapList(), maatXmlExpr13, transaction, 13,
|
||
idRelaRedisDBIndex, expressionList, isAdd);
|
||
}
|
||
if (maatConfig.getStrRegionMapList() != null && maatConfig.getStrRegionMapList().size() > 0) {
|
||
count += maatConfig.getStrRegionMapList().size();
|
||
addReionToCommonGroup(maatConfig.getStrRegionMapList(), maatXmlExpr14, transaction, 14,
|
||
idRelaRedisDBIndex, expressionList, isAdd);
|
||
}
|
||
if (maatConfig.getStrStrRegionMapList() != null && maatConfig.getStrStrRegionMapList().size() > 0) {
|
||
count += maatConfig.getStrStrRegionMapList().size();
|
||
addReionToCommonGroup(maatConfig.getStrStrRegionMapList(), maatXmlExpr15, transaction, 15,
|
||
idRelaRedisDBIndex, expressionList, isAdd);
|
||
}
|
||
if (maatConfig.getFileDigestRegionMapList() != null && maatConfig.getFileDigestRegionMapList().size() > 0) {
|
||
count += maatConfig.getFileDigestRegionMapList().size();
|
||
addReionToCommonGroup(maatConfig.getFileDigestRegionMapList(), maatXmlExpr16, transaction,
|
||
16, idRelaRedisDBIndex, expressionList, isAdd);
|
||
}
|
||
if (maatConfig.getFileLikeRegionMapList() != null && maatConfig.getFileLikeRegionMapList().size() > 0) {
|
||
count += maatConfig.getFileLikeRegionMapList().size();
|
||
addReionToCommonGroup(maatConfig.getFileLikeRegionMapList(), maatXmlExpr17, transaction, 17,
|
||
idRelaRedisDBIndex, expressionList, isAdd);
|
||
}
|
||
if (maatConfig.getIpClientRangeMapList() != null && maatConfig.getIpClientRangeMapList().size() > 0) {
|
||
count += maatConfig.getIpClientRangeMapList().size();
|
||
addReionToCommonGroup(maatConfig.getIpClientRangeMapList(), maatXmlExpr18, transaction, 18,
|
||
idRelaRedisDBIndex, expressionList, isAdd);
|
||
}
|
||
|
||
}
|
||
if (count == 0) {
|
||
throw new ServiceRuntimeException("添加公共组域配置时,未发现对应的配置信息,请检查配置参数是否正确",
|
||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
}
|
||
for (String db : Constants.COMMONGROUPDBARR) {
|
||
transaction.select(Integer.parseInt(db));
|
||
transaction.incrBy("MAAT_VERSION", 1l);
|
||
logger.info("向{}号redis数据库更新了MAAT_VERSION", Integer.parseInt(db));
|
||
// logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDb, maatVersion);
|
||
}
|
||
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException("添加公共组域配置时,未发现对应的配置信息,请检查配置参数是否正确",
|
||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
}
|
||
transaction.exec();
|
||
return true;
|
||
} catch (JedisConnectionException e) {
|
||
String error = "连接redis异常,保存公共组maat类域配置失败" + e.getMessage();
|
||
logger.error(error + " " + ExceptionUtil.getExceptionMsg(e));
|
||
throw new ServiceRuntimeException(error, RestBusinessCode.CannotConnectionRedis.getValue());
|
||
} catch (Exception e) {
|
||
|
||
String error = "保存公共组maat类域配置发生了异常" + e.getMessage();
|
||
logger.error(error + " " + ExceptionUtil.getExceptionMsg(e));
|
||
transaction.discard();
|
||
throw new ServiceRuntimeException(error, RestBusinessCode.SaveDataInError.getValue());
|
||
} finally {
|
||
// 释放连接到连接池
|
||
JedisUtils.returnResource(resource);
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public boolean updateGroupRegion(List<MaatConfig> configList) {
|
||
Jedis resource = JedisUtils.getResource(0);
|
||
Transaction transaction = resource.multi();
|
||
try {
|
||
if (configList != null && configList.size() > 0) {
|
||
int idRelaRedisDBIndex = Configurations.getIntProperty("idRelaRedisDBIndex", 15);
|
||
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(37);// maat类配置的表达式都一样,这里因为没有service所以就随便取了一个
|
||
MaatXmlExpr maatXmlExpr12 = null;
|
||
MaatXmlExpr maatXmlExpr13 = null;
|
||
MaatXmlExpr maatXmlExpr14 = null;
|
||
MaatXmlExpr maatXmlExpr15 = null;
|
||
MaatXmlExpr maatXmlExpr16 = null;
|
||
MaatXmlExpr maatXmlExpr17 = null;
|
||
MaatXmlExpr maatXmlExpr18 = null;
|
||
if (maatXmlConfig != null) {
|
||
List<MaatXmlExpr> expressionList = maatXmlConfig.getExpressionList();
|
||
if (expressionList != null && expressionList.size() > 0) {
|
||
for (MaatXmlExpr maatXmlExpr : expressionList) {
|
||
if (maatXmlExpr.getType().intValue() == 12) {
|
||
maatXmlExpr12 = maatXmlExpr;
|
||
} else if (maatXmlExpr.getType().intValue() == 13) {
|
||
maatXmlExpr13 = maatXmlExpr;
|
||
} else if (maatXmlExpr.getType().intValue() == 14) {
|
||
maatXmlExpr14 = maatXmlExpr;
|
||
} else if (maatXmlExpr.getType().intValue() == 15) {
|
||
maatXmlExpr15 = maatXmlExpr;
|
||
} else if (maatXmlExpr.getType().intValue() == 16) {
|
||
maatXmlExpr16 = maatXmlExpr;
|
||
} else if (maatXmlExpr.getType().intValue() == 17) {
|
||
maatXmlExpr17 = maatXmlExpr;
|
||
} else if (maatXmlExpr.getType().intValue() == 18) {
|
||
maatXmlExpr18 = maatXmlExpr;
|
||
}
|
||
}
|
||
}
|
||
int count = 0;
|
||
for (MaatConfig maatConfig : configList) {
|
||
if (maatConfig.getIpRegionMapList() != null && maatConfig.getIpRegionMapList().size() > 0) {
|
||
count += maatConfig.getIpRegionMapList().size();
|
||
updateCommonGroupRegion(maatConfig.getIpRegionMapList(), maatXmlExpr12, transaction, 12,
|
||
idRelaRedisDBIndex, expressionList);
|
||
}
|
||
if (maatConfig.getNumRegionMapList() != null && maatConfig.getNumRegionMapList().size() > 0) {
|
||
count += maatConfig.getNumRegionMapList().size();
|
||
updateCommonGroupRegion(maatConfig.getNumRegionMapList(), maatXmlExpr13, transaction, 13,
|
||
idRelaRedisDBIndex, expressionList);
|
||
}
|
||
if (maatConfig.getStrRegionMapList() != null && maatConfig.getStrRegionMapList().size() > 0) {
|
||
count += maatConfig.getStrRegionMapList().size();
|
||
updateCommonGroupRegion(maatConfig.getStrRegionMapList(), maatXmlExpr14, transaction, 14,
|
||
idRelaRedisDBIndex, expressionList);
|
||
}
|
||
if (maatConfig.getStrStrRegionMapList() != null
|
||
&& maatConfig.getStrStrRegionMapList().size() > 0) {
|
||
count += maatConfig.getStrStrRegionMapList().size();
|
||
updateCommonGroupRegion(maatConfig.getStrStrRegionMapList(), maatXmlExpr15, transaction, 15,
|
||
idRelaRedisDBIndex, expressionList);
|
||
}
|
||
if (maatConfig.getFileDigestRegionMapList() != null
|
||
&& maatConfig.getFileDigestRegionMapList().size() > 0) {
|
||
count += maatConfig.getFileDigestRegionMapList().size();
|
||
updateCommonGroupRegion(maatConfig.getFileDigestRegionMapList(), maatXmlExpr16, transaction,
|
||
16, idRelaRedisDBIndex, expressionList);
|
||
}
|
||
if (maatConfig.getFileLikeRegionMapList() != null
|
||
&& maatConfig.getFileLikeRegionMapList().size() > 0) {
|
||
count += maatConfig.getFileLikeRegionMapList().size();
|
||
updateCommonGroupRegion(maatConfig.getFileLikeRegionMapList(), maatXmlExpr17, transaction,
|
||
17, idRelaRedisDBIndex, expressionList);
|
||
}
|
||
if (maatConfig.getIpClientRangeMapList() != null
|
||
&& maatConfig.getIpClientRangeMapList().size() > 0) {
|
||
count += maatConfig.getIpClientRangeMapList().size();
|
||
updateCommonGroupRegion(maatConfig.getIpClientRangeMapList(), maatXmlExpr18, transaction,
|
||
18, idRelaRedisDBIndex, expressionList);
|
||
}
|
||
}
|
||
|
||
if (count == 0) {
|
||
throw new ServiceRuntimeException("修改公共组域配置时,未发现对应的配置信息,请检查配置参数是否正确",
|
||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
}
|
||
|
||
for (String db : Constants.COMMONGROUPDBARR) {
|
||
transaction.select(Integer.parseInt(db));
|
||
transaction.incrBy("MAAT_VERSION", 1l);
|
||
logger.info("向{}号redis数据库更新了MAAT_VERSION", Integer.parseInt(db));
|
||
// logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDb, maatVersion);
|
||
}
|
||
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException("修改公共组域配置时,未发现对应的配置信息,请检查配置参数是否正确",
|
||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
}
|
||
transaction.exec();
|
||
return true;
|
||
} catch (JedisConnectionException e) {
|
||
String error = "连接redis异常,保存公共组maat类域配置失败" + e.getMessage();
|
||
logger.error(error + " " + ExceptionUtil.getExceptionMsg(e));
|
||
throw new ServiceRuntimeException(error, RestBusinessCode.CannotConnectionRedis.getValue());
|
||
} catch (Exception e) {
|
||
|
||
String error = "保存公共组maat类域配置发生了异常" + e.getMessage();
|
||
logger.error(error + " " + ExceptionUtil.getExceptionMsg(e));
|
||
transaction.discard();
|
||
throw new ServiceRuntimeException(error, RestBusinessCode.SaveDataInError.getValue());
|
||
} finally {
|
||
// 释放连接到连接池
|
||
JedisUtils.returnResource(resource);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 添加分组和域的关联关系
|
||
*
|
||
* @param maatConfig
|
||
* @param transaction
|
||
* @param redisDBIndex
|
||
* @param redisDBSetStr
|
||
* @param idRelaRedisDBIndex
|
||
*/
|
||
private void addGroupRegionReal(MaatConfig maatConfig, Transaction transaction, String redisDBSetStr,
|
||
int idRelaRedisDBIndex, List<Long> commonGroupIdList) {
|
||
Map<String, Set<String>> map = new HashMap<String, Set<String>>();
|
||
int service = maatConfig.getService();
|
||
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service);
|
||
addGroupAndRegionRelations(maatXmlConfig, service, 12, maatConfig.getIpRegionMapList(), map, redisDBSetStr,
|
||
commonGroupIdList);
|
||
addGroupAndRegionRelations(maatXmlConfig, service, 13, maatConfig.getNumRegionMapList(), map, redisDBSetStr,
|
||
commonGroupIdList);
|
||
addGroupAndRegionRelations(maatXmlConfig, service, 14, maatConfig.getStrRegionMapList(), map, redisDBSetStr,
|
||
commonGroupIdList);
|
||
addGroupAndRegionRelations(maatXmlConfig, service, 15, maatConfig.getStrStrRegionMapList(), map, redisDBSetStr,
|
||
commonGroupIdList);
|
||
addGroupAndRegionRelations(maatXmlConfig, service, 16, maatConfig.getFileDigestRegionMapList(), map,
|
||
redisDBSetStr, commonGroupIdList);
|
||
addGroupAndRegionRelations(maatXmlConfig, service, 17, maatConfig.getFileLikeRegionMapList(), map,
|
||
redisDBSetStr, commonGroupIdList);
|
||
addGroupAndRegionRelations(maatXmlConfig, service, 18, maatConfig.getIpClientRangeMapList(), map, redisDBSetStr,
|
||
commonGroupIdList);
|
||
|
||
for (String groupIdStr : map.keySet()) {
|
||
Set<String> list = map.get(groupIdStr);
|
||
StringBuffer sb = new StringBuffer();
|
||
if (list != null && list.size() > 0) {
|
||
for (String regionIdStr : list) {
|
||
sb.append(regionIdStr);
|
||
sb.append(";");
|
||
}
|
||
}
|
||
transaction.select(idRelaRedisDBIndex);// 选择关联关系库
|
||
transaction.append(groupIdStr, ";" + sb.substring(0, sb.length() - 1));
|
||
|
||
}
|
||
}
|
||
|
||
/**
|
||
*
|
||
*
|
||
* @param maatConfig
|
||
* @param maatXmlConfig
|
||
* @param maatVersion
|
||
* @param service
|
||
* @param transaction
|
||
* @param redisDBIndex
|
||
* @param isCommonGroup 是否是公共组,如果是公共组则需要向所有的db下发
|
||
*/
|
||
|
||
/**
|
||
* 保存域配置信息,如果当前域所属的组是公共组则需要向所有的redisdb中下发域配置,否则只向指定的组下发
|
||
*
|
||
* @param maatConfig 配置信息
|
||
* @param maatXmlConfig 配置的表达式
|
||
* @param maatVersion 版本号
|
||
* @param service 业务类型
|
||
* @param transaction redis连接
|
||
* @param redisDBIndex redis编号
|
||
* @param commonGroupIdList
|
||
* @return
|
||
*/
|
||
private Integer addRegion(MaatConfig maatConfig, MaatXmlConfig maatXmlConfig, Double maatVersion, int service,
|
||
Transaction transaction, Integer redisDBIndex, List<Long> commonGroupIdList) {
|
||
int count = 0;// 计算下所有的域
|
||
List<Map<String, String>> ipRegionMapList = maatConfig.getIpRegionMapList();
|
||
|
||
if (ipRegionMapList != null && ipRegionMapList.size() > 0) {
|
||
count += ipRegionMapList.size();
|
||
for (Map<String, String> map : ipRegionMapList) {
|
||
Long groupId = Long.parseLong(map.get("group_id"));
|
||
boolean isCommonGroup = commonGroupIdList.contains(groupId);// 判断当前域是否属于公共组
|
||
if (isCommonGroup) {// 如果属于公共组则向所有的数据库下发一份
|
||
for (String db : Constants.COMMONGROUPDBARR) {
|
||
setCommonConfig(maatXmlConfig, map, 12, maatVersion, service, transaction, Integer.parseInt(db),
|
||
null);// 12代表是ip类域配置
|
||
}
|
||
} else {
|
||
setCommonConfig(maatXmlConfig, map, 12, maatVersion, service, transaction, redisDBIndex, null);// 12代表是ip类域配置
|
||
}
|
||
}
|
||
}
|
||
List<Map<String, String>> numRegionMapList = maatConfig.getNumRegionMapList();
|
||
if (numRegionMapList != null && numRegionMapList.size() > 0) {
|
||
count += numRegionMapList.size();
|
||
for (Map<String, String> map : numRegionMapList) {
|
||
Long groupId = Long.parseLong(map.get("group_id"));
|
||
boolean isCommonGroup = commonGroupIdList.contains(groupId);
|
||
if (isCommonGroup) {
|
||
for (String db : Constants.COMMONGROUPDBARR) {
|
||
setCommonConfig(maatXmlConfig, map, 13, maatVersion, service, transaction, Integer.parseInt(db),
|
||
null);// 13代表是数值类配置
|
||
}
|
||
} else {
|
||
setCommonConfig(maatXmlConfig, map, 13, maatVersion, service, transaction, redisDBIndex, null);// 13代表是数值类配置
|
||
}
|
||
}
|
||
}
|
||
List<Map<String, String>> strRegionMapList = maatConfig.getStrRegionMapList();
|
||
if (strRegionMapList != null && strRegionMapList.size() > 0) {
|
||
count += strRegionMapList.size();
|
||
for (Map<String, String> map : strRegionMapList) {
|
||
Long groupId = Long.parseLong(map.get("group_id"));
|
||
boolean isCommonGroup = commonGroupIdList.contains(groupId);
|
||
if (isCommonGroup) {
|
||
for (String db : Constants.COMMONGROUPDBARR) {
|
||
setCommonConfig(maatXmlConfig, map, 14, maatVersion, service, transaction, Integer.parseInt(db),
|
||
null);// 14代表是字符串类域配置
|
||
}
|
||
} else {
|
||
setCommonConfig(maatXmlConfig, map, 14, maatVersion, service, transaction, redisDBIndex, null);// 14代表是字符串类域配置
|
||
}
|
||
}
|
||
}
|
||
List<Map<String, String>> strStrRegionMapList = maatConfig.getStrStrRegionMapList();
|
||
if (strStrRegionMapList != null && strStrRegionMapList.size() > 0) {
|
||
count += strStrRegionMapList.size();
|
||
for (Map<String, String> map : strStrRegionMapList) {
|
||
Long groupId = Long.parseLong(map.get("group_id"));
|
||
boolean isCommonGroup = commonGroupIdList.contains(groupId);
|
||
if (isCommonGroup) {
|
||
for (String db : Constants.COMMONGROUPDBARR) {
|
||
setCommonConfig(maatXmlConfig, map, 15, maatVersion, service, transaction, Integer.parseInt(db),
|
||
null);// 15代表是增强字符串类域配置
|
||
}
|
||
} else {
|
||
setCommonConfig(maatXmlConfig, map, 15, maatVersion, service, transaction, redisDBIndex, null);// 15代表是增强字符串类域配置
|
||
}
|
||
}
|
||
}
|
||
List<Map<String, String>> fileDigestRegionMapList = maatConfig.getFileDigestRegionMapList();
|
||
if (fileDigestRegionMapList != null && fileDigestRegionMapList.size() > 0) {
|
||
count += fileDigestRegionMapList.size();
|
||
for (Map<String, String> map : fileDigestRegionMapList) {
|
||
Long groupId = Long.parseLong(map.get("group_id"));
|
||
boolean isCommonGroup = commonGroupIdList.contains(groupId);
|
||
if (isCommonGroup) {
|
||
for (String db : Constants.COMMONGROUPDBARR) {
|
||
setCommonConfig(maatXmlConfig, map, 16, maatVersion, service, transaction, Integer.parseInt(db),
|
||
null);// 16代表是文件摘要类域配置
|
||
}
|
||
} else {
|
||
setCommonConfig(maatXmlConfig, map, 16, maatVersion, service, transaction, redisDBIndex, null);// 16代表是文件摘要类域配置
|
||
}
|
||
}
|
||
}
|
||
List<Map<String, String>> fileLikeRegionMapList = maatConfig.getFileLikeRegionMapList();
|
||
if (fileLikeRegionMapList != null && fileLikeRegionMapList.size() > 0) {
|
||
count += fileLikeRegionMapList.size();
|
||
for (Map<String, String> map : fileLikeRegionMapList) {
|
||
Long groupId = Long.parseLong(map.get("group_id"));
|
||
boolean isCommonGroup = commonGroupIdList.contains(groupId);
|
||
if (isCommonGroup) {
|
||
for (String db : Constants.COMMONGROUPDBARR) {
|
||
setCommonConfig(maatXmlConfig, map, 17, maatVersion, service, transaction, Integer.parseInt(db),
|
||
null);// 17代表是文本相似性域配置
|
||
}
|
||
} else {
|
||
setCommonConfig(maatXmlConfig, map, 17, maatVersion, service, transaction, redisDBIndex, null);// 17代表是文本相似性域配置
|
||
}
|
||
}
|
||
}
|
||
List<Map<String, String>> ipclientList = maatConfig.getIpClientRangeMapList();
|
||
if (ipclientList != null && ipclientList.size() > 0) {
|
||
count += ipclientList.size();
|
||
for (Map<String, String> map : ipclientList) {
|
||
Long groupId = Long.parseLong(map.get("group_id"));
|
||
boolean isCommonGroup = commonGroupIdList.contains(groupId);
|
||
if (isCommonGroup) {
|
||
for (String db : Constants.COMMONGROUPDBARR) {
|
||
setCommonConfig(maatXmlConfig, map, 18, maatVersion, service, transaction, Integer.parseInt(db),
|
||
null);// 18代表是区域ip域配置
|
||
}
|
||
} else {
|
||
setCommonConfig(maatXmlConfig, map, 18, maatVersion, service, transaction, redisDBIndex, null);// 18代表是区域ip域配置
|
||
}
|
||
}
|
||
}
|
||
|
||
// if (count == 0 && isReuseSaveRegion) {
|
||
// throw new ServiceRuntimeException("添加公共组域配置时,所有的域配置都为空,请检查配置参数是否正确",
|
||
// RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
// }
|
||
return count;
|
||
}
|
||
|
||
@Override
|
||
public boolean delCommonGroup(Set<Long> groupIdList) {
|
||
Jedis resource = JedisUtils.getResource(0);
|
||
Transaction transaction = resource.multi();
|
||
try {
|
||
if (groupIdList != null && groupIdList.size() > 0) {
|
||
int idRelaRedisDBIndex = Configurations.getIntProperty("idRelaRedisDBIndex", 15);
|
||
transaction.select(idRelaRedisDBIndex);
|
||
// Map<String, Set<String>> groupReionKey = new HashMap<>();
|
||
Set<String> set = new HashSet<>();
|
||
for (Long groupId : groupIdList) {
|
||
String commonGroup = "COMMONGROUPREGION:" + groupId;
|
||
String regionKey = JedisUtils.get(commonGroup, idRelaRedisDBIndex);
|
||
if (regionKey == null || regionKey.trim().equals("")) {
|
||
throw new ServiceRuntimeException("无法删除公共组" + groupId + ",该组下未找到对应的域,请检查",
|
||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
}
|
||
String[] keyAndDBArr = org.apache.commons.lang.StringUtils.split(regionKey, ";");
|
||
|
||
for (String keyAndDB : keyAndDBArr) {
|
||
String key = keyAndDB.substring(0, keyAndDB.indexOf("-"));
|
||
set.add(key);
|
||
}
|
||
// groupReionKey.put(commonGroup, set);
|
||
}
|
||
|
||
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(37);// maat类配置的表达式都一样,这里因为没有service所以就随便取了一个
|
||
List<MaatXmlExpr> expressionList = maatXmlConfig.getExpressionList();
|
||
for (String db : Constants.COMMONGROUPDBARR) {
|
||
int everyDb = Integer.parseInt(db);
|
||
|
||
Double maatVersion = null;
|
||
String maatVersionStr = JedisUtils.get("MAAT_VERSION", everyDb);
|
||
if (maatVersionStr == null) {
|
||
maatVersionStr = "0";
|
||
}
|
||
maatVersion = Double.valueOf(maatVersionStr) + 1D;
|
||
transaction.select(everyDb);
|
||
for (String regionKey : set) {
|
||
String obsKey = regionKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
|
||
transaction.rename(regionKey, obsKey);
|
||
updateMaatInfo(expressionList, obsKey, transaction, maatVersion, everyDb, true);
|
||
}
|
||
}
|
||
for (String db : Constants.COMMONGROUPDBARR) {
|
||
transaction.select(Integer.parseInt(db));
|
||
transaction.incrBy("MAAT_VERSION", 1l);
|
||
logger.info("向{}号redis数据库更新了MAAT_VERSION", Integer.parseInt(db));
|
||
// logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDb, maatVersion);
|
||
}
|
||
transaction.exec();
|
||
return true;
|
||
} else {
|
||
throw new ServiceRuntimeException("删除公共组时,参数为空,请检查", RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
|
||
}
|
||
|
||
} catch (JedisConnectionException e) {
|
||
String error = "连接redis异常,删除公共组maat类域配置失败" + e.getMessage();
|
||
logger.error(error + " " + ExceptionUtil.getExceptionMsg(e));
|
||
throw new ServiceRuntimeException(error, RestBusinessCode.CannotConnectionRedis.getValue());
|
||
} catch (Exception e) {
|
||
|
||
String error = "删除公共组maat类域配置发生了异常" + e.getMessage();
|
||
logger.error(error + " " + ExceptionUtil.getExceptionMsg(e));
|
||
transaction.discard();
|
||
throw new ServiceRuntimeException(error, RestBusinessCode.SaveDataInError.getValue());
|
||
} finally {
|
||
// 释放连接到连接池
|
||
JedisUtils.returnResource(resource);
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public boolean delCommonGroupRegion(Map<Long, Set<Long>> groupIdAndregionIdMap) {
|
||
Jedis resource = JedisUtils.getResource(0);
|
||
Transaction transaction = resource.multi();
|
||
try {
|
||
if (groupIdAndregionIdMap != null && groupIdAndregionIdMap.size() > 0) {
|
||
int idRelaRedisDBIndex = Configurations.getIntProperty("idRelaRedisDBIndex", 15);
|
||
transaction.select(idRelaRedisDBIndex);
|
||
// 获取需要删除的regionkey
|
||
Set<String> set = new HashSet<>();
|
||
Set<Long> groupIdSet = groupIdAndregionIdMap.keySet();
|
||
for (Long groupId : groupIdSet) {
|
||
Set<Long> regionSet = groupIdAndregionIdMap.get(groupId);
|
||
String commonGroup = "COMMONGROUPREGION:" + groupId;
|
||
String regionKey = JedisUtils.get(commonGroup, idRelaRedisDBIndex);
|
||
if (regionKey == null || regionKey.trim().equals("")) {
|
||
throw new ServiceRuntimeException("无法删除公共组下的域,公共组" + groupId + ",不存在,请检查数据是否正确",
|
||
RestBusinessCode.CommonGroupIsNotExist.getValue());
|
||
}
|
||
String[] keyAndDBArr = org.apache.commons.lang.StringUtils.split(regionKey, ";");
|
||
for (String keyAndDB : keyAndDBArr) {
|
||
String key = keyAndDB.substring(0, keyAndDB.indexOf("-"));
|
||
String regionId = key.substring(keyAndDB.indexOf(",") + 1);
|
||
if (regionId != null && !regionId.trim().equals("")) {
|
||
if (regionSet.contains(Long.parseLong(regionId))) {
|
||
set.add(key);
|
||
}
|
||
} else {
|
||
throw new ServiceRuntimeException("获取公共组下面域配置的id,域配置的key为" + key + ",请检查数据是否正确",
|
||
RestBusinessCode.CommonGroupIsNotExist.getValue());
|
||
}
|
||
}
|
||
}
|
||
|
||
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(37);// maat类配置的表达式都一样,这里因为没有service所以就随便取了一个
|
||
List<MaatXmlExpr> expressionList = maatXmlConfig.getExpressionList();
|
||
for (String db : Constants.COMMONGROUPDBARR) {
|
||
int everyDb = Integer.parseInt(db);
|
||
|
||
Double maatVersion = null;
|
||
String maatVersionStr = JedisUtils.get("MAAT_VERSION", everyDb);
|
||
if (maatVersionStr == null) {
|
||
maatVersionStr = "0";
|
||
}
|
||
maatVersion = Double.valueOf(maatVersionStr) + 1D;
|
||
transaction.select(everyDb);
|
||
for (String regionKey : set) {
|
||
String obsKey = regionKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
|
||
transaction.rename(regionKey, obsKey);
|
||
updateMaatInfo(expressionList, obsKey, transaction, maatVersion, everyDb, true);
|
||
}
|
||
}
|
||
for (String db : Constants.COMMONGROUPDBARR) {
|
||
transaction.select(Integer.parseInt(db));
|
||
transaction.incrBy("MAAT_VERSION", 1l);
|
||
logger.info("向{}号redis数据库更新了MAAT_VERSION", Integer.parseInt(db));
|
||
}
|
||
transaction.exec();
|
||
return true;
|
||
} else {
|
||
throw new ServiceRuntimeException("删除公共组中域配置时,参数为空,请检查",
|
||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||
|
||
}
|
||
|
||
} catch (JedisConnectionException e) {
|
||
String error = "连接redis异常,删除公共组maat类域配置失败" + e.getMessage();
|
||
logger.error(error + " " + ExceptionUtil.getExceptionMsg(e));
|
||
throw new ServiceRuntimeException(error, RestBusinessCode.CannotConnectionRedis.getValue());
|
||
} catch (Exception e) {
|
||
String error = "删除公共组maat类域配置发生了异常" + e.getMessage();
|
||
logger.error(error + " " + ExceptionUtil.getExceptionMsg(e));
|
||
transaction.discard();
|
||
throw new ServiceRuntimeException(error, RestBusinessCode.SaveDataInError.getValue());
|
||
} finally {
|
||
// 释放连接到连接池
|
||
JedisUtils.returnResource(resource);
|
||
}
|
||
}
|
||
|
||
private void updateMaatInfo(List<MaatXmlExpr> list, String maatKey, Transaction transaction, Double maatVersion,
|
||
int redisDBIndex, Boolean idDel) {
|
||
if (list != null && list.size() > 0) {
|
||
for (MaatXmlExpr maatXmlExpr : list) {
|
||
if (maatXmlExpr.getKeyExpression().toUpperCase().equals("MAAT_UPDATE_STATUS")) {
|
||
if (maatKey != null) {
|
||
String zset = null;
|
||
if (idDel == null || idDel) {
|
||
zset = maatKey.replace("OBSOLETE_RULE:", "DEL,");
|
||
} else {
|
||
zset = maatKey.replace("EFFECTIVE_RULE:", "ADD,");
|
||
}
|
||
transaction.zadd("MAAT_UPDATE_STATUS", maatVersion, zset);
|
||
logger.info("向{}号redis数据库更新了MAAT_UPDATE_STATUS,内容是{},SCORES是{}", redisDBIndex,
|
||
zset.toUpperCase(), maatVersion);
|
||
}
|
||
} else if (maatXmlExpr.getKeyExpression().toUpperCase().equals("MAAT_RULE_TIMER")) {
|
||
if (maatKey != null) {
|
||
Double score = 0d;// 界面下发的配置没有超时时间所以这里设置为0
|
||
transaction.zadd("MAAT_RULE_TIMER", score, maatKey);
|
||
logger.info("向{}号redis数据库更新了MAAT_RULE_TIMER,内容是{},SCORES是{}", redisDBIndex, maatKey, score);
|
||
|
||
}
|
||
} else if (maatXmlExpr.getKeyExpression().toUpperCase().equals("MAAT_VERSION_TIMER")) {
|
||
Long nowTime = new Date().getTime();
|
||
nowTime = nowTime / 1000l;
|
||
Double score = nowTime.doubleValue();// 使用redis自带的time,得到当前时间的秒
|
||
transaction.zadd("MAAT_VERSION_TIMER", score, maatVersion + "");
|
||
logger.info("向{}号redis数据库更新了MAAT_VERSION_TIMER,内容是{},SCORES是{}", redisDBIndex, maatVersion, score);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public Map<Integer, Map<String, String>> getAllKVByCompileId(Long id) {
|
||
Jedis resource = JedisUtils.getResource(1);
|
||
try {
|
||
Map<Integer, Map<String, String>> map = new HashMap<>();
|
||
String pattern = "*," + id;
|
||
|
||
for (int index = 2; index < 6; index++) {
|
||
resource.select(index);
|
||
List<String> keyByScan = getKeyByScan(pattern, resource);
|
||
for (String redisKey : keyByScan) {
|
||
if (resource.exists(redisKey)) {
|
||
|
||
String statusMebmer = null;
|
||
if (redisKey.startsWith("EFFECTIVE_RULE:")) {
|
||
statusMebmer = redisKey.replace("EFFECTIVE_RULE:", "ADD,");
|
||
} else if (redisKey.startsWith("OBSOLETE_RULE:")) {
|
||
statusMebmer = redisKey.replace("OBSOLETE_RULE:", "DEL,");
|
||
}
|
||
if (statusMebmer != null) {
|
||
Double zscore = resource.zscore("MAAT_UPDATE_STATUS", statusMebmer);
|
||
if (zscore != null) {
|
||
Set<String> zrangeByScore = resource.zrangeByScore("MAAT_UPDATE_STATUS", zscore,
|
||
zscore);
|
||
for (String statusKey : zrangeByScore) {
|
||
String replaceKey = null;
|
||
if (statusKey.startsWith("ADD,")) {
|
||
replaceKey = statusKey.replace("ADD,", "EFFECTIVE_RULE:");
|
||
|
||
} else if (statusKey.startsWith("DEL,")) {
|
||
replaceKey = statusKey.replace("DEL,", "OBSOLETE_RULE:");
|
||
}
|
||
if (replaceKey != null) {
|
||
String val = resource.get(replaceKey);
|
||
if (val != null && !val.trim().equals("")) {
|
||
if (map.containsKey(index)) {
|
||
Map<String, String> map2 = map.get(index);
|
||
map2.put(replaceKey, val);
|
||
} else {
|
||
Map<String, String> map2 = new HashMap<>();
|
||
map2.put(replaceKey, val);
|
||
map.put(index, map2);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
return map;
|
||
} finally {
|
||
resource.close();
|
||
}
|
||
}
|
||
|
||
public List<String> getKeyByScan(String pattern, Jedis resource) {
|
||
List<String> list = new ArrayList<>();
|
||
int count = 1000;
|
||
String cursor = ScanParams.SCAN_POINTER_START;
|
||
ScanParams scanParams = new ScanParams();
|
||
scanParams.count(count);
|
||
scanParams.match(pattern);
|
||
do {
|
||
ScanResult<String> scanResult = resource.scan(cursor, scanParams);
|
||
list.addAll(scanResult.getResult());
|
||
cursor = scanResult.getStringCursor();
|
||
} while ("!0".equals(cursor));
|
||
return list;
|
||
|
||
}
|
||
|
||
}
|