feat(config): 添加阿里云OSS配置和Druid数据源配置
- 在application.yml中添加阿里云OSS相关配置 - 添加阿里云OSS SDK依赖 - 创建Druid数据源配置类,支持主从数据源 - 更新数据库连接URL指向新的mtpay数据库 - 添加全局异常处理器和设备、电商信息DTO - 添加日期工具类用于时间格式化 - 添加spring-boot-starter-webmvc依赖 - 设置默认激活dev环境配置 - 配置服务器端口为8080
This commit is contained in:
@@ -56,6 +56,13 @@
|
|||||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||||
<version>3.5.7</version>
|
<version>3.5.7</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 阿里云OSS SDK -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun.oss</groupId>
|
||||||
|
<artifactId>aliyun-sdk-oss</artifactId>
|
||||||
|
<version>3.17.4</version>
|
||||||
|
</dependency>
|
||||||
<!-- Druid数据源 -->
|
<!-- Druid数据源 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba</groupId>
|
<groupId>com.alibaba</groupId>
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package com.mtkj.mtpay.config;
|
||||||
|
|
||||||
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Druid数据源配置
|
||||||
|
* 支持主从数据源配置
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Configuration
|
||||||
|
public class DruidDataSourceConfig {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 主数据源配置
|
||||||
|
* 使用@Primary注解确保这是默认数据源
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
@Primary
|
||||||
|
@ConfigurationProperties(prefix = "spring.datasource.druid.master")
|
||||||
|
public DataSource masterDataSource() {
|
||||||
|
log.info("初始化Druid主数据源");
|
||||||
|
DruidDataSource dataSource = new DruidDataSource();
|
||||||
|
log.info("Druid主数据源配置完成");
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从数据源配置(可选)
|
||||||
|
* 只有当slave.enabled=true时才会创建
|
||||||
|
*/
|
||||||
|
@Bean(name = "slaveDataSource")
|
||||||
|
@ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
|
||||||
|
@ConfigurationProperties(prefix = "spring.datasource.druid.slave")
|
||||||
|
public DataSource slaveDataSource() {
|
||||||
|
log.info("初始化Druid从数据源");
|
||||||
|
DruidDataSource dataSource = new DruidDataSource();
|
||||||
|
log.info("Druid从数据源配置完成");
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
19
mt-pay/src/main/java/com/mtkj/mtpay/dto/risk/DeviceDTO.java
Normal file
19
mt-pay/src/main/java/com/mtkj/mtpay/dto/risk/DeviceDTO.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package com.mtkj.mtpay.dto.risk;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备信息DTO
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class DeviceDTO implements Serializable {
|
||||||
|
|
||||||
|
private String orderTerminal;
|
||||||
|
|
||||||
|
private String fingerprintId;
|
||||||
|
|
||||||
|
private String browserInfo;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.mtkj.mtpay.dto.risk;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 电商信息DTO
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class EcommerceDTO implements Serializable {
|
||||||
|
|
||||||
|
private String freeShipping;
|
||||||
|
|
||||||
|
private String shippingMethod;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package com.mtkj.mtpay.exception;
|
||||||
|
|
||||||
|
import com.mtkj.mtpay.common.Result;
|
||||||
|
import com.mtkj.mtpay.common.ResultCode;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.validation.FieldError;
|
||||||
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 全局异常处理器
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@RestControllerAdvice
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理业务异常
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(BusinessException.class)
|
||||||
|
public ResponseEntity<Result<Object>> handleBusinessException(BusinessException e) {
|
||||||
|
log.warn("业务异常: {}", e.getMessage());
|
||||||
|
return ResponseEntity.ok(Result.fail(e.getCode(), e.getMessage()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理参数验证异常
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
|
public ResponseEntity<Result<Map<String, String>>> handleValidationException(MethodArgumentNotValidException e) {
|
||||||
|
Map<String, String> errors = new HashMap<>();
|
||||||
|
e.getBindingResult().getAllErrors().forEach(error -> {
|
||||||
|
String fieldName = ((FieldError) error).getField();
|
||||||
|
String errorMessage = error.getDefaultMessage();
|
||||||
|
errors.put(fieldName, errorMessage);
|
||||||
|
});
|
||||||
|
|
||||||
|
log.warn("参数验证失败: {}", errors);
|
||||||
|
return ResponseEntity.badRequest()
|
||||||
|
.body(Result.fail(ResultCode.VALIDATION_ERROR.getCode(), ResultCode.VALIDATION_ERROR.getMessage(), errors));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理运行时异常
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(RuntimeException.class)
|
||||||
|
public ResponseEntity<Result<Object>> handleRuntimeException(RuntimeException e) {
|
||||||
|
log.error("运行时异常", e);
|
||||||
|
return ResponseEntity.internalServerError()
|
||||||
|
.body(Result.fail(ResultCode.SYSTEM_ERROR.getCode(), e.getMessage()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理其他异常
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
public ResponseEntity<Result<Object>> handleException(Exception e) {
|
||||||
|
log.error("系统异常", e);
|
||||||
|
return ResponseEntity.internalServerError()
|
||||||
|
.body(Result.fail(ResultCode.SYSTEM_ERROR));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
52
mt-pay/src/main/java/com/mtkj/mtpay/util/DateUtils.java
Normal file
52
mt-pay/src/main/java/com/mtkj/mtpay/util/DateUtils.java
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package com.mtkj.mtpay.util;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日期工具类
|
||||||
|
*/
|
||||||
|
public class DateUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认日期时间格式:yyyyMMddHHmmss
|
||||||
|
*/
|
||||||
|
public static final String DEFAULT_DATETIME_FORMAT = "yyyyMMddHHmmss";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 标准日期时间格式:yyyy-MM-dd HH:mm:ss
|
||||||
|
*/
|
||||||
|
public static final String STANDARD_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 格式化日期时间为字符串(yyyyMMddHHmmss)
|
||||||
|
*/
|
||||||
|
public static String formatDateTime(LocalDateTime dateTime) {
|
||||||
|
return formatDateTime(dateTime, DEFAULT_DATETIME_FORMAT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 格式化日期时间为字符串
|
||||||
|
*/
|
||||||
|
public static String formatDateTime(LocalDateTime dateTime, String pattern) {
|
||||||
|
if (dateTime == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return dateTime.format(DateTimeFormatter.ofPattern(pattern));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前时间字符串(yyyyMMddHHmmss)
|
||||||
|
*/
|
||||||
|
public static String getCurrentDateTimeString() {
|
||||||
|
return formatDateTime(LocalDateTime.now());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前时间字符串(标准格式)
|
||||||
|
*/
|
||||||
|
public static String getCurrentDateTimeStringStandard() {
|
||||||
|
return formatDateTime(LocalDateTime.now(), STANDARD_DATETIME_FORMAT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -5,7 +5,7 @@ spring:
|
|||||||
druid:
|
druid:
|
||||||
# 主库数据源(开发环境)
|
# 主库数据源(开发环境)
|
||||||
master:
|
master:
|
||||||
url: jdbc:mysql://rm-j6c3u06k2afwn8hxw6o.mysql.rds.aliyuncs.com:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
|
url: jdbc:mysql://rm-j6c3u06k2afwn8hxw6o.mysql.rds.aliyuncs.com:3306/mtpay?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
|
||||||
username: mtkj2025
|
username: mtkj2025
|
||||||
password: aXs-Q876#pxBesA
|
password: aXs-Q876#pxBesA
|
||||||
# 初始连接数
|
# 初始连接数
|
||||||
|
|||||||
@@ -72,6 +72,14 @@ pingpong:
|
|||||||
# 服务器配置
|
# 服务器配置
|
||||||
server:
|
server:
|
||||||
port: 8080
|
port: 8080
|
||||||
|
|
||||||
|
# 阿里云OSS相关配置
|
||||||
|
aliyun:
|
||||||
|
oss:
|
||||||
|
accessId: LTAI5tHbwvzWfANvNxju2yN1
|
||||||
|
accessKey: sAQR2swByBgmMOofH97hSJT638aVcJ
|
||||||
|
endpoint: https://oss-cn-hangzhou.aliyuncs.com
|
||||||
|
bucketName: mtkj2025
|
||||||
servlet:
|
servlet:
|
||||||
context-path: /
|
context-path: /
|
||||||
|
|
||||||
|
|||||||
4
pom.xml
4
pom.xml
@@ -34,6 +34,10 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-restclient</artifactId>
|
<artifactId>spring-boot-starter-restclient</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-webmvc</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
|||||||
@@ -1 +1,3 @@
|
|||||||
spring.application.name=MTKJPAY
|
spring.application.name=MTKJPAY
|
||||||
|
server.port=8080
|
||||||
|
spring.profiles.active=dev
|
||||||
|
|||||||
Reference in New Issue
Block a user