Commit a7d0f30853c6aac96d039810acd2bef75126ff5c

Authored by YunaiV
1 parent f370fcd3

迁移 Redis 监控接口

ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/CacheController.java deleted
1   -package com.ruoyi.web.controller.monitor;
2   -
3   -import java.util.ArrayList;
4   -import java.util.HashMap;
5   -import java.util.List;
6   -import java.util.Map;
7   -import java.util.Properties;
8   -import org.springframework.beans.factory.annotation.Autowired;
9   -import org.springframework.data.redis.core.RedisCallback;
10   -import org.springframework.data.redis.core.RedisTemplate;
11   -import org.springframework.security.access.prepost.PreAuthorize;
12   -import org.springframework.web.bind.annotation.GetMapping;
13   -import org.springframework.web.bind.annotation.RequestMapping;
14   -import org.springframework.web.bind.annotation.RestController;
15   -import com.ruoyi.common.core.domain.AjaxResult;
16   -import com.ruoyi.common.utils.StringUtils;
17   -
18   -/**
19   - * 缓存监控
20   - *
21   - * @author ruoyi
22   - */
23   -@RestController
24   -@RequestMapping("/monitor/cache")
25   -public class CacheController
26   -{
27   - @Autowired
28   - private RedisTemplate<String, String> redisTemplate;
29   -
30   - @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
31   - @GetMapping()
32   - public AjaxResult getInfo() throws Exception
33   - {
34   - Properties info = (Properties) redisTemplate.execute((RedisCallback<Object>) connection -> connection.info());
35   - Properties commandStats = (Properties) redisTemplate.execute((RedisCallback<Object>) connection -> connection.info("commandstats"));
36   - Object dbSize = redisTemplate.execute((RedisCallback<Object>) connection -> connection.dbSize());
37   -
38   - Map<String, Object> result = new HashMap<>(3);
39   - result.put("info", info);
40   - result.put("dbSize", dbSize);
41   -
42   - List<Map<String, String>> pieList = new ArrayList<>();
43   - commandStats.stringPropertyNames().forEach(key -> {
44   - Map<String, String> data = new HashMap<>(2);
45   - String property = commandStats.getProperty(key);
46   - data.put("name", StringUtils.removeStart(key, "cmdstat_"));
47   - data.put("value", StringUtils.substringBetween(property, "calls=", ",usec"));
48   - pieList.add(data);
49   - });
50   - result.put("commandStats", pieList);
51   - return AjaxResult.success(result);
52   - }
53   -}
src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/RedisController.http 0 → 100644
  1 +### 请求 /get-permission-info 接口 => 成功
  2 +GET {{baseUrl}}/infra/redis/get-monitor-info
  3 +Authorization: Bearer {{token}}
