first commit
This commit is contained in:
@ -2,19 +2,45 @@ server:
|
|||||||
port: 8080
|
port: 8080
|
||||||
spring:
|
spring:
|
||||||
main:
|
main:
|
||||||
allow-bean-definition-overriding: true #新创建的bean覆盖旧的bean
|
#新创建的bean覆盖旧的bean
|
||||||
|
allow-bean-definition-overriding: true
|
||||||
application:
|
application:
|
||||||
name: qiaoba-boot
|
name: qiaoba-boot
|
||||||
profiles:
|
profiles:
|
||||||
active: dev
|
active: dev
|
||||||
servlet:
|
servlet:
|
||||||
multipart:
|
multipart:
|
||||||
enabled: true #开启文件上传
|
#开启文件上传
|
||||||
max-file-size: 200MB #限制文件上传大小为10M
|
enabled: true
|
||||||
max-request-size: 200MB #限制文件上传大小为10M
|
#限制文件上传大小为 200M
|
||||||
|
max-file-size: 200MB
|
||||||
|
#限制文件上传大小为 200M
|
||||||
|
max-request-size: 200MB
|
||||||
jackson:
|
jackson:
|
||||||
date-format: yyyy-MM-dd HH:mm:ss
|
date-format: yyyy-MM-dd HH:mm:ss
|
||||||
time-zone: GMT+8
|
time-zone: GMT+8
|
||||||
|
# redis 配置
|
||||||
|
redis:
|
||||||
|
# 地址
|
||||||
|
host: 192.168.0.202
|
||||||
|
# 端口,默认为6379
|
||||||
|
port: 6379
|
||||||
|
# 数据库索引
|
||||||
|
database: 0
|
||||||
|
# 密码
|
||||||
|
password:
|
||||||
|
# 连接超时时间
|
||||||
|
timeout: 10s
|
||||||
|
lettuce:
|
||||||
|
pool:
|
||||||
|
# 连接池中的最小空闲连接
|
||||||
|
min-idle: 0
|
||||||
|
# 连接池中的最大空闲连接
|
||||||
|
max-idle: 8
|
||||||
|
# 连接池的最大数据库连接数
|
||||||
|
max-active: 8
|
||||||
|
# #连接池最大阻塞等待时间(使用负值表示没有限制)
|
||||||
|
max-wait: -1ms
|
||||||
|
|
||||||
qiaoba:
|
qiaoba:
|
||||||
auth:
|
auth:
|
||||||
@ -63,9 +89,3 @@ knife4j:
|
|||||||
enable-footer-custom: true
|
enable-footer-custom: true
|
||||||
footer-custom-content: create by ailanyin
|
footer-custom-content: create by ailanyin
|
||||||
|
|
||||||
# mybatis-plus:
|
|
||||||
# 对应的 XML 文件位置
|
|
||||||
# mapperLocations: classpath*:mapper/**/*Mapper.xml
|
|
||||||
# 实体扫描,多个package用逗号或者分号分隔
|
|
||||||
# typeAliasesPackage: com.qiaoba.**.entity
|
|
||||||
|
|
||||||
|
|||||||
@ -40,6 +40,7 @@ public class DatasourceConnectionMonitor {
|
|||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
|
// 1s钟运行一次
|
||||||
new Timer().schedule(new TimerTask() {
|
new Timer().schedule(new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@ -62,6 +63,10 @@ public class DatasourceConnectionMonitor {
|
|||||||
if (check(connection)) {
|
if (check(connection)) {
|
||||||
// 说明数据源正常
|
// 说明数据源正常
|
||||||
log.trace("租户[{}]-目前主数据源正常, 无需切换数据源", tenantId);
|
log.trace("租户[{}]-目前主数据源正常, 无需切换数据源", tenantId);
|
||||||
|
// 主数据 处理任务
|
||||||
|
if (TenantConstant.DEFAULT_TENANT_ID.equals(tenantId)) {
|
||||||
|
handleJob();
|
||||||
|
}
|
||||||
IoUtil.close(connection);
|
IoUtil.close(connection);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -74,7 +79,7 @@ public class DatasourceConnectionMonitor {
|
|||||||
IoUtil.close(dataSource);
|
IoUtil.close(dataSource);
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
//e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -89,7 +94,8 @@ public class DatasourceConnectionMonitor {
|
|||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
stmt = conn.createStatement();
|
stmt = conn.createStatement();
|
||||||
stmt.setQueryTimeout(1);
|
// 允许 2s 延时
|
||||||
|
stmt.setQueryTimeout(2);
|
||||||
rs = stmt.executeQuery(CHECK_SQL);
|
rs = stmt.executeQuery(CHECK_SQL);
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -129,13 +135,9 @@ public class DatasourceConnectionMonitor {
|
|||||||
// 切换成功
|
// 切换成功
|
||||||
DynamicDataSource dynamicDataSource = dataSources.get(backupIndex);
|
DynamicDataSource dynamicDataSource = dataSources.get(backupIndex);
|
||||||
// 更改数据库中该数据源为主要数据源
|
// 更改数据库中该数据源为主要数据源
|
||||||
if (!TenantConstant.DEFAULT_TENANT_ID.equals(dynamicDataSource.getTenantId())) {
|
if (Objects.nonNull(dynamicDataSource.getTenantId()) && !TenantConstant.DEFAULT_TENANT_ID.equals(dynamicDataSource.getTenantId())) {
|
||||||
try {
|
// 添加到待处理任务中
|
||||||
dynamicDatasourceService.changePrimaryDatasource(dynamicDataSource.getTenantId(), dynamicDataSource.getDatasourceId());
|
WAIT_UPDATE_DATASOURCE_STATUS.put(dynamicDataSource.getTenantId(), dynamicDataSource.getDatasourceId());
|
||||||
} catch (Exception e) {
|
|
||||||
// 说明主数据源也挂了
|
|
||||||
WAIT_UPDATE_DATASOURCE_STATUS.put(dynamicDataSource.getTenantId(), dynamicDataSource.getDatasourceId());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// 备用数据源集合删除该数据源
|
// 备用数据源集合删除该数据源
|
||||||
dataSources.remove((int) backupIndex);
|
dataSources.remove((int) backupIndex);
|
||||||
@ -146,4 +148,20 @@ public class DatasourceConnectionMonitor {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void handleJob() {
|
||||||
|
Set<String> keys = WAIT_UPDATE_DATASOURCE_STATUS.keySet();
|
||||||
|
for (String key : keys) {
|
||||||
|
try {
|
||||||
|
log.info("开始更新数据库中租户数据源状态, 租户ID: {}", key);
|
||||||
|
// 防止更新过程中主数据挂了
|
||||||
|
dynamicDatasourceService.changePrimaryDatasource(key, WAIT_UPDATE_DATASOURCE_STATUS.get(key));
|
||||||
|
// 处理完成 删除任务
|
||||||
|
WAIT_UPDATE_DATASOURCE_STATUS.remove(key);
|
||||||
|
log.info("更新数据库中租户数据源状态完成, 租户ID: {}", key);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("更新数据库中租户数据源状态完成, 租户ID: {}, 失败原因: {}", key, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user