diff --git a/src/main/java/com/nis/util/Constants.java b/src/main/java/com/nis/util/Constants.java index 74620ed..d892b14 100644 --- a/src/main/java/com/nis/util/Constants.java +++ b/src/main/java/com/nis/util/Constants.java @@ -211,4 +211,6 @@ public final class Constants { *是否使用Minio */ public static final Boolean IS_USE_MINIO = Configurations.getBooleanProperty("isUseMinio", true); + public static final int MAXTHREADNUM = Configurations.getIntProperty("maxThreadNum", 10); + public static final int EVERTHREADNUM = Configurations.getIntProperty("everThreadNum", 10000); } diff --git a/src/main/java/com/nis/util/JedisUtils.java b/src/main/java/com/nis/util/JedisUtils.java index 36bb6be..20d8456 100644 --- a/src/main/java/com/nis/util/JedisUtils.java +++ b/src/main/java/com/nis/util/JedisUtils.java @@ -17,10 +17,11 @@ import com.nis.web.service.SpringContextHolder; public class JedisUtils { private static Logger logger = LoggerFactory.getLogger(JedisUtils.class); private static JedisPool jedisPool = SpringContextHolder.getBean(JedisPool.class); - /** * 获取缓存 - * @param key 键 + * + * @param key + * 键 * @return 值 */ public static String get(String key, int redisDb) { @@ -34,7 +35,8 @@ public class JedisUtils { logger.debug("get {} = {}", key, value); } } catch (Exception e) { - throw new ServiceRuntimeException("从" + redisDb + "号redisDB中获取" + key + "对应的值失败", RestBusinessCode.KeyNotExistsInRedis.getValue()); + throw new ServiceRuntimeException("从" + redisDb + "号redisDB中获取" + key + "对应的值失败", + RestBusinessCode.KeyNotExistsInRedis.getValue()); } finally { returnResource(jedis); } @@ -43,7 +45,9 @@ public class JedisUtils { /** * 获取缓存 - * @param key 键 + * + * @param key + * 键 * @return 值 */ public static Object getObject(String key, int redisDb) { @@ -56,7 +60,8 @@ public class JedisUtils { logger.debug("getObject {} = {}", key, value); } } catch (Exception e) { - throw new ServiceRuntimeException("从" + redisDb + "号redisDB中获取" + key + "对应的值失败", RestBusinessCode.KeyNotExistsInRedis.getValue()); + throw new ServiceRuntimeException("从" + redisDb + "号redisDB中获取" + key + "对应的值失败", + RestBusinessCode.KeyNotExistsInRedis.getValue()); } finally { returnResource(jedis); } @@ -64,14 +69,15 @@ public class JedisUtils { } /** - * 可以作为获取唯一id的方法
- * 将key对应的value加上指定的值,只有value可以转为数字时该方法才可用 - * - * @param String - * key - * @param long number 要减去的值 - * @return long 相加后的值 - * */ + * 可以作为获取唯一id的方法
+ * 将key对应的value加上指定的值,只有value可以转为数字时该方法才可用 + * + * @param String + * key + * @param long + * number 要减去的值 + * @return long 相加后的值 + */ public static long incrBy(String key, long number, int redisDb) { Jedis jedis = getResource(redisDb); long len = jedis.incrBy(key, number); @@ -81,9 +87,13 @@ public class JedisUtils { /** * 设置缓存 - * @param key 键 - * @param value 值 - * @param cacheSeconds 超时时间,0为不超时 + * + * @param key + * 键 + * @param value + * 值 + * @param cacheSeconds + * 超时时间,0为不超时 * @return */ public static String set(String key, String value, int cacheSeconds, int redisDb) { @@ -97,7 +107,8 @@ public class JedisUtils { } logger.debug("set {} = {}", key, value); } catch (Exception e) { - throw new ServiceRuntimeException("向" + redisDb + "号redisDB中设置zset失败,key=" + key + ",value=" + value, RestBusinessCode.ZsetFailed.getValue()); + throw new ServiceRuntimeException("向" + redisDb + "号redisDB中设置zset失败,key=" + key + ",value=" + value, + RestBusinessCode.ZsetFailed.getValue()); } finally { returnResource(jedis); } @@ -106,7 +117,9 @@ public class JedisUtils { /** * 获取List缓存 - * @param key 键 + * + * @param key + * 键 * @return 值 */ public static List getList(String key, int redisDb) { @@ -119,7 +132,8 @@ public class JedisUtils { logger.debug("getList {} = {}", key, value); } } catch (Exception e) { - throw new ServiceRuntimeException("从" + redisDb + "号redisDB中获取" + key + "对应的值失败", RestBusinessCode.KeyNotExistsInRedis.getValue()); + throw new ServiceRuntimeException("从" + redisDb + "号redisDB中获取" + key + "对应的值失败", + RestBusinessCode.KeyNotExistsInRedis.getValue()); } finally { returnResource(jedis); } @@ -128,7 +142,9 @@ public class JedisUtils { /** * 获取List缓存 - * @param key 键 + * + * @param key + * 键 * @return 值 */ public static List getObjectList(String key, int redisDb) { @@ -145,7 +161,8 @@ public class JedisUtils { logger.debug("getObjectList {} = {}", key, value); } } catch (Exception e) { - throw new ServiceRuntimeException("从" + redisDb + "号redisDB中获取" + key + "对应的值失败", RestBusinessCode.KeyNotExistsInRedis.getValue()); + throw new ServiceRuntimeException("从" + redisDb + "号redisDB中获取" + key + "对应的值失败", + RestBusinessCode.KeyNotExistsInRedis.getValue()); } finally { returnResource(jedis); } @@ -154,7 +171,9 @@ public class JedisUtils { /** * 缓存是否存在 - * @param key 键 + * + * @param key + * 键 * @return */ public static boolean exists(String key, int redisDb) { @@ -165,7 +184,8 @@ public class JedisUtils { result = jedis.exists(key); logger.debug("exists {}", key); } catch (Exception e) { - throw new ServiceRuntimeException("从" + redisDb + "号redisDB中判断" + key + "是否存在失败", RestBusinessCode.ExistsKeyFailed.getValue()); + throw new ServiceRuntimeException("从" + redisDb + "号redisDB中判断" + key + "是否存在失败", + RestBusinessCode.ExistsKeyFailed.getValue()); } finally { returnResource(jedis); } @@ -174,7 +194,9 @@ public class JedisUtils { /** * 缓存是否存在 - * @param key 键 + * + * @param key + * 键 * @return */ public static boolean existsObject(String key, int redisDb) { @@ -185,7 +207,8 @@ public class JedisUtils { result = jedis.exists(getBytesKey(key)); logger.debug("existsObject {}", key); } catch (Exception e) { - throw new ServiceRuntimeException("从" + redisDb + "号redisDB中判断" + key + "是否存在失败", RestBusinessCode.ExistsKeyFailed.getValue()); + throw new ServiceRuntimeException("从" + redisDb + "号redisDB中判断" + key + "是否存在失败", + RestBusinessCode.ExistsKeyFailed.getValue()); } finally { returnResource(jedis); } @@ -194,20 +217,24 @@ public class JedisUtils { /** * 获取资源 + * * @return * @throws JedisException */ public static Jedis getResource(int redisDb) throws JedisException { Jedis jedis = null; if (jedisPool == null) { - throw new ServiceRuntimeException("redis连接池为空,请联系管理员检查程序",RestBusinessCode.CannotConnectionRedis.getValue()); + throw new ServiceRuntimeException("redis连接池为空,请联系管理员检查程序", + RestBusinessCode.CannotConnectionRedis.getValue()); } try { jedis = jedisPool.getResource(); jedis.select(redisDb); } catch (JedisException e) { returnBrokenResource(jedis); - throw new ServiceRuntimeException("获取redis连接失败,请联系管理员检查程序", RestBusinessCode.CannotConnectionRedis.getValue()); + logger.error("获取redis连接失败,异常信息:{}" ,ExceptionUtil.getExceptionMsg(e)); + throw new ServiceRuntimeException("获取redis连接失败,请联系管理员检查程序", + RestBusinessCode.CannotConnectionRedis.getValue()); } return jedis; @@ -215,6 +242,7 @@ public class JedisUtils { /** * 归还资源 + * * @param jedis * @param isBroken */ @@ -226,6 +254,7 @@ public class JedisUtils { /** * 释放资源 + * * @param jedis * @param isBroken */ @@ -237,6 +266,7 @@ public class JedisUtils { /** * 获取byte[]类型Key + * * @param key * @return */ @@ -250,6 +280,7 @@ public class JedisUtils { /** * Object转换byte[]类型 + * * @param key * @return */ @@ -259,6 +290,7 @@ public class JedisUtils { /** * byte[]型转换Object + * * @param key * @return */ diff --git a/src/main/java/com/nis/web/service/restful/CommRegionThreadMethod.java b/src/main/java/com/nis/web/service/restful/CommRegionThreadMethod.java new file mode 100644 index 0000000..8be6caa --- /dev/null +++ b/src/main/java/com/nis/web/service/restful/CommRegionThreadMethod.java @@ -0,0 +1,86 @@ +package com.nis.web.service.restful; + +import java.util.Date; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.nis.domain.MaatXmlExpr; + +import redis.clients.jedis.Pipeline; +import redis.clients.jedis.Transaction; + +public class CommRegionThreadMethod { + private static Logger logger = LoggerFactory.getLogger(CommRegionThreadMethod.class); + + protected void updateMaatInfoByPipelined(List list, String maatKey, Pipeline pipelined, + 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) { + zset = maatKey.replace("OBSOLETE_RULE:", "DEL,"); + } else { + zset = maatKey.replace("EFFECTIVE_RULE:", "ADD,"); + } + pipelined.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 + pipelined.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,得到当前时间的秒 + pipelined.zadd("MAAT_VERSION_TIMER", score, maatVersion + ""); + logger.info("向{}号redis数据库更新了MAAT_VERSION_TIMER,内容是{},SCORES是{}", redisDBIndex, maatVersion, score); + } + } + + } + + } + + protected void updateMaatInfo(List 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) { + 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); + } + } + } + } +} diff --git a/src/main/java/com/nis/web/service/restful/ConfigJedisServiceimpl.java b/src/main/java/com/nis/web/service/restful/ConfigJedisServiceimpl.java index e0520cf..0120a91 100644 --- a/src/main/java/com/nis/web/service/restful/ConfigJedisServiceimpl.java +++ b/src/main/java/com/nis/web/service/restful/ConfigJedisServiceimpl.java @@ -8,6 +8,10 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -21,12 +25,15 @@ 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.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.Pipeline; +import redis.clients.jedis.Response; import redis.clients.jedis.Transaction; import redis.clients.jedis.exceptions.JedisConnectionException; @@ -763,6 +770,12 @@ public class ConfigJedisServiceimpl implements ConfigRedisService { 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>> idMap, boolean isInvalid) { if (idMap != null && idMap.size() > 0) { @@ -1445,7 +1458,8 @@ public class ConfigJedisServiceimpl implements ConfigRedisService { } private void addTmpReion(List> regionMapList, MaatXmlConfig maatXmlConfig, - Transaction transaction, int type, int tmpStorageReuseRegionDB, int idRelaRedisDBIndex, Integer service1) { + Transaction transaction, int type, int tmpStorageReuseRegionDB, int idRelaRedisDBIndex ) throws Exception { + if (regionMapList != null && regionMapList.size() > 0) { for (Map map : regionMapList) { List expressionList = maatXmlConfig.getExpressionList(); @@ -1569,6 +1583,51 @@ public class ConfigJedisServiceimpl implements ConfigRedisService { } } } + + } + + private void addTmpReionByPipeLine(List> regionMapList, MaatXmlConfig maatXmlConfig, + Pipeline pipelined, int type, int tmpStorageReuseRegionDB, int idRelaRedisDBIndex) throws Exception { + if (regionMapList != null && regionMapList.size() > 0) { + int execNum = Constants.MAXTHREADNUM; + // 每10000条数据开启一条线程 + int threadSize = Constants.EVERTHREADNUM; + // 总数据条数 + int dataSize = regionMapList.size(); + // 线程数 + int countSize = dataSize / threadSize ; + // 定义标记,过滤threadNum为整数 + boolean special = dataSize % threadSize == 0; + if (!special && dataSize > threadSize) { + countSize++; + } + if (countSize < execNum) { + execNum = countSize; + } + // 创建一个线程池 + ExecutorService exec = Executors.newFixedThreadPool(execNum); + // 定义一个任务集合 + List> tasks = new ArrayList>(); + Callable task = null; + List> cutList = null; + // 确定每条线程的数据 + for (int i = 0; i < countSize; i++) { + cutList = regionMapList.subList(threadSize * i, + threadSize * (i + 1) > dataSize ? dataSize : threadSize * (i + 1)); + final List> listStr = cutList; + task = new SaveRegionThreadByPipeline(listStr, maatXmlConfig, pipelined, type, tmpStorageReuseRegionDB, + idRelaRedisDBIndex); + // 这里提交的任务容器列表和返回的Future列表存在顺序对应的关系 + tasks.add(task); + } + List> results; + results = exec.invokeAll(tasks); +// for (Future future : results) { +// logger.info("SaveRegionThread添加分组复用域配置线程执行结果:" + future.get()); +// } + // 关闭线程池 + exec.shutdown(); + } } /** @@ -1587,11 +1646,11 @@ public class ConfigJedisServiceimpl implements ConfigRedisService { for (MaatConfig maatConfig : configList) { MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(37);// maat类配置的表达式都一样,这里因为没有service所以就随便取了一个 addTmpReion(maatConfig.getIpRegionMapList(), maatXmlConfig, transaction, 12, - tmpStorageReuseRegionDB, idRelaRedisDBIndex, maatConfig.getService()); + tmpStorageReuseRegionDB, idRelaRedisDBIndex ); addTmpReion(maatConfig.getNumRegionMapList(), maatXmlConfig, transaction, 13, - tmpStorageReuseRegionDB, idRelaRedisDBIndex, maatConfig.getService()); + tmpStorageReuseRegionDB, idRelaRedisDBIndex ); addTmpReion(maatConfig.getStrRegionMapList(), maatXmlConfig, transaction, 14, - tmpStorageReuseRegionDB, idRelaRedisDBIndex, maatConfig.getService()); + tmpStorageReuseRegionDB, idRelaRedisDBIndex ); } } else { throw new ServiceRuntimeException("添加分组复用域配置时,未发现对应的配置信息,请检查配置参数是否正确", @@ -1614,6 +1673,56 @@ public class ConfigJedisServiceimpl implements ConfigRedisService { } } + public boolean saveGroupReuseConfigByPipeLine(List configList) { + Jedis resource = JedisUtils.getResource(0); + Pipeline pipelined = resource.pipelined(); + try { + if (configList != null && configList.size() > 0) { + int tmpStorageReuseRegionDB = Configurations.getIntProperty("tmpStorageReuseRegionDB", 15); + int idRelaRedisDBIndex = Configurations.getIntProperty("idRelaRedisDBIndex", 15); + pipelined.select(tmpStorageReuseRegionDB); + pipelined.multi(); + for (MaatConfig maatConfig : configList) { + MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(37);// maat类配置的表达式都一样,这里因为没有service所以就随便取了一个 + addTmpReionByPipeLine(maatConfig.getIpRegionMapList(), maatXmlConfig, pipelined, 12, + tmpStorageReuseRegionDB, idRelaRedisDBIndex); + addTmpReionByPipeLine(maatConfig.getNumRegionMapList(), maatXmlConfig, pipelined, 13, + tmpStorageReuseRegionDB, idRelaRedisDBIndex); + addTmpReionByPipeLine(maatConfig.getStrRegionMapList(), maatXmlConfig, pipelined, 14, + tmpStorageReuseRegionDB, idRelaRedisDBIndex); + } + } else { + throw new ServiceRuntimeException("添加分组复用域配置时,未发现对应的配置信息,请检查配置参数是否正确", + RestBusinessCode.ConfigSourceIsNull.getValue()); + } + Response> exec = pipelined.exec(); + pipelined.sync();// 关闭pipeline + // 结束pipeline,并开始从相应中获得数据 + // List responses = exec.get(); + // if (responses == null || responses.isEmpty()) { + // throw new RuntimeException("Pipeline error: no response..."); + // } + // for (Object resp : responses) { + // System.out.println("Response:" + resp.toString());// 注意,此处resp的类型为Long + // } + + return true; + } catch (JedisConnectionException e) { + String error = "连接redis异常,保存分组复用maat类域配置失败" + e.getMessage(); + logger.error(error); + throw new ServiceRuntimeException(error, RestBusinessCode.CannotConnectionRedis.getValue()); + } catch (Exception e) { + + String error = "保存分组复用maat类域配置发生了异常" + e.getMessage(); + logger.error(error); + // pipelined.discard(); + throw new ServiceRuntimeException(error, RestBusinessCode.SaveDataInError.getValue()); + } finally { + // 释放连接到连接池 + JedisUtils.returnResource(resource); + } + } + /** * 添加分组和域的关联关系 * @@ -1836,6 +1945,52 @@ public class ConfigJedisServiceimpl implements ConfigRedisService { } + private void removeReuseReionByPipelined(long groupId, List regionList, Pipeline pipelined, + MaatXmlConfig maatXmlConfig, int tmpStorageReuseRegionDB, int idRelaRedisDBIndex) throws Exception { + if (regionList != null && regionList.size() > 0) { + int execNum = Constants.MAXTHREADNUM; + // 每10000条数据开启一条线程 + int threadSize = Constants.EVERTHREADNUM; + // 总数据条数 + int dataSize = regionList.size(); + // 线程数 + int countSize = dataSize / threadSize + 1; + // 定义标记,过滤threadNum为整数 + boolean special = dataSize % threadSize == 0; + if (!special && dataSize > threadSize) { + countSize++; + } + if (countSize < execNum) { + execNum = countSize; + } + // 创建一个线程池 + ExecutorService exec = Executors.newFixedThreadPool(execNum); + // 定义一个任务集合 + List> tasks = new ArrayList>(); + Callable task = null; + List cutList = null; + // 确定每条线程的数据 + for (int i = 0; i < countSize; i++) { + cutList = regionList.subList(threadSize * i, + threadSize * (i + 1) > dataSize ? dataSize : threadSize * (i + 1)); + final List listStr = cutList; + task = new DelRegionThreadByPipeline(groupId, listStr, pipelined, maatXmlConfig, tmpStorageReuseRegionDB, + idRelaRedisDBIndex); + // 这里提交的任务容器列表和返回的Future列表存在顺序对应的关系 + tasks.add(task); + } + List> results; + results = exec.invokeAll(tasks); + for (Future future : results) { + logger.info("DelRegionThread删除分组复用域配置线程执行结果:" + future.get()); + } + // 关闭线程池 + exec.shutdown(); + + } + + } + @Override public boolean delGroupReuseConfig(Map> groupAndRegionMap) { Jedis resource = JedisUtils.getResource(0); @@ -1876,6 +2031,52 @@ public class ConfigJedisServiceimpl implements ConfigRedisService { } } + public boolean delGroupReuseConfigByPipeline(Map> groupAndRegionMap) { + Jedis resource = JedisUtils.getResource(0); + Pipeline pipelined = resource.pipelined(); + // Transaction transaction = resource.multi(); + try { + if (groupAndRegionMap != null && groupAndRegionMap.size() > 0) { + int tmpStorageReuseRegionDB = Configurations.getIntProperty("tmpStorageReuseRegionDB", 15); + int idRelaRedisDBIndex = Configurations.getIntProperty("idRelaRedisDBIndex", 15); + pipelined.select(tmpStorageReuseRegionDB); + MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(37);// maat类配置的表达式都一样,这里因为没有service所以就随便取了一个 + for (Long groupId : groupAndRegionMap.keySet()) { + List regionList = groupAndRegionMap.get(groupId); + if (regionList != null && regionList.size() > 0) { + removeReuseReionByPipelined(groupId, regionList, pipelined, maatXmlConfig, + tmpStorageReuseRegionDB, idRelaRedisDBIndex); + } else { + throw new ServiceRuntimeException("删除分组复用域配置时,参数都为空,请检查配置参数是否正确", + RestBusinessCode.ConfigSourceIsNull.getValue()); + } + } + Response> exec = pipelined.exec(); + pipelined.sync();// 关闭pipeline + return true; + } else { + throw new ServiceRuntimeException("单独删除分组复用配置的域配置时,参数为空,请检查", + RestBusinessCode.ConfigSourceIsNull.getValue()); + + } + + } catch (JedisConnectionException e) { + String error = "连接redis异常,删除分组复用maat类域配置失败" + e.getMessage(); + logger.error(error); + // pipelined.discard(); + throw new ServiceRuntimeException(error, RestBusinessCode.CannotConnectionRedis.getValue()); + } catch (Exception e) { + String error = "删除分组复用maat类域配置发生了异常" + e.getMessage(); + logger.error(error); + // pipelined.discard(); + throw new ServiceRuntimeException(error, RestBusinessCode.SaveDataInError.getValue()); + } finally { + + // 释放连接到连接池 + JedisUtils.returnResource(resource); + } + } + /** * 添加和删除时更新MAAT_*状态信息 * @@ -1917,9 +2118,7 @@ public class ConfigJedisServiceimpl implements ConfigRedisService { logger.info("向{}号redis数据库更新了MAAT_VERSION_TIMER,内容是{},SCORES是{}", redisDBIndex, maatVersion, score); } } - } - } } diff --git a/src/main/java/com/nis/web/service/restful/ConfigPzIdService.java b/src/main/java/com/nis/web/service/restful/ConfigPzIdService.java index 751b4c6..9e09f14 100644 --- a/src/main/java/com/nis/web/service/restful/ConfigPzIdService.java +++ b/src/main/java/com/nis/web/service/restful/ConfigPzIdService.java @@ -5,6 +5,7 @@ package com.nis.web.service.restful; import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -13,9 +14,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.nis.domain.restful.ConfigPzIdSource; -import com.zdjizhi.utils.StringUtil; import com.nis.web.dao.ConfigPzIdDao; import com.nis.web.service.BaseLogService; +import com.zdjizhi.utils.StringUtil; /** * @ClassName:ConfigPzIdService @@ -33,26 +34,45 @@ public class ConfigPzIdService extends BaseLogService { */ @Autowired protected ConfigPzIdDao dao; - + @Autowired ConfigRedisService configRedisService; - + private static Map sourceNameMap = new HashMap(); static { sourceNameMap.put("CONFIG_COMPILE", "SEQ_COMPILEID"); sourceNameMap.put("CONFIG_GROUP", "SEQ_GROUPID"); sourceNameMap.put("CONFIG_REGION", "SEQ_REGIONID"); } - - public ConfigPzIdSource getConfigPzIdList(ConfigPzIdSource entity){ + + public ConfigPzIdSource getConfigPzIdListOld(ConfigPzIdSource entity) { List pzIdList = new ArrayList(); entity.setSourceName(entity.getSourceName().toUpperCase()); - String seqName= StringUtil.isEmpty(sourceNameMap.get(entity.getSourceName()))?"SEQ_COMPILEID":sourceNameMap.get(entity.getSourceName()); + String seqName = StringUtil.isEmpty(sourceNameMap.get(entity.getSourceName())) ? "SEQ_COMPILEID" + : sourceNameMap.get(entity.getSourceName()); for (int i = 0; i < entity.getNum(); i++) { -// pzIdList.add(dao.getConfigPzIdList(entity)); //直接从数据库序列号获取 + // pzIdList.add(dao.getConfigPzIdList(entity)); //直接从数据库序列号获取 pzIdList.add(configRedisService.getIncrId(seqName)); } entity.setPzIdList(pzIdList); return entity; } + + public ConfigPzIdSource getConfigPzIdList(ConfigPzIdSource entity) { + List pzIdList = new LinkedList(); + entity.setSourceName(entity.getSourceName().toUpperCase()); + String seqName = StringUtil.isEmpty(sourceNameMap.get(entity.getSourceName())) ? "SEQ_COMPILEID" + : sourceNameMap.get(entity.getSourceName()); + long endNum = configRedisService.getIncrById(seqName, entity.getNum()); + + long startNum=endNum-entity.getNum(); + + for (long i = endNum; i > startNum; i--) { + // pzIdList.add(dao.getConfigPzIdList(entity)); //直接从数据库序列号获取 + pzIdList.add(i); + } + entity.setPzIdList(pzIdList); + return entity; + } + } diff --git a/src/main/java/com/nis/web/service/restful/ConfigRedisService.java b/src/main/java/com/nis/web/service/restful/ConfigRedisService.java index 65f6036..91d59f2 100644 --- a/src/main/java/com/nis/web/service/restful/ConfigRedisService.java +++ b/src/main/java/com/nis/web/service/restful/ConfigRedisService.java @@ -35,6 +35,12 @@ public interface ConfigRedisService { * @return 成功返回true,失败返回false或抛出异常 */ public boolean saveGroupReuseConfig( List configList); + /** + * 使用pipeline方式,分组复用域配置新增接口,value是配置集合 + * @param configList + * @return 成功返回true,失败返回false或抛出异常 + */ + public boolean saveGroupReuseConfigByPipeLine( List configList); /** * 获取指定key的自增长值 @@ -42,6 +48,13 @@ public interface ConfigRedisService { * @return */ public Long getIncrId(String key); + /** + * 批量获取接口,利用java进行遍历,比从redis中自增要快很多 + * @param key + * @param num + * @return + */ + public Long getIncrById(String key,int num); /** * 删除非maat类配置,第一个key是redisDBIndex,第二个key是业务类型,value是配置id集合 @@ -64,5 +77,11 @@ public interface ConfigRedisService { * @return */ public boolean delGroupReuseConfig( Map> reuseMap); + /** + * 使用pipeline方式删除分组复用的域配置 + * @param reuseMap key是groupId,value是"tableName,regionId" + * @return + */ + public boolean delGroupReuseConfigByPipeline( Map> reuseMap); } diff --git a/src/main/java/com/nis/web/service/restful/ConfigSourcesService.java b/src/main/java/com/nis/web/service/restful/ConfigSourcesService.java index 0122d54..6b66f77 100644 --- a/src/main/java/com/nis/web/service/restful/ConfigSourcesService.java +++ b/src/main/java/com/nis/web/service/restful/ConfigSourcesService.java @@ -11,10 +11,13 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; import net.sf.json.JSONObject; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -871,6 +874,7 @@ public class ConfigSourcesService extends BaseService { // 调用接口入redis logger.info("---------------调用Redis 分组复用配置新增接口---------------------"); configRedisService.saveGroupReuseConfig(list); +// configRedisService.saveGroupReuseConfigByPipeLine(list); } /** @@ -936,6 +940,7 @@ public class ConfigSourcesService extends BaseService { logger.info("调用接口删除Redis中分组复用的域配置接口"); // 所有的都删除成功返回true if (!configRedisService.delGroupReuseConfig(reuseMap)) { +// if (!configRedisService.delGroupReuseConfigByPipeline(reuseMap)) { throw new ServiceRuntimeException("删除分组利用域配置时出现异常,具体原因不详,请联系管理员", RestBusinessCode.service_runtime_error.getValue()); } diff --git a/src/main/java/com/nis/web/service/restful/DelRegionThreadByPipeline.java b/src/main/java/com/nis/web/service/restful/DelRegionThreadByPipeline.java new file mode 100644 index 0000000..f8d6688 --- /dev/null +++ b/src/main/java/com/nis/web/service/restful/DelRegionThreadByPipeline.java @@ -0,0 +1,126 @@ +package com.nis.web.service.restful; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.Callable; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.nis.domain.MaatXmlConfig; +import com.nis.restful.RestBusinessCode; +import com.nis.restful.ServiceRuntimeException; +import com.nis.util.JedisUtils; + +import redis.clients.jedis.Pipeline; + +public class DelRegionThreadByPipeline extends CommRegionThreadMethod implements Callable { + private static Logger logger = LoggerFactory.getLogger(DelRegionThreadByPipeline.class); + + long groupId; + List regionList; + Pipeline pipelined; + MaatXmlConfig maatXmlConfig; + int tmpStorageReuseRegionDB; + int idRelaRedisDBIndex; + + public DelRegionThreadByPipeline(long groupId, List regionList, Pipeline pipelined, MaatXmlConfig maatXmlConfig, + int tmpStorageReuseRegionDB, int idRelaRedisDBIndex) { + super(); + this.groupId = groupId; + this.regionList = regionList; + this.pipelined = pipelined; + this.maatXmlConfig = maatXmlConfig; + this.tmpStorageReuseRegionDB = tmpStorageReuseRegionDB; + this.idRelaRedisDBIndex = idRelaRedisDBIndex; + } + + private void removeReuseReionByPipelined(long groupId, List regionList, Pipeline pipelined, + MaatXmlConfig maatXmlConfig, int tmpStorageReuseRegionDB, int idRelaRedisDBIndex) { + if (regionList != null && regionList.size() > 0) { + pipelined.select(tmpStorageReuseRegionDB); + for (String tableAndId : regionList) { + String regionKey = "EFFECTIVE_RULE:" + tableAndId; + if (JedisUtils.exists(regionKey, tmpStorageReuseRegionDB)) { + pipelined.del(regionKey); + String groupStr = "GROUPREGION:" + groupId; + String groupCompileVal = JedisUtils.get(groupStr, idRelaRedisDBIndex); + StringBuffer newGroupRegion = new StringBuffer(); + + if (groupCompileVal != null && !groupCompileVal.trim().equals("")) { + Set redisDBSet = new HashSet(); + String[] split = groupCompileVal.split(";"); + if (split != null && split.length > 0) { + for (String compileStr : split) { + if (compileStr.split("-")[0].equals(regionKey)) { + String[] dbArr = compileStr.split("-")[1].split(","); + for (String db : dbArr) { + if (db != null && !db.trim().equals("")) { + redisDBSet.add(Integer.parseInt(db.trim())); + } + } + if (split.length == 1) { + pipelined.select(idRelaRedisDBIndex); + pipelined.del(groupStr); + } + } else { + newGroupRegion.append(compileStr + ";"); + } + } + + if (newGroupRegion.length() > 0) { + pipelined.select(idRelaRedisDBIndex); + pipelined.set(groupStr, newGroupRegion.substring(0, newGroupRegion.length() - 1)); + } + + } + for (Integer redisDb : redisDBSet) { + if (JedisUtils.exists(regionKey, redisDb)) { + pipelined.select(redisDb); + + String isValidKey = regionKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE"); + pipelined.rename(regionKey, isValidKey); + + String maatVersionStr = JedisUtils.get("MAAT_VERSION", redisDb); + if (maatVersionStr == null) { + maatVersionStr = "0"; + } + if (maatVersionStr != null) { + Double maatVersion = Double.valueOf(maatVersionStr) + 1D; + updateMaatInfoByPipelined(maatXmlConfig.getExpressionList(), isValidKey, pipelined, + maatVersion, redisDb, true); + pipelined.incrBy("MAAT_VERSION", 1l); + logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDb, + Integer.valueOf(maatVersionStr) + 1); + } + + } else { + throw new ServiceRuntimeException( + "从" + tmpStorageReuseRegionDB + "号redis库中删除分组复用域配置时,regionKey=" + regionKey + + "不存在,请检查配置参数是否正确,或redis数据是否正确", + RestBusinessCode.KeyNotExistsInRedis.getValue()); + } + } + } + + } else { + throw new ServiceRuntimeException("临时存放region的" + tmpStorageReuseRegionDB + "号redis库中regionKey=" + + regionKey + "不存在,请检查配置参数是否正确", RestBusinessCode.KeyNotExistsInRedis.getValue()); + } + } + } else { + throw new ServiceRuntimeException("删除分组复用域配置时,参数都为空,请检查配置参数是否正确", + RestBusinessCode.ConfigSourceIsNull.getValue()); + } + + } + + @Override + public Integer call() throws Exception { + removeReuseReionByPipelined(groupId, regionList, pipelined, maatXmlConfig, tmpStorageReuseRegionDB, + idRelaRedisDBIndex); + return 1; + } + +} diff --git a/src/main/java/com/nis/web/service/restful/SaveRegionThread.java b/src/main/java/com/nis/web/service/restful/SaveRegionThread.java new file mode 100644 index 0000000..ff654b3 --- /dev/null +++ b/src/main/java/com/nis/web/service/restful/SaveRegionThread.java @@ -0,0 +1,187 @@ +package com.nis.web.service.restful; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.Callable; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.StringUtils; + +import com.nis.domain.MaatXmlConfig; +import com.nis.domain.MaatXmlExpr; +import com.nis.restful.RestBusinessCode; +import com.nis.restful.ServiceRuntimeException; +import com.nis.util.JedisUtils; + +import redis.clients.jedis.Transaction; + +public class SaveRegionThread extends CommRegionThreadMethod implements Callable { + private static Logger logger = LoggerFactory.getLogger(SaveRegionThread.class); + + List> regionMapList; + MaatXmlConfig maatXmlConfig; + Transaction transaction; + int type; + int tmpStorageReuseRegionDB; + int idRelaRedisDBIndex; + + + + public SaveRegionThread(List> regionMapList, MaatXmlConfig maatXmlConfig, + Transaction transaction, int type, int tmpStorageReuseRegionDB, int idRelaRedisDBIndex) { + super(); + this.regionMapList = regionMapList; + this.maatXmlConfig = maatXmlConfig; + this.transaction = transaction; + this.type = type; + this.tmpStorageReuseRegionDB = tmpStorageReuseRegionDB; + this.idRelaRedisDBIndex = idRelaRedisDBIndex; + } + + private void addTmpReion(List> regionMapList, MaatXmlConfig maatXmlConfig, + Transaction transaction, int type, int tmpStorageReuseRegionDB, int idRelaRedisDBIndex) { + if (regionMapList != null && regionMapList.size() > 0) { + for (Map map : regionMapList) { + List 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("]", ""); + 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 (service != null && service.intValue() == 1028 + // && valStr.toLowerCase().equals("op_time") && type == 12) { + 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) { + 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()); + } + } + transaction.select(tmpStorageReuseRegionDB); + maatKey = keyBF.toString(); + transaction.set(maatKey.toUpperCase(), valBF.toString()); + logger.info("向{}号redis数据库添加了一条配置,key是{},value是{}", tmpStorageReuseRegionDB, + maatKey.toUpperCase(), valBF.toString()); + + String groupIdStr = "GROUPCOMPILE:" + groupId; + String groupCompileVal = JedisUtils.get(groupIdStr, idRelaRedisDBIndex); + if (groupCompileVal != null && !groupCompileVal.trim().equals("")) { + Set redisDBSet = new HashSet(); + String[] split = groupCompileVal.split(";"); + if (split != null && split.length > 0) { + for (String compileStr : split) { + String[] dbArr = compileStr.split("-")[1].split(","); + for (String db : dbArr) { + if (db != null && !db.trim().equals("")) { + redisDBSet.add(Integer.parseInt(db.trim())); + } + } + } + } + String groupRegionStr = "GROUPREGION:" + groupId; + String regionVal = JedisUtils.get(groupIdStr, idRelaRedisDBIndex); + if (regionVal != null && !regionVal.trim().equals("")) { + transaction.append(groupRegionStr, ";" + maatKey.toUpperCase() + "-" + + redisDBSet.toString().replace("[", "").replace("]", "")); + } else { + transaction.set(groupRegionStr, maatKey.toUpperCase() + "-" + + redisDBSet.toString().replace("[", "").replace("]", "")); + } + for (Integer redisDb : redisDBSet) { + transaction.select(redisDb); + transaction.set(maatKey.toUpperCase(), valBF.toString()); + logger.info("向{}号redis数据库添加了一条配置,key是{},value是{}", redisDb, maatKey.toUpperCase(), + valBF.toString()); + String maatVersionStr = JedisUtils.get("MAAT_VERSION", redisDb); + if (maatVersionStr == null) { + maatVersionStr = "0"; + } + if (maatVersionStr != null) { + Double maatVersion = Double.valueOf(maatVersionStr) + 1D; + updateMaatInfo(expressionList, maatKey, transaction, maatVersion, redisDb, false); + transaction.incrBy("MAAT_VERSION", 1l); + logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDb, + Integer.valueOf(maatVersionStr) + 1); + } + } + } + } + } + } + } + } + + // @Override + // public void run() { + // addTmpReionByPipeLine(regionMapList, maatXmlConfig, pipelined, type, + // tmpStorageReuseRegionDB, + // idRelaRedisDBIndex); + // + // } + + @Override + public Integer call() throws Exception { + addTmpReion( regionMapList, maatXmlConfig, + transaction, type, tmpStorageReuseRegionDB, idRelaRedisDBIndex); + + return 1; + } + +} diff --git a/src/main/java/com/nis/web/service/restful/SaveRegionThreadByPipeline.java b/src/main/java/com/nis/web/service/restful/SaveRegionThreadByPipeline.java new file mode 100644 index 0000000..a3b09ac --- /dev/null +++ b/src/main/java/com/nis/web/service/restful/SaveRegionThreadByPipeline.java @@ -0,0 +1,186 @@ +package com.nis.web.service.restful; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.Callable; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.StringUtils; + +import com.nis.domain.MaatXmlConfig; +import com.nis.domain.MaatXmlExpr; +import com.nis.restful.RestBusinessCode; +import com.nis.restful.ServiceRuntimeException; +import com.nis.util.JedisUtils; + +import redis.clients.jedis.Pipeline; + +public class SaveRegionThreadByPipeline extends CommRegionThreadMethod implements Callable{ + private static Logger logger = LoggerFactory.getLogger(SaveRegionThreadByPipeline.class); + + List> regionMapList; + MaatXmlConfig maatXmlConfig; + Pipeline pipelined; + int type; + int tmpStorageReuseRegionDB; + int idRelaRedisDBIndex; + + public SaveRegionThreadByPipeline(List> regionMapList, MaatXmlConfig maatXmlConfig, + Pipeline pipelined, int type, int tmpStorageReuseRegionDB, int idRelaRedisDBIndex) { + super(); + this.regionMapList = regionMapList; + this.maatXmlConfig = maatXmlConfig; + this.pipelined = pipelined; + this.type = type; + this.tmpStorageReuseRegionDB = tmpStorageReuseRegionDB; + this.idRelaRedisDBIndex = idRelaRedisDBIndex; + } + + private void addTmpReionByPipeLine(List> regionMapList, MaatXmlConfig maatXmlConfig, + Pipeline pipelined, int type, int tmpStorageReuseRegionDB, int idRelaRedisDBIndex) { + if (regionMapList != null && regionMapList.size() > 0) { + for (Map map : regionMapList) { + List 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("]", ""); + 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 (service != null && service.intValue() == 1028 + // && valStr.toLowerCase().equals("op_time") && type == 12) { + 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) { + 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()); + } + } + pipelined.select(tmpStorageReuseRegionDB); + maatKey = keyBF.toString(); + pipelined.set(maatKey.toUpperCase(), valBF.toString()); + logger.info("向{}号redis数据库添加了一条配置,key是{},value是{}", tmpStorageReuseRegionDB, + maatKey.toUpperCase(), valBF.toString()); + + String groupIdStr = "GROUPCOMPILE:" + groupId; + String groupCompileVal = JedisUtils.get(groupIdStr, idRelaRedisDBIndex); + if (groupCompileVal != null && !groupCompileVal.trim().equals("")) { + Set redisDBSet = new HashSet(); + String[] split = groupCompileVal.split(";"); + if (split != null && split.length > 0) { + for (String compileStr : split) { + String[] dbArr = compileStr.split("-")[1].split(","); + for (String db : dbArr) { + if (db != null && !db.trim().equals("")) { + redisDBSet.add(Integer.parseInt(db.trim())); + } + } + } + } + String groupRegionStr = "GROUPREGION:" + groupId; + String regionVal = JedisUtils.get(groupIdStr, idRelaRedisDBIndex); + if (regionVal != null && !regionVal.trim().equals("")) { + pipelined.append(groupRegionStr, ";" + maatKey.toUpperCase() + "-" + + redisDBSet.toString().replace("[", "").replace("]", "")); + } else { + pipelined.set(groupRegionStr, maatKey.toUpperCase() + "-" + + redisDBSet.toString().replace("[", "").replace("]", "")); + } + for (Integer redisDb : redisDBSet) { + pipelined.select(redisDb); + pipelined.set(maatKey.toUpperCase(), valBF.toString()); + logger.info("向{}号redis数据库添加了一条配置,key是{},value是{}", redisDb, maatKey.toUpperCase(), + valBF.toString()); + String maatVersionStr = JedisUtils.get("MAAT_VERSION", redisDb); + if (maatVersionStr == null) { + maatVersionStr = "0"; + } + if (maatVersionStr != null) { + Double maatVersion = Double.valueOf(maatVersionStr) + 1D; + updateMaatInfoByPipelined(expressionList, maatKey, pipelined, maatVersion, redisDb, + false); + pipelined.incrBy("MAAT_VERSION", 1l); + logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDb, + Integer.valueOf(maatVersionStr) + 1); + } + } + } + } + } + } + } + } + + + +// @Override +// public void run() { +// addTmpReionByPipeLine(regionMapList, maatXmlConfig, pipelined, type, tmpStorageReuseRegionDB, +// idRelaRedisDBIndex); +// +// } + + @Override + public Integer call() throws Exception { + addTmpReionByPipeLine(regionMapList, maatXmlConfig, pipelined, type, tmpStorageReuseRegionDB, + idRelaRedisDBIndex); + return 1; + } + +} diff --git a/src/main/resources/applicationContext-redis.xml b/src/main/resources/applicationContext-redis.xml index 0644880..cecbaec 100644 --- a/src/main/resources/applicationContext-redis.xml +++ b/src/main/resources/applicationContext-redis.xml @@ -1,11 +1,11 @@  + ignore-unresolvable="true" location="classpath:jdbc.properties" /> @@ -24,13 +24,18 @@ + + type="int" /> + + + diff --git a/src/main/resources/jdbc.properties b/src/main/resources/jdbc.properties index b57d605..da61333 100644 --- a/src/main/resources/jdbc.properties +++ b/src/main/resources/jdbc.properties @@ -5,9 +5,9 @@ jdbc.log.driver=com.mysql.jdbc.Driver #华严 #jdbc.log.url=jdbc:mysql://192.168.11.242:3306/galaxy?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull #元辰鑫外网-开发环境 -jdbc.log.url=jdbc:mysql://192.168.10.204:3306/galaxy-dev?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull +#jdbc.log.url=jdbc:mysql://192.168.10.204:3306/galaxy-dev?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull #元辰鑫外网 -#jdbc.log.url=jdbc:mysql://192.168.10.204:3306/galaxy?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull +jdbc.log.url=jdbc:mysql://192.168.10.204:3306/galaxy?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull #亦庄演示环境 #jdbc.log.url=jdbc:mysql://10.3.48.5:3306/galaxy?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull jdbc.log.username=root @@ -145,6 +145,8 @@ druid.log.filters=stat ######################################################################### ##minio地址 ######################################################################### +##亦庄测试地址,注意前面的http://不要忘记写 +#minio_url=http://10.3.48.2:9001 ##华严minio的地址,注意前面的http://不要忘记写 #minio_url=http://192.168.10.61:9000 ##元辰鑫外网minio的地址,注意前面的http://不要忘记写 @@ -175,10 +177,15 @@ redis.host=192.168.10.12 #redis.host=10.3.34.1 redis.port=6379 redis.pass= -redis.maxIdle=5 +#最大空闲连接数 +redis.maxIdle=50 +#最大连接数 redis.maxTotal=250 +#获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1 redis.maxWaitMillis=100000 +#在获取连接的时候检查有效性 redis.testOnBorrow=true +#在归还连接的时候检查有效性 redis.testOnReturn=true #客户端超时时间单位是毫秒 redis.timeout=100000 diff --git a/src/main/resources/maatXml/applicationConfig-maatRedis.xml b/src/main/resources/maatXml/applicationConfig-maatRedis.xml index 9dee8e3..77f463d 100644 --- a/src/main/resources/maatXml/applicationConfig-maatRedis.xml +++ b/src/main/resources/maatXml/applicationConfig-maatRedis.xml @@ -161,7 +161,7 @@ EFFECTIVE_RULE;:;{un_maat_table_name};,;[cfg_id] - [cfg_id];\t;[service];\t;[keyring_name];\t;[keyring_type];\t;[private_key_file];\t;[public_key_file];\t;[expire_after];\t;[public_key_algo];\t;[crl];\t;[is_valid];\t;[redisFile_private_key_file];\t;[redisFile_public_key_file];\t;[op_time];&nbsp;0;\n + [cfg_id];\t;[service];\t;[keyring_name];\t;[keyring_type];\t;[private_key_file];\t;[public_key_file];\t;[expire_after];\t;[public_key_algo];\t;[crl];\t;[is_valid];\t;[redisFile_private_key_file];\t;[redisFile_public_key_file];\t;[op_time];&nbsp;0 1 @@ -181,7 +181,7 @@ EFFECTIVE_RULE;:;{un_maat_table_name};,;[cfg_id] - [cfg_id];\t;[service];\t;[file_desc];\t;[content_type];\t;[content_length];\t;[file_path];\t;[is_valid];\t;[redisFile_file_path];\t;[op_time];&nbsp;0;\n + [cfg_id];\t;[service];\t;[file_desc];\t;[content_type];\t;[content_length];\t;[file_path];\t;[is_valid];\t;[redisFile_file_path];\t;[op_time];&nbsp;0 1 @@ -203,7 +203,7 @@ EFFECTIVE_RULE;:;{un_maat_table_name};,;[cfg_id] - [cfg_id];\t;[cert_name];\t;[cert_file];\t;[is_valid];\t;[redisFile_private_cert_file];\t;[service];\t;[op_time];&nbsp;0;\n + [cfg_id];\t;[cert_name];\t;[redisFile_private_cert_file];\t;[is_valid];\t;[service];\t;[op_time];&nbsp;0 1 @@ -224,7 +224,7 @@ EFFECTIVE_RULE;:;{un_maat_table_name};,;[cfg_id] - [cfg_id];\t;[cert_id];\t;[crl_file];\t;[is_valid];\t;[redisFile_private_crl_file];\t;[service];\t;[op_time];&nbsp;0;\n + [cfg_id];\t;[cert_id];\t;[redisFile_private_crl_file];\t;[is_valid];\t;[service];\t;[op_time];&nbsp;0 1 diff --git a/src/main/resources/nis.properties b/src/main/resources/nis.properties index bc848d8..b240936 100644 --- a/src/main/resources/nis.properties +++ b/src/main/resources/nis.properties @@ -213,3 +213,8 @@ fileProtocol=redis:// #是否开启日志查询count和last功能 isOpenLogCountAndLast=true +#定义单独添加域与删除域开启的最大线程数 +maxThreadNum=20 +#每个线程处理多少条数据 +everThreadNum=500 +