diff --git a/src/main/java/neatlogic/framework/heartbeat/core/HeartbeatManager.java b/src/main/java/neatlogic/framework/heartbeat/core/HeartbeatManager.java index 8f66e5151f9f6ab98ad8ce2b56a5c129e2571b21..857b8e9d45facdee7f1100a5ea6dd45e66775fa4 100644 --- a/src/main/java/neatlogic/framework/heartbeat/core/HeartbeatManager.java +++ b/src/main/java/neatlogic/framework/heartbeat/core/HeartbeatManager.java @@ -17,12 +17,16 @@ package neatlogic.framework.heartbeat.core; import neatlogic.framework.applicationlistener.core.ModuleInitializedListenerBase; import neatlogic.framework.asynchronization.thread.NeatLogicThread; +import neatlogic.framework.asynchronization.threadlocal.TenantContext; import neatlogic.framework.asynchronization.threadpool.CachedThreadPool; import neatlogic.framework.bootstrap.NeatLogicWebApplicationContext; import neatlogic.framework.common.RootComponent; import neatlogic.framework.common.config.Config; import neatlogic.framework.common.constvalue.SystemUser; +import neatlogic.framework.dao.mapper.TenantMapper; +import neatlogic.framework.dto.TenantVo; import neatlogic.framework.heartbeat.dao.mapper.ServerMapper; +import neatlogic.framework.heartbeat.dao.mapper.TenantServerMapper; import neatlogic.framework.heartbeat.dto.ServerClusterVo; import neatlogic.framework.heartbeat.dto.ServerCounterVo; import neatlogic.framework.transaction.util.TransactionUtil; @@ -40,9 +44,19 @@ import java.util.concurrent.TimeUnit; @RootComponent public class HeartbeatManager extends ModuleInitializedListenerBase { private final Logger logger = LoggerFactory.getLogger(HeartbeatManager.class); + + // 记录服务器启动时间 + private final static Date START_TIME = new Date(); + @Autowired private ServerMapper serverMapper; + @Autowired + private TenantServerMapper tenantServerMapper; + + @Autowired + private TenantMapper tenantMapper; + @Autowired private TransactionUtil transactionUtil;//强迫TransactionUtil先加载,否则可能会出现空指针 @@ -60,6 +74,7 @@ public class HeartbeatManager extends ModuleInitializedListenerBase { server.setHeartbeatRate(Config.SERVER_HEARTBEAT_RATE()); server.setHeartbeatThreshold(Config.SERVER_HEARTBEAT_THRESHOLD()); serverMapper.insertServer(server); + serverMapper.insertServerRunTime(Config.SCHEDULE_SERVER_ID, START_TIME); ScheduledExecutorService heartbeatService = Executors.newScheduledThreadPool(1, r -> { Thread t = new Thread(r); t.setDaemon(true); @@ -96,6 +111,8 @@ public class HeartbeatManager extends ModuleInitializedListenerBase { } } serverMapper.updateServerHeartbeatTimeByServerId(Config.SCHEDULE_SERVER_ID); + serverMapper.insertServerRunTime(Config.SCHEDULE_SERVER_ID, START_TIME); + insertTenantServerRunTime(); } catch (Exception e) { logger.error(e.getMessage(), e); } @@ -135,6 +152,15 @@ public class HeartbeatManager extends ModuleInitializedListenerBase { return returnVal; } + private void insertTenantServerRunTime() { + List tenantList = tenantMapper.getAllActiveTenant(); + for (TenantVo tenantVo : tenantList) { + TenantContext.get().switchTenant(tenantVo.getUuid()); + tenantServerMapper.insertTenantServerRunTime(Config.SCHEDULE_SERVER_ID, START_TIME); + } + TenantContext.get().setUseDefaultDatasource(true); + } + @Override public void onInitialized(NeatLogicWebApplicationContext context) { // 找出所有实现ServerObserver接口的类 diff --git a/src/main/java/neatlogic/framework/heartbeat/dao/mapper/ServerMapper.java b/src/main/java/neatlogic/framework/heartbeat/dao/mapper/ServerMapper.java index 1ee9cf6e89d6c8efdea07095e3f512665fc9933f..62331de9fd90489ef4aa782f4fa5fc454fd90427 100644 --- a/src/main/java/neatlogic/framework/heartbeat/dao/mapper/ServerMapper.java +++ b/src/main/java/neatlogic/framework/heartbeat/dao/mapper/ServerMapper.java @@ -1,11 +1,11 @@ package neatlogic.framework.heartbeat.dao.mapper; -import java.util.List; - -import org.apache.ibatis.annotations.Param; - import neatlogic.framework.heartbeat.dto.ServerClusterVo; import neatlogic.framework.heartbeat.dto.ServerCounterVo; +import org.apache.ibatis.annotations.Param; + +import java.util.Date; +import java.util.List; public interface ServerMapper { //SELECT @@ -27,7 +27,8 @@ public interface ServerMapper { int insertServer(ServerClusterVo server); int insertServerCounter(ServerCounterVo serverCounter); - + + int insertServerRunTime(@Param("serverId") Integer serverId, @Param("startTime") Date startTime); //DELETE int deleteCounterByToServerId(int serverId); } diff --git a/src/main/java/neatlogic/framework/heartbeat/dao/mapper/ServerMapper.xml b/src/main/java/neatlogic/framework/heartbeat/dao/mapper/ServerMapper.xml index 9b17050b4a9d703e4da2115e506317185a951100..4d3c2fa096fb9d9eb5375716dcb9087f41eefd27 100644 --- a/src/main/java/neatlogic/framework/heartbeat/dao/mapper/ServerMapper.xml +++ b/src/main/java/neatlogic/framework/heartbeat/dao/mapper/ServerMapper.xml @@ -78,6 +78,12 @@ ON DUPLICATE KEY UPDATE `counter` = `counter` + 1 + + INSERT INTO `server_run_time`(`server_id`, `start_time`, `heartbeat_time`) + VALUES(#{serverId}, #{startTime}, NOW(3)) + ON DUPLICATE KEY UPDATE `heartbeat_time` = NOW(3) + + DELETE FROM `server_counter` WHERE `to_server_id` = #{value} diff --git a/src/main/java/neatlogic/framework/heartbeat/dao/mapper/TenantServerMapper.java b/src/main/java/neatlogic/framework/heartbeat/dao/mapper/TenantServerMapper.java new file mode 100644 index 0000000000000000000000000000000000000000..dc3dfa15fc18261f1e9ba97ee6528cbf09976c8e --- /dev/null +++ b/src/main/java/neatlogic/framework/heartbeat/dao/mapper/TenantServerMapper.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2024 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.framework.heartbeat.dao.mapper; + +import org.apache.ibatis.annotations.Param; + +import java.util.Date; + +public interface TenantServerMapper { + + int insertTenantServerRunTime(@Param("serverId") Integer serverId, @Param("startTime") Date startTime); +} diff --git a/src/main/java/neatlogic/framework/heartbeat/dao/mapper/TenantServerMapper.xml b/src/main/java/neatlogic/framework/heartbeat/dao/mapper/TenantServerMapper.xml new file mode 100644 index 0000000000000000000000000000000000000000..c96b063f8d451dd8a570f005658457655b34b98e --- /dev/null +++ b/src/main/java/neatlogic/framework/heartbeat/dao/mapper/TenantServerMapper.xml @@ -0,0 +1,27 @@ + + + + + + + + INSERT INTO `tenant_server_run_time`(`server_id`, `start_time`, `heartbeat_time`) + VALUES(#{serverId}, #{startTime}, NOW(3)) + ON DUPLICATE KEY UPDATE `heartbeat_time` = NOW(3) + + \ No newline at end of file diff --git a/src/main/resources/neatlogic/resources/framework/changelog/2024-08-01/neatlogic.sql b/src/main/resources/neatlogic/resources/framework/changelog/2024-08-01/neatlogic.sql new file mode 100644 index 0000000000000000000000000000000000000000..8ec6d6839d7dedcccaf32019f72fa09effc893c2 --- /dev/null +++ b/src/main/resources/neatlogic/resources/framework/changelog/2024-08-01/neatlogic.sql @@ -0,0 +1,6 @@ +CREATE TABLE IF NOT EXISTS `server_run_time` ( + `start_time` TIMESTAMP(3) NOT NULL COMMENT '服务器启动时间', + `server_id` INT NOT NULL COMMENT '服务器ID', + `heartbeat_time` TIMESTAMP(3) NOT NULL COMMENT '心跳时间', + PRIMARY KEY (`start_time`,`server_id`) +) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='服务器运行时间表'; \ No newline at end of file diff --git a/src/main/resources/neatlogic/resources/framework/changelog/2024-08-01/neatlogic_tenant.sql b/src/main/resources/neatlogic/resources/framework/changelog/2024-08-01/neatlogic_tenant.sql new file mode 100644 index 0000000000000000000000000000000000000000..ab892f866ee66f6e40185816991e0ab226574631 --- /dev/null +++ b/src/main/resources/neatlogic/resources/framework/changelog/2024-08-01/neatlogic_tenant.sql @@ -0,0 +1,6 @@ +CREATE TABLE IF NOT EXISTS `tenant_server_run_time` ( + `start_time` TIMESTAMP(3) NOT NULL COMMENT '服务器启动时间', + `server_id` INT NOT NULL COMMENT '服务器ID', + `heartbeat_time` TIMESTAMP(3) NOT NULL COMMENT '心跳时间', + PRIMARY KEY (`start_time`,`server_id`) +) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='租户服务器运行时间表'; \ No newline at end of file diff --git a/src/main/resources/neatlogic/resources/framework/changelog/2024-08-01/version.json b/src/main/resources/neatlogic/resources/framework/changelog/2024-08-01/version.json new file mode 100644 index 0000000000000000000000000000000000000000..83c47dcdca4dadfc57fed554e4d90e8db33ed3bb --- /dev/null +++ b/src/main/resources/neatlogic/resources/framework/changelog/2024-08-01/version.json @@ -0,0 +1,10 @@ +{ + "content":[ + { + "type":"新增功能", + "detail":[ + {"msg":"1.增加记录服务器运行时间表server_run_time"} + ] + } + ] +} diff --git a/src/main/resources/neatlogic/resources/framework/sqlscript/ddl.sql b/src/main/resources/neatlogic/resources/framework/sqlscript/ddl.sql index 65db70dcb725c5db08cc43f4f7a40c94635e233f..0a1c11f3177bbf3d952fcf756f9632ed61d3cc1f 100644 --- a/src/main/resources/neatlogic/resources/framework/sqlscript/ddl.sql +++ b/src/main/resources/neatlogic/resources/framework/sqlscript/ddl.sql @@ -1339,4 +1339,14 @@ CREATE TABLE IF NOT EXISTS `home_page_authority` ( `type` enum('common','user','team','role') COLLATE utf8mb4_general_ci NOT NULL COMMENT '类型', `uuid` char(32) COLLATE utf8mb4_general_ci NOT NULL COMMENT 'UUID', PRIMARY KEY (`home_page_id`,`type`,`uuid`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='首页设置授权表'; \ No newline at end of file +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='首页设置授权表'; + +-- ---------------------------- +-- Table structure for tenant_server_run_time +-- ---------------------------- +CREATE TABLE IF NOT EXISTS `tenant_server_run_time` ( + `start_time` TIMESTAMP(3) NOT NULL COMMENT '服务器启动时间', + `server_id` INT NOT NULL COMMENT '服务器ID', + `heartbeat_time` TIMESTAMP(3) NOT NULL COMMENT '心跳时间', + PRIMARY KEY (`start_time`,`server_id`) +) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='租户服务器运行时间表'; \ No newline at end of file