新增:新增系统监控模块(存放系统监控模块相关功能,例如:日志管理、服务监控等),新增操作日志引擎,记录 HTTP 请求信息

This commit is contained in:
2022-12-25 13:16:15 +08:00
parent 78e84e8941
commit 727850933f
28 changed files with 1523 additions and 12 deletions

View File

@@ -0,0 +1,73 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.config;
import java.util.Arrays;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import cn.hutool.core.util.ArrayUtil;
import top.charles7c.cnadmin.common.exception.ServiceException;
/**
* 异步任务执行配置
*
* @author Charles7c
* @since 2022/12/23 22:33
*/
@Slf4j
@Configuration
@RequiredArgsConstructor
@EnableAsync(proxyTargetClass = true)
public class AsyncConfiguration implements AsyncConfigurer {
private final ScheduledExecutorService scheduledExecutorService;
/**
* 异步任务执行时,使用 Java 内置线程池
*/
@Override
public Executor getAsyncExecutor() {
return scheduledExecutorService;
}
/**
* 异步任务执行时的异常处理
*/
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return (throwable, method, objects) -> {
throwable.printStackTrace();
StringBuilder sb = new StringBuilder();
sb.append("Exception message - ").append(throwable.getMessage()).append(", Method name - ")
.append(method.getName());
if (ArrayUtil.isNotEmpty(objects)) {
sb.append(", Parameter value - ").append(Arrays.toString(objects));
}
throw new ServiceException(sb.toString());
};
}
}

View File

@@ -25,6 +25,9 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.stereotype.Component;
import cn.hutool.core.convert.Convert;
import cn.hutool.extra.spring.SpringUtil;
/**
* 项目配置属性
*
@@ -72,4 +75,13 @@ public class ContinewAdminProperties {
*/
@NestedConfigurationProperty
private License license;
/**
* 是否本地解析 IP 归属地
*/
public static final boolean IP_ADDR_LOCAL_PARSE_ENABLED;
static {
IP_ADDR_LOCAL_PARSE_ENABLED = Convert.toBool(SpringUtil.getProperty("continew-admin.ipAddrLocalParseEnabled"));
}
}

View File

