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