feat(core): 初始化支付系统核心配置与启动模块
- 添加Druid主从数据源配置属性类 - 创建支付系统主启动类MtkjpayApplication - 配置mt-startup模块pom.xml引入必要依赖 - 添加RestClient配置类用于HTTP请求 - 创建统一响应结果类Result及响应码枚举 - 实现美观的系统启动成功/失败日志输出 - 配置Lombok注解处理器支持
This commit is contained in:
106
mt-pay/src/main/java/com/mtkj/mtpay/common/Result.java
Normal file
106
mt-pay/src/main/java/com/mtkj/mtpay/common/Result.java
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
package com.mtkj.mtpay.common;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 统一响应结果类
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class Result<T> implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 响应码
|
||||||
|
*/
|
||||||
|
private String code;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 响应消息
|
||||||
|
*/
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 响应数据
|
||||||
|
*/
|
||||||
|
private T data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 时间戳
|
||||||
|
*/
|
||||||
|
private Long timestamp;
|
||||||
|
|
||||||
|
public Result() {
|
||||||
|
this.timestamp = System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result(String code, String message) {
|
||||||
|
this();
|
||||||
|
this.code = code;
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result(String code, String message, T data) {
|
||||||
|
this(code, message);
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 成功响应
|
||||||
|
*/
|
||||||
|
public static <T> Result<T> success() {
|
||||||
|
return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 成功响应(带数据)
|
||||||
|
*/
|
||||||
|
public static <T> Result<T> success(T data) {
|
||||||
|
return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 成功响应(自定义消息)
|
||||||
|
*/
|
||||||
|
public static <T> Result<T> success(String message) {
|
||||||
|
return new Result<>(ResultCode.SUCCESS.getCode(), message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 成功响应(自定义消息和数据)
|
||||||
|
*/
|
||||||
|
public static <T> Result<T> success(String message, T data) {
|
||||||
|
return new Result<>(ResultCode.SUCCESS.getCode(), message, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 失败响应
|
||||||
|
*/
|
||||||
|
public static <T> Result<T> fail() {
|
||||||
|
return new Result<>(ResultCode.FAIL.getCode(), ResultCode.FAIL.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 失败响应(自定义消息)
|
||||||
|
*/
|
||||||
|
public static <T> Result<T> fail(String message) {
|
||||||
|
return new Result<>(ResultCode.FAIL.getCode(), message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 失败响应(自定义码和消息)
|
||||||
|
*/
|
||||||
|
public static <T> Result<T> fail(String code, String message) {
|
||||||
|
return new Result<>(code, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 失败响应(使用ResultCode)
|
||||||
|
*/
|
||||||
|
public static <T> Result<T> fail(ResultCode resultCode) {
|
||||||
|
return new Result<>(resultCode.getCode(), resultCode.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.mtkj.mtpay.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Druid主数据源配置属性
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ConfigurationProperties(prefix = "spring.datasource.druid.master")
|
||||||
|
public class DruidMasterProperties {
|
||||||
|
private String url;
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
private Integer initialSize;
|
||||||
|
private Integer minIdle;
|
||||||
|
private Integer maxActive;
|
||||||
|
private Long maxWait;
|
||||||
|
private Integer connectTimeout;
|
||||||
|
private Integer socketTimeout;
|
||||||
|
private Long timeBetweenEvictionRunsMillis;
|
||||||
|
private Long minEvictableIdleTimeMillis;
|
||||||
|
private Long maxEvictableIdleTimeMillis;
|
||||||
|
private String validationQuery;
|
||||||
|
private Boolean testWhileIdle;
|
||||||
|
private Boolean testOnBorrow;
|
||||||
|
private Boolean testOnReturn;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.mtkj.mtpay.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Druid从数据源配置属性
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ConfigurationProperties(prefix = "spring.datasource.druid.slave")
|
||||||
|
public class DruidSlaveProperties {
|
||||||
|
private String url;
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
private Integer initialSize;
|
||||||
|
private Integer minIdle;
|
||||||
|
private Integer maxActive;
|
||||||
|
private Long maxWait;
|
||||||
|
private Integer connectTimeout;
|
||||||
|
private Integer socketTimeout;
|
||||||
|
private Long timeBetweenEvictionRunsMillis;
|
||||||
|
private Long minEvictableIdleTimeMillis;
|
||||||
|
private Long maxEvictableIdleTimeMillis;
|
||||||
|
private String validationQuery;
|
||||||
|
private Boolean testWhileIdle;
|
||||||
|
private Boolean testOnBorrow;
|
||||||
|
private Boolean testOnReturn;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.mtkj.mtpay.config;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||||
|
import org.springframework.web.client.RestClient;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RestClient配置
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Configuration
|
||||||
|
public class RestClientConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RestClient restClient(ObjectMapper objectMapper) {
|
||||||
|
log.info("初始化RestClient,配置JSON消息转换器");
|
||||||
|
RestClient restClient = RestClient.builder()
|
||||||
|
.messageConverters(converters -> {
|
||||||
|
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
|
||||||
|
converter.setObjectMapper(objectMapper);
|
||||||
|
converters.add(converter);
|
||||||
|
})
|
||||||
|
.build();
|
||||||
|
log.info("RestClient配置完成");
|
||||||
|
return restClient;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
82
mt-startup/pom.xml
Normal file
82
mt-startup/pom.xml
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.mtkj</groupId>
|
||||||
|
<artifactId>MTKJPAY</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<artifactId>mt-startup</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<name>mt-startup</name>
|
||||||
|
<description>MTKJ PAY 启动模块</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>17</java.version>
|
||||||
|
<lombok.version>1.18.30</lombok.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- 引入 mt-pay 模块 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mtkj</groupId>
|
||||||
|
<artifactId>mt-pay</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring Boot Web -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring Boot DevTools -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-devtools</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Lombok -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<annotationProcessorPaths>
|
||||||
|
<path>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
</path>
|
||||||
|
</annotationProcessorPaths>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</exclude>
|
||||||
|
</excludes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
|
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
package com.mtkj.mtkjpay;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MTKJ PAY 支付系统启动类
|
||||||
|
* 这是项目的唯一启动入口
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@SpringBootApplication(
|
||||||
|
scanBasePackages = {"com.mtkj.mtpay", "com.mtkj.mtkjpay"},
|
||||||
|
exclude = {
|
||||||
|
com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure.class
|
||||||
|
}
|
||||||
|
)
|
||||||
|
public class MtkjpayApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
log.info("""
|
||||||
|
|
||||||
|
╔══════════════════════════════════════════════════════════╗
|
||||||
|
║ ║
|
||||||
|
║ MTKJ PAY 支付系统正在启动... ║
|
||||||
|
║ ║
|
||||||
|
╚══════════════════════════════════════════════════════════╝
|
||||||
|
""");
|
||||||
|
|
||||||
|
SpringApplication app = new SpringApplication(MtkjpayApplication.class);
|
||||||
|
Environment env = app.run(args).getEnvironment();
|
||||||
|
|
||||||
|
String applicationName = env.getProperty("spring.application.name", "MTKJPAY");
|
||||||
|
String serverPort = env.getProperty("server.port", "8082");
|
||||||
|
String contextPath = env.getProperty("server.servlet.context-path", "");
|
||||||
|
String activeProfiles = String.join(",", env.getActiveProfiles());
|
||||||
|
|
||||||
|
// 构建完整的访问地址
|
||||||
|
String baseUrl = "http://localhost:" + serverPort + contextPath;
|
||||||
|
String apiUrl = baseUrl + "/api";
|
||||||
|
String druidUrl = baseUrl + "/druid";
|
||||||
|
|
||||||
|
// 打印醒目的启动成功标识
|
||||||
|
log.info("""
|
||||||
|
|
||||||
|
╔══════════════════════════════════════════════════════════╗
|
||||||
|
║ ║
|
||||||
|
║ ✅ MTKJ PAY 支付系统启动成功! ✅ ║
|
||||||
|
║ ║
|
||||||
|
╠══════════════════════════════════════════════════════════╣
|
||||||
|
║ 应用信息 ║
|
||||||
|
╠══════════════════════════════════════════════════════════╣
|
||||||
|
║ 应用名称: {:<45} ║
|
||||||
|
║ 运行环境: {:<45} ║
|
||||||
|
║ 服务端口: {:<45} ║
|
||||||
|
╠══════════════════════════════════════════════════════════╣
|
||||||
|
║ 访问地址 ║
|
||||||
|
╠══════════════════════════════════════════════════════════╣
|
||||||
|
║ 后端服务: {:<45} ║
|
||||||
|
║ API接口: {:<45} ║
|
||||||
|
║ Druid监控: {:<45} ║
|
||||||
|
╠══════════════════════════════════════════════════════════╣
|
||||||
|
║ 状态: 🟢 服务运行中,可以接收请求 ║
|
||||||
|
╚══════════════════════════════════════════════════════════╝
|
||||||
|
""",
|
||||||
|
applicationName,
|
||||||
|
activeProfiles.isEmpty() ? "default" : activeProfiles,
|
||||||
|
serverPort,
|
||||||
|
baseUrl,
|
||||||
|
apiUrl,
|
||||||
|
druidUrl);
|
||||||
|
|
||||||
|
// 额外提示信息
|
||||||
|
log.info("""
|
||||||
|
|
||||||
|
📌 提示:
|
||||||
|
- 前端代理地址: http://localhost:3000
|
||||||
|
- 后端API地址: {}
|
||||||
|
- 图片上传接口: {}/product/upload/image
|
||||||
|
- 商品管理接口: {}/product
|
||||||
|
""", apiUrl, apiUrl, apiUrl);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("""
|
||||||
|
|
||||||
|
╔══════════════════════════════════════════════════════════╗
|
||||||
|
║ ║
|
||||||
|
║ ❌ MTKJ PAY 支付系统启动失败! ❌ ║
|
||||||
|
║ ║
|
||||||
|
╚══════════════════════════════════════════════════════════╝
|
||||||
|
""", e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user