✨ 添加新特性。缓存监控 web redis 终端
This commit is contained in:
1 parent
96202dc7cd
commit
8affb5519e
4 files changed
+173
No files matched your search
+42
@@ -0,0 +1,42 @@
|
||||
package com.pig4cloud.pigx.monitor.controller;
|
||||
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import com.pig4cloud.pigx.monitor.service.RedisService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2019-05-08
|
||||
* <p>
|
||||
* redis 数据
|
||||
*/
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/redis")
|
||||
public class RedisController {
|
||||
private final RedisService redisService;
|
||||
|
||||
/**
|
||||
* 查询redis信息
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/info")
|
||||
public R memory() {
|
||||
return R.builder().data(redisService.getInfo()).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行用户输入命令
|
||||
*
|
||||
* @param command 命令
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/exec")
|
||||
public R exec(String command) {
|
||||
return redisService.exec(command);
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.pig4cloud.pigx.monitor.service;
|
||||
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2019-05-08
|
||||
* <p>
|
||||
* redis 监控
|
||||
*/
|
||||
public interface RedisService {
|
||||
/**
|
||||
* 获取内存信息
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Map<String, Object> getInfo();
|
||||
|
||||
/**
|
||||
* 执行运行命令
|
||||
*
|
||||
* @param command
|
||||
* @return
|
||||
*/
|
||||
R exec(String command);
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
package com.pig4cloud.pigx.monitor.service.impl;
|
||||
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import com.pig4cloud.pigx.monitor.service.RedisService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.RedisCallback;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2019-05-08
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class RedisServiceImpl implements RedisService {
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
/**
|
||||
* 获取内存信息
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> getInfo() {
|
||||
Properties info = (Properties) redisTemplate.execute((RedisCallback) redisConnection -> redisConnection.info());
|
||||
Properties commandStats = (Properties) redisTemplate.execute((RedisCallback) redisConnection -> redisConnection.info("commandstats"));
|
||||
Object dbSize = redisTemplate.execute((RedisCallback) redisConnection -> redisConnection.dbSize());
|
||||
|
||||
Map<String, Object> result = new HashMap<>(4);
|
||||
result.put("info", info);
|
||||
result.put("dbSize", dbSize);
|
||||
result.put("time", DateUtil.format(new Date(), DatePattern.NORM_TIME_PATTERN));
|
||||
|
||||
List<Map<String, String>> pieList = new ArrayList<>();
|
||||
commandStats.stringPropertyNames().forEach(key -> {
|
||||
Map<String, String> data = new HashMap<>(4);
|
||||
String property = commandStats.getProperty(key);
|
||||
data.put("name", StrUtil.removePrefix(key, "cmdstat_"));
|
||||
data.put("value", StrUtil.subBetween(property, "calls=", ",usec"));
|
||||
pieList.add(data);
|
||||
});
|
||||
result.put("commandStats", pieList);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行运行命令
|
||||
*
|
||||
* @param input 用户输入
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public R exec(String input) {
|
||||
try {
|
||||
Object result = redisTemplate.execute((RedisCallback) connection -> {
|
||||
String[] arrs = input.split(StrUtil.SPACE);
|
||||
if (arrs.length == 1) {
|
||||
return connection.execute(arrs[0]);
|
||||
}
|
||||
|
||||
String[] args = ArrayUtil.sub(arrs, 1, arrs.length);
|
||||
byte[][] bytes = new byte[args.length][];
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
bytes[i] = StrUtil.bytes(args[i], CharsetUtil.UTF_8);
|
||||
}
|
||||
return connection.execute(arrs[0], bytes);
|
||||
});
|
||||
return R.builder()
|
||||
.msg("success")
|
||||
.data(StrUtil.str(result, CharsetUtil.UTF_8)).build();
|
||||
} catch (Exception e) {
|
||||
log.error("REDIS 命令执行失败", e);
|
||||
return R.builder()
|
||||
.msg("error")
|
||||
.data(e.getMessage()).build();
|
||||
}
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.pig4cloud.pigx.monitor.service.impl;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2019-05-09
|
||||
*/
|
||||
public class RedisServiceImplTest {
|
||||
|
||||
@Test
|
||||
public void exec() {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user