将数据库和数据桶报错时机提取到启动时
This commit is contained in:
parent
93dc3d4df3
commit
2bd8ae351d
@ -12,10 +12,10 @@ spring:
|
|||||||
username: root
|
username: root
|
||||||
password: 123456
|
password: 123456
|
||||||
# 从库数据源
|
# 从库数据源
|
||||||
# SLAVE:
|
SLAVE:
|
||||||
# url: jdbc:mysql://127.0.0.1/ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
|
url: jdbc:mysql://127.0.0.1/ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
|
||||||
# username: root
|
username: root
|
||||||
# password: 123456
|
password: 123456
|
||||||
druid:
|
druid:
|
||||||
# 初始连接数
|
# 初始连接数
|
||||||
initialSize: 5
|
initialSize: 5
|
||||||
|
@ -30,12 +30,12 @@ minio:
|
|||||||
primary: MASTER
|
primary: MASTER
|
||||||
client:
|
client:
|
||||||
MASTER:
|
MASTER:
|
||||||
url: http://192.168.3.13:9000
|
url: http://localhost:9000
|
||||||
accessKey:
|
accessKey:
|
||||||
secretKey:
|
secretKey:
|
||||||
buketName: plaform
|
buketName: ruoyi
|
||||||
SLAVE:
|
# SLAVE:
|
||||||
url: http://192.168.3.13:9000
|
# url: http://127.0.0.1:9000
|
||||||
accessKey:
|
# accessKey:
|
||||||
secretKey:
|
# secretKey:
|
||||||
buketName: plaform
|
# buketName: ry
|
||||||
|
@ -13,7 +13,7 @@ ruoyi:
|
|||||||
# 验证码类型 math 数组计算 char 字符验证
|
# 验证码类型 math 数组计算 char 字符验证
|
||||||
captchaType: math
|
captchaType: math
|
||||||
# 指定默认文件服务类型(值为disk代表使用磁盘作为文件操作服务,minio代表使用minio作为文件操作服务)
|
# 指定默认文件服务类型(值为disk代表使用磁盘作为文件操作服务,minio代表使用minio作为文件操作服务)
|
||||||
fileServer: minio
|
fileServer: disk
|
||||||
|
|
||||||
# 开发环境配置
|
# 开发环境配置
|
||||||
server:
|
server:
|
||||||
|
@ -1,11 +1,17 @@
|
|||||||
package com.ruoyi.framework.config;
|
package com.ruoyi.framework.config;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
@ -22,6 +28,7 @@ import com.ruoyi.framework.config.properties.DruidProperties;
|
|||||||
@DependsOn({ "transactionManager" })
|
@DependsOn({ "transactionManager" })
|
||||||
@ConfigurationProperties(prefix = "spring.datasource.dynamic")
|
@ConfigurationProperties(prefix = "spring.datasource.dynamic")
|
||||||
public class DynamicDataSourceProperties implements InitializingBean {
|
public class DynamicDataSourceProperties implements InitializingBean {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(DynamicDataSourceProperties.class);
|
||||||
private Map<String, DataSourceProperties> datasource;
|
private Map<String, DataSourceProperties> datasource;
|
||||||
private String primary;
|
private String primary;
|
||||||
private Map<String, DataSource> targetDataSources = new HashMap<>();
|
private Map<String, DataSource> targetDataSources = new HashMap<>();
|
||||||
@ -35,15 +42,31 @@ public class DynamicDataSourceProperties implements InitializingBean {
|
|||||||
Properties prop = build(dataSourceProperties);
|
Properties prop = build(dataSourceProperties);
|
||||||
DruidXADataSource dataSource = new DruidXADataSource();
|
DruidXADataSource dataSource = new DruidXADataSource();
|
||||||
dataSource.setConnectProperties(prop);
|
dataSource.setConnectProperties(prop);
|
||||||
|
setProperties(dataSource, prop);
|
||||||
|
validateDataSource(dataSource);
|
||||||
|
logger.info("数据源:{} 链接成功", name);
|
||||||
AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
|
AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
|
||||||
ds.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
|
ds.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
|
||||||
ds.setUniqueResourceName(name);
|
ds.setUniqueResourceName(name);
|
||||||
ds.setXaProperties(prop);
|
ds.setXaProperties(prop);
|
||||||
ds.setXaDataSource(dataSource);
|
ds.setXaDataSource(dataSource);
|
||||||
setProperties(dataSource, prop);
|
|
||||||
return ds;
|
return ds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void validateDataSource(DataSource dataSource) {
|
||||||
|
try (Connection conn = dataSource.getConnection()) {
|
||||||
|
String validationQuery = "SELECT 1";
|
||||||
|
try (Statement stmt = conn.createStatement();
|
||||||
|
ResultSet rs = stmt.executeQuery(validationQuery)) {
|
||||||
|
if (!(rs.next() && rs.getInt(1) == 1)) {
|
||||||
|
throw new RuntimeException("数据源连接验证失败:查询结果不正确");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException("数据源连接验证失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected Properties build(DataSourceProperties dataSourceProperties) {
|
protected Properties build(DataSourceProperties dataSourceProperties) {
|
||||||
Properties prop = new Properties();
|
Properties prop = new Properties();
|
||||||
DruidProperties druidProperties = SpringUtils.getBean(DruidProperties.class);
|
DruidProperties druidProperties = SpringUtils.getBean(DruidProperties.class);
|
||||||
|
@ -4,6 +4,8 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.http.impl.io.EmptyInputStream;
|
import org.apache.http.impl.io.EmptyInputStream;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
@ -22,7 +24,7 @@ import io.minio.PutObjectArgs;
|
|||||||
@ConditionalOnProperty(prefix = "minio", name = { "enable" }, havingValue = "true", matchIfMissing = false)
|
@ConditionalOnProperty(prefix = "minio", name = { "enable" }, havingValue = "true", matchIfMissing = false)
|
||||||
@ConfigurationProperties("minio")
|
@ConfigurationProperties("minio")
|
||||||
public class MinioConfig implements InitializingBean {
|
public class MinioConfig implements InitializingBean {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(MinioConfig.class);
|
||||||
public static int maxSize;
|
public static int maxSize;
|
||||||
private String prefix = "/minio";
|
private String prefix = "/minio";
|
||||||
private Map<String, MinioClientProperties> client;
|
private Map<String, MinioClientProperties> client;
|
||||||
@ -39,6 +41,23 @@ public class MinioConfig implements InitializingBean {
|
|||||||
masterBucket = targetMinioBucket.get(primary);
|
masterBucket = targetMinioBucket.get(primary);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void validateMinioBucket(MinioBucket minioBucket) {
|
||||||
|
BucketExistsArgs bucketExistArgs = BucketExistsArgs.builder().bucket(minioBucket.getBuketName()).build();
|
||||||
|
boolean b = false;
|
||||||
|
try {
|
||||||
|
b = minioBucket.getClient().bucketExists(bucketExistArgs);
|
||||||
|
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
|
||||||
|
.object(FileUtils.getRelativePath(RuoYiConfig.getProfile()) + "/")
|
||||||
|
.stream(EmptyInputStream.nullInputStream(), 0, -1).bucket(minioBucket.getBuketName()).build();
|
||||||
|
minioBucket.getClient().putObject(putObjectArgs);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e.getMessage());
|
||||||
|
}
|
||||||
|
if (!b) {
|
||||||
|
throw new RuntimeException("Bucket " + minioBucket.getBuketName() + " does not exist");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private MinioBucket createMinioClient(String name, MinioClientProperties props) {
|
private MinioBucket createMinioClient(String name, MinioClientProperties props) {
|
||||||
MinioClient client;
|
MinioClient client;
|
||||||
if (StringUtils.isEmpty(props.getAccessKey())) {
|
if (StringUtils.isEmpty(props.getAccessKey())) {
|
||||||
@ -51,21 +70,10 @@ public class MinioConfig implements InitializingBean {
|
|||||||
.credentials(props.getAccessKey(), props.getSecretKey())
|
.credentials(props.getAccessKey(), props.getSecretKey())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
BucketExistsArgs bucketExistArgs = BucketExistsArgs.builder().bucket(props.getBuketName()).build();
|
MinioBucket minioBucket = new MinioBucket(client, props.getBuketName());
|
||||||
boolean b = false;
|
validateMinioBucket(minioBucket);
|
||||||
try {
|
logger.info("数据桶:{} - 链接成功", name);
|
||||||
b = client.bucketExists(bucketExistArgs);
|
return minioBucket;
|
||||||
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
|
|
||||||
.object(FileUtils.getRelativePath(RuoYiConfig.getProfile()) + "/")
|
|
||||||
.stream(EmptyInputStream.nullInputStream(), 0, -1).bucket(props.getBuketName()).build();
|
|
||||||
client.putObject(putObjectArgs);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e.getMessage());
|
|
||||||
}
|
|
||||||
if (!b) {
|
|
||||||
throw new RuntimeException("Bucket " + props.getBuketName() + " does not exist");
|
|
||||||
}
|
|
||||||
return new MinioBucket(client, props.getBuketName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMaxSize() {
|
public int getMaxSize() {
|
||||||
|
@ -24,6 +24,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnProperty(prefix = "spring.cache", name = { "type" }, havingValue = "redis", matchIfMissing = false)
|
@ConditionalOnProperty(prefix = "spring.cache", name = { "type" }, havingValue = "redis", matchIfMissing = false)
|
||||||
public class RedisConfig implements CachingConfigurer {
|
public class RedisConfig implements CachingConfigurer {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Primary
|
@Primary
|
||||||
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
|
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
|
||||||
@ -50,6 +51,7 @@ public class RedisConfig implements CachingConfigurer {
|
|||||||
@Bean
|
@Bean
|
||||||
@SuppressWarnings(value = { "unchecked", "rawtypes" })
|
@SuppressWarnings(value = { "unchecked", "rawtypes" })
|
||||||
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
|
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
|
||||||
|
|
||||||
RedisTemplate<Object, Object> template = new RedisTemplate<>();
|
RedisTemplate<Object, Object> template = new RedisTemplate<>();
|
||||||
template.setConnectionFactory(connectionFactory);
|
template.setConnectionFactory(connectionFactory);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user