5 Commits
Author SHA1 Message Date
蓝白 e6576abcf1 更新描述 2024-07-13 16:29:05 +08:00
蓝白 508584e377 更新版本 2024-07-13 16:21:57 +08:00
蓝白 25854e7e6a 优化定时任务任务配置 2024-07-13 16:21:23 +08:00
蓝白 bd68775e92 优化数据库连接更新逻辑 2024-07-13 16:20:17 +08:00
蓝白 b4be8a7dac 更新版本号 2024-07-01 09:39:29 +08:00
8 changed files with 45 additions and 19 deletions

No files matched your search

+3 -1
View File
@@ -57,13 +57,15 @@ gitee: https://gitee.com/linebyte/crabc
<dependency>
<groupId>cn.crabc</groupId>
<artifactId>crabc-spring-boot-starter</artifactId>
<version>最新版本</version>
<version>2.8.8</version>
</dependency>
```
在程序启动类中添加下面注解
```
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
```
集成Demo参考: <a href="https://gitee.com/linebyte/crabc-spring-boot-starter-demo" style="text-decoration: none;" target="_blank">crabc-spring-boot-starter-demo</a>
## Docker启动
先执行db/dml.sql脚本,创建库表和初始化数据,建议mysql 8.0+版本
```
@@ -44,7 +44,7 @@ public class DataSourceConfig {
dataSource.setPassword(password);
dataSource.setInitialSize(1);
dataSource.setMinIdle(1);
dataSource.setMaxActive(5);
dataSource.setMaxActive(20);
// 配置获取连接等待超时的时间
dataSource.setMaxWait(6000);
dataSource.setKeepAlive(true);
@@ -52,7 +52,7 @@ public class BaseApiInfoServiceImpl implements IBaseApiInfoService {
@Qualifier("apiCache")
Cache<String, Object> apiInfoCache;
@Scheduled(cron = "*/30 * * * * ?")
@Scheduled(cron = "${crabc.corn.api:0 0/1 * * * ?}")
public void task() {
initApi();
}
@@ -10,6 +10,9 @@ import cn.crabc.core.app.util.UserThreadLocal;
import cn.crabc.core.datasource.driver.DataSourceManager;
import cn.crabc.core.spi.bean.BaseDataSource;
import com.github.pagehelper.PageHelper;
import com.zaxxer.hikari.HikariDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@@ -30,6 +33,8 @@ import java.util.List;
@Service
public class BaseDataSourceServiceImpl implements IBaseDataSourceService {
private static Logger log = LoggerFactory.getLogger(BaseDataSourceServiceImpl.class);
@Autowired
private BaseDataSourceMapper dataSourceMapper;
@Autowired
@@ -80,19 +85,31 @@ public class BaseDataSourceServiceImpl implements IBaseDataSourceService {
}
@Override
@Scheduled(cron = "0 0/2 * * * ?")
@Scheduled(cron = "${crabc.corn.api:0 0/5 * * * ?}")
public void init() {
List<BaseDataSource> baseDataSources = this.getList();
for (BaseDataSource dataSource : baseDataSources) {
DataSource ds = DataSourceManager.DATA_SOURCE_POOL_JDBC.get(dataSource.getDatasourceId());
if (ds != null) {
continue;
}
try {
String jdbcUrl = null;
String username = null;
String password = null;
if (ds instanceof HikariDataSource) {
HikariDataSource hikari = (HikariDataSource) ds;
jdbcUrl = hikari.getJdbcUrl();
username = hikari.getUsername();
password = hikari.getPassword();
}
byte[] decode = Base64.getDecoder().decode(dataSource.getPassword());
dataSource.setPassword(new String(decode));
// 判断数据库连接关键属性是否有修改,有修改则刷新缓存
if (ds != null && dataSource.getJdbcUrl().equals(jdbcUrl)
&& dataSource.getUsername().equals(username) && dataSource.getPassword().equals(password)) {
continue;
}
dataSourceManager.createDataSource(dataSource);
}catch (Exception e) {
log.error("-数据源加载异常,dataSourceId:{}",dataSource.getDatasourceId(),e);
}
}
}
@@ -36,7 +36,7 @@ public class BaseFlowRuleServiceImpi implements IBaseFlowRuleService {
@Autowired
private BaseGroupMapper baseGroupMapper;
@Scheduled(cron = "*/50 * * * * ?")
@Scheduled(cron = "${crabc.corn.flow:0 0/1 * * * ?}")
public void task() {
initFlowRule();
}
@@ -6,6 +6,8 @@ import cn.crabc.core.datasource.enums.ErrorStatusEnum;
import cn.crabc.core.datasource.exception.CustomException;
import com.alibaba.druid.pool.DruidDataSource;
import com.zaxxer.hikari.HikariDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import javax.sql.DataSource;
@@ -19,6 +21,7 @@ import java.sql.SQLException;
*/
public class JdbcDataSourceRouter extends AbstractRoutingDataSource {
private static Logger log = LoggerFactory.getLogger(JdbcDataSourceRouter.class);
/**
* 当前线程数据源KEY
*/
@@ -155,22 +158,26 @@ public class JdbcDataSourceRouter extends AbstractRoutingDataSource {
*/
@Override
public Connection getConnection() throws SQLException {
Connection connection = this.determineTargetDataSource().getConnection();
Object dataSourceKey = this.determineCurrentLookupKey();
Connection connection = null;
Object dataSourceKey = null;
try {
connection = this.determineTargetDataSource().getConnection();
dataSourceKey = this.determineCurrentLookupKey();
// dataSouceId:dataSourceType:schemaName
if (dataSourceKey != null && dataSourceKey.toString().contains(":")) {
String[] dataSourceStr = dataSourceKey.toString().split(":");
String dataSourceType = dataSourceStr[1];
String schema = dataSourceStr[2];
if (BaseConstant.CATALOG_DATA_SOURCE.contains(dataSourceType)) {
connection.setCatalog(schema);
}else {
connection.setSchema(schema);
if (dataSourceStr.length == 3) {
String dataSourceType = dataSourceStr[1];
String schema = dataSourceStr[2];
if (BaseConstant.CATALOG_DATA_SOURCE.contains(dataSourceType)) {
connection.setCatalog(schema);
}else {
connection.setSchema(schema);
}
}
}
}catch (Exception e){
e.printStackTrace();
log.error("-数据源连接获取失败,dataSourceKey:{}",dataSourceKey,e);
}
return connection;
}
@@ -71,7 +71,7 @@ public abstract class DefaultDataSourceDriver implements DataSourceDriver {
dataSource.setPassword(password);
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setMinimumIdle(1);
dataSource.setMaximumPoolSize(10);
dataSource.setMaximumPoolSize(20);
dataSource.setMaxLifetime(900000);
dataSource.setIdleTimeout(300000);
dataSource.setConnectionTimeout(10000);
+1 -1
View File
@@ -17,7 +17,7 @@
<properties>
<!-- crabc 版本-->
<revision>2.8.6</revision>
<revision>2.8.8</revision>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>