新增缓存监控功能

pull/229/head
RuoYi 2020-11-23 10:18:58 +08:00
parent 784cc3a10f
commit 30850c1fdf
9 changed files with 406 additions and 10 deletions

View File

@ -0,0 +1,82 @@
package com.ruoyi.web.controller.monitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.service.CacheService;
/**
*
*
* @author ruoyi
*/
@Controller
@RequestMapping("/monitor/cache")
public class CacheController extends BaseController
{
private String prefix = "monitor/cache";
@Autowired
private CacheService cacheService;
@GetMapping()
public String cache(ModelMap mmap)
{
mmap.put("cacheNames", cacheService.getCacheNames());
return prefix + "/cache";
}
@PostMapping("/getNames")
public String getCacheNames(String fragment, ModelMap mmap)
{
mmap.put("cacheNames", cacheService.getCacheNames());
return prefix + "/cache::" + fragment;
}
@PostMapping("/getKeys")
public String getCacheKeys(String fragment, String cacheName, ModelMap mmap)
{
mmap.put("cacheName", cacheName);
mmap.put("cacheKyes", cacheService.getCacheKeys(cacheName));
return prefix + "/cache::" + fragment;
}
@PostMapping("/getValue")
public String getCacheValue(String fragment, String cacheName, String cacheKey, ModelMap mmap)
{
mmap.put("cacheName", cacheName);
mmap.put("cacheKey", cacheKey);
mmap.put("cacheValue", cacheService.getCacheValue(cacheName, cacheKey));
return prefix + "/cache::" + fragment;
}
@PostMapping("/clearCacheName")
@ResponseBody
public AjaxResult clearCacheName(String cacheName, ModelMap mmap)
{
cacheService.clearCacheName(cacheName);
return AjaxResult.success();
}
@PostMapping("/clearCacheKey")
@ResponseBody
public AjaxResult clearCacheKey(String cacheName, String cacheKey, ModelMap mmap)
{
cacheService.clearCacheKey(cacheName, cacheKey);
return AjaxResult.success();
}
@GetMapping("/clearAll")
@ResponseBody
public AjaxResult clearAll(ModelMap mmap)
{
cacheService.clearAll();
return AjaxResult.success();
}
}

View File

@ -42,6 +42,17 @@
statistics="false">
</cache>
<!-- 系统用户授权缓存 没必要过期 -->
<cache name="sys-authCache"
maxEntriesLocalHeap="10000"
overflowToDisk="false"
eternal="false"
diskPersistent="false"
timeToLiveSeconds="0"
timeToIdleSeconds="0"
memoryStoreEvictionPolicy="LRU"
statistics="false"/>
<!-- 系统缓存 -->
<cache name="sys-cache"
maxEntriesLocalHeap="1000"

View File