... ...
src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/RedisController.java 0 → 100644
  1 +package cn.iocoder.dashboard.modules.infra.controller.redis;
  2 +
  3 +import cn.hutool.core.util.StrUtil;
  4 +import cn.iocoder.dashboard.common.pojo.CommonResult;
  5 +import cn.iocoder.dashboard.modules.infra.controller.redis.vo.InfRedisMonitorRespVO;
  6 +import org.springframework.data.redis.connection.RedisServerCommands;
  7 +import org.springframework.data.redis.core.RedisCallback;
  8 +import org.springframework.data.redis.core.StringRedisTemplate;
  9 +import org.springframework.security.access.prepost.PreAuthorize;
  10 +import org.springframework.web.bind.annotation.GetMapping;
  11 +import org.springframework.web.bind.annotation.RequestMapping;
  12 +import org.springframework.web.bind.annotation.RestController;
  13 +
  14 +import javax.annotation.Resource;
  15 +import java.util.ArrayList;
  16 +import java.util.Properties;
  17 +
  18 +import static cn.iocoder.dashboard.common.pojo.CommonResult.success;
  19 +
  20 +@RestController
  21 +@RequestMapping("/infra/redis")
  22 +public class RedisController {
  23 +
  24 + @Resource
  25 + private StringRedisTemplate stringRedisTemplate;
  26 +
  27 + @PreAuthorize("@ss.hasPermission('monitor:cache:list')")
  28 + @GetMapping("/get-monitor-info")
  29 + public CommonResult<InfRedisMonitorRespVO> getRedisMonitorInfo() {
  30 + // 获得 Redis 统计信息
  31 + Properties info = stringRedisTemplate.execute((RedisCallback<Properties>) RedisServerCommands::info);
  32 + Long dbSize = stringRedisTemplate.execute(RedisServerCommands::dbSize);
  33 + Properties commandStats = stringRedisTemplate.execute((RedisCallback<Properties>) connection -> connection.info("commandstats"));
  34 + assert commandStats != null; // 断言,避免警告
  35 +
  36 + // 拼接结果返回
  37 + InfRedisMonitorRespVO respVO = InfRedisMonitorRespVO.builder().info(info).dbSize(dbSize)
  38 + .commandStats(new ArrayList<>(commandStats.size())).build();
  39 + commandStats.forEach((key, value) -> {
  40 + respVO.getCommandStats().add(InfRedisMonitorRespVO.CommandStat.builder()
  41 + .command(StrUtil.subAfter((String) key, "cmdstat_", false))
  42 + .calls(Integer.valueOf(StrUtil.subBetween((String) value, "calls=", ",")))
  43 + .usec(Integer.valueOf(StrUtil.subBetween((String) value, "usec=", ",")))
  44 + .build());
  45 + });
  46 + return success(respVO);
  47 + }
  48 +
  49 +}
... ...
src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/vo/InfRedisMonitorRespVO.java 0 → 100644
  1 +package cn.iocoder.dashboard.modules.infra.controller.redis.vo;
  2 +
  3 +import lombok.AllArgsConstructor;
  4 +import lombok.Builder;
  5 +import lombok.Data;
  6 +
  7 +import java.util.List;
  8 +import java.util.Properties;
  9 +
  10 +@Data
  11 +@Builder
  12 +@AllArgsConstructor
  13 +public class InfRedisMonitorRespVO {
  14 +
  15 + private Properties info;
  16 +
  17 + private Long dbSize;
  18 +
  19 + private List<Object> commandStats;
  20 +
  21 + @Data
  22 + @Builder
  23 + @AllArgsConstructor
  24 + public static class CommandStat {
  25 +
  26 + private String command;
  27 +
  28 + private Integer calls;
  29 +
  30 + private Integer usec;
  31 +
  32 + }
  33 +
  34 +}
... ...
src/main/java/cn/iocoder/dashboard/util/json/JsonUtils.java
1 1 package cn.iocoder.dashboard.util.json;
2 2  
  3 +import cn.hutool.core.util.ArrayUtil;
  4 +import cn.hutool.core.util.StrUtil;
3 5 import com.fasterxml.jackson.core.JsonProcessingException;
4 6 import com.fasterxml.jackson.core.type.TypeReference;
5 7 import com.fasterxml.jackson.databind.ObjectMapper;
... ... @@ -36,6 +38,9 @@ public class JsonUtils {
36 38 }
37 39  
38 40 public static <T> T parseObject(String text, Class<T> clazz) {
  41 + if (StrUtil.isEmpty(text)) {
  42 + return null;
  43 + }
39 44 try {
40 45 return objectMapper.readValue(text, clazz);
41 46 } catch (IOException e) {
... ... @@ -44,6 +49,9 @@ public class JsonUtils {
44 49 }
45 50  
46 51 public static <T> T parseObject(byte[] bytes, Class<T> clazz) {
  52 + if (ArrayUtil.isEmpty(bytes)) {
  53 + return null;
  54 + }
47 55 try {
48 56 return objectMapper.readValue(bytes, clazz);
49 57 } catch (IOException e) {
... ...