启动输出,h2
This commit is contained in:
parent
46cb97b675
commit
699c514c41
@ -40,6 +40,54 @@ public interface BaseStartupOutput extends CommandLineRunner {
|
||||
return this.getClass().getSimpleName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取服务器端口
|
||||
*
|
||||
* @param environment 环境配置
|
||||
* @return 端口号
|
||||
*/
|
||||
default String getServerPort(Environment environment) {
|
||||
return environment.getProperty("server.port", "8080");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上下文路径
|
||||
*
|
||||
* @param environment 环境配置
|
||||
* @return 上下文路径
|
||||
*/
|
||||
default String getContextPath(Environment environment) {
|
||||
return environment.getProperty("server.servlet.context-path", "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取基础URL
|
||||
*
|
||||
* @param environment 环境配置
|
||||
* @return 基础URL
|
||||
*/
|
||||
default String getBaseUrl(Environment environment) {
|
||||
String port = getServerPort(environment);
|
||||
String path = getContextPath(environment);
|
||||
return "http://localhost:" + port + path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取完整的API URL
|
||||
*
|
||||
* @param environment 环境配置
|
||||
* @param apiPath API路径
|
||||
* @return 完整的API URL
|
||||
*/
|
||||
default String getApiUrl(Environment environment, String apiPath) {
|
||||
String baseUrl = getBaseUrl(environment);
|
||||
if (apiPath.startsWith("/")) {
|
||||
return baseUrl + apiPath;
|
||||
} else {
|
||||
return baseUrl + "/" + apiPath;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动输出执行方法
|
||||
*
|
||||
|
@ -35,17 +35,10 @@ public class ApiDocOutput implements BaseStartupOutput {
|
||||
*/
|
||||
@Override
|
||||
public void outputOnStartup(Environment environment, ApplicationContext context) {
|
||||
String port = environment.getProperty("server.port", "8080");
|
||||
String path = environment.getProperty("server.servlet.context-path", "");
|
||||
|
||||
String apiDocUrl = "http://localhost:" + port + path + "/doc.html";
|
||||
String swaggerUrl = "http://localhost:" + port + path + "/swagger-ui.html";
|
||||
|
||||
System.out.println("\n📚 API文档地址:");
|
||||
// 使用接口提供的通用方法获取API文档地址
|
||||
String apiDocUrl = getApiUrl(environment, "/doc.html");
|
||||
System.out.println("📚 API文档地址:");
|
||||
System.out.println(" • Knife4j文档: " + apiDocUrl);
|
||||
log.trace("API文档地址输出完成 - Knife4j: {}", apiDocUrl);
|
||||
|
||||
// System.out.println(" • Swagger文档: " + swaggerUrl);
|
||||
// log.trace("API文档地址输出完成 - Knife4j: {}, Swagger: {}", apiDocUrl, swaggerUrl);
|
||||
}
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
package com.example.demo.common.output;
|
||||
|
||||
import com.example.demo.common.base.BaseStartupOutput;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 应用配置信息输出
|
||||
* 在启动后输出应用配置信息
|
||||
*
|
||||
* @author 岳佳君 (2025年09月29日 16:45:00)
|
||||
* @version 1.0.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AppConfigOutput implements BaseStartupOutput {
|
||||
|
||||
/**
|
||||
* 获取执行优先级
|
||||
*
|
||||
* @return 优先级(数值越小优先级越高)
|
||||
*/
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动输出执行方法
|
||||
*
|
||||
* @param environment 环境配置
|
||||
* @param context 应用上下文
|
||||
*/
|
||||
@Override
|
||||
public void outputOnStartup(Environment environment, ApplicationContext context) {
|
||||
System.out.println("\n🚀 XML解析器服务启动成功");
|
||||
System.out.println("📋 应用配置信息:");
|
||||
|
||||
// 服务器配置
|
||||
String port = environment.getProperty("server.port", "8080");
|
||||
String contextPath = environment.getProperty("server.servlet.context-path", "");
|
||||
String baseUrl = "http://localhost:" + port + contextPath;
|
||||
|
||||
System.out.println(" • 服务端口: " + port);
|
||||
System.out.println(" • 上下文路径: " + (contextPath.isEmpty() ? "/" : contextPath));
|
||||
System.out.println(" • 访问地址: " + baseUrl);
|
||||
|
||||
// 激活的Profile
|
||||
String[] activeProfiles = environment.getActiveProfiles();
|
||||
if (activeProfiles.length > 0) {
|
||||
System.out.println(" • 激活配置: " + String.join(", ", activeProfiles));
|
||||
} else {
|
||||
System.out.println(" • 激活配置: default");
|
||||
}
|
||||
|
||||
// 应用信息
|
||||
String appName = environment.getProperty("spring.application.name", "xml-to-txt-service");
|
||||
String appVersion = environment.getProperty("app.version", "1.0.0");
|
||||
|
||||
System.out.println(" • 应用名称: " + appName);
|
||||
System.out.println(" • 应用版本: " + appVersion);
|
||||
|
||||
log.trace("应用配置信息输出完成 - 端口: {}, 上下文: {}", port, contextPath);
|
||||
}
|
||||
}
|
@ -1,80 +0,0 @@
|
||||
package com.example.demo.common.output;
|
||||
|
||||
import com.example.demo.common.base.BaseStartupOutput;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* 数据库连接状态输出
|
||||
* 在启动后输出数据库连接状态
|
||||
*
|
||||
* @author 岳佳君 (2025年09月29日 16:45:00)
|
||||
* @version 1.0.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class DatabaseStatusOutput implements BaseStartupOutput {
|
||||
|
||||
/**
|
||||
* 获取执行优先级
|
||||
*
|
||||
* @return 优先级(数值越小优先级越高)
|
||||
*/
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 5;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动输出执行方法
|
||||
*
|
||||
* @param environment 环境配置
|
||||
* @param context 应用上下文
|
||||
*/
|
||||
@Override
|
||||
public void outputOnStartup(Environment environment, ApplicationContext context) {
|
||||
System.out.println("\n🗄️ 数据库连接状态:");
|
||||
|
||||
// 检查SQLite数据库连接
|
||||
checkDataSource("SQLite", "sqlite", context);
|
||||
|
||||
// 检查H2数据库连接
|
||||
checkDataSource("H2", "h2", context);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查数据源连接状态
|
||||
*
|
||||
* @param dbName 数据库名称
|
||||
* @param beanName Bean名称
|
||||
* @param context 应用上下文
|
||||
*/
|
||||
private void checkDataSource(String dbName, String beanName, ApplicationContext context) {
|
||||
try {
|
||||
DataSource dataSource = context.getBean(beanName + "DataSource", DataSource.class);
|
||||
if (dataSource != null) {
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
if (connection.isValid(5)) {
|
||||
System.out.println(" ✅ " + dbName + "数据库: 连接正常");
|
||||
log.trace("{}数据库连接正常", dbName);
|
||||
} else {
|
||||
System.out.println(" ❌ " + dbName + "数据库: 连接无效");
|
||||
log.trace("{}数据库连接无效", dbName);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
System.out.println(" ⚠️ " + dbName + "数据库: 未配置");
|
||||
log.info("{}数据库未配置", dbName);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println(" ❌ " + dbName + "数据库: 连接失败 - " + e.getMessage());
|
||||
log.warn("{}数据库连接失败: {}", dbName, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
154
src/main/java/com/example/demo/common/output/H2Output.java
Normal file
154
src/main/java/com/example/demo/common/output/H2Output.java
Normal file
@ -0,0 +1,154 @@
|
||||
package com.example.demo.common.output;
|
||||
|
||||
import com.example.demo.common.base.BaseStartupOutput;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* H2数据库控制台地址和连通情况输出
|
||||
* 在启动后输出H2控制台访问地址并检查数据库连通情况
|
||||
* 仅在h2激活环境下生效
|
||||
*
|
||||
* @author 岳佳君 (2025年09月29日 16:45:00)
|
||||
* @version 1.0.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class H2Output implements BaseStartupOutput {
|
||||
|
||||
/**
|
||||
* 获取执行优先级
|
||||
*
|
||||
* @return 优先级(数值越小优先级越高)
|
||||
*/
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 8;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否启用H2输出
|
||||
* 仅在h2激活环境下生效
|
||||
*
|
||||
* @return 是否启用
|
||||
*/
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return true; // 由outputOnStartup方法内部检查环境
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动输出执行方法
|
||||
*
|
||||
* @param environment 环境配置
|
||||
* @param context 应用上下文
|
||||
*/
|
||||
@Override
|
||||
public void outputOnStartup(Environment environment, ApplicationContext context) {
|
||||
// 检查是否在h2激活环境下
|
||||
if (!isH2Active(environment)) {
|
||||
log.debug("H2环境未激活,跳过H2控制台输出");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("💾 H2数据库信息:");
|
||||
|
||||
// 输出H2控制台地址
|
||||
String h2ConsoleUrl = getApiUrl(environment, "/h2-console");
|
||||
System.out.println(" • H2控制台地址: " + h2ConsoleUrl);
|
||||
|
||||
// 检查H2数据库连通情况
|
||||
checkH2Connection(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查H2环境是否激活
|
||||
*
|
||||
* @param environment 环境配置
|
||||
* @return 是否激活
|
||||
*/
|
||||
private boolean isH2Active(Environment environment) {
|
||||
String[] activeProfiles = environment.getActiveProfiles();
|
||||
for (String profile : activeProfiles) {
|
||||
if ("h2".equalsIgnoreCase(profile)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否配置了H2数据源
|
||||
String h2Url = environment.getProperty("spring.datasource.h2.url");
|
||||
String h2Enabled = environment.getProperty("spring.h2.console.enabled");
|
||||
|
||||
return h2Url != null || "true".equals(h2Enabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查H2数据库连通情况
|
||||
*
|
||||
* @param context 应用上下文
|
||||
*/
|
||||
private void checkH2Connection(ApplicationContext context) {
|
||||
try {
|
||||
// 尝试获取H2数据源
|
||||
DataSource h2DataSource = null;
|
||||
|
||||
// 首先尝试按名称获取
|
||||
if (context.containsBean("h2DataSource")) {
|
||||
h2DataSource = context.getBean("h2DataSource", DataSource.class);
|
||||
} else if (context.containsBean("dataSource")) {
|
||||
// 尝试获取默认数据源
|
||||
DataSource dataSource = context.getBean("dataSource", DataSource.class);
|
||||
// 检查是否是H2数据源
|
||||
String url = getDataSourceUrl(dataSource);
|
||||
if (url != null && url.contains(":h2:")) {
|
||||
h2DataSource = dataSource;
|
||||
}
|
||||
}
|
||||
|
||||
if (h2DataSource != null) {
|
||||
try (Connection connection = h2DataSource.getConnection()) {
|
||||
if (connection.isValid(5)) {
|
||||
System.out.println(" • H2数据库连接: ✅ 正常");
|
||||
log.debug("H2数据库连接正常");
|
||||
} else {
|
||||
System.out.println(" • H2数据库连接: ❌ 无效");
|
||||
log.warn("H2数据库连接无效");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
System.out.println(" • H2数据库连接: ⚠️ 未配置");
|
||||
log.info("H2数据库未配置");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.out.println(" • H2数据库连接: ❌ 失败 - " + e.getMessage());
|
||||
log.error("H2数据库连接失败: {}", e.getMessage());
|
||||
} catch (Exception e) {
|
||||
System.out.println(" • H2数据库连接: ❌ 检查失败 - " + e.getMessage());
|
||||
log.error("H2数据库检查失败: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据源的URL
|
||||
*
|
||||
* @param dataSource 数据源
|
||||
* @return URL字符串
|
||||
*/
|
||||
private String getDataSourceUrl(DataSource dataSource) {
|
||||
try {
|
||||
// 尝试通过连接获取URL
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
return connection.getMetaData().getURL();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.debug("无法获取数据源URL: {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
# 服务器配置
|
||||
server:
|
||||
port: 19290
|
||||
# servlet:
|
||||
# context-path: /parser
|
||||
servlet:
|
||||
context-path: /parser
|
||||
|
||||
# 通用 log 输出配置
|
||||
logging:
|
||||
@ -21,8 +21,6 @@ spring:
|
||||
max-file-size: 10MB
|
||||
max-request-size: 10MB
|
||||
profiles:
|
||||
# active: orm,h2,sqlite,springdoc
|
||||
# active: ${spring.profiles.group.dev}
|
||||
active: dev
|
||||
group:
|
||||
dev: orm,h2,sqlite,springdoc
|
||||
|
Loading…
x
Reference in New Issue
Block a user