@ -0,0 +1,184 @@
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
<th:block th:include="include :: header('缓存监控')" />
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content">
<input type="hidden" id="cacheName">
<div class="col-sm-12">
<div class="row">
<div class="col-sm-4">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>缓存列表</h5>
<div class="ibox-tools">
<a href="javascript:getCacheNames()"><i class="fa fa-refresh"></i></a>
</div>
</div>
<div class="ibox-content">
<table class="table table-hover no-margins">
<thead>
<tr>
<th></th>
<th>缓存名称</th>
<th>操作</th>
</tr>
</thead>
<tbody id="cacheNames">
<tr th:fragment="fragment-cache-names" th:each="cacheName, stat : ${cacheNames}">
<td>[[${stat.index + 1}]]</td>
<td style="word-wrap:break-word;word-break:break-all;" th:onclick="getCacheKeys([[${cacheName}]])">[[${cacheName}]]</td>
<td style="width: 50px"><a href="#" th:onclick="clearCacheName([[${cacheName}]])" title="清空"><i class="fa fa-trash-o text-danger"></i></a></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="col-sm-4">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>键名列表</h5>
<div class="ibox-tools">
<a href="javascript:getCacheKeys('', true)"><i class="fa fa-refresh"></i></a>
</div>
</div>
<div class="ibox-content">
<table class="table table-hover no-margins">
<thead>
<tr>
<th></th>
<th>缓存键名</th>
<th>操作</th>
</tr>
</thead>
<tbody id="cacheKyes">
<tr th:fragment="fragment-cache-kyes" th:each="cacheKey, stat : ${cacheKyes}">
<td>[[${stat.index + 1}]]</td>
<td style="word-wrap:break-word;word-break:break-all;" th:onclick="getCacheValue([[${cacheName}]], [[${cacheKey}]])">[[${cacheKey}]]</td>
<td style="width: 50px"><a href="#" th:onclick="clearCacheKey([[${cacheName}]], [[${cacheKey}]])" title="清空"><i class="fa fa-trash-o text-danger"></i></a></td>
</tr>
</tbody>
</div>
</table>
</div>
</div>
</div>
<div class="col-sm-4">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>缓存内容</h5>
<div class="ibox-tools">
<a href="javascript:clearAll()"><i class="fa fa-refresh"></i> 清理全部</a>
</div>
</div>
<div class="ibox-content">
<div class="row" id="cacheValue">
<div class="col-sm-12" th:fragment="fragment-cache-value">
<div class="form-group">
<label>缓存名称:</label>
<input type="text" class="form-control" th:value="${cacheName}">
</div>
<div class="form-group">
<label>缓存键名:</label>
<input type="text"class="form-control" th:value="${cacheKey}">
</div>
<div class="form-group">
<label>缓存内容:</label>
<textarea class="form-control" style="height: 100px">[[${cacheValue}]]</textarea>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
var prefix = ctx + "monitor/cache";
function getCacheNames() {
$.ajax({
type: "post",
url: prefix + "/getNames",
data: {
"fragment": 'fragment-cache-names'
},
success: function(data) {
$("#cacheNames").html(data);
$.modal.msgSuccess("刷新缓存列表成功");
}
});
}
function getCacheKeys(cacheName, isMsg) {
var _cacheName = $.common.isNotEmpty(cacheName) ? cacheName : $("#cacheName").val();
$.ajax({
type: "post",
url: prefix + "/getKeys",
data: {
"cacheName": _cacheName,
"fragment": 'fragment-cache-kyes'
},
success: function(data) {
$("#cacheKyes").html(data);
$("#cacheName").val(_cacheName);
if (isMsg) {
$.modal.msgSuccess("刷新键名列表成功");
}
}
});
}
function getCacheValue(cacheName, cacheKey) {
$.ajax({
type: "post",
url: prefix + "/getValue",
data: {
"cacheName": cacheName,
"cacheKey": cacheKey,
"fragment": 'fragment-cache-value'
},
success: function(data) {
$("#cacheValue").html(data);
}
});
}
function clearCacheName(cacheName){
$.post(prefix + "/clearCacheName", {cacheName: cacheName}, function(result) {
if (result.code == web_status.SUCCESS) {
$.modal.msgSuccess("清理缓存[" + cacheName + "]成功")
getCacheKeys(cacheName);
} else {
$.modal.msgError(result.msg);
}
});
}
function clearCacheKey(cacheName, cacheKey) {
$.post(prefix + "/clearCacheKey", {cacheName: cacheName, cacheKey: cacheKey}, function(result) {
if (result.code == web_status.SUCCESS) {
$.modal.msgSuccess("清理缓存[" + cacheKey + "]成功")
getCacheKeys(cacheName);
} else {
$.modal.msgError(result.msg);
}
});
}
function clearAll(){
$.get(prefix + "/clearAll", function(result) {
if (result.code == web_status.SUCCESS) {
$.modal.msgSuccess("清理缓存成功")
} else {
$.modal.msgError(result.msg);
}
});
}
</script>
</html>

View File

@ -66,6 +66,11 @@ public class Constants
* "desc" "asc".
*/
public static final String IS_ASC = "isAsc";
/**
*
*/
public static final String SYS_AUTH_CACHE = "sys-authCache";
/**
* cache name

View File

@ -4,6 +4,7 @@ import java.util.Iterator;
import java.util.Set;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.utils.spring.SpringUtils;
@ -174,7 +175,7 @@ public class CacheUtils
* @param cacheName
* @return
*/
private static Cache<String, Object> getCache(String cacheName)
public static Cache<String, Object> getCache(String cacheName)
{
Cache<String, Object> cache = cacheManager.getCache(cacheName);
if (cache == null)
@ -184,4 +185,13 @@ public class CacheUtils
return cache;
}
/**
*
*
* @return
*/
public static String[] getCacheNames()
{
return ((EhCacheManager) cacheManager).getCacheManager().getCacheNames();
}
}

