mirror of
https://gitee.com/lab1024/smart-admin.git
synced 2025-10-08 13:16:41 +08:00
heart-beat-优化
This commit is contained in:
parent
e4c3895e59
commit
b077d1c2f6
@ -79,7 +79,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
// 过滤请求
|
||||
.authorizeRequests();
|
||||
//可以匿名登录的URL
|
||||
String [] anonymousUrlArray = smartSecurityUrlMatchers.getPermitUrlArray();
|
||||
String [] anonymousUrlArray = smartSecurityUrlMatchers.getAnonymousUrlArray();
|
||||
interceptUrlRegistry.antMatchers(anonymousUrlArray).permitAll();
|
||||
|
||||
//登录的URL
|
||||
|
@ -1,7 +1,7 @@
|
||||
package net.lab1024.smartadmin.service.module.support.heartbeat;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.lab1024.smartadmin.service.module.support.heartbeat.core.HeartBeatRecordDTO;
|
||||
import net.lab1024.smartadmin.service.module.support.heartbeat.core.HeartBeatRecord;
|
||||
import net.lab1024.smartadmin.service.module.support.heartbeat.core.IHeartBeatRecordHandler;
|
||||
import net.lab1024.smartadmin.service.util.SmartBeanUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -21,11 +21,11 @@ public class HeartBeatRecordHandler implements IHeartBeatRecordHandler {
|
||||
|
||||
/**
|
||||
* 心跳日志处理方法
|
||||
* @param heartBeatRecordDTO
|
||||
* @param heartBeatRecord
|
||||
*/
|
||||
@Override
|
||||
public void handler(HeartBeatRecordDTO heartBeatRecordDTO) {
|
||||
HeartBeatRecordEntity heartBeatRecordEntity = SmartBeanUtil.copy(heartBeatRecordDTO, HeartBeatRecordEntity.class);
|
||||
public void handler(HeartBeatRecord heartBeatRecord) {
|
||||
HeartBeatRecordEntity heartBeatRecordEntity = SmartBeanUtil.copy(heartBeatRecord, HeartBeatRecordEntity.class);
|
||||
HeartBeatRecordEntity heartBeatRecordOld = heartBeatRecordDao.query(heartBeatRecordEntity);
|
||||
if (heartBeatRecordOld == null) {
|
||||
heartBeatRecordDao.insertHeartBeat(heartBeatRecordEntity);
|
||||
|
@ -1,98 +0,0 @@
|
||||
package net.lab1024.smartadmin.service.module.support.heartbeat.core;
|
||||
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
import net.lab1024.smartadmin.service.util.SmartIPUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
/**
|
||||
* @Description: 心跳服务
|
||||
* @Author: 1024lab
|
||||
* @Date: 2018/7/9 16:26
|
||||
*/
|
||||
public abstract class AbstractHeartBeatManager {
|
||||
|
||||
|
||||
ScheduledExecutorService executorService;
|
||||
|
||||
int threadNum = 1;
|
||||
|
||||
/**
|
||||
* 项目路径
|
||||
*/
|
||||
private String projectPath;
|
||||
/**
|
||||
* 服务器ip(多网卡)
|
||||
*/
|
||||
private List<String> serverIps;
|
||||
/**
|
||||
* 进程号
|
||||
*/
|
||||
private Integer processNo;
|
||||
/**
|
||||
* 进程开启时间
|
||||
*/
|
||||
private Date processStartTime;
|
||||
|
||||
HeartBeatLogger logger;
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
public void init(HeartBeatConfig config, HeartBeatLogger logger){
|
||||
this.handlerHeartBeat();
|
||||
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("AbstractHeartBeatCommand-%s").build();
|
||||
executorService = Executors.newScheduledThreadPool(threadNum, threadFactory);
|
||||
executorService.scheduleWithFixedDelay(new DoHeartBeat(), config.getDelayHandlerTime(), config.getIntervalTime(), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public void handlerHeartBeat(){
|
||||
try {
|
||||
projectPath = HeatBeatRecordHelper.getProjectPath();
|
||||
serverIps = SmartIPUtil.getLocalHostIPList();
|
||||
processNo = HeatBeatRecordHelper.getProcessID();
|
||||
processStartTime = HeatBeatRecordHelper.getStartTime();
|
||||
}catch (Throwable e){
|
||||
logger.error("get heart beat info error.", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁线程池
|
||||
*/
|
||||
public void destroy(){
|
||||
if (executorService != null && !executorService.isShutdown()) {
|
||||
executorService.shutdown();
|
||||
executorService = null;
|
||||
}
|
||||
}
|
||||
|
||||
public class DoHeartBeat implements Runnable{
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
HeartBeatRecordDTO heartBeatRecord = new HeartBeatRecordDTO();
|
||||
heartBeatRecord.setProjectPath(projectPath);
|
||||
heartBeatRecord.setServerIp(StringUtils.join(serverIps,";"));
|
||||
heartBeatRecord.setProcessNo(processNo);
|
||||
heartBeatRecord.setProcessStartTime(processStartTime);
|
||||
heartBeatRecord.setHeartBeatTime(new Date());
|
||||
handler(heartBeatRecord);
|
||||
}catch (Throwable t){
|
||||
logger.error("handler heartbeat error.", t);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
abstract void handler(HeartBeatRecordDTO heartBeatRecord);
|
||||
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package net.lab1024.smartadmin.service.module.support.heartbeat.core;
|
||||
|
||||
/**
|
||||
* [ ]
|
||||
*
|
||||
* @author 罗伊
|
||||
*/
|
||||
public interface HeartBeatLogger {
|
||||
|
||||
void error(String string);
|
||||
|
||||
void error(String string, Throwable e);
|
||||
|
||||
void info(String string);
|
||||
}
|
@ -1,54 +1,68 @@
|
||||
package net.lab1024.smartadmin.service.module.support.heartbeat.core;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.lab1024.smartadmin.service.util.SmartIPUtil;
|
||||
import net.lab1024.smartadmin.service.util.SmartThreadFactory;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* [ ]
|
||||
*
|
||||
* @author 罗伊
|
||||
*/
|
||||
@Slf4j
|
||||
public class HeartBeatManager extends AbstractHeartBeatManager {
|
||||
public class HeartBeatManager {
|
||||
|
||||
private ScheduledThreadPoolExecutor threadPoolExecutor;
|
||||
|
||||
/**
|
||||
* 服务状态持久化处理类
|
||||
*/
|
||||
private IHeartBeatRecordHandler heartBeatRecordHandler;
|
||||
|
||||
/**
|
||||
* 调度配置信息
|
||||
*/
|
||||
private HeartBeatConfig config;
|
||||
|
||||
/**
|
||||
* 服务信息
|
||||
*/
|
||||
private HeartBeatServer server;
|
||||
|
||||
/**
|
||||
* @param delayHandlerTime 延迟执行时间
|
||||
* @param intervalTime 间隔执行时间
|
||||
*/
|
||||
public HeartBeatManager(Long delayHandlerTime, Long intervalTime, IHeartBeatRecordHandler heartBeatRecordHandler) {
|
||||
HeartBeatConfig config = HeartBeatConfig.builder().delayHandlerTime(delayHandlerTime).intervalTime(intervalTime).build();
|
||||
super.init(config, new HeartBeatLogger() {
|
||||
@Override
|
||||
public void error(String string) {
|
||||
log.error(string);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(String string, Throwable e) {
|
||||
log.error(string, e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(String string) {
|
||||
log.info(string);
|
||||
}
|
||||
});
|
||||
public HeartBeatManager(Long delayHandlerTime,
|
||||
Long intervalTime,
|
||||
IHeartBeatRecordHandler heartBeatRecordHandler) {
|
||||
this.config = HeartBeatConfig.builder().delayHandlerTime(delayHandlerTime).intervalTime(intervalTime).build();
|
||||
this.heartBeatRecordHandler = heartBeatRecordHandler;
|
||||
this.server = handlerHeartServer();
|
||||
this.threadPoolExecutor = new ScheduledThreadPoolExecutor(1, new HeartBeatThreadFactory());
|
||||
this.heartBeatScheduler();
|
||||
}
|
||||
|
||||
|
||||
@PreDestroy
|
||||
@Override
|
||||
public void destroy() {
|
||||
super.destroy();
|
||||
/**
|
||||
* 调度监控服务状态
|
||||
*/
|
||||
private void heartBeatScheduler() {
|
||||
HeartBeatRunnable heartBeatRunnable = new HeartBeatRunnable(heartBeatRecordHandler,server);
|
||||
threadPoolExecutor.scheduleWithFixedDelay(heartBeatRunnable, config.getDelayHandlerTime(), config.getIntervalTime(), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handler(HeartBeatRecordDTO heartBeatRecordDTO) {
|
||||
heartBeatRecordHandler.handler(heartBeatRecordDTO);
|
||||
/**
|
||||
* 服务信息
|
||||
* @return
|
||||
*/
|
||||
private HeartBeatServer handlerHeartServer(){
|
||||
HeartBeatServer server = new HeartBeatServer();
|
||||
server.setProjectPath(HeatBeatRecordHelper.getProjectPath());
|
||||
server.setServerIps(SmartIPUtil.getLocalHostIPList());
|
||||
server.setProcessNo(HeatBeatRecordHelper.getProcessID());
|
||||
server.setProcessStartTime(HeatBeatRecordHelper.getStartTime());
|
||||
return server;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import java.util.Date;
|
||||
* @Date: 2018/7/9 11:11
|
||||
*/
|
||||
@Data
|
||||
public class HeartBeatRecordDTO {
|
||||
public class HeartBeatRecord {
|
||||
|
||||
/**
|
||||
* 项目名字
|
@ -0,0 +1,34 @@
|
||||
package net.lab1024.smartadmin.service.module.support.heartbeat.core;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* [ ]
|
||||
*
|
||||
* @author 罗伊
|
||||
* @date 2021/9/23 18:52
|
||||
*/
|
||||
public class HeartBeatRunnable implements Runnable {
|
||||
|
||||
private IHeartBeatRecordHandler recordHandler;
|
||||
|
||||
private HeartBeatServer server;
|
||||
|
||||
public HeartBeatRunnable(IHeartBeatRecordHandler recordHandler, HeartBeatServer server) {
|
||||
this.recordHandler = recordHandler;
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
HeartBeatRecord heartBeatRecord = new HeartBeatRecord();
|
||||
heartBeatRecord.setProjectPath(server.getProjectPath());
|
||||
heartBeatRecord.setServerIp(StringUtils.join(server.getServerIps(), ";"));
|
||||
heartBeatRecord.setProcessNo(server.getProcessNo());
|
||||
heartBeatRecord.setProcessStartTime(server.getProcessStartTime());
|
||||
heartBeatRecord.setHeartBeatTime(new Date());
|
||||
recordHandler.handler(heartBeatRecord);
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package net.lab1024.smartadmin.service.module.support.heartbeat.core;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* [ ]
|
||||
*
|
||||
* @author 罗伊
|
||||
* @date 2021/9/23 19:39
|
||||
*/
|
||||
@Data
|
||||
public class HeartBeatServer {
|
||||
|
||||
/**
|
||||
* 项目路径
|
||||
*/
|
||||
private String projectPath;
|
||||
/**
|
||||
* 服务器ip(多网卡)
|
||||
*/
|
||||
private List<String> serverIps;
|
||||
/**
|
||||
* 进程号
|
||||
*/
|
||||
private Integer processNo;
|
||||
/**
|
||||
* 进程开启时间
|
||||
*/
|
||||
private Date processStartTime;
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package net.lab1024.smartadmin.service.module.support.heartbeat.core;
|
||||
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
|
||||
/**
|
||||
* [ ]
|
||||
*
|
||||
* @author 罗伊
|
||||
* @date 2021/9/23 20:19
|
||||
*/
|
||||
public class HeartBeatThreadFactory implements ThreadFactory {
|
||||
|
||||
private static String namePrefix = "heart-beat";
|
||||
|
||||
@Override
|
||||
public Thread newThread(Runnable r) {
|
||||
Thread t = new Thread(r, namePrefix);
|
||||
if (!t.isDaemon()) {
|
||||
t.setDaemon(true);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
}
|
@ -10,7 +10,7 @@ public interface IHeartBeatRecordHandler {
|
||||
/**
|
||||
* 心跳日志处理方法
|
||||
*
|
||||
* @param recordDTO
|
||||
* @param heartBeatRecord
|
||||
*/
|
||||
void handler(HeartBeatRecordDTO recordDTO);
|
||||
void handler(HeartBeatRecord heartBeatRecord);
|
||||
}
|
||||
|
@ -22,9 +22,9 @@ spring.jackson.time-zone=GMT+8
|
||||
spring.jackson.serialization.write-dates-as-timestamps=false
|
||||
|
||||
######################### database #########################
|
||||
spring.datasource.url=jdbc:p6spy:mysql://127.0.0.1:3306/smart_admin_v2?autoReconnect=true&useServerPreparedStmts=false&rewriteBatchedStatements=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai
|
||||
spring.datasource.url=jdbc:p6spy:mysql://115.29.150.222:11024/smart_admin_v2?autoReconnect=true&useServerPreparedStmts=false&rewriteBatchedStatements=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=1024lab
|
||||
spring.datasource.password=11024Lab
|
||||
spring.datasource.initial-size=2
|
||||
spring.datasource.min-idle=1
|
||||
spring.datasource.max-active=10
|
||||
@ -40,14 +40,14 @@ spring.datasource.druid.service.scanner=net.lab1024.smartadmin.module..*Service.
|
||||
|
||||
######################### redis #######################################
|
||||
spring.redis.database=1
|
||||
spring.redis.host=127.0.0.1
|
||||
spring.redis.host=115.29.150.222
|
||||
spring.redis.lettuce.pool.max-active=100
|
||||
spring.redis.lettuce.pool.min-idle=5
|
||||
spring.redis.lettuce.pool.max-idle=10
|
||||
spring.redis.lettuce.pool.max-wait=30000ms
|
||||
spring.redis.port=6379
|
||||
spring.redis.port=21024
|
||||
spring.redis.timeout=10000ms
|
||||
spring.redis.password=
|
||||
spring.redis.password=21024Lab
|
||||
|
||||
######################### swagger #########################
|
||||
swagger.apiGroupName=smartAdmin
|
||||
@ -70,7 +70,7 @@ spring.servlet.multipart.max-file-size=30MB
|
||||
spring.servlet.multipart.max-request-size=30MB
|
||||
|
||||
######################### ali oss #########################
|
||||
file.storage.mode=cloud
|
||||
file.storage.mode=local
|
||||
file.storage.local.path=/home/upload/
|
||||
file.storage.cloud.region=oss-cn-qingdao
|
||||
file.storage.cloud.endpoint=oss-cn-qingdao.aliyuncs.com
|
||||
|
Loading…
Reference in New Issue
Block a user