diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/CacheController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/CacheController.java deleted file mode 100644 index fa9c623f2..000000000 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/CacheController.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.ruoyi.web.controller.monitor; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.RedisCallback; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import com.ruoyi.common.core.domain.AjaxResult; -import com.ruoyi.common.utils.StringUtils; - -/** - * 缓存监控 - * - * @author ruoyi - */ -@RestController -@RequestMapping("/monitor/cache") -public class CacheController -{ - @Autowired - private RedisTemplate redisTemplate; - - @PreAuthorize("@ss.hasPermi('monitor:cache:list')") - @GetMapping() - public AjaxResult getInfo() throws Exception - { - Properties info = (Properties) redisTemplate.execute((RedisCallback) connection -> connection.info()); - Properties commandStats = (Properties) redisTemplate.execute((RedisCallback) connection -> connection.info("commandstats")); - Object dbSize = redisTemplate.execute((RedisCallback) connection -> connection.dbSize()); - - Map result = new HashMap<>(3); - result.put("info", info); - result.put("dbSize", dbSize); - - List> pieList = new ArrayList<>(); - commandStats.stringPropertyNames().forEach(key -> { - Map data = new HashMap<>(2); - String property = commandStats.getProperty(key); - data.put("name", StringUtils.removeStart(key, "cmdstat_")); - data.put("value", StringUtils.substringBetween(property, "calls=", ",usec")); - pieList.add(data); - }); - result.put("commandStats", pieList); - return AjaxResult.success(result); - } -} diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/RedisController.http b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/RedisController.http new file mode 100644 index 000000000..ce46e5e7e --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/RedisController.http @@ -0,0 +1,3 @@ +### 请求 /get-permission-info 接口 => 成功 +GET {{baseUrl}}/infra/redis/get-monitor-info +Authorization: Bearer {{token}} diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/RedisController.java b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/RedisController.java new file mode 100644 index 000000000..63f39cf7a --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/RedisController.java @@ -0,0 +1,49 @@ +package cn.iocoder.dashboard.modules.infra.controller.redis; + +import cn.hutool.core.util.StrUtil; +import cn.iocoder.dashboard.common.pojo.CommonResult; +import cn.iocoder.dashboard.modules.infra.controller.redis.vo.InfRedisMonitorRespVO; +import org.springframework.data.redis.connection.RedisServerCommands; +import org.springframework.data.redis.core.RedisCallback; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.Properties; + +import static cn.iocoder.dashboard.common.pojo.CommonResult.success; + +@RestController +@RequestMapping("/infra/redis") +public class RedisController { + + @Resource + private StringRedisTemplate stringRedisTemplate; + + @PreAuthorize("@ss.hasPermission('monitor:cache:list')") + @GetMapping("/get-monitor-info") + public CommonResult getRedisMonitorInfo() { + // 获得 Redis 统计信息 + Properties info = stringRedisTemplate.execute((RedisCallback) RedisServerCommands::info); + Long dbSize = stringRedisTemplate.execute(RedisServerCommands::dbSize); + Properties commandStats = stringRedisTemplate.execute((RedisCallback) connection -> connection.info("commandstats")); + assert commandStats != null; // 断言,避免警告 + + // 拼接结果返回 + InfRedisMonitorRespVO respVO = InfRedisMonitorRespVO.builder().info(info).dbSize(dbSize) + .commandStats(new ArrayList<>(commandStats.size())).build(); + commandStats.forEach((key, value) -> { + respVO.getCommandStats().add(InfRedisMonitorRespVO.CommandStat.builder() + .command(StrUtil.subAfter((String) key, "cmdstat_", false)) + .calls(Integer.valueOf(StrUtil.subBetween((String) value, "calls=", ","))) + .usec(Integer.valueOf(StrUtil.subBetween((String) value, "usec=", ","))) + .build()); + }); + return success(respVO); + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/vo/InfRedisMonitorRespVO.java b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/vo/InfRedisMonitorRespVO.java new file mode 100644 index 000000000..ea9517e43 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/vo/InfRedisMonitorRespVO.java @@ -0,0 +1,34 @@ +package cn.iocoder.dashboard.modules.infra.controller.redis.vo; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; + +import java.util.List; +import java.util.Properties; + +@Data +@Builder +@AllArgsConstructor +public class InfRedisMonitorRespVO { + + private Properties info; + + private Long dbSize; + + private List commandStats; + + @Data + @Builder + @AllArgsConstructor + public static class CommandStat { + + private String command; + + private Integer calls; + + private Integer usec; + + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/util/json/JsonUtils.java b/src/main/java/cn/iocoder/dashboard/util/json/JsonUtils.java index d1ef1cda3..f6727459c 100644 --- a/src/main/java/cn/iocoder/dashboard/util/json/JsonUtils.java +++ b/src/main/java/cn/iocoder/dashboard/util/json/JsonUtils.java @@ -1,5 +1,7 @@ package cn.iocoder.dashboard.util.json; +import cn.hutool.core.util.ArrayUtil; +import cn.hutool.core.util.StrUtil; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; @@ -36,6 +38,9 @@ public class JsonUtils { } public static T parseObject(String text, Class clazz) { + if (StrUtil.isEmpty(text)) { + return null; + } try { return objectMapper.readValue(text, clazz); } catch (IOException e) { @@ -44,6 +49,9 @@ public class JsonUtils { } public static T parseObject(byte[] bytes, Class clazz) { + if (ArrayUtil.isEmpty(bytes)) { + return null; + } try { return objectMapper.readValue(bytes, clazz); } catch (IOException e) {