View File

@ -21,6 +21,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.shiro.realm.UserRealm;
@ -175,6 +176,7 @@ public class ShiroConfig
public UserRealm userRealm(EhCacheManager cacheManager)
{
UserRealm userRealm = new UserRealm();
userRealm.setAuthorizationCacheName(Constants.SYS_AUTH_CACHE);
userRealm.setCacheManager(cacheManager);
return userRealm;
}

View File

@ -1,5 +1,7 @@
package com.ruoyi.framework.shiro.session;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.shiro.session.mgt.SimpleSession;
import com.ruoyi.common.enums.OnlineStatus;
@ -145,4 +147,19 @@ public class OnlineSession extends SimpleSession
{
return super.removeAttribute(key);
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("userId", getUserId())
.append("loginName", getLoginName())
.append("deptName", getDeptName())
.append("avatar", getAvatar())
.append("host", getHost())
.append("browser", getBrowser())
.append("os", getOs())
.append("status", getStatus())
.append("attributeChanged", isAttributeChanged())
.toString();
}
}

View File

@ -0,0 +1,83 @@
package com.ruoyi.framework.web.service;
import java.util.Set;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.CacheUtils;
/**
*
*
* @author ruoyi
*/
@Service
public class CacheService
{
/**
*
*
* @return
*/
public String[] getCacheNames()
{
String[] cacheNames = CacheUtils.getCacheNames();
return ArrayUtils.removeElement(cacheNames, Constants.SYS_AUTH_CACHE);
}
/**
*
*
* @param cacheName
* @return
*/
public Set<String> getCacheKeys(String cacheName)
{
return CacheUtils.getCache(cacheName).keys();
}
/**
*
*
* @param cacheName
* @param cacheKey
* @return
*/
public Object getCacheValue(String cacheName, String cacheKey)
{
return CacheUtils.get(cacheName, cacheKey);
}
/**
*
*
* @param cacheName
*/
public void clearCacheName(String cacheName)
{
CacheUtils.removeAll(cacheName);
}
/**
*
*
* @param cacheName
* @param cacheKey
*/
public void clearCacheKey(String cacheName, String cacheKey)
{
CacheUtils.remove(cacheName, cacheKey);
}
/**
*
*/
public void clearAll()
{
String[] cacheNames = getCacheNames();
for (String cacheName : cacheNames)
{
CacheUtils.removeAll(cacheName);
}
}
}

View File

