Commit 6d764c30 by inrgihc

补充代码逻辑

parent b369724d
...@@ -34,7 +34,6 @@ logging: ...@@ -34,7 +34,6 @@ logging:
eureka: eureka:
instance: instance:
ip-address: ${MANAGER_HOST}
prefer-ip-address: true prefer-ip-address: true
secure-port-enabled: false secure-port-enabled: false
non-secure-port-enabled: true non-secure-port-enabled: true
......
...@@ -67,7 +67,6 @@ logging: ...@@ -67,7 +67,6 @@ logging:
eureka: eureka:
instance: instance:
ip-address: ${MANAGER_HOST}
prefer-ip-address: true prefer-ip-address: true
secure-port-enabled: false secure-port-enabled: false
non-secure-port-enabled: true non-secure-port-enabled: true
......
...@@ -20,6 +20,8 @@ import java.nio.file.Paths; ...@@ -20,6 +20,8 @@ import java.nio.file.Paths;
import java.security.CodeSource; import java.security.CodeSource;
import java.security.ProtectionDomain; import java.security.ProtectionDomain;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
...@@ -34,7 +36,13 @@ public final class PomVersionUtils { ...@@ -34,7 +36,13 @@ public final class PomVersionUtils {
private static final String PREFIX = "version="; 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; Class<?> clazz = PomVersionUtils.class;
String resourcePath = clazz.getResource("").toString(); String resourcePath = clazz.getResource("").toString();
if (resourcePath.startsWith("file:")) { 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 { ...@@ -132,7 +132,11 @@ public class ClientTokenService {
} else if (appClient.getExpireAt() == 0) { } else if (appClient.getExpireAt() == 0) {
clientToken.setExpireSeconds(0L); clientToken.setExpireSeconds(0L);
} else { } else {
clientToken.setExpireSeconds(-1L); if (AliveTimeEnum.LONGEVITY.equals(appClient.getTokenAlive())) {
clientToken.setExpireSeconds(-1L);
} else {
clientToken.setExpireSeconds(Constants.CLIENT_TOKEN_DURATION_SECONDS);
}
} }
// 将token持久化到数据库中,以备重启服务器后原token继续可用 // 将token持久化到数据库中,以备重启服务器后原token继续可用
......
...@@ -18,8 +18,10 @@ import com.zaxxer.hikari.HikariDataSource; ...@@ -18,8 +18,10 @@ import com.zaxxer.hikari.HikariDataSource;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.sql.Connection; import java.sql.Connection;
import java.sql.Statement; import java.sql.Statement;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
...@@ -80,6 +82,10 @@ public final class DataSourceUtils { ...@@ -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) { public static HikariDataSource createDataSource(DataSourceEntity properties, String driverPath) {
Properties parameters = new Properties(); Properties parameters = new Properties();
HikariDataSource ds = new HikariDataSource(); HikariDataSource ds = new HikariDataSource();
......
...@@ -34,7 +34,6 @@ logging: ...@@ -34,7 +34,6 @@ logging:
eureka: eureka:
instance: instance:
ip-address: ${MANAGER_HOST}
prefer-ip-address: true prefer-ip-address: true
secure-port-enabled: false secure-port-enabled: false
non-secure-port-enabled: true non-secure-port-enabled: true
......
...@@ -67,7 +67,6 @@ logging: ...@@ -67,7 +67,6 @@ logging:
eureka: eureka:
instance: instance:
ip-address: ${MANAGER_HOST}
prefer-ip-address: true prefer-ip-address: true
secure-port-enabled: false secure-port-enabled: false
non-secure-port-enabled: true non-secure-port-enabled: true
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
///////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////
package com.gitee.sqlrest.manager.config; 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.CommonException;
import com.gitee.sqlrest.common.exception.ResponseErrorCode; import com.gitee.sqlrest.common.exception.ResponseErrorCode;
import com.gitee.sqlrest.common.util.CacheUtils; import com.gitee.sqlrest.common.util.CacheUtils;
...@@ -53,6 +54,7 @@ public class AuthenticationConfiguration implements WebMvcConfigurer { ...@@ -53,6 +54,7 @@ public class AuthenticationConfiguration implements WebMvcConfigurer {
"/actuator/**", "/actuator/**",
"/eureka/**", "/eureka/**",
"/error**", "/error**",
Constants.MANGER_API_V1 + "/health/**",
"/dashboard", "/dashboard",
"/dashboard/**" "/dashboard/**"
)) ))
......
...@@ -62,7 +62,7 @@ public class McpServerConfiguration { ...@@ -62,7 +62,7 @@ public class McpServerConfiguration {
@Bean @Bean
public McpSyncServer mcpSyncServer(WebMvcSseServerTransportProvider transportProvider) { public McpSyncServer mcpSyncServer(WebMvcSseServerTransportProvider transportProvider) {
McpSyncServer syncServer = McpServer.sync(transportProvider) McpSyncServer syncServer = McpServer.sync(transportProvider)
.serverInfo(Constants.MCP_SERVER_NAME, PomVersionUtils.getProjectVersion()) .serverInfo(Constants.MCP_SERVER_NAME, PomVersionUtils.getCachedProjectVersion())
.capabilities( .capabilities(
ServerCapabilities.builder() ServerCapabilities.builder()
.resources(true, true) .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 @@ ...@@ -9,11 +9,14 @@
///////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////
package com.gitee.sqlrest.persistence.dao; 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.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gitee.sqlrest.persistence.entity.DataSourceEntity; import com.gitee.sqlrest.persistence.entity.DataSourceEntity;
import com.gitee.sqlrest.persistence.mapper.DataSourceMapper; import com.gitee.sqlrest.persistence.mapper.DataSourceMapper;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Resource; import javax.annotation.Resource;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -46,6 +49,13 @@ public class DataSourceDao { ...@@ -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) { public void updateById(DataSourceEntity databaseConnectionEntity) {
dataSourceMapper.updateById(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