Commit 6d764c30 by inrgihc

补充代码逻辑

parent b369724d
......@@ -34,7 +34,6 @@ logging:
eureka:
instance:
ip-address: ${MANAGER_HOST}
prefer-ip-address: true
secure-port-enabled: false
non-secure-port-enabled: true
......
......@@ -67,7 +67,6 @@ logging:
eureka:
instance:
ip-address: ${MANAGER_HOST}
prefer-ip-address: true
secure-port-enabled: false
non-secure-port-enabled: true
......
......@@ -20,6 +20,8 @@ import java.nio.file.Paths;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.Enumeration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import lombok.experimental.UtilityClass;
......@@ -34,7 +36,13 @@ public final class PomVersionUtils {
private static final String PREFIX = "version=";
public static String getProjectVersion() {
private static final Map<String, String> persistCache = new ConcurrentHashMap<>();
public static String getCachedProjectVersion() {
return persistCache.computeIfAbsent(PREFIX, key -> getProjectVersion());
}
private static String getProjectVersion() {
Class<?> clazz = PomVersionUtils.class;
String resourcePath = clazz.getResource("").toString();
if (resourcePath.startsWith("file:")) {
......
// Copyright tang. All rights reserved.
// https://gitee.com/inrgihc/sqlrest
//
// Use of this source code is governed by a BSD-style license
//
// Author: tang (inrgihc@126.com)
// Date : 2024/3/31
// Location: beijing , china
/////////////////////////////////////////////////////////////
package com.gitee.sqlrest.core.executor;
import com.gitee.sqlrest.core.util.DataSourceUtils;
import com.gitee.sqlrest.persistence.dao.DataSourceDao;
import java.util.Set;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class DataSourceCleanService {
@Resource
private DataSourceDao dataSourceDao;
/*每小时执行一次*/
@EventListener(ApplicationReadyEvent.class)
@Scheduled(cron = "${cron.datasource.clean.expression:0 0 0/1 * * ? }")
public void autoClean() {
log.info("Start check deleted datasource for close it ...");
try {
Set<Long> running = DataSourceUtils.getAllDataSourceIdSet();
Set<Long> exists = dataSourceDao.getAllIdList();
for (Long id : running) {
if (!exists.contains(id)) {
DataSourceUtils.dropHikariDataSource(id);
}
}
} catch (Exception e) {
log.warn("Failed to auto clean deleted datasource,error: {}", e.getMessage(), e);
}
}
}
......@@ -132,7 +132,11 @@ public class ClientTokenService {
} else if (appClient.getExpireAt() == 0) {
clientToken.setExpireSeconds(0L);
} else {
if (AliveTimeEnum.LONGEVITY.equals(appClient.getTokenAlive())) {
clientToken.setExpireSeconds(-1L);
} else {
clientToken.setExpireSeconds(Constants.CLIENT_TOKEN_DURATION_SECONDS);
}
}
// 将token持久化到数据库中,以备重启服务器后原token继续可用
......
......@@ -18,8 +18,10 @@ import com.zaxxer.hikari.HikariDataSource;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import lombok.experimental.UtilityClass;
......@@ -80,6 +82,10 @@ public final class DataSourceUtils {
}
}
public static Set<Long> getAllDataSourceIdSet() {
return new HashSet<>(datasourceMap.keySet());
}
public static HikariDataSource createDataSource(DataSourceEntity properties, String driverPath) {
Properties parameters = new Properties();
HikariDataSource ds = new HikariDataSource();
......
......@@ -34,7 +34,6 @@ logging:
eureka:
instance:
ip-address: ${MANAGER_HOST}
prefer-ip-address: true
secure-port-enabled: false
non-secure-port-enabled: true
......
......@@ -67,7 +67,6 @@ logging:
eureka:
instance:
ip-address: ${MANAGER_HOST}
prefer-ip-address: true
secure-port-enabled: false
non-secure-port-enabled: true
......
......@@ -9,6 +9,7 @@
/////////////////////////////////////////////////////////////
package com.gitee.sqlrest.manager.config;
import com.gitee.sqlrest.common.consts.Constants;
import com.gitee.sqlrest.common.exception.CommonException;
import com.gitee.sqlrest.common.exception.ResponseErrorCode;
import com.gitee.sqlrest.common.util.CacheUtils;
......@@ -53,6 +54,7 @@ public class AuthenticationConfiguration implements WebMvcConfigurer {
"/actuator/**",
"/eureka/**",
"/error**",
Constants.MANGER_API_V1 + "/health/**",
"/dashboard",
"/dashboard/**"
))
......
......@@ -62,7 +62,7 @@ public class McpServerConfiguration {
@Bean
public McpSyncServer mcpSyncServer(WebMvcSseServerTransportProvider transportProvider) {
McpSyncServer syncServer = McpServer.sync(transportProvider)
.serverInfo(Constants.MCP_SERVER_NAME, PomVersionUtils.getProjectVersion())
.serverInfo(Constants.MCP_SERVER_NAME, PomVersionUtils.getCachedProjectVersion())
.capabilities(
ServerCapabilities.builder()
.resources(true, true)
......
// Copyright tang. All rights reserved.
// https://gitee.com/inrgihc/sqlrest
//
// Use of this source code is governed by a BSD-style license
//
// Author: tang (inrgihc@126.com)
// Date : 2024/3/31
// Location: beijing , china
/////////////////////////////////////////////////////////////
package com.gitee.sqlrest.manager.controller;
import com.gitee.sqlrest.common.consts.Constants;
import com.gitee.sqlrest.common.dto.ResultEntity;
import com.gitee.sqlrest.common.util.PomVersionUtils;
import io.swagger.annotations.Api;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(tags = {"存活健康接口"})
@RestController
@RequestMapping(value = Constants.MANGER_API_V1 + "/health")
public class AliveHealthController {
@GetMapping(value = "/version", produces = MediaType.APPLICATION_JSON_VALUE)
public ResultEntity<String> queryFirewallRules() {
return ResultEntity.success(PomVersionUtils.getCachedProjectVersion());
}
}
......@@ -9,11 +9,14 @@
/////////////////////////////////////////////////////////////
package com.gitee.sqlrest.persistence.dao;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gitee.sqlrest.persistence.entity.DataSourceEntity;
import com.gitee.sqlrest.persistence.mapper.DataSourceMapper;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
......@@ -46,6 +49,13 @@ public class DataSourceDao {
);
}
public Set<Long> getAllIdList() {
LambdaQueryWrapper<DataSourceEntity> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.select(DataSourceEntity::getId);
return dataSourceMapper.selectList(queryWrapper).stream().map(DataSourceEntity::getId)
.collect(Collectors.toSet());
}
public void updateById(DataSourceEntity databaseConnectionEntity) {
dataSourceMapper.updateById(databaseConnectionEntity);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment