Phase 1 #17: Spring Cloud Alibaba 微服务框架搭建
- Maven 父工程 wm-parent (Spring Boot 3.3.5 + Cloud 2023.0.3 + Alibaba 2023.0.1) - 12个子模块: wm-common/gateway/base/iot/data-engine/bpm/production/revenue/patrol/bi/notify/job - wm-common: 统一响应 R<T> + 全局异常处理 + BusinessException - wm-gateway: Spring Cloud Gateway 路由配置(7个微服务路由) - 集成: Nacos(注册/配置) + PostgreSQL + Redis + Kafka + Sa-Token + MyBatis-Plus + Knife4j - docker-compose.yml: 一键启动10个中间件(PG+PostGIS/TDengine/Redis/Kafka/EMQX/Nacos/ES/Kibana/MinIO/GeoServer)
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package com.water.common.core.exception;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class BusinessException extends RuntimeException {
|
||||
private final int code;
|
||||
public BusinessException(int code, String message) { super(message); this.code = code; }
|
||||
public BusinessException(String message) { this(400, message); }
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.water.common.core.exception;
|
||||
|
||||
import com.water.common.core.result.R;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(BusinessException.class)
|
||||
public R<Void> handleBusiness(BusinessException e, HttpServletRequest request) {
|
||||
log.warn("Business exception: {} - {}", e.getCode(), e.getMessage());
|
||||
return R.fail(e.getCode(), e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public R<Void> handleException(Exception e, HttpServletRequest request) {
|
||||
log.error("System exception: {}", e.getMessage(), e);
|
||||
return R.fail(500, "系统内部错误");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.water.common.core.result;
|
||||
|
||||
import lombok.Data;
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
public class R<T> implements Serializable {
|
||||
|
||||
private int code;
|
||||
private String message;
|
||||
private T data;
|
||||
private long timestamp = System.currentTimeMillis();
|
||||
|
||||
public static <T> R<T> ok() {
|
||||
return ok(null);
|
||||
}
|
||||
|
||||
public static <T> R<T> ok(T data) {
|
||||
R<T> r = new R<>();
|
||||
r.code = 200;
|
||||
r.message = "success";
|
||||
r.data = data;
|
||||
return r;
|
||||
}
|
||||
|
||||
public static <T> R<T> fail(int code, String message) {
|
||||
R<T> r = new R<>();
|
||||
r.code = code;
|
||||
r.message = message;
|
||||
return r;
|
||||
}
|
||||
|
||||
public static <T> R<T> fail(String message) {
|
||||
return fail(500, message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user