2018-05-19 18:35:24 +08:00
|
|
|
package com.nis.web.service.restful;
|
|
|
|
|
|
2018-05-31 17:07:16 +08:00
|
|
|
import java.util.ArrayList;
|
2018-05-23 19:54:35 +08:00
|
|
|
import java.util.Date;
|
2018-06-07 18:37:30 +08:00
|
|
|
import java.util.HashMap;
|
2018-05-31 10:09:29 +08:00
|
|
|
import java.util.HashSet;
|
2018-05-19 18:35:24 +08:00
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
2018-05-29 15:36:42 +08:00
|
|
|
import java.util.Set;
|
2018-05-19 18:35:24 +08:00
|
|
|
|
2018-05-29 15:36:42 +08:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-05-31 10:09:29 +08:00
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
2018-05-19 18:35:24 +08:00
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
import com.nis.domain.MaatXmlConfig;
|
|
|
|
|
import com.nis.domain.MaatXmlExpr;
|
|
|
|
|
import com.nis.domain.MaatXmlSeq;
|
2018-06-01 10:27:03 +08:00
|
|
|
import com.nis.domain.restful.CompileAndGroupRelations;
|
|
|
|
|
import com.nis.domain.restful.GroupAndRegionRelations;
|
2018-05-23 19:54:35 +08:00
|
|
|
import com.nis.domain.restful.MaatConfig;
|
2018-05-29 15:36:42 +08:00
|
|
|
import com.nis.domain.restful.MaatRelation;
|
|
|
|
|
import com.nis.listener.CompileGroupRegionRela;
|
2018-06-01 14:20:08 +08:00
|
|
|
import com.nis.util.Configurations;
|
2018-05-19 18:35:24 +08:00
|
|
|
import com.nis.util.ReadMaatXmlUtil;
|
2018-05-31 10:09:29 +08:00
|
|
|
import com.nis.util.ServiceAndRDBIndexReal;
|
|
|
|
|
import com.nis.web.service.SpringContextHolder;
|
2018-05-19 18:35:24 +08:00
|
|
|
|
2018-05-23 19:54:35 +08:00
|
|
|
@Service()
|
2018-05-31 10:09:29 +08:00
|
|
|
public class ConfigRedisServiceimpl implements ConfigRedisService {
|
2018-05-29 15:36:42 +08:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(ConfigRedisServiceimpl.class);
|
|
|
|
|
|
2018-05-19 18:35:24 +08:00
|
|
|
@Transactional
|
2018-06-01 18:04:48 +08:00
|
|
|
public boolean saveUnMaatConfig(Map<Integer, List<Map<String, String>>> configMap) {
|
2018-05-31 10:09:29 +08:00
|
|
|
if (configMap != null && configMap.size() > 0) {
|
2018-06-02 12:49:20 +08:00
|
|
|
int count = 0;
|
2018-05-31 10:09:29 +08:00
|
|
|
for (Integer redisDBIndex : configMap.keySet()) {
|
2018-06-01 14:20:08 +08:00
|
|
|
if (redisDBIndex >= 0 && redisDBIndex < Configurations.getIntProperty("maxRedisDBIndex", 6)) {
|
2018-05-31 10:09:29 +08:00
|
|
|
RedisTemplate<String, String> redisTemplate = SpringContextHolder
|
|
|
|
|
.getBean("redisTemplate" + redisDBIndex);
|
|
|
|
|
List<Map<String, String>> listMap = configMap.get(redisDBIndex);
|
2018-06-04 10:18:31 +08:00
|
|
|
if (listMap != null && listMap.size() > 0) {
|
|
|
|
|
String maatVersionStr = redisTemplate.opsForValue().get("MAAT_VERSION");
|
|
|
|
|
if (maatVersionStr == null) {
|
|
|
|
|
maatVersionStr = "0";
|
|
|
|
|
}
|
|
|
|
|
if (maatVersionStr != null) {
|
|
|
|
|
Long maatVersion = Long.valueOf(maatVersionStr) + 1;
|
|
|
|
|
for (Map<String, String> map : listMap) {
|
|
|
|
|
String serviceStr = map.get("service");
|
|
|
|
|
int service = Integer.parseInt(serviceStr);
|
|
|
|
|
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("]", "");
|
|
|
|
|
keyBF.append(map.get(keyStr));
|
2018-06-04 17:52:03 +08:00
|
|
|
} 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 RuntimeException("未从业务类型和表对应关系中,找到非maat配置业务类型:"
|
|
|
|
|
+ service + "对应的真实表名");
|
|
|
|
|
} else {
|
|
|
|
|
keyBF.append(maatTableName);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
keyBF.append(keyStr.trim());
|
|
|
|
|
}
|
2018-05-31 10:09:29 +08:00
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
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("]", "");
|
|
|
|
|
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");
|
|
|
|
|
}
|
2018-05-31 10:09:29 +08:00
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
maatKey = keyBF.toString();
|
|
|
|
|
redisTemplate.opsForValue().set(keyBF.toString().toUpperCase(),
|
|
|
|
|
valBF.toString());
|
|
|
|
|
logger.info("向{}号redis数据库添加了一条配置,key是{},value是{}", redisDBIndex,
|
|
|
|
|
keyBF.toString().toUpperCase(), valBF.toString());
|
|
|
|
|
break;
|
2018-05-31 10:09:29 +08:00
|
|
|
}
|
2018-05-23 19:54:35 +08:00
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
for (MaatXmlExpr maatXmlExpr : expressionList) {
|
|
|
|
|
if (maatXmlExpr.getKeyExpression().toUpperCase().equals("MAAT_UPDATE_STATUS")) {
|
|
|
|
|
if (maatKey != null) {
|
|
|
|
|
String zset = maatKey.replace("EFFECTIVE_RULE:", "ADD,");
|
|
|
|
|
redisTemplate.boundZSetOps("MAAT_UPDATE_STATUS").add(zset, maatVersion);
|
|
|
|
|
logger.info("向{}号redis数据库更新了MAAT_UPDATE_STATUS,内容是{},SCORES是{}",
|
|
|
|
|
redisDBIndex, zset.toUpperCase(), maatVersion);
|
2018-06-01 14:20:08 +08:00
|
|
|
|
2018-06-04 10:18:31 +08:00
|
|
|
}
|
|
|
|
|
} else if (maatXmlExpr.getKeyExpression().toUpperCase()
|
|
|
|
|
.equals("MAAT_RULE_TIMER")) {
|
|
|
|
|
if (maatKey != null) {
|
|
|
|
|
Double score = 0d;// 界面下发的配置没有超时时间所以这里设置为0
|
|
|
|
|
redisTemplate.boundZSetOps("MAAT_RULE_TIMER").add(maatKey, score);
|
2018-06-01 14:20:08 +08:00
|
|
|
|
2018-06-04 10:18:31 +08:00
|
|
|
logger.info("向{}号redis数据库更新了MAAT_RULE_TIMER,内容是{},SCORES是{}",
|
|
|
|
|
redisDBIndex, maatKey, score);
|
2018-06-01 14:20:08 +08:00
|
|
|
|
2018-06-04 10:18:31 +08:00
|
|
|
}
|
|
|
|
|
} else if (maatXmlExpr.getKeyExpression().toUpperCase()
|
|
|
|
|
.equals("MAAT_VERSION_TIMER")) {
|
|
|
|
|
Long nowTime = new Date().getTime();
|
|
|
|
|
nowTime = nowTime / 1000l;
|
|
|
|
|
Double score = nowTime.doubleValue();// 使用redis自带的time,得到当前时间的秒
|
|
|
|
|
redisTemplate.boundZSetOps("MAAT_VERSION_TIMER").add(maatVersion + "",
|
|
|
|
|
score);
|
|
|
|
|
|
|
|
|
|
logger.info("向{}号redis数据库更新了MAAT_VERSION_TIMER,内容是{},SCORES是{}",
|
|
|
|
|
redisDBIndex, maatVersion, score);
|
2018-06-01 14:20:08 +08:00
|
|
|
}
|
2018-05-23 19:54:35 +08:00
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
List<MaatXmlSeq> seqList = maatXmlConfig.getSequenceList();
|
|
|
|
|
for (MaatXmlSeq maatXmlSeq : seqList) {
|
|
|
|
|
// setRedisDataBase(maatXmlSeq.getRedisDB(),
|
|
|
|
|
// redisTemplate);
|
|
|
|
|
String seqKey = maatXmlSeq.getSequenceKey();
|
|
|
|
|
if (!seqKey.toUpperCase().equals("MAAT_VERSION")) {
|
|
|
|
|
Integer operation = maatXmlSeq.getOperation();
|
|
|
|
|
if (operation == 1) {
|
|
|
|
|
redisTemplate.boundValueOps(seqKey.toUpperCase()).increment(1l);
|
|
|
|
|
}
|
2018-05-31 10:09:29 +08:00
|
|
|
}
|
2018-05-23 19:54:35 +08:00
|
|
|
|
2018-06-04 10:18:31 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("无法从maat.xml中获取业务类型" + service + "对应的规则,请检查业务类型是否正确");
|
2018-05-23 19:54:35 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
redisTemplate.boundValueOps("MAAT_VERSION").increment(1l);
|
|
|
|
|
logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDBIndex,
|
|
|
|
|
Integer.valueOf(maatVersionStr) + 1);
|
|
|
|
|
count++;
|
2018-05-23 19:54:35 +08:00
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("向" + redisDBIndex + "号redis库中添加配置时,未发现对应的配置信息,请检查配置参数是否正确");
|
2018-05-19 18:35:24 +08:00
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("redis数据库编号:" + redisDBIndex + "不正确,请检查数据库编号");
|
2018-05-19 18:35:24 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-06-02 12:49:20 +08:00
|
|
|
if (count == configMap.size()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("参数不能为空");
|
2018-05-19 18:35:24 +08:00
|
|
|
}
|
2018-06-01 18:04:48 +08:00
|
|
|
return false;
|
2018-05-19 18:35:24 +08:00
|
|
|
}
|
2018-05-21 10:21:26 +08:00
|
|
|
|
2018-06-01 14:20:08 +08:00
|
|
|
/**
|
|
|
|
|
* 下发配置成功后,需要更新编译,组,域等配置id的对应关系
|
|
|
|
|
*/
|
2018-06-07 18:37:30 +08:00
|
|
|
private void addMaatRelation(Map<Integer, List<MaatConfig>> configMap) {
|
|
|
|
|
if (configMap != null && configMap.size() > 0) {
|
|
|
|
|
for (Integer redisDBIndex : configMap.keySet()) {
|
|
|
|
|
if (redisDBIndex >= 0 && redisDBIndex < Configurations.getIntProperty("maxRedisDBIndex", 6)) {
|
|
|
|
|
int idRelaRedisDBIndex = Configurations.getIntProperty("idRelaRedisDBIndex", 15);
|
|
|
|
|
RedisTemplate<String, String> redisTemplate = SpringContextHolder
|
|
|
|
|
.getBean("redisTemplate" + idRelaRedisDBIndex);
|
|
|
|
|
List<MaatConfig> maatConfigList = configMap.get(redisDBIndex);
|
|
|
|
|
if (maatConfigList != null && maatConfigList.size() > 0) {
|
|
|
|
|
Map<String, List<String>> compileAndGroupMap = new HashMap<String, List<String>>();
|
|
|
|
|
Map<String, List<String>> groupAndCompileMap = new HashMap<String, List<String>>();
|
|
|
|
|
for (MaatConfig maatConfig : maatConfigList) {
|
|
|
|
|
String compileId = maatConfig.getCompileMap().get("compile_id");
|
|
|
|
|
List<Map<String, String>> groupMapList = maatConfig.getGroupMapList();
|
|
|
|
|
if (groupMapList != null && groupMapList.size() > 0) {
|
|
|
|
|
for (Map<String, String> map : groupMapList) {
|
|
|
|
|
String cfgIdStr = redisDBIndex + ":COMPILEGROUP:" + map.get("compile_id");
|
|
|
|
|
String groupIdStr = redisDBIndex + ":GROUPCOMPILE:" + map.get("group_id");
|
|
|
|
|
// + map.get("compile_id");
|
|
|
|
|
if (cfgIdStr != null && groupIdStr != null && !cfgIdStr.equals("")
|
|
|
|
|
&& !groupIdStr.equals("")) {
|
|
|
|
|
if (compileAndGroupMap.containsKey(cfgIdStr.toUpperCase())) {
|
|
|
|
|
compileAndGroupMap.get(cfgIdStr.toUpperCase())
|
|
|
|
|
.add(groupIdStr.toUpperCase());
|
|
|
|
|
} else {
|
|
|
|
|
List<String> list = new ArrayList<String>();
|
|
|
|
|
list.add(groupIdStr.toUpperCase());
|
|
|
|
|
compileAndGroupMap.put(cfgIdStr.toUpperCase(), list);
|
|
|
|
|
}
|
|
|
|
|
if (groupAndCompileMap.containsKey(groupIdStr.toUpperCase())) {
|
|
|
|
|
groupAndCompileMap.get(groupIdStr.toUpperCase())
|
|
|
|
|
.add(cfgIdStr.toUpperCase());
|
|
|
|
|
} else {
|
|
|
|
|
List<String> list = new ArrayList<String>();
|
|
|
|
|
list.add(cfgIdStr.toUpperCase());
|
|
|
|
|
groupAndCompileMap.put(groupIdStr.toUpperCase(), list);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<String, List<String>> map = new HashMap<String, List<String>>();
|
|
|
|
|
int service = maatConfig.getService();
|
|
|
|
|
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service);
|
|
|
|
|
addGroupAndRegionRelations(maatXmlConfig, service, 12, maatConfig.getIpRegionMapList(),
|
|
|
|
|
redisTemplate, map, redisDBIndex, compileId);
|
|
|
|
|
addGroupAndRegionRelations(maatXmlConfig, service, 13, maatConfig.getNumRegionMapList(),
|
|
|
|
|
redisTemplate, map, redisDBIndex, compileId);
|
|
|
|
|
addGroupAndRegionRelations(maatXmlConfig, service, 14, maatConfig.getStrRegionMapList(),
|
|
|
|
|
redisTemplate, map, redisDBIndex, compileId);
|
|
|
|
|
addGroupAndRegionRelations(maatXmlConfig, service, 15, maatConfig.getStrStrRegionMapList(),
|
|
|
|
|
redisTemplate, map, redisDBIndex, compileId);
|
|
|
|
|
addGroupAndRegionRelations(maatXmlConfig, service, 16,
|
|
|
|
|
maatConfig.getFileDigestRegionMapList(), redisTemplate, map, redisDBIndex,
|
|
|
|
|
compileId);
|
|
|
|
|
addGroupAndRegionRelations(maatXmlConfig, service, 17,
|
|
|
|
|
maatConfig.getFileLikeRegionMapList(), redisTemplate, map, redisDBIndex, compileId);
|
|
|
|
|
addGroupAndRegionRelations(maatXmlConfig, service, 18, maatConfig.getIpClientRangeMapList(),
|
|
|
|
|
redisTemplate, map, redisDBIndex, compileId);
|
|
|
|
|
|
|
|
|
|
for (String groupIdStr : map.keySet()) {
|
|
|
|
|
List<String> list = map.get(groupIdStr);
|
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
|
if (list != null && list.size() > 0) {
|
|
|
|
|
for (String regionIdStr : list) {
|
|
|
|
|
sb.append(regionIdStr);
|
|
|
|
|
sb.append(";");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
redisTemplate.opsForValue().set(groupIdStr, sb.substring(0, sb.length() - 1));// 保存分组id和域id的关系(每个域配置,只会属于一个组)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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(";");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
redisTemplate.opsForValue().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(";");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
redisTemplate.opsForValue().append(group, sb.substring(0, sb.length() - 1));// 保存分组id和域id的关系(每个域配置,只会属于一个组)
|
|
|
|
|
// redisTemplate.opsForValue().set(group, sb.substring(0, sb.length() - 1));//
|
|
|
|
|
// 保存分组id和域id的关系(每个域配置,只会属于一个组)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("redis数据库编号:" + redisDBIndex + "不正确,请检查数据库编号");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("参数不能为空");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 封装组id与域id对应关系并添加到对象中
|
|
|
|
|
* @param regionMapList
|
|
|
|
|
* @param compileAndGroupRelations
|
|
|
|
|
*/
|
|
|
|
|
private Map<String, List<String>> addGroupAndRegionRelations(MaatXmlConfig maatXmlConfig, int service, int type,
|
|
|
|
|
List<Map<String, String>> regionMapList, RedisTemplate<String, String> redisTemplate,
|
|
|
|
|
Map<String, List<String>> groupAndRegionMap, int redisDBIndex, String compileId) {
|
|
|
|
|
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("tableName");
|
|
|
|
|
String maatTableName = ServiceAndRDBIndexReal.getMaatTableName(service, type,
|
|
|
|
|
argTableName == null ? null : argTableName);
|
|
|
|
|
if (maatTableName == null) {
|
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
"未从业务类型和表对应关系中,找到业务类型:" + service + ",配置类型:" + type + ",对应的真实表名");
|
|
|
|
|
} else {
|
|
|
|
|
keyBF.append(maatTableName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
keyBF.append(keyStr.trim());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
maatKey = keyBF.toString();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// String groupIdStr = redisDBIndex + ":GROUPREGION:" + map.get("group_id") +
|
|
|
|
|
// compileId;
|
|
|
|
|
String groupIdStr = redisDBIndex + ":GROUPREGION:" + map.get("group_id");
|
|
|
|
|
if (groupAndRegionMap.containsKey(groupIdStr.toUpperCase())) {
|
|
|
|
|
groupAndRegionMap.get(groupIdStr.toUpperCase()).add(redisDBIndex + ":" + maatKey.toUpperCase());
|
|
|
|
|
} else {
|
|
|
|
|
List<String> list = new ArrayList<String>();
|
|
|
|
|
list.add(redisDBIndex + ":" + maatKey.toUpperCase());
|
|
|
|
|
groupAndRegionMap.put(groupIdStr.toUpperCase(), list);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return groupAndRegionMap;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 下发配置成功后,需要更新编译,组,域等配置id的对应关系
|
|
|
|
|
*/
|
|
|
|
|
public void addMaatRelationOld(Map<Integer, List<MaatConfig>> configMap) {
|
2018-06-01 10:27:03 +08:00
|
|
|
if (configMap != null && configMap.size() > 0) {
|
|
|
|
|
for (Integer redisDBIndex : configMap.keySet()) {
|
2018-06-01 14:20:08 +08:00
|
|
|
if (redisDBIndex >= 0 && redisDBIndex < Configurations.getIntProperty("maxRedisDBIndex", 6)) {
|
2018-06-01 10:27:03 +08:00
|
|
|
List<MaatConfig> maatConfigList = configMap.get(redisDBIndex);
|
|
|
|
|
if (maatConfigList != null && maatConfigList.size() > 0) {
|
|
|
|
|
for (MaatConfig maatConfig : maatConfigList) {
|
|
|
|
|
Map<String, String> compileMap = maatConfig.getCompileMap();
|
2018-06-01 14:20:08 +08:00
|
|
|
String cfgIdStr = compileMap.get("compile_id");
|
2018-06-01 10:27:03 +08:00
|
|
|
CompileAndGroupRelations compileAndGroupRelations = new CompileAndGroupRelations();
|
|
|
|
|
compileAndGroupRelations.setCompileId(Long.valueOf(cfgIdStr));
|
|
|
|
|
compileAndGroupRelations.setGroupIdList(new ArrayList<GroupAndRegionRelations>());
|
|
|
|
|
|
2018-06-04 17:52:03 +08:00
|
|
|
int service = maatConfig.getService();
|
|
|
|
|
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service);
|
2018-06-07 18:37:30 +08:00
|
|
|
addGroupAndRegionRelationsOld(maatXmlConfig, service, 12, maatConfig.getIpRegionMapList(),
|
2018-06-04 17:52:03 +08:00
|
|
|
compileAndGroupRelations);
|
2018-06-07 18:37:30 +08:00
|
|
|
addGroupAndRegionRelationsOld(maatXmlConfig, service, 13, maatConfig.getNumRegionMapList(),
|
2018-06-04 17:52:03 +08:00
|
|
|
compileAndGroupRelations);
|
2018-06-07 18:37:30 +08:00
|
|
|
addGroupAndRegionRelationsOld(maatXmlConfig, service, 14, maatConfig.getStrRegionMapList(),
|
2018-06-04 17:52:03 +08:00
|
|
|
compileAndGroupRelations);
|
2018-06-07 18:37:30 +08:00
|
|
|
addGroupAndRegionRelationsOld(maatXmlConfig, service, 15,
|
|
|
|
|
maatConfig.getStrStrRegionMapList(), compileAndGroupRelations);
|
|
|
|
|
addGroupAndRegionRelationsOld(maatXmlConfig, service, 16,
|
2018-06-04 17:52:03 +08:00
|
|
|
maatConfig.getFileDigestRegionMapList(), compileAndGroupRelations);
|
2018-06-07 18:37:30 +08:00
|
|
|
addGroupAndRegionRelationsOld(maatXmlConfig, service, 17,
|
2018-06-04 17:52:03 +08:00
|
|
|
maatConfig.getFileLikeRegionMapList(), compileAndGroupRelations);
|
2018-06-07 18:37:30 +08:00
|
|
|
addGroupAndRegionRelationsOld(maatXmlConfig, service, 18,
|
|
|
|
|
maatConfig.getIpClientRangeMapList(), compileAndGroupRelations);
|
2018-06-01 10:27:03 +08:00
|
|
|
|
|
|
|
|
CompileGroupRegionRela.addIdRelation(redisDBIndex, compileAndGroupRelations);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("redis数据库编号:" + redisDBIndex + "不正确,请检查数据库编号");
|
2018-06-01 10:27:03 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("参数不能为空");
|
2018-06-01 10:27:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-01 14:20:08 +08:00
|
|
|
/**
|
|
|
|
|
* 封装组id与域id对应关系并添加到对象中
|
|
|
|
|
* @param regionMapList
|
|
|
|
|
* @param compileAndGroupRelations
|
|
|
|
|
*/
|
2018-06-07 18:37:30 +08:00
|
|
|
private void addGroupAndRegionRelationsOld(MaatXmlConfig maatXmlConfig, int service, int type,
|
2018-06-04 17:52:03 +08:00
|
|
|
List<Map<String, String>> regionMapList, CompileAndGroupRelations compileAndGroupRelations) {
|
2018-06-01 10:27:03 +08:00
|
|
|
if (regionMapList != null && regionMapList.size() > 0 && compileAndGroupRelations != null) {
|
|
|
|
|
for (Map<String, String> map : regionMapList) {
|
2018-06-04 17:52:03 +08:00
|
|
|
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("tableName");
|
|
|
|
|
String maatTableName = ServiceAndRDBIndexReal.getMaatTableName(service, type,
|
|
|
|
|
argTableName == null ? null : argTableName);
|
|
|
|
|
if (maatTableName == null) {
|
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
"未从业务类型和表对应关系中,找到业务类型:" + service + ",配置类型:" + type + ",对应的真实表名");
|
|
|
|
|
} else {
|
|
|
|
|
keyBF.append(maatTableName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
keyBF.append(keyStr.trim());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
maatKey = keyBF.toString();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-01 10:27:03 +08:00
|
|
|
String groupIdStr = map.get("group_id");
|
|
|
|
|
GroupAndRegionRelations groupAndRegionRelations = new GroupAndRegionRelations();
|
|
|
|
|
groupAndRegionRelations.setGroupId(Long.valueOf(groupIdStr));
|
2018-06-04 17:52:03 +08:00
|
|
|
groupAndRegionRelations.setRegionId(maatKey);
|
2018-06-01 10:27:03 +08:00
|
|
|
compileAndGroupRelations.getGroupIdList().add(groupAndRegionRelations);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-31 10:09:29 +08:00
|
|
|
@Override
|
2018-05-31 10:32:29 +08:00
|
|
|
@Transactional
|
2018-06-01 18:04:48 +08:00
|
|
|
public boolean saveMaatConfig(Map<Integer, List<MaatConfig>> configMap) {
|
2018-05-31 10:09:29 +08:00
|
|
|
if (configMap != null && configMap.size() > 0) {
|
2018-06-02 12:49:20 +08:00
|
|
|
int count = 0;
|
2018-05-31 10:09:29 +08:00
|
|
|
for (Integer redisDBIndex : configMap.keySet()) {
|
2018-06-01 14:20:08 +08:00
|
|
|
if (redisDBIndex >= 0 && redisDBIndex < Configurations.getIntProperty("maxRedisDBIndex", 6)) {
|
2018-05-31 10:09:29 +08:00
|
|
|
RedisTemplate<String, String> redisTemplate = SpringContextHolder
|
|
|
|
|
.getBean("redisTemplate" + redisDBIndex);
|
|
|
|
|
String maatVersionStr = redisTemplate.opsForValue().get("MAAT_VERSION");
|
|
|
|
|
if (maatVersionStr == null) {
|
|
|
|
|
maatVersionStr = "0";
|
|
|
|
|
}
|
|
|
|
|
if (maatVersionStr != null) {
|
|
|
|
|
Long maatVersion = Long.valueOf(maatVersionStr) + 1;
|
|
|
|
|
List<MaatConfig> maatConfigList = configMap.get(redisDBIndex);
|
|
|
|
|
if (maatConfigList != null && maatConfigList.size() > 0) {
|
|
|
|
|
for (MaatConfig maatConfig : maatConfigList) {
|
|
|
|
|
int service = maatConfig.getService();
|
|
|
|
|
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service);
|
2018-06-01 14:20:08 +08:00
|
|
|
setConfig(maatConfig, maatXmlConfig, maatVersion, service, redisTemplate, redisDBIndex);
|
2018-05-31 10:09:29 +08:00
|
|
|
}
|
2018-06-01 18:04:48 +08:00
|
|
|
redisTemplate.boundValueOps("MAAT_VERSION").increment(1l);
|
|
|
|
|
logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDBIndex,
|
|
|
|
|
Integer.valueOf(maatVersionStr) + 1);
|
2018-06-02 12:49:20 +08:00
|
|
|
count++;
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("向" + redisDBIndex + "号redis库中添加配置时,未发现对应的配置信息,请检查配置参数是否正确");
|
2018-05-31 10:09:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2018-06-01 18:04:48 +08:00
|
|
|
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("redis数据库编号:" + redisDBIndex + "不正确,请检查数据库编号");
|
2018-05-23 19:54:35 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-06-02 12:49:20 +08:00
|
|
|
if (count == configMap.size()) {
|
2018-06-07 18:37:30 +08:00
|
|
|
addMaatRelation(configMap);
|
2018-06-02 12:49:20 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("参数不能为空");
|
2018-05-23 19:54:35 +08:00
|
|
|
}
|
2018-06-01 18:04:48 +08:00
|
|
|
return false;
|
|
|
|
|
|
2018-05-23 19:54:35 +08:00
|
|
|
}
|
|
|
|
|
|
2018-06-01 14:20:08 +08:00
|
|
|
/**
|
|
|
|
|
* 将编译,分组,域等配置拆开分别添加到redis中
|
|
|
|
|
* @param maatConfig
|
|
|
|
|
* @param maatXmlConfig
|
|
|
|
|
* @param maatVersion
|
|
|
|
|
* @param service
|
|
|
|
|
* @param redisTemplate
|
|
|
|
|
* @param redisDBIndex
|
|
|
|
|
*/
|
2018-05-31 10:09:29 +08:00
|
|
|
public void setConfig(MaatConfig maatConfig, MaatXmlConfig maatXmlConfig, Long maatVersion, int service,
|
2018-06-01 14:20:08 +08:00
|
|
|
RedisTemplate<String, String> redisTemplate, Integer redisDBIndex) {
|
2018-05-23 19:54:35 +08:00
|
|
|
Map<String, String> compileMap = maatConfig.getCompileMap();
|
2018-06-07 18:37:30 +08:00
|
|
|
String compileId = compileMap.get("compile_id");
|
2018-06-04 10:18:31 +08:00
|
|
|
if (compileMap != null && compileMap.size() > 0) {
|
|
|
|
|
setCommonConfig(maatXmlConfig, compileMap, 10, maatVersion.doubleValue(), service, redisTemplate,
|
2018-06-07 18:37:30 +08:00
|
|
|
redisDBIndex, null);// 10代表是编译配置
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("无法从参数中获取" + redisDBIndex + "号redis库,业务类型为:" + service + "的编译配置信息,请检查配置参数是否正确");
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-25 15:04:51 +08:00
|
|
|
List<Map<String, String>> groupMapList = maatConfig.getGroupMapList();
|
|
|
|
|
if (groupMapList != null && groupMapList.size() > 0) {
|
|
|
|
|
for (Map<String, String> map : groupMapList) {
|
2018-06-07 18:37:30 +08:00
|
|
|
setCommonConfig(maatXmlConfig, map, 11, maatVersion.doubleValue(), service, redisTemplate, redisDBIndex,
|
|
|
|
|
compileId);// 11代表是分组配置
|
2018-05-25 15:04:51 +08:00
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("无法从参数中获取" + redisDBIndex + "号redis库,业务类型为:" + service + "的分组配置信息,请检查配置参数是否正确");
|
2018-05-25 15:04:51 +08:00
|
|
|
}
|
2018-05-31 10:09:29 +08:00
|
|
|
|
2018-05-25 15:04:51 +08:00
|
|
|
List<Map<String, String>> ipRegionMapList = maatConfig.getIpRegionMapList();
|
|
|
|
|
if (ipRegionMapList != null && ipRegionMapList.size() > 0) {
|
|
|
|
|
for (Map<String, String> map : ipRegionMapList) {
|
2018-06-07 18:37:30 +08:00
|
|
|
setCommonConfig(maatXmlConfig, map, 12, maatVersion.doubleValue(), service, redisTemplate, redisDBIndex,
|
|
|
|
|
null);// 12代表是ip类域配置
|
2018-05-25 15:04:51 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
List<Map<String, String>> numRegionMapList = maatConfig.getNumRegionMapList();
|
|
|
|
|
if (numRegionMapList != null && numRegionMapList.size() > 0) {
|
|
|
|
|
for (Map<String, String> map : numRegionMapList) {
|
2018-06-07 18:37:30 +08:00
|
|
|
setCommonConfig(maatXmlConfig, map, 13, maatVersion.doubleValue(), service, redisTemplate, redisDBIndex,
|
|
|
|
|
null);// 13代表是数值类配置
|
2018-05-25 15:04:51 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
List<Map<String, String>> strRegionMapList = maatConfig.getStrRegionMapList();
|
|
|
|
|
if (strRegionMapList != null && strRegionMapList.size() > 0) {
|
|
|
|
|
for (Map<String, String> map : strRegionMapList) {
|
2018-06-07 18:37:30 +08:00
|
|
|
setCommonConfig(maatXmlConfig, map, 14, maatVersion.doubleValue(), service, redisTemplate, redisDBIndex,
|
|
|
|
|
null);// 14代表是字符串类域配置
|
2018-05-25 15:04:51 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
List<Map<String, String>> strStrRegionMapList = maatConfig.getStrStrRegionMapList();
|
|
|
|
|
if (strStrRegionMapList != null && strStrRegionMapList.size() > 0) {
|
|
|
|
|
for (Map<String, String> map : strStrRegionMapList) {
|
2018-06-07 18:37:30 +08:00
|
|
|
setCommonConfig(maatXmlConfig, map, 15, maatVersion.doubleValue(), service, redisTemplate, redisDBIndex,
|
|
|
|
|
null);// 15代表是增强字符串类域配置
|
2018-05-25 15:04:51 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
List<Map<String, String>> fileDigestRegionMapList = maatConfig.getFileDigestRegionMapList();
|
|
|
|
|
if (fileDigestRegionMapList != null && fileDigestRegionMapList.size() > 0) {
|
|
|
|
|
for (Map<String, String> map : fileDigestRegionMapList) {
|
2018-06-07 18:37:30 +08:00
|
|
|
setCommonConfig(maatXmlConfig, map, 16, maatVersion.doubleValue(), service, redisTemplate, redisDBIndex,
|
|
|
|
|
null);// 16代表是文件摘要类域配置
|
2018-05-25 15:04:51 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
List<Map<String, String>> fileLikeRegionMapList = maatConfig.getFileLikeRegionMapList();
|
|
|
|
|
if (fileLikeRegionMapList != null && fileLikeRegionMapList.size() > 0) {
|
|
|
|
|
for (Map<String, String> map : fileLikeRegionMapList) {
|
2018-06-07 18:37:30 +08:00
|
|
|
setCommonConfig(maatXmlConfig, map, 17, maatVersion.doubleValue(), service, redisTemplate, redisDBIndex,
|
|
|
|
|
null);// 17代表是文本相似性域配置
|
2018-05-25 15:04:51 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
|
|
|
|
|
List<Map<String, String>> ipclientList = maatConfig.getIpClientRangeMapList();
|
|
|
|
|
if (ipclientList != null && ipclientList.size() > 0) {
|
|
|
|
|
for (Map<String, String> map : ipclientList) {
|
2018-06-07 18:37:30 +08:00
|
|
|
setCommonConfig(maatXmlConfig, map, 18, maatVersion.doubleValue(), service, redisTemplate, redisDBIndex,
|
|
|
|
|
null);// 18代表是区域ip域配置
|
2018-06-04 10:18:31 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-29 15:36:42 +08:00
|
|
|
// updateCommonKey(maatXmlConfig);
|
2018-05-23 19:54:35 +08:00
|
|
|
}
|
|
|
|
|
|
2018-06-01 14:20:08 +08:00
|
|
|
/**
|
|
|
|
|
* 将整理好的数据添加到redis中
|
|
|
|
|
* @param maatXmlConfig
|
|
|
|
|
* @param map
|
|
|
|
|
* @param type
|
|
|
|
|
* @param maatVersion
|
|
|
|
|
* @param service
|
|
|
|
|
* @param redisTemplate
|
|
|
|
|
* @param redisDBIndex
|
|
|
|
|
*/
|
2018-05-23 19:54:35 +08:00
|
|
|
public void setCommonConfig(MaatXmlConfig maatXmlConfig, Map<String, String> map, int type, Double maatVersion,
|
2018-06-07 18:37:30 +08:00
|
|
|
int service, RedisTemplate<String, String> redisTemplate, Integer redisDBIndex, String compileId) {
|
2018-05-23 19:54:35 +08:00
|
|
|
if (maatXmlConfig != null && map != null && map.size() > 0) {
|
|
|
|
|
List<MaatXmlExpr> expressionList = maatXmlConfig.getExpressionList();
|
2018-06-01 14:20:08 +08:00
|
|
|
String maatKey = null;
|
2018-05-23 19:54:35 +08:00
|
|
|
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));
|
2018-06-07 18:37:30 +08:00
|
|
|
if (type == 11 && keyStr.toLowerCase().equals("group_id")) {
|
|
|
|
|
keyBF.append(compileId);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-23 19:54:35 +08:00
|
|
|
} else if (!StringUtils.isEmpty(keyStr) && keyStr.trim().startsWith("{")) {
|
|
|
|
|
keyStr = keyStr.trim().replace("{", "").replace("}", "");
|
|
|
|
|
if (keyStr.toLowerCase().contains("table_name")) {
|
2018-06-04 17:52:03 +08:00
|
|
|
String argTableName = map.get("tableName");
|
|
|
|
|
String maatTableName = ServiceAndRDBIndexReal.getMaatTableName(service, type,
|
|
|
|
|
argTableName == null ? null : argTableName);
|
|
|
|
|
if (maatTableName == null) {
|
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
"未从业务类型和表对应关系中,找到业务类型:" + service + ",配置类型:" + type + ",对应的真实表名");
|
|
|
|
|
} else {
|
|
|
|
|
keyBF.append(maatTableName);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-23 19:54:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} 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("]", "");
|
|
|
|
|
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");
|
2018-06-05 16:47:58 +08:00
|
|
|
} else {
|
|
|
|
|
valBF.append(valStr.trim());
|
2018-05-23 19:54:35 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
maatKey = keyBF.toString();
|
|
|
|
|
redisTemplate.opsForValue().set(maatKey.toUpperCase(), valBF.toString());
|
2018-06-01 14:20:08 +08:00
|
|
|
logger.info("向{}号redis数据库添加了一条配置,key是{},value是{}", redisDBIndex, maatKey.toUpperCase(),
|
|
|
|
|
valBF.toString());
|
|
|
|
|
|
2018-05-23 19:54:35 +08:00
|
|
|
// redisTemplate.boundValueOps(keyBF.toString()).set(valBF.toString());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
for (MaatXmlExpr maatXmlExpr : expressionList) {
|
|
|
|
|
if (maatXmlExpr.getKeyExpression().toUpperCase().equals("MAAT_UPDATE_STATUS")) {
|
2018-06-01 14:20:08 +08:00
|
|
|
if (maatKey != null) {
|
|
|
|
|
String zset = maatKey.replace("EFFECTIVE_RULE:", "ADD,");
|
|
|
|
|
redisTemplate.boundZSetOps("MAAT_UPDATE_STATUS").add(zset, maatVersion);
|
|
|
|
|
logger.info("向{}号redis数据库更新了MAAT_UPDATE_STATUS,内容是{},SCORES是{}", redisDBIndex,
|
|
|
|
|
zset.toUpperCase(), maatVersion);
|
|
|
|
|
}
|
2018-05-23 19:54:35 +08:00
|
|
|
} else if (maatXmlExpr.getKeyExpression().toUpperCase().equals("MAAT_RULE_TIMER")) {
|
2018-06-01 14:20:08 +08:00
|
|
|
if (maatKey != null) {
|
|
|
|
|
Double score = 0d;// 界面下发的配置没有超时时间所以这里设置为0
|
|
|
|
|
redisTemplate.boundZSetOps("MAAT_RULE_TIMER").add(maatKey, score);
|
|
|
|
|
logger.info("向{}号redis数据库更新了MAAT_RULE_TIMER,内容是{},SCORES是{}", redisDBIndex, maatKey, score);
|
|
|
|
|
|
|
|
|
|
}
|
2018-05-23 19:54:35 +08:00
|
|
|
} else if (maatXmlExpr.getKeyExpression().toUpperCase().equals("MAAT_VERSION_TIMER")) {
|
|
|
|
|
Long nowTime = new Date().getTime();
|
|
|
|
|
nowTime = nowTime / 1000l;
|
|
|
|
|
Double score = nowTime.doubleValue();// 使用redis自带的time,得到当前时间的秒
|
|
|
|
|
redisTemplate.boundZSetOps("MAAT_VERSION_TIMER").add(maatVersion + "", score);
|
2018-06-01 14:20:08 +08:00
|
|
|
logger.info("向{}号redis数据库更新了MAAT_VERSION_TIMER,内容是{},SCORES是{}", redisDBIndex, maatVersion, score);
|
2018-05-23 19:54:35 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("无法获取业务类型" + service + "对应的maat规则或传入的配置信息有误,请检查!");
|
2018-05-23 19:54:35 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-31 10:09:29 +08:00
|
|
|
public void updateCommonKey(MaatXmlConfig maatXmlConfig, RedisTemplate<String, String> redisTemplate) {
|
2018-05-23 19:54:35 +08:00
|
|
|
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) {
|
|
|
|
|
redisTemplate.boundValueOps(seqKey).increment(1l);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-24 16:36:26 +08:00
|
|
|
public Long getIncrId(String key) {
|
2018-05-31 10:09:29 +08:00
|
|
|
RedisTemplate<String, String> redisTemplate = SpringContextHolder.getBean("redisTemplate0");
|
2018-06-01 14:20:08 +08:00
|
|
|
Long id = redisTemplate.boundValueOps(key.toUpperCase()).increment(1l);
|
|
|
|
|
logger.info("从0号redis数据库获取{}成功,自增后的值是{}", key, id);
|
|
|
|
|
return id;
|
2018-05-21 10:21:26 +08:00
|
|
|
}
|
|
|
|
|
|
2018-05-29 15:36:42 +08:00
|
|
|
@Override
|
2018-06-01 10:27:03 +08:00
|
|
|
@Transactional
|
2018-06-01 18:04:48 +08:00
|
|
|
public boolean delUnMaatConfig(Map<Integer, Map<Integer, List<Long>>> idMap) {
|
2018-05-31 10:09:29 +08:00
|
|
|
if (idMap != null && idMap.size() > 0) {
|
2018-06-02 12:49:20 +08:00
|
|
|
int count = 0;
|
2018-05-31 10:09:29 +08:00
|
|
|
for (Integer redisDBIndex : idMap.keySet()) {
|
|
|
|
|
RedisTemplate<String, String> redisTemplate = SpringContextHolder
|
|
|
|
|
.getBean("redisTemplate" + redisDBIndex);
|
|
|
|
|
Map<Integer, List<Long>> serviceConfigMap = idMap.get(redisDBIndex);
|
|
|
|
|
if (serviceConfigMap != null && serviceConfigMap.size() > 0) {
|
|
|
|
|
String maatVersionStr = redisTemplate.opsForValue().get("MAAT_VERSION");
|
|
|
|
|
if (maatVersionStr == null) {
|
|
|
|
|
maatVersionStr = "0";
|
|
|
|
|
}
|
|
|
|
|
if (maatVersionStr != null) {
|
|
|
|
|
Long maatVersion = Long.valueOf(maatVersionStr) + 1;
|
|
|
|
|
for (Integer service : serviceConfigMap.keySet()) {
|
|
|
|
|
List<Long> compileIdList = serviceConfigMap.get(service);
|
|
|
|
|
if (compileIdList != null && compileIdList.size() > 0) {
|
|
|
|
|
for (Long id : compileIdList) {
|
|
|
|
|
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service);
|
|
|
|
|
if (maatXmlConfig != null) {
|
|
|
|
|
List<MaatXmlExpr> expressionList = maatXmlConfig.getExpressionList();
|
2018-06-01 14:20:08 +08:00
|
|
|
String maatKey = null;
|
2018-05-31 10:09:29 +08:00
|
|
|
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")) {
|
|
|
|
|
keyStr = "OBSOLETE_RULE";
|
|
|
|
|
keyBF.append(keyStr.trim());
|
|
|
|
|
} else if (!StringUtils.isEmpty(keyStr)
|
|
|
|
|
&& keyStr.trim().startsWith("[")) {
|
|
|
|
|
// keyStr = keyStr.trim().replace("[", "").replace("]", "");
|
|
|
|
|
keyBF.append(id);
|
2018-06-04 17:52:03 +08:00
|
|
|
} 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 RuntimeException(
|
|
|
|
|
"未从业务类型和表对应关系中,找到非maat配置业务类型:" + service
|
|
|
|
|
+ "对应的真实表名");
|
|
|
|
|
} else {
|
|
|
|
|
keyBF.append(maatTableName);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-31 10:09:29 +08:00
|
|
|
} else {
|
|
|
|
|
keyBF.append(keyStr.trim());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
maatKey = keyBF.toString();
|
|
|
|
|
String oldKey = maatKey.replace("OBSOLETE_RULE", "EFFECTIVE_RULE");
|
2018-06-04 17:52:03 +08:00
|
|
|
if (redisTemplate.hasKey(oldKey.toString().toUpperCase())) {
|
2018-06-04 11:08:14 +08:00
|
|
|
redisTemplate.rename(oldKey.toString().toUpperCase(),
|
|
|
|
|
keyBF.toString().toUpperCase());
|
|
|
|
|
logger.info("向{}号redis数据库修改了一条配置,修改前key是{},修改后key是{}", redisDBIndex,
|
|
|
|
|
oldKey.toString().toUpperCase(),
|
|
|
|
|
keyBF.toString().toUpperCase());
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
2018-06-04 17:52:03 +08:00
|
|
|
throw new RuntimeException(redisDBIndex + "号redis库中不存在key=" + oldKey
|
|
|
|
|
+ "请检查id映射关系是否正确");
|
2018-06-04 11:08:14 +08:00
|
|
|
}
|
2018-05-31 10:09:29 +08:00
|
|
|
}
|
|
|
|
|
// redisTemplate.boundValueOps(keyBF.toString().toUpperCase()).set(valBF.toString());
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
for (MaatXmlExpr maatXmlExpr : expressionList) {
|
|
|
|
|
if (maatXmlExpr.getKeyExpression().toUpperCase()
|
|
|
|
|
.equals("MAAT_UPDATE_STATUS")) {
|
2018-06-01 14:20:08 +08:00
|
|
|
if (maatKey != null) {
|
|
|
|
|
String zset = maatKey.replace("OBSOLETE_RULE:", "DEL,");
|
|
|
|
|
redisTemplate.boundZSetOps("MAAT_UPDATE_STATUS").add(zset,
|
|
|
|
|
maatVersion);
|
|
|
|
|
logger.info("向{}号redis数据库更新了MAAT_UPDATE_STATUS,内容是{},SCORES是{}",
|
|
|
|
|
redisDBIndex, zset.toUpperCase(), maatVersion);
|
|
|
|
|
}
|
2018-05-31 10:09:29 +08:00
|
|
|
} else if (maatXmlExpr.getKeyExpression().toUpperCase()
|
|
|
|
|
.equals("MAAT_RULE_TIMER")) {
|
2018-06-01 14:20:08 +08:00
|
|
|
if (maatKey != null) {
|
|
|
|
|
Double score = 0d;// 界面下发的配置没有超时时间所以这里设置为0
|
|
|
|
|
redisTemplate.boundZSetOps("MAAT_RULE_TIMER").add(maatKey, score);
|
|
|
|
|
logger.info("向{}号redis数据库更新了MAAT_RULE_TIMER,内容是{},SCORES是{}",
|
|
|
|
|
redisDBIndex, maatKey, score);
|
|
|
|
|
}
|
2018-05-31 10:09:29 +08:00
|
|
|
} else if (maatXmlExpr.getKeyExpression().toUpperCase()
|
|
|
|
|
.equals("MAAT_VERSION_TIMER")) {
|
|
|
|
|
Long nowTime = new Date().getTime();
|
|
|
|
|
nowTime = nowTime / 1000l;
|
|
|
|
|
Double score = nowTime.doubleValue();// 使用redis自带的time,得到当前时间的秒
|
|
|
|
|
redisTemplate.boundZSetOps("MAAT_VERSION_TIMER").add(maatVersion + "",
|
|
|
|
|
score);
|
2018-06-01 14:20:08 +08:00
|
|
|
logger.info("向{}号redis数据库更新了MAAT_VERSION_TIMER,内容是{},SCORES是{}",
|
|
|
|
|
redisDBIndex, maatVersion, score);
|
2018-05-31 10:09:29 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
List<MaatXmlSeq> seqList = maatXmlConfig.getSequenceList();
|
|
|
|
|
for (MaatXmlSeq maatXmlSeq : seqList) {
|
|
|
|
|
// setRedisDataBase(maatXmlSeq.getRedisDB(),
|
|
|
|
|
// redisTemplate);
|
|
|
|
|
String seqKey = maatXmlSeq.getSequenceKey();
|
|
|
|
|
if (!seqKey.toUpperCase().equals("MAAT_VERSION")) {
|
|
|
|
|
Integer operation = maatXmlSeq.getOperation();
|
|
|
|
|
if (operation == 1) {
|
|
|
|
|
redisTemplate.boundValueOps(seqKey.toUpperCase()).increment(1l);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
"无法从maat.xml中获取业务类型" + service + "对应的规则,请检查业务类型是否正确");
|
2018-05-29 15:36:42 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-06-02 12:49:20 +08:00
|
|
|
redisTemplate.boundValueOps("MAAT_VERSION").increment(1l);
|
|
|
|
|
logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDBIndex,
|
|
|
|
|
Integer.valueOf(maatVersionStr) + 1);
|
|
|
|
|
count++;
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
"无法从参数中获取" + redisDBIndex + "号redis库,业务类型为:" + service + "的配置id信息,请检查配置参数是否正确");
|
2018-05-29 15:36:42 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-06-02 12:49:20 +08:00
|
|
|
|
2018-05-29 15:36:42 +08:00
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("向" + redisDBIndex + "号redis库中添加配置时,未发现对应的配置信息,请检查配置参数是否正确");
|
2018-05-29 15:36:42 +08:00
|
|
|
}
|
2018-05-31 10:09:29 +08:00
|
|
|
|
2018-05-29 15:36:42 +08:00
|
|
|
}
|
2018-06-02 12:49:20 +08:00
|
|
|
if (count == idMap.size()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("参数信息有误,请检查!");
|
2018-05-29 15:36:42 +08:00
|
|
|
}
|
2018-06-01 18:04:48 +08:00
|
|
|
return false;
|
2018-05-29 15:36:42 +08:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 10:09:29 +08:00
|
|
|
@Override
|
2018-06-01 10:27:03 +08:00
|
|
|
@Transactional
|
2018-06-01 18:04:48 +08:00
|
|
|
public boolean delMaatConfig(Map<Integer, Map<Integer, List<Long>>> idMap) {
|
2018-05-31 10:09:29 +08:00
|
|
|
if (idMap != null && idMap.size() > 0) {
|
2018-06-02 12:49:20 +08:00
|
|
|
int count = 0;
|
2018-05-31 10:09:29 +08:00
|
|
|
for (Integer redisDBIndex : idMap.keySet()) {
|
2018-06-07 18:37:30 +08:00
|
|
|
|
|
|
|
|
int idRelaRedisDBIndex = Configurations.getIntProperty("idRelaRedisDBIndex", 15);
|
|
|
|
|
RedisTemplate<String, String> idRredisTemplate = SpringContextHolder
|
|
|
|
|
.getBean("redisTemplate" + idRelaRedisDBIndex);
|
|
|
|
|
|
2018-05-31 10:09:29 +08:00
|
|
|
RedisTemplate<String, String> redisTemplate = SpringContextHolder
|
|
|
|
|
.getBean("redisTemplate" + redisDBIndex);
|
|
|
|
|
Map<Integer, List<Long>> serviceConfigMap = idMap.get(redisDBIndex);
|
|
|
|
|
if (serviceConfigMap != null && serviceConfigMap.size() > 0) {
|
|
|
|
|
String maatVersionStr = redisTemplate.opsForValue().get("MAAT_VERSION");
|
|
|
|
|
if (maatVersionStr == null) {
|
|
|
|
|
maatVersionStr = "0";
|
|
|
|
|
}
|
2018-06-07 18:37:30 +08:00
|
|
|
// MaatRelation maatRelation =
|
|
|
|
|
// CompileGroupRegionRela.getIdRelationMap().get(redisDBIndex);
|
2018-05-31 10:09:29 +08:00
|
|
|
if (maatVersionStr != null) {
|
|
|
|
|
Long maatVersion = Long.valueOf(maatVersionStr) + 1;
|
2018-06-07 18:37:30 +08:00
|
|
|
// if (maatRelation != null && redisTemplate != null) {
|
|
|
|
|
for (Integer service : serviceConfigMap.keySet()) {
|
|
|
|
|
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service);
|
|
|
|
|
removeConfig(serviceConfigMap.get(service), maatXmlConfig, maatVersion, service,
|
|
|
|
|
redisTemplate, redisDBIndex, idRredisTemplate);
|
2018-05-29 15:36:42 +08:00
|
|
|
|
2018-05-31 10:09:29 +08:00
|
|
|
}
|
2018-06-07 18:37:30 +08:00
|
|
|
redisTemplate.boundValueOps("MAAT_VERSION").increment(1l);
|
|
|
|
|
logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDBIndex,
|
|
|
|
|
Integer.valueOf(maatVersionStr) + 1);
|
|
|
|
|
count++;
|
|
|
|
|
// }
|
2018-05-31 10:09:29 +08:00
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("向" + redisDBIndex + "号redis库中添加配置时,未发现对应的配置信息,请检查配置参数是否正确");
|
2018-05-31 10:09:29 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-06-02 12:49:20 +08:00
|
|
|
if (count == idMap.size()) {
|
2018-06-07 18:37:30 +08:00
|
|
|
delMaatRelation(idMap);
|
2018-06-02 12:49:20 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("参数信息有误,请检查!");
|
2018-05-29 15:36:42 +08:00
|
|
|
}
|
2018-06-01 18:04:48 +08:00
|
|
|
return false;
|
2018-05-21 19:06:19 +08:00
|
|
|
}
|
|
|
|
|
|
2018-06-01 14:20:08 +08:00
|
|
|
/**
|
|
|
|
|
* 删除maat类配置
|
|
|
|
|
* @param idList 配置id集合
|
|
|
|
|
* @param maatXmlConfig maat.xml中关于当前业务类型的key和value写法的对象
|
|
|
|
|
* @param maatVersion 版本号
|
|
|
|
|
* @param service 业务类型
|
|
|
|
|
* @param redisTemplate
|
|
|
|
|
* @param maatRelation id对应关系对象
|
|
|
|
|
*/
|
|
|
|
|
private void removeConfig(List<Long> idList, MaatXmlConfig maatXmlConfig, Long maatVersion, int service,
|
2018-06-07 18:37:30 +08:00
|
|
|
RedisTemplate<String, String> redisTemplate, int redisDBIndex,
|
|
|
|
|
RedisTemplate<String, String> idRredisTemplate) {
|
|
|
|
|
if (idList != null && idList.size() > 0 && maatXmlConfig != null) {
|
2018-05-31 17:07:16 +08:00
|
|
|
for (Long id : idList) {
|
2018-06-07 18:37:30 +08:00
|
|
|
removeCompileAndGroupConfig(maatXmlConfig, id + "", 10, maatVersion.doubleValue(), service,
|
|
|
|
|
redisTemplate, redisDBIndex, null);// 10代表是编译配置
|
|
|
|
|
String compileStr = redisDBIndex + ":COMPILEGROUP:" + id;
|
|
|
|
|
// 获取当前编译下所有的分组id
|
|
|
|
|
String groupCompileStrs = idRredisTemplate.opsForValue().get(compileStr);
|
|
|
|
|
if (groupCompileStrs != null && !groupCompileStrs.equals("")) {
|
|
|
|
|
String[] split = groupCompileStrs.split(";");
|
|
|
|
|
for (String groupId : split) {
|
|
|
|
|
String compileGroupStr = idRredisTemplate.opsForValue().get(groupId);
|
|
|
|
|
String[] compileGroupArr = compileGroupStr.split(";");// 获取组对应的编译id
|
|
|
|
|
if (compileGroupArr != null && compileGroupArr.length == 1) {// 如果只有一个编译id且与上面的编译id相同则说明未被分组复用,可以将其下的所有域置失效,否则不处理域配置,只把编译,分组关系置为无效
|
|
|
|
|
for (String compileId : compileGroupArr) {
|
|
|
|
|
if (compileId.equals(compileStr)) {//
|
|
|
|
|
String groupRegionKey = groupId.replace("GROUPCOMPILE", "GROUPREGION");
|
|
|
|
|
String regionStr = idRredisTemplate.opsForValue().get(groupRegionKey);
|
|
|
|
|
String[] regionKeyArr = regionStr.split(";");
|
|
|
|
|
if (regionKeyArr != null && regionKeyArr.length > 0) {
|
|
|
|
|
removeRegionConfig(maatXmlConfig, regionKeyArr, maatVersion.doubleValue(),
|
|
|
|
|
service, redisTemplate, redisDBIndex);
|
2018-06-04 17:52:03 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-31 17:07:16 +08:00
|
|
|
}
|
2018-06-07 18:37:30 +08:00
|
|
|
removeCompileAndGroupConfig(maatXmlConfig, groupId.replace(redisDBIndex + ":GROUPCOMPILE:", ""),
|
|
|
|
|
11, maatVersion.doubleValue(), service, redisTemplate, redisDBIndex, id + "");// 11代表是分组配置
|
2018-05-31 17:07:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("无法获取内存中记录的id映射关系,无法获取业务类型" + service + "对应的maat规则或传入的配置id有误,请检查!");
|
2018-05-31 17:07:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-01 14:20:08 +08:00
|
|
|
/**
|
|
|
|
|
* 删除maat配置时,对redis进行相关操作,主要是重命名key和记录相关状态
|
|
|
|
|
* @param maatXmlConfig
|
|
|
|
|
* @param idList
|
|
|
|
|
* @param type
|
|
|
|
|
* @param maatVersion
|
|
|
|
|
* @param service
|
|
|
|
|
* @param redisTemplate
|
|
|
|
|
*/
|
2018-06-07 18:37:30 +08:00
|
|
|
private void removeCompileAndGroupConfig(MaatXmlConfig maatXmlConfig, String id, int type, Double maatVersion,
|
|
|
|
|
int service, RedisTemplate<String, String> redisTemplate, int redisDBIndex, String compileId) {
|
2018-06-04 17:52:03 +08:00
|
|
|
if (maatXmlConfig != null) {
|
2018-05-31 17:07:16 +08:00
|
|
|
List<MaatXmlExpr> expressionList = maatXmlConfig.getExpressionList();
|
2018-06-01 10:27:03 +08:00
|
|
|
String maatKey = null;
|
2018-06-04 17:52:03 +08:00
|
|
|
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 RuntimeException(
|
|
|
|
|
"未从业务类型和表对应关系中,找到业务类型:" + service + ",配置类型:" + type + ",对应的真实表名");
|
|
|
|
|
} else {
|
|
|
|
|
keyBF.append(maatTableName);
|
2018-05-31 17:07:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-06-04 17:52:03 +08:00
|
|
|
|
2018-06-01 18:04:48 +08:00
|
|
|
} else {
|
2018-06-04 17:52:03 +08:00
|
|
|
keyBF.append(keyStr.trim());
|
2018-06-01 18:04:48 +08:00
|
|
|
}
|
2018-05-31 17:07:16 +08:00
|
|
|
}
|
2018-06-04 17:52:03 +08:00
|
|
|
String oldKey = keyBF.toString().toUpperCase();
|
2018-06-07 18:37:30 +08:00
|
|
|
if (compileId != null) {
|
|
|
|
|
oldKey += compileId;
|
|
|
|
|
}
|
2018-06-04 17:52:03 +08:00
|
|
|
maatKey = oldKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
|
|
|
|
|
if (redisTemplate.hasKey(oldKey)) {
|
|
|
|
|
redisTemplate.rename(oldKey, maatKey.toUpperCase());
|
|
|
|
|
logger.info("向{}号redis数据库修改了一条配置,修改前key是{},修改后key是{}", redisDBIndex, oldKey,
|
|
|
|
|
maatKey.toUpperCase());
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException(redisDBIndex + "号redis库中不存在key=" + oldKey + "请检查id映射关系是否正确");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (MaatXmlExpr maatXmlExpr : expressionList) {
|
|
|
|
|
if (maatXmlExpr.getKeyExpression().toUpperCase().equals("MAAT_UPDATE_STATUS")) {
|
|
|
|
|
if (maatKey != null) {
|
|
|
|
|
String zset = maatKey.replace("OBSOLETE_RULE:", "DEL,");
|
|
|
|
|
redisTemplate.boundZSetOps("MAAT_UPDATE_STATUS").add(zset, maatVersion);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
redisTemplate.boundZSetOps("MAAT_RULE_TIMER").add(maatKey, score);
|
|
|
|
|
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,得到当前时间的秒
|
|
|
|
|
redisTemplate.boundZSetOps("MAAT_VERSION_TIMER").add(maatVersion + "", score);
|
|
|
|
|
logger.info("向{}号redis数据库更新了MAAT_VERSION_TIMER,内容是{},SCORES是{}", redisDBIndex, maatVersion, score);
|
|
|
|
|
|
2018-05-31 17:07:16 +08:00
|
|
|
}
|
2018-06-04 17:52:03 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("无法获取业务类型" + service + "对应的maat规则,请检查!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-07 18:37:30 +08:00
|
|
|
private void removeRegionConfig(MaatXmlConfig maatXmlConfig, String[] regionArr, Double maatVersion, int service,
|
2018-06-05 16:47:58 +08:00
|
|
|
RedisTemplate<String, String> redisTemplate, int redisDBIndex) {
|
2018-06-07 18:37:30 +08:00
|
|
|
if (maatXmlConfig != null && regionArr != null && regionArr.length > 0) {
|
2018-06-04 17:52:03 +08:00
|
|
|
List<MaatXmlExpr> expressionList = maatXmlConfig.getExpressionList();
|
|
|
|
|
String maatKey = null;
|
2018-06-07 18:37:30 +08:00
|
|
|
for (String oldKey : regionArr) {
|
|
|
|
|
oldKey = oldKey.replace(redisDBIndex + ":", "");
|
2018-06-04 17:52:03 +08:00
|
|
|
maatKey = oldKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
|
|
|
|
|
if (redisTemplate.hasKey(oldKey)) {
|
|
|
|
|
redisTemplate.rename(oldKey, maatKey.toUpperCase());
|
|
|
|
|
logger.info("向{}号redis数据库修改了一条配置,修改前key是{},修改后key是{}", redisDBIndex, oldKey, maatKey.toUpperCase());
|
|
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException(redisDBIndex + "号redis库中不存在key=" + oldKey + "请检查id映射关系是否正确");
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-31 17:07:16 +08:00
|
|
|
for (MaatXmlExpr maatXmlExpr : expressionList) {
|
|
|
|
|
if (maatXmlExpr.getKeyExpression().toUpperCase().equals("MAAT_UPDATE_STATUS")) {
|
2018-06-01 10:27:03 +08:00
|
|
|
if (maatKey != null) {
|
|
|
|
|
String zset = maatKey.replace("OBSOLETE_RULE:", "DEL,");
|
|
|
|
|
redisTemplate.boundZSetOps("MAAT_UPDATE_STATUS").add(zset, maatVersion);
|
2018-06-01 14:20:08 +08:00
|
|
|
|
|
|
|
|
logger.info("向{}号redis数据库更新了MAAT_UPDATE_STATUS,内容是{},SCORES是{}", redisDBIndex,
|
|
|
|
|
zset.toUpperCase(), maatVersion);
|
|
|
|
|
|
2018-06-01 10:27:03 +08:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 17:07:16 +08:00
|
|
|
} else if (maatXmlExpr.getKeyExpression().toUpperCase().equals("MAAT_RULE_TIMER")) {
|
2018-06-01 10:27:03 +08:00
|
|
|
if (maatKey != null) {
|
|
|
|
|
Double score = 0d;// 界面下发的配置没有超时时间所以这里设置为0
|
|
|
|
|
redisTemplate.boundZSetOps("MAAT_RULE_TIMER").add(maatKey, score);
|
2018-06-01 14:20:08 +08:00
|
|
|
logger.info("向{}号redis数据库更新了MAAT_RULE_TIMER,内容是{},SCORES是{}", redisDBIndex, maatKey, score);
|
|
|
|
|
|
2018-06-01 10:27:03 +08:00
|
|
|
}
|
2018-05-31 17:07:16 +08:00
|
|
|
} else if (maatXmlExpr.getKeyExpression().toUpperCase().equals("MAAT_VERSION_TIMER")) {
|
|
|
|
|
Long nowTime = new Date().getTime();
|
|
|
|
|
nowTime = nowTime / 1000l;
|
|
|
|
|
Double score = nowTime.doubleValue();// 使用redis自带的time,得到当前时间的秒
|
|
|
|
|
redisTemplate.boundZSetOps("MAAT_VERSION_TIMER").add(maatVersion + "", score);
|
2018-06-01 14:20:08 +08:00
|
|
|
logger.info("向{}号redis数据库更新了MAAT_VERSION_TIMER,内容是{},SCORES是{}", redisDBIndex, maatVersion,
|
|
|
|
|
score);
|
|
|
|
|
|
2018-05-31 17:07:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("无法获取业务类型" + service + "对应的maat规则或传入的配置信息有误,请检查!");
|
2018-05-31 17:07:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-05-29 15:36:42 +08:00
|
|
|
|
2018-06-01 14:20:08 +08:00
|
|
|
/**
|
|
|
|
|
* 删除配置成功后,需要更新编译,组,域等配置id的对应关系
|
|
|
|
|
* @param idMap
|
|
|
|
|
*/
|
2018-06-07 18:37:30 +08:00
|
|
|
private void delMaatRelation(Map<Integer, Map<Integer, List<Long>>> idMap) {
|
|
|
|
|
if (idMap != null && idMap.size() > 0) {
|
|
|
|
|
int idRelaRedisDBIndex = Configurations.getIntProperty("idRelaRedisDBIndex", 15);
|
|
|
|
|
RedisTemplate<String, String> redisTemplate = SpringContextHolder
|
|
|
|
|
.getBean("redisTemplate" + idRelaRedisDBIndex);
|
|
|
|
|
for (Integer redisDBIndex : idMap.keySet()) {
|
|
|
|
|
if (redisDBIndex >= 0 && redisDBIndex < Configurations.getIntProperty("maxRedisDBIndex", 6)) {
|
|
|
|
|
Map<Integer, List<Long>> map = idMap.get(redisDBIndex);
|
|
|
|
|
if (map != null && map.size() > 0) {
|
|
|
|
|
for (Integer service : map.keySet()) {
|
|
|
|
|
List<Long> idList = map.get(service);
|
|
|
|
|
if (idList != null && idList.size() > 0) {
|
|
|
|
|
for (Long compileId : idList) {
|
|
|
|
|
String compileStr = redisDBIndex + ":COMPILEGROUP:" + compileId;
|
|
|
|
|
String groupCompileStr = redisTemplate.opsForValue().get(compileStr);// 根据编译id获取该编译下的分组关系
|
|
|
|
|
if (groupCompileStr != null && !groupCompileStr.equals("")) {
|
|
|
|
|
String[] groupCompileStrSplit = groupCompileStr.split(";");// 得到分组关系
|
|
|
|
|
for (String groupCompile : groupCompileStrSplit) {// 遍历所有分组关系
|
|
|
|
|
String compileGroupStr = redisTemplate.opsForValue()
|
|
|
|
|
.get(groupCompile.toUpperCase());// 获取当前分组关系对应的编译信息
|
|
|
|
|
if (compileGroupStr != null && !compileGroupStr.equals("")) {
|
|
|
|
|
String[] compileGroupStrSplit = compileGroupStr.split(";");
|
|
|
|
|
if (compileGroupStrSplit != null && compileGroupStrSplit.length == 1
|
|
|
|
|
&& compileGroupStr.equals(compileStr.toUpperCase())) {// 当前的分组关系只属于当前的compileid,说明没有被分组复用,需要将编译配置,分组关系,域配置,置无效
|
|
|
|
|
if (compileGroupStrSplit[0].toUpperCase()
|
|
|
|
|
.equals(compileStr.toUpperCase())) {
|
|
|
|
|
String groupRegion = groupCompile.replaceAll("GROUPCOMPILE",
|
|
|
|
|
"GROUPREGION");
|
|
|
|
|
if (redisTemplate.hasKey(groupRegion)) {
|
|
|
|
|
redisTemplate.delete(groupRegion);// 删除组对应的域
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (redisTemplate.hasKey(groupCompile.toUpperCase())) {
|
|
|
|
|
redisTemplate.delete(groupCompile.toUpperCase());// 删除当前组所对应的编译
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
} else {// 当前的分组被复用了,属于多个编译,需要将当前的分组关系置为无效
|
|
|
|
|
Set<String> groupCompileSet = new HashSet<String>();
|
|
|
|
|
for (String compileGroup : compileGroupStrSplit) {
|
|
|
|
|
if (!compileGroup.equals(compileStr.toUpperCase())) {
|
|
|
|
|
groupCompileSet.add(compileGroup);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
|
// 更新组对应的编译
|
|
|
|
|
for (String str : groupCompileSet) {
|
|
|
|
|
sb.append(str);
|
|
|
|
|
sb.append(";");
|
|
|
|
|
}
|
|
|
|
|
redisTemplate.opsForValue().set(groupCompile,
|
|
|
|
|
sb.substring(0, sb.length() - 1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (redisTemplate.hasKey(compileStr.toUpperCase())) {
|
|
|
|
|
redisTemplate.delete(compileStr.toUpperCase());// 删除编译下面所有的组
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("redis数据库编号:" + redisDBIndex + "不正确,请检查数据库编号");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("参数不能为空");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void delMaatRelationOld(Map<Integer, Map<Integer, List<Long>>> idMap) {
|
2018-05-31 17:07:16 +08:00
|
|
|
if (idMap != null && idMap.size() > 0) {
|
|
|
|
|
for (Integer redisDBIndex : idMap.keySet()) {
|
2018-06-01 14:20:08 +08:00
|
|
|
if (redisDBIndex >= 0 && redisDBIndex < Configurations.getIntProperty("maxRedisDBIndex", 6)) {
|
2018-06-01 10:27:03 +08:00
|
|
|
// MaatRelation maatRelation =
|
|
|
|
|
// CompileGroupRegionRela.getIdRelationMap().get(redisDBIndex);
|
|
|
|
|
Map<Integer, List<Long>> map = idMap.get(redisDBIndex);
|
|
|
|
|
if (map != null && map.size() > 0) {
|
|
|
|
|
for (Integer service : map.keySet()) {
|
|
|
|
|
List<Long> idList = map.get(service);
|
|
|
|
|
if (idList != null && idList.size() > 0) {
|
|
|
|
|
for (Long compileId : idList) {
|
|
|
|
|
CompileGroupRegionRela.delIdRelation(redisDBIndex, compileId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("redis数据库编号:" + redisDBIndex + "不正确,请检查数据库编号");
|
2018-05-31 17:07:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-06-04 10:18:31 +08:00
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("参数不能为空");
|
2018-05-31 17:07:16 +08:00
|
|
|
}
|
2018-05-29 15:36:42 +08:00
|
|
|
}
|
2018-05-19 18:35:24 +08:00
|
|
|
}
|