From b9d4c38e55fe97d13b90ead01d4183c6c9be783f Mon Sep 17 00:00:00 2001 From: "castle.zang" Date: Tue, 8 Mar 2022 14:16:22 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96redis=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/base/RedisInfoController.java | 284 +++++++++++++++++++++ 1 file changed, 284 insertions(+) create mode 100644 modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/controller/base/RedisInfoController.java diff --git a/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/controller/base/RedisInfoController.java b/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/controller/base/RedisInfoController.java new file mode 100644 index 0000000..f996fd5 --- /dev/null +++ b/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/controller/base/RedisInfoController.java @@ -0,0 +1,284 @@ +package cn.estsh.i3plus.core.apiservice.controller.base; + +import cn.estsh.i3plus.platform.common.util.CommonConstWords; +import cn.estsh.i3plus.platform.common.util.PlatformConstWords; +import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil; +import cn.estsh.impp.framework.boot.auth.AuthUtil; +import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder; +import cn.estsh.impp.framework.boot.util.ImppRedis; +import cn.estsh.impp.framework.boot.util.ResultBean; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; +import org.apache.logging.log4j.util.Strings; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.util.*; + +/** + * @Description :获取Redis信息展示,类似RDM + * @Reference : + * @Author : Castle + * @CreateDate : 2022/2/24 9:55 + * @Modify: + **/ +@Slf4j +@Api(tags = "redis信息") +@RestController +@RequestMapping(PlatformConstWords.BASE_URL + "/redis") +public class RedisInfoController { + /** + * redis db0 resource + */ + @Resource(name = CommonConstWords.IMPP_REDIS_RES) + private ImppRedis redisRes; + + /** + * redis db1 sessionDb + * + * @return + */ + @Resource(name = CommonConstWords.IMPP_REDIS_SESSION) + private ImppRedis redisSession; + + + /** + * redis db2 coreDb + */ + @Resource(name = CommonConstWords.IMPP_REDIS_CORE) + private ImppRedis redisCore; + + /** + * redis db3 redisWms + */ + @Resource(name = CommonConstWords.IMPP_REDIS_WMS) + private ImppRedis redisWms; + + /** + * redis db4 redisMes + */ + @Resource(name = CommonConstWords.IMPP_REDIS_MES) + private ImppRedis redisMes; + /** + * redis db5 redisMesPcn + */ + @Resource(name = CommonConstWords.IMPP_REDIS_MES_PCN) + private ImppRedis redisMesPcn; + + /** + * redis db6 redisMes + */ + @Resource(name = CommonConstWords.IMPP_REDIS_SWEB) + private ImppRedis redisSweb; + + /** + * redis db7 redisAndon + */ + @Resource(name = CommonConstWords.IMPP_REDIS_ANDON) + private ImppRedis redisAndon; + + /** + * redis db8 lacDb + */ + + @Resource(name = CommonConstWords.IMPP_REDIS_LAC) + private ImppRedis redisLac; + + /** + * redis db9 blockDb; + */ + + @Resource(name = CommonConstWords.IMPP_REDIS_BLOCK) + private ImppRedis redisBlock; + + /** + * redis db10 ptl + */ + + @Resource(name = CommonConstWords.IMPP_REDIS_PTL) + private ImppRedis redisPtl; + + /** + * redis db11 ptl_pcn + */ + + @Resource(name = CommonConstWords.IMPP_REDIS_PTL_PCN) + private ImppRedis redisPtlPcn; + + /** + * redis db12 mdm + */ + + @Resource(name = CommonConstWords.IMPP_REDIS_MDM) + private ImppRedis redisMdm; + /** + * redis db13 eam + */ + + @Resource(name = CommonConstWords.IMPP_REDIS_EAM) + private ImppRedis redisEam; + + /** + * redis db15 qms + */ + + @Resource(name = CommonConstWords.IMPP_REDIS_QMS) + private ImppRedis redisQms; + + + @ApiOperation(value = "获取redis的keyspace信息", notes = "获取redis的keyspace信息") + @GetMapping("/info") + public ResultBean getInfo() { + Map keys = new HashMap<>(16); + //获取所有的数据库信息 + Properties infos = redisRes.getRedisConnectionInfo("keyspace"); + //分解infos信息 + for (int i = 0; i < 16; i++) { + // info信息db0=keys=19510,expires=0,avg_ttl=0, + String key = "db" + i; + String info = infos.getProperty(key); + if (Strings.isNotBlank(info)) { + JSONObject infoJson = JSON.parseObject(("{" + info + "}").replaceAll("=", ":")); + keys.put(key, Integer.parseInt(infoJson.get("keys").toString())); + } else { + keys.put(key, 0); + } + } + return ResultBean.success("查询成功").setResultMap(keys); + } + + @ApiOperation(value = "获取指定数据库的所有keys", notes = "获取指定数据库的所有keys") + @GetMapping("/db/{id}") + public ResultBean getDbKeys(@PathVariable("id") Integer id) { + //根据id获取所有的key + ImppRedis redis = getRedis(id); + Set keySet = redis.findKeySet("*"); + return ResultBean.success("查询成功").setResultList(new ArrayList<>(keySet)); + } + + @ApiOperation(value = "根据key获取redis对应的值", notes = "根据key获取redis对应的值") + @GetMapping("/get/{id}/{key}") + public ResultBean getValue(@PathVariable("id") Integer id, @PathVariable("key") String key) { + ImppRedis redis = getRedis(id); + try { + key = URLDecoder.decode(key, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw ImppExceptionBuilder.newInstance().setErrorDetail("redisDb:{},key:{}解码失败", id, key).build(); + } + String keyType = redis.getKeyType(key).toLowerCase(); + Object result; + switch (keyType) { + case "string": + result = redis.getObject(key); + break; + case "hash": + result = redis.getHashMap(key); + break; + case "list": + result = redis.getList(key, 0, -1); + break; + case "set": + result = redis.getSet(key); + break; + case "zset": + result = null; + break; + default: + throw ImppExceptionBuilder.newInstance().setErrorDetail("无效的key类型,请检查传入的key").build(); + } + return ResultBean.success("查询成功").setResultObject(result); + } + + @ApiOperation(value = "删除对应key的值", notes = "删除对应key的值") + @DeleteMapping("/del/{id}/{key}") + public ResultBean deleteKey(@PathVariable("id") Integer id, @PathVariable("key") String key) { + ImppRedis redis = getRedis(id); + try { + key = URLDecoder.decode(key, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw ImppExceptionBuilder.newInstance().setErrorDetail("redisDb:{},key:{}解码失败", id, key).build(); + } + String keyType = redis.getKeyType(key).toLowerCase(); + switch (keyType) { + case "string": + redis.deleteKey(key); + break; + case "hash": + redis.deleteHash(key); + break; + case "list": + redis.removeList(key,0,null); + break; + default: + throw ImppExceptionBuilder.newInstance().setErrorDetail("暂不支持的key类型").build(); + } + + log.info("sysUser:{}, delete key:{}", AuthUtil.getSessionUser().getUserName(), key); + return ResultBean.success("删除成功"); + } + + /** + * 根据DB选取不同的redis链接实例 + * + * @param indexDb + * @return + */ + private ImppRedis getRedis(Integer indexDb) { + ImppRedis redis; + switch (indexDb) { + case 0: + redis = redisRes; + break; + case 1: + redis = redisSession; + break; + case 2: + redis = redisCore; + break; + case 3: + redis = redisWms; + break; + case 4: + redis = redisMes; + break; + case 5: + redis = redisMesPcn; + break; + case 6: + redis = redisSweb; + break; + case 7: + redis = redisAndon; + break; + case 8: + redis = redisLac; + break; + case 9: + redis = redisBlock; + break; + case 10: + redis = redisPtl; + break; + case 11: + redis = redisPtlPcn; + break; + case 12: + redis = redisMdm; + break; + case 13: + redis = redisEam; + break; + case 15: + redis = redisQms; + break; + default: + redis = redisRes; + } + return redis; + } +}