使用 @controlleradvice 结合 @exceptionhandler 可以捕获指标采集过程中的异常。具体做法是定义一个全局异常处理器,针对 exception 或具体的采集异常类(如
ResponseEntityExceptionHandler
或直接使用 @RestControllerAdvice 注解的类来实现。在该类中定义处理特定异常的方法,Spring Boot 会自动将这些方法注册为全局异常处理器。当控制器或过滤器抛出匹配的异常时,该方法会被调用。

import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<string> handleException(Exception e) {
// 记录异常日志
System.err.println("Caught exception: " + e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("An error occurred during metric collection.");
}
}</string>
上述代码定义了一个全局异常处理器。当任何 Exception 发生时,都会进入 handleException 方法。该方法返回一个状态码为 500 的 HTTP 响应,消息体为自定义的错误信息。这确保了即使指标采集失败,API 也能返回结构化的响应,而不是直接抛出堆栈跟踪。
如何区分不同类型的采集异常
为了更精细地控制响应,可以为不同的异常类型定义单独的 @ExceptionHandler 方法。例如,可以分别处理 IllegalArgumentException 和 TimeoutException。这样可以根据异常的具体原因返回不同的 HTTP 状态码和错误消息,帮助调用者快速定位问题。

import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import java.net.SocketTimeoutException;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<string> handleIllegalArgument(IllegalArgumentException e) {
return ResponseEntity.badRequest().body("Invalid argument: " + e.getMessage());
}
@ExceptionHandler(SocketTimeoutException.class)
public ResponseEntity<string> handleTimeout(SocketTimeoutException e) {
return ResponseEntity.status(HttpStatus.GATEWAY_TIMEOUT).body("Collection timed out.");
}
@ExceptionHandler(Exception.class)
public ResponseEntity<string> handleGeneral(Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Internal error.");
}
}</string></string></string>
这段代码展示了如何针对不同异常提供定制化的响应。handleIllegalArgument 处理参数错误,返回 400 状态码;handleTimeout 处理超时异常,返回 504 状态码;而通用的 handleGeneral 则作为兜底,处理其他所有未预期的异常。这种分层处理方式提高了系统的可维护性和调试效率。
如果指标采集涉及外部服务调用,如何处理连接拒绝异常
当指标采集依赖于外部服务(如 Prometheus、InfluxDB 等)时,可能会遇到 ConnectException 或 NoRouteToHostException。可以在全局异常处理器中添加对这些网络异常的捕获,返回适当的错误信息,提示用户检查网络连接或服务状态。
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import java.net.ConnectException;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ConnectException.class)
public ResponseEntity<string> handleConnectionError(ConnectException e) {
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body("Service unavailable: " + e.getMessage());
}
@ExceptionHandler(Exception.class)
public ResponseEntity<string> handleGeneral(Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Internal error.");
}
}</string></string>
此代码片段专门处理 ConnectException,当外部服务不可达时,返回 503 Service Unavailable 状态码。这有助于前端或监控系统识别出这是暂时性的服务故障,而非应用程序本身的逻辑错误。同时,保留了通用的异常处理作为后备方案,确保所有异常都能被妥善管理。











