1:修改全量配置同步,controller对状态的验证

2:添加从集群中恢复数据的功能并记录相关的maat_version
3:添加无验证的保存maat和unmaat类的方法
4:修改集群中保存同步数据的实效时间为24小时
5:修改集群获取连接的方式为每次使用获取,使用完关闭,避免连接出现问题
This commit is contained in:
renkaige
2018-12-03 18:09:12 +06:00
parent ab0d5575ff
commit 6340ea3f9e
3 changed files with 457 additions and 32 deletions

View File

@@ -307,6 +307,185 @@ public class ConfigSourcesService extends BaseService {
configRedisService.saveMaatConfig(configMap);
}
/**
* @Description:
* @author(zdx) @date 2018年12月3日 下午6:48:32
* @param configCompileList
* @throws Exception
*/
public void saveMaatConfig(List<ConfigCompile> configCompileList) throws Exception {
Map<Integer, List<MaatConfig>> maatMap = new HashMap<Integer, List<MaatConfig>>();
Map<Integer, List<MaatConfig>> configMap = new HashMap<Integer, List<MaatConfig>>();
for (ConfigCompile configCompile : configCompileList) {
Integer service = Integer.valueOf(configCompile.getService().toString());
MaatConfig maatConfig = new MaatConfig();
maatConfig.setService(service);
// 编译
maatConfig.setCompileMap(convertObjectToMap(configCompile, ConfigCompile.class));
// 分组
List<Map<String, String>> dstMaplList = null;
if (!StringUtil.isEmpty(configCompile.getGroupRelationList())) {
dstMaplList = new ArrayList<Map<String, String>>();
for (ConfigGroupRelation group : configCompile.getGroupRelationList()) {
dstMaplList.add(convertObjectToMap(group, ConfigGroupRelation.class));
}
}
maatConfig.setGroupMapList(dstMaplList);
// 字符串域
dstMaplList = null;
List<Map<String, String>> strongMapList = null;
if (!StringUtil.isEmpty(configCompile.getStrRegionList())) {
dstMaplList = new ArrayList<Map<String, String>>();
for (StrRegion region : configCompile.getStrRegionList()) {
if (StringUtil.isEmpty(region.getDistrict())) {
dstMaplList.add(convertObjectToMap(region, StrRegion.class));
} else {
if (StringUtil.isEmpty(strongMapList)) {
strongMapList = new ArrayList<Map<String, String>>();
}
strongMapList.add(convertObjectToMap(region, StrRegion.class));
}
}
}
maatConfig.setStrRegionMapList(dstMaplList);
// 增强字符串域
if (!StringUtil.isEmpty(strongMapList) && strongMapList.size() > 0) {
maatConfig.setStrStrRegionMapList((strongMapList));
}
// 数值域
dstMaplList = null;
if (!StringUtil.isEmpty(configCompile.getNumRegionList())) {
dstMaplList = new ArrayList<Map<String, String>>();
for (NumRegion region : configCompile.getNumRegionList()) {
dstMaplList.add(convertObjectToMap(region, NumRegion.class));
}
}
maatConfig.setNumRegionMapList(dstMaplList);
// Ip域
dstMaplList = null;
if (!StringUtil.isEmpty(configCompile.getIpRegionList())) {
dstMaplList = new ArrayList<Map<String, String>>();
for (IpRegion region : configCompile.getIpRegionList()) {
dstMaplList.add(convertObjectToMap(region, IpRegion.class));
}
}
maatConfig.setIpRegionMapList(dstMaplList);
// 摘要类域
dstMaplList = null;
if (!StringUtil.isEmpty(configCompile.getDigestRegionList())) {
dstMaplList = new ArrayList<Map<String, String>>();
for (DigestRegion region : configCompile.getDigestRegionList()) {
dstMaplList.add(convertObjectToMap(region, DigestRegion.class));
}
}
maatConfig.setFileDigestRegionMapList(dstMaplList);
// 文本相似性域
// dstMaplList = null;
// maatConfig.setFileLikeRegionMapList(dstMaplList);
// 生效范围IP域
dstMaplList = null;
if (!StringUtil.isEmpty(configCompile.getIpClientRangeList())) {
dstMaplList = new ArrayList<Map<String, String>>();
for (IpRegion region : configCompile.getIpClientRangeList()) {
dstMaplList.add(convertObjectToMap(region, IpRegion.class));
}
}
maatConfig.setIpClientRangeMapList(dstMaplList);
if (maatMap.containsKey(service)) {
maatMap.get(service).add(maatConfig);
} else {
List<MaatConfig> maatCfgList = new ArrayList<MaatConfig>();
maatCfgList.add(maatConfig);
maatMap.put(service, maatCfgList);
}
}
// 调用接口入redis
Iterator serviceIterator = maatMap.keySet().iterator();
while (serviceIterator.hasNext()) {
Integer service = Integer.valueOf(serviceIterator.next().toString());
List<Integer> dbIndexList = ServiceAndRDBIndexReal.getRedisDBByService(service);
if (!StringUtil.isEmpty(dbIndexList) && dbIndexList.size() > 0) {
for (Integer dbIndex : dbIndexList) {
// 分发到阀门有些业务需要添加编译属性到域配置
List<MaatConfig> newMaatConfigList = new ArrayList<MaatConfig>();
newMaatConfigList.addAll(maatMap.get(service));
if (dbIndex.intValue() == ServiceAndRDBIndexReal.getValveDBIndex().intValue()) {
Map<Integer, Map<String, String[]>> maatToValueMap = ServiceAndRDBIndexReal.getMaatToValveMap();
if (maatToValueMap.containsKey(service)) {
Map<String, String[]> regionAndFiledMap = maatToValueMap.get(service);
for (int i = 0; i < newMaatConfigList.size(); i++) {
MaatConfig maatConfig = newMaatConfigList.get(i);
MaatConfig newMaatConfig = (MaatConfig) JsonMapper
.fromJsonString(JsonMapper.toJsonString(maatConfig), MaatConfig.class);
Iterator iterator = regionAndFiledMap.keySet().iterator();
while (iterator.hasNext()) {
String regionName = iterator.next().toString();
PropertyDescriptor pd;
try {
pd = new PropertyDescriptor(regionName + "MapList", MaatConfig.class);
Method method = pd.getReadMethod();
Object object = method.invoke(newMaatConfig);
if (object != null) {
List<Map<String, String>> listMaps = new ArrayList<Map<String, String>>();
listMaps.addAll((List<Map<String, String>>) object);
String[] fields = regionAndFiledMap.get(regionName);
for (String fieldName : fields) {
String value = newMaatConfig.getCompileMap()
.get(fieldName.toLowerCase());
if (!StringUtil.isEmpty(value)) {
for (Map<String, String> map : listMaps) {
map.put(fieldName.toLowerCase(), value);
}
}
}
method = pd.getWriteMethod();
method.invoke(newMaatConfig, listMaps);
}
newMaatConfigList.set(i, newMaatConfig);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RestServiceException("未找到域列表,请检查配置文件中域类型是否正确!:" + e.getMessage(),
RestBusinessCode.service_runtime_error.getValue());
}
}
}
}
}
if (configMap.containsKey(dbIndex)) {
configMap.get(dbIndex).addAll(newMaatConfigList);
} else {
List<MaatConfig> list = new ArrayList<MaatConfig>();
list.addAll(newMaatConfigList);
configMap.put(dbIndex, list);
}
}
} else {
throw new ServiceRuntimeException(RestBusinessCode.ServiceNoFoundDBIndex.getErrorReason(),
RestBusinessCode.ServiceNoFoundDBIndex.getValue());
}
}
logger.info("---------------调用Redis maat配置新增接口---------------------");
configRedisService.saveMaatConfig(configMap);
}
private Map<String, String> convertObjectToMap(Object obj, Class clazz) throws Exception {
Map<String, String> dstMap = new HashMap<String, String>();
Field[] fields = obj.getClass().getDeclaredFields();
@@ -630,6 +809,96 @@ public class ConfigSourcesService extends BaseService {
configRedisService.saveUnMaatConfig(configMap);
}
public void saveCommonSources(String jsonString) throws Exception {
JsonArray jsonObjectList = null;
try {
jsonObjectList = new JsonParser().parse(jsonString).getAsJsonArray();
} catch (Exception e) {
// TODO: handle exception
throw new RestServiceException(RestBusinessCode.CBParamFormateError.getErrorReason() + "," + e.getMessage(),
RestBusinessCode.CBParamFormateError.getValue());
}
Map<Integer, List<Map<String, String>>> dstMaps = new HashMap<Integer, List<Map<String, String>>>();
for (int i = 0; i < jsonObjectList.size(); i++) {
JsonObject jsonObj = (JsonObject) jsonObjectList.get(i);
Map<String, Object> srcMap = JSONObject.fromObject(JSONObject.fromObject((jsonObj.toString())));
if (srcMap.containsKey("service")) {
Map<String, String> dstMap = new HashMap<String, String>();
List<CommonSourceFieldCfg> commonSourceFieldCfgList = ReadCommSourceXmlUtil
.getCommonSourceCfgByService(srcMap.get("service").toString().trim());
// 获取IP类型
Integer ipType = null;
String ipTypeName = "";
for (CommonSourceFieldCfg commonSourceFieldCfg : commonSourceFieldCfgList) {
if (commonSourceFieldCfg.getDstName().equals("addr_type")) {
String dstVal = srcMap.get(commonSourceFieldCfg.getSrcName()).toString();
ipType = Integer.parseInt(dstVal);
}
}
if (ipType == null) {
ipType = 4;
}
for (CommonSourceFieldCfg commonSourceFieldCfg : commonSourceFieldCfgList) {
// 字段类型 String Number Date Ip Port
String dstStr = StringUtil.isEmpty(srcMap.get(commonSourceFieldCfg.getSrcName()))
? commonSourceFieldCfg.getDefaultVal()
: srcMap.get(commonSourceFieldCfg.getSrcName()).toString();
if (!StringUtil.isEmpty(dstStr) && dstStr.startsWith("[") && dstStr.endsWith("]")) {
dstStr = srcMap.get(dstStr.substring(1, dstStr.length() - 1)).toString();
}
if ("dstFile".equals(commonSourceFieldCfg.getSrcName())) {
if ("dst_file".equals(commonSourceFieldCfg.getDstName())) {
String maatTableName = ServiceAndRDBIndexReal
.getUnMaatTableName(Integer.valueOf(srcMap.get("service").toString().trim()));
String dstPath = Constants.MM_SAMPLE_DST_PATH.replace("{tableType}",
maatTableName.substring(maatTableName.lastIndexOf("_") + 1));
dstStr = dstPath.replace("{fileName}", dstStr.substring(dstStr.lastIndexOf("/") + 1));
} else if ("file_id".equals(commonSourceFieldCfg.getDstName())) {
// dstStr = dstStr.substring(dstStr.indexOf("group"));
}
}
dstMap.put(commonSourceFieldCfg.getDstName(), dstStr);
}
if (StringUtil.isEmpty(dstMaps.get(Integer.valueOf(srcMap.get("service").toString())))) {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(dstMap);
dstMaps.put(Integer.valueOf(srcMap.get("service").toString()), list);
} else {
List<Map<String, String>> list = dstMaps.get(Integer.valueOf(srcMap.get("service").toString()));
list.add(dstMap);
}
}
}
logger.info("------------------调用非maat配置新增接口-------------------");
// 按service分库
Map<Integer, List<Map<String, String>>> configMap = new HashMap<Integer, List<Map<String, String>>>();
Iterator serviceIterator = dstMaps.keySet().iterator();
while (serviceIterator.hasNext()) {
Integer service = Integer.valueOf(serviceIterator.next().toString());
List<Integer> dbIndexList = ServiceAndRDBIndexReal.getRedisDBByService(service);
if (!StringUtil.isEmpty(dbIndexList) && dbIndexList.size() > 0) {
for (Integer dbIndex : dbIndexList) {
if (configMap.containsKey(dbIndex)) {
configMap.get(dbIndex).addAll(dstMaps.get(service));
} else {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.addAll(dstMaps.get(service));
configMap.put(dbIndex, list);
}
}
} else {
throw new ServiceRuntimeException("service与写入数据库序号映射关系不存在",
RestBusinessCode.ServiceNoFoundDBIndex.getValue());
}
}
configRedisService.saveUnMaatConfig(configMap);
}
/**
*
* @Description:回调类配置状态更新(停/启用)
@@ -1077,7 +1346,7 @@ public class ConfigSourcesService extends BaseService {
* @param value
*/
public void setRedisClusterKey(String key, String value) {
JedisClusterUtils.set(key, value, 0);
JedisClusterUtils.set(key, value, 86400);// 24小时超时
}
/**