添加健康检查接口

- 新增 /health 健康检查端点,返回服务状态信息
- 新增 /health/ping 简单检查端点,返回 pong 响应
- 配置 Spring Security 允许健康检查端点无需认证访问
- 支持负载均衡器和监控系统进行服务可用性检查
This commit is contained in:
irving
2025-08-30 15:03:56 -04:00
parent f4c412be9d
commit fd514edf60
3 changed files with 69 additions and 0 deletions

18
fetch-log.sh Executable file
View File

@@ -0,0 +1,18 @@
#!/bin/sh
# 拉取日志脚本
set -e
# 获取当前时间并格式化为指定格式
current_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "开始拉取日志,当前时间是:$current_time"
# 创建本地log目录如果不存在
mkdir -p ./log
# 从远程服务器拉取整个log文件夹
echo "正在从远程服务器拉取日志文件..."
scp -r root@122.51.20.105:/www/wwwroot/july.hucs.top/log/* ./log/
# 获取当前时间并格式化为指定格式
current_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "日志拉取完成,当前时间是:$current_time"

View File

@@ -59,6 +59,8 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers(HttpMethod.GET, "/", "/*.html", "/favicon.ico", "/**/*.html", "/**/*.css", "/**/*.js", "/swagger-resources/**", "/v2/api-docs/**").permitAll() .antMatchers(HttpMethod.GET, "/", "/*.html", "/favicon.ico", "/**/*.html", "/**/*.css", "/**/*.js", "/swagger-resources/**", "/v2/api-docs/**").permitAll()
// 对登录注册要允许匿名访问 // 对登录注册要允许匿名访问
.antMatchers("/login", "/captcha/get-captcha", "/wx/**").permitAll() .antMatchers("/login", "/captcha/get-captcha", "/wx/**").permitAll()
// 允许健康检查接口匿名访问
.antMatchers("/health", "/health/**").permitAll()
// 跨域请求会先进行一次options请求 // 跨域请求会先进行一次options请求
.antMatchers(HttpMethod.OPTIONS).permitAll().anyRequest()// 除上面外的所有请求全部需要鉴权认证 .antMatchers(HttpMethod.OPTIONS).permitAll().anyRequest()// 除上面外的所有请求全部需要鉴权认证
.authenticated().and().cors().configurationSource(this.corsConfigurationSource()); .authenticated().and().cors().configurationSource(this.corsConfigurationSource());

View File

@@ -0,0 +1,49 @@
package com.starry.admin.modules.system.controller;
import com.starry.common.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
/**
* Health Check Controller
* @author admin
* @since 2025/08/30
*/
@Api(tags = "健康检查", description = "服务健康状态检查接口")
@RestController
@RequestMapping("/health")
public class HealthController {
@ApiOperation(value = "健康检查", notes = "检查服务是否正常运行")
@ApiResponses({
@ApiResponse(code = 200, message = "服务正常运行")
})
@GetMapping
public R health() {
Map<String, Object> data = new HashMap<>();
data.put("status", "UP");
data.put("message", "Hello, I'm alive!");
data.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
data.put("service", "peipei-backend");
return R.ok(data);
}
@ApiOperation(value = "简单检查", notes = "最简单的存活检查")
@ApiResponses({
@ApiResponse(code = 200, message = "服务正常")
})
@GetMapping("/ping")
public R ping() {
return R.ok("pong");
}
}