@ -170,10 +170,11 @@ insert into sys_menu values('108', '日志管理', '1', '9', '#',
insert into sys_menu values('109', '在线用户', '2', '1', '/monitor/online', '', 'C', '0', 'monitor:online:view', 'fa fa-user-circle', 'admin', sysdate(), '', null, '在线用户菜单');
insert into sys_menu values('110', '定时任务', '2', '2', '/monitor/job', '', 'C', '0', 'monitor:job:view', 'fa fa-tasks', 'admin', sysdate(), '', null, '定时任务菜单');
insert into sys_menu values('111', '数据监控', '2', '3', '/monitor/data', '', 'C', '0', 'monitor:data:view', 'fa fa-bug', 'admin', sysdate(), '', null, '数据监控菜单');
insert into sys_menu values('112', '服务监控', '2', '3', '/monitor/server', '', 'C', '0', 'monitor:server:view', 'fa fa-server', 'admin', sysdate(), '', null, '服务监控菜单');
insert into sys_menu values('113', '表单构建', '3', '1', '/tool/build', '', 'C', '0', 'tool:build:view', 'fa fa-wpforms', 'admin', sysdate(), '', null, '表单构建菜单');
insert into sys_menu values('114', '代码生成', '3', '2', '/tool/gen', '', 'C', '0', 'tool:gen:view', 'fa fa-code', 'admin', sysdate(), '', null, '代码生成菜单');
insert into sys_menu values('115', '系统接口', '3', '3', '/tool/swagger', '', 'C', '0', 'tool:swagger:view', 'fa fa-gg', 'admin', sysdate(), '', null, '系统接口菜单');
insert into sys_menu values('112', '服务监控', '2', '4', '/monitor/server', '', 'C', '0', 'monitor:server:view', 'fa fa-server', 'admin', sysdate(), '', null, '服务监控菜单');
insert into sys_menu values('113', '缓存监控', '2', '5', '/monitor/cache', '', 'C', '0', 'monitor:cache:view', 'fa fa-cube', 'admin', sysdate(), '', null, '缓存监控菜单');
insert into sys_menu values('114', '表单构建', '3', '1', '/tool/build', '', 'C', '0', 'tool:build:view', 'fa fa-wpforms', 'admin', sysdate(), '', null, '表单构建菜单');
insert into sys_menu values('115', '代码生成', '3', '2', '/tool/gen', '', 'C', '0', 'tool:gen:view', 'fa fa-code', 'admin', sysdate(), '', null, '代码生成菜单');
insert into sys_menu values('116', '系统接口', '3', '3', '/tool/swagger', '', 'C', '0', 'tool:swagger:view', 'fa fa-gg', 'admin', sysdate(), '', null, '系统接口菜单');
-- 三级菜单
insert into sys_menu values('500', '操作日志', '108', '1', '/monitor/operlog', '', 'C', '0', 'monitor:operlog:view', 'fa fa-address-book', 'admin', sysdate(), '', null, '操作日志菜单');
insert into sys_menu values('501', '登录日志', '108', '2', '/monitor/logininfor', '', 'C', '0', 'monitor:logininfor:view', 'fa fa-file-image-o', 'admin', sysdate(), '', null, '登录日志菜单');
@ -247,11 +248,11 @@ insert into sys_menu values('1054', '状态修改', '110', '5', '#', '', 'F', '
insert into sys_menu values('1055', '任务详细', '110', '6', '#', '', 'F', '0', 'monitor:job:detail', '#', 'admin', sysdate(), '', null, '');
insert into sys_menu values('1056', '任务导出', '110', '7', '#', '', 'F', '0', 'monitor:job:export', '#', 'admin', sysdate(), '', null, '');
-- 代码生成按钮
insert into sys_menu values('1057', '生成查询', '114', '1', '#', '', 'F', '0', 'tool:gen:list', '#', 'admin', sysdate(), '', null, '');
insert into sys_menu values('1058', '生成修改', '114', '2', '#', '', 'F', '0', 'tool:gen:edit', '#', 'admin', sysdate(), '', null, '');
insert into sys_menu values('1059', '生成删除', '114', '3', '#', '', 'F', '0', 'tool:gen:remove', '#', 'admin', sysdate(), '', null, '');
insert into sys_menu values('1060', '预览代码', '114', '4', '#', '', 'F', '0', 'tool:gen:preview', '#', 'admin', sysdate(), '', null, '');
insert into sys_menu values('1061', '生成代码', '114', '5', '#', '', 'F', '0', 'tool:gen:code', '#', 'admin', sysdate(), '', null, '');
insert into sys_menu values('1057', '生成查询', '115', '1', '#', '', 'F', '0', 'tool:gen:list', '#', 'admin', sysdate(), '', null, '');
insert into sys_menu values('1058', '生成修改', '115', '2', '#', '', 'F', '0', 'tool:gen:edit', '#', 'admin', sysdate(), '', null, '');
insert into sys_menu values('1059', '生成删除', '115', '3', '#', '', 'F', '0', 'tool:gen:remove', '#', 'admin', sysdate(), '', null, '');
insert into sys_menu values('1060', '预览代码', '115', '4', '#', '', 'F', '0', 'tool:gen:preview', '#', 'admin', sysdate(), '', null, '');
insert into sys_menu values('1061', '生成代码', '115', '5', '#', '', 'F', '0', 'tool:gen:code', '#', 'admin', sysdate(), '', null, '');
-- ----------------------------
@ -304,6 +305,7 @@ insert into sys_role_menu values ('2', '112');
insert into sys_role_menu values ('2', '113');
insert into sys_role_menu values ('2', '114');
insert into sys_role_menu values ('2', '115');
insert into sys_role_menu values ('2', '116');
insert into sys_role_menu values ('2', '500');
insert into sys_role_menu values ('2', '501');
insert into sys_role_menu values ('2', '1000');