@@ -0,0 +1,85 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.config.threadpool;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import top.charles7c.cnadmin.common.util.ExceptionUtils;
/**
* 线程池配置
*
* @author Charles7c
* @since 2022/12/23 23:13
*/
@Slf4j
@Configuration
@RequiredArgsConstructor
public class ThreadPoolConfiguration {
private final ThreadPoolProperties threadPoolProperties;
/** 核心(最小)线程数 = CPU 核心数 + 1 */
private final int corePoolSize = Runtime.getRuntime().availableProcessors() + 1;
/**
* Spring 内置线程池ThreadPoolTaskExecutor
*/
@Bean
@ConditionalOnProperty(prefix = "thread-pool", name = "enabled", havingValue = "true")
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 核心(最小)线程数
executor.setCorePoolSize(corePoolSize);
// 最大线程数
executor.setMaxPoolSize(corePoolSize * 2);
// 队列容量
executor.setQueueCapacity(threadPoolProperties.getQueueCapacity());
// 活跃时间
executor.setKeepAliveSeconds(threadPoolProperties.getKeepAliveSeconds());
// 配置当池内线程数已达到上限的时候,该如何处理新任务:不在新线程中执行任务,而是由调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
/**
* Java 内置线程池ScheduledExecutorService适用于执行周期性或定时任务
*/
@Bean
public ScheduledExecutorService scheduledExecutorService() {
return new ScheduledThreadPoolExecutor(corePoolSize,
new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build(),
new ThreadPoolExecutor.CallerRunsPolicy()) {
@Override
protected void afterExecute(Runnable runnable, Throwable throwable) {
super.afterExecute(runnable, throwable);
ExceptionUtils.printException(runnable, throwable);
}
};
}
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.config.threadpool;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 线程池配置属性
*
* @author Charles7c
* @since 2022/12/23 23:06
*/
@Data
@Component
@ConfigurationProperties(prefix = "thread-pool")
public class ThreadPoolProperties {
/**
* 队列容量
*/
private int queueCapacity;
/**
* 活跃时间
*/
private int keepAliveSeconds;
}

View File

@@ -38,9 +38,12 @@ import cn.dev33.satoken.exception.NotLoginException;
import cn.hutool.core.util.StrUtil;
import top.charles7c.cnadmin.common.exception.BadRequestException;
import top.charles7c.cnadmin.common.exception.ServiceException;
import top.charles7c.cnadmin.common.model.dto.OperationLog;
import top.charles7c.cnadmin.common.model.vo.R;
import top.charles7c.cnadmin.common.util.ExceptionUtils;
import top.charles7c.cnadmin.common.util.StreamUtils;
import top.charles7c.cnadmin.common.util.holder.LogContextHolder;
/**
* 全局异常处理器
@@ -58,6 +61,7 @@ public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public R handleException(Exception e, HttpServletRequest request) {
this.setException(e);
log.error("请求地址'{}',发生未知异常", request.getRequestURI(), e);
return R.fail(e.getMessage());
}
@@ -68,10 +72,22 @@ public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(RuntimeException.class)
public R handleRuntimeException(RuntimeException e, HttpServletRequest request) {
this.setException(e);
log.error("请求地址'{}',发生系统异常", request.getRequestURI(), e);
return R.fail(e.getMessage());
}
/**
* 拦截业务异常
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(ServiceException.class)
public R handleServiceException(ServiceException e, HttpServletRequest request) {
this.setException(e);
log.error("请求地址'{}',发生业务异常", request.getRequestURI(), e);
return R.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
}
/**
* 拦截自定义验证异常-错误请求
*/
@@ -147,4 +163,17 @@ public class GlobalExceptionHandler {
log.error("请求地址'{}',认证失败'{}',无法访问系统资源", request.getRequestURI(), e.getMessage());
return R.fail(HttpStatus.UNAUTHORIZED.value(), "认证失败,无法访问系统资源");
}
/**
* 操作日志保存异常信息
*
* @param e
* 异常信息
*/
private void setException(Exception e) {
OperationLog operationLog = LogContextHolder.get();
if (operationLog != null) {
operationLog.setException(e);
}
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.model.dto;
import java.util.Date;
import lombok.Data;
/**
* 操作日志
*
* @author Charles7c
* @since 2022/12/25 8:59
*/
@Data
public class OperationLog {
/**
* 操作人
*/
private Long createUser;
/**
* 操作时间
*/
private Date createTime;
/**
* 异常
*/
private Exception exception;
}

View File

@@ -16,10 +16,14 @@
package top.charles7c.cnadmin.common.util;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
/**
* 异常工具类
@@ -27,9 +31,38 @@ import lombok.NoArgsConstructor;
* @author Charles7c
* @since 2022/12/21 20:56
*/
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ExceptionUtils {
/**
* 打印线程异常信息
*
* @param runnable
* 线程执行内容
* @param throwable
* 异常
*/
public static void printException(Runnable runnable, Throwable throwable) {
if (throwable == null && runnable instanceof Future<?>) {
try {
Future<?> future = (Future<?>)runnable;
if (future.isDone()) {
future.get();
}
} catch (CancellationException e) {
throwable = e;
} catch (ExecutionException e) {
throwable = e.getCause();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
if (throwable != null) {
log.error(throwable.getMessage(), throwable);
}
}
/**
* 如果有异常,返回 null
*

View File

@@ -0,0 +1,111 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.util;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.net.NetUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.HtmlUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import top.charles7c.cnadmin.common.config.properties.ContinewAdminProperties;
import net.dreamlu.mica.ip2region.core.Ip2regionSearcher;
import net.dreamlu.mica.ip2region.core.IpInfo;
/**
* IP 工具类
*
* @author Charles7c
* @since 2022/12/23 20:00
*/
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class IpUtils {
/**
* 太平洋网开放 API查询 IP 归属地
*/
private static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp?ip=%s&json=true";
/**
* 根据IP获取详细地址
*
* @param ip
* IP地址
* @return 详细地址
*/
public static String getCityInfo(String ip) {
if (ContinewAdminProperties.IP_ADDR_LOCAL_PARSE_ENABLED) {
return getLocalCityInfo(ip);
} else {
return getHttpCityInfo(ip);
}
}
/**
* 根据 IP 获取详细地址(网络解析)
*
* @param ip
* IP地址
* @return 详细地址
*/
public static String getHttpCityInfo(String ip) {
if (isInnerIP(ip)) {
return "内网IP";
}
String api = String.format(IP_URL, ip);
JSONObject object = JSONUtil.parseObj(HttpUtil.get(api));
return object.get("addr", String.class);
}
/**
* 根据 IP 获取详细地址(本地解析)
*
* @param ip
* IP 地址
* @return 详细地址
*/
public static String getLocalCityInfo(String ip) {
if (isInnerIP(ip)) {
return "内网IP";
}
Ip2regionSearcher ip2regionSearcher = SpringUtil.getBean(Ip2regionSearcher.class);
IpInfo ipInfo = ip2regionSearcher.memorySearch(ip);
if (ipInfo != null) {
return ipInfo.getAddress();
}
return null;
}
/**
* 是否为内网IPv4
*
* @param ip
* IP 地址
* @return 是否为内网IP
*/
public static boolean isInnerIP(String ip) {
ip = "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : HtmlUtil.cleanHtmlTag(ip);
return NetUtil.isInnerIP(ip);
}
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.util;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
/**
* Servlet 工具类
*
* @author Charles7c
* @since 2022/12/23 20:00
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ServletUtils {
/**
* 获取请求对象
*
* @return /
*/
public static HttpServletRequest getRequest() {
return getServletRequestAttributes().getRequest();
}
/**
* 获取响应对象
*
* @return /
*/
public static HttpServletResponse getResponse() {
return getServletRequestAttributes().getResponse();
}
private static ServletRequestAttributes getServletRequestAttributes() {
return (ServletRequestAttributes)Objects.requireNonNull(RequestContextHolder.getRequestAttributes());
}
/**
* 获取浏览器及其版本信息
*
* @param request
* 请求信息
* @return 浏览器及其版本信息
*/
public static String getBrowser(HttpServletRequest request) {
if (request == null) {
return null;
}
UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent"));
return userAgent.getBrowser().getName() + " " + userAgent.getVersion();
}
/**
* 获取响应所有的头header信息
*
* @param response
* 响应对象{@link HttpServletResponse}
* @return header值
*/
public static Map<String, Collection<String>> getHeaderMap(HttpServletResponse response) {
final Map<String, Collection<String>> headerMap = new HashMap<>();
final Collection<String> names = response.getHeaderNames();
for (String name : names) {
headerMap.put(name, response.getHeaders(name));
}
return headerMap;
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.util.holder;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import top.charles7c.cnadmin.common.model.dto.OperationLog;
/**
* 操作日志上下文持有者
*
* @author Charles7c
* @since 2022/12/25 8:55
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class LogContextHolder {
private static final ThreadLocal<OperationLog> LOG_THREAD_LOCAL = new ThreadLocal<>();
/**
* 存储操作日志
*
* @param operationLog
* 操作日志信息
*/
public static void set(OperationLog operationLog) {
LOG_THREAD_LOCAL.set(operationLog);
}
/**
* 获取操作日志
*
* @return 操作日志信息
*/
public static OperationLog get() {
return LOG_THREAD_LOCAL.get();
}
/**
* 移除操作日志
*/
public static void remove() {
LOG_THREAD_LOCAL.remove();
}
}