use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project longmarch by yuyueqty.
the class JdbcDataSourceConfig method jdbcDynamicDataSourceProvider.
@Bean
public DynamicDataSourceProvider jdbcDynamicDataSourceProvider() {
return new AbstractJdbcDataSourceProvider(driver, url, username, password) {
@Override
protected Map<String, DataSourceProperty> executeStmt(Statement statement) throws SQLException {
ResultSet rs = statement.executeQuery("SELECT * FROM " + DATA_SOURCE_INFO);
Map<String, DataSourceProperty> map = new HashMap<>(8);
while (rs.next()) {
String name = rs.getString("data_source_name");
String username = rs.getString("username");
String password = rs.getString("password");
String url = rs.getString("url");
String driver = rs.getString("driver_class_name");
if (StrUtil.isBlank(name) || StrUtil.isBlank(username) || StrUtil.isBlank(password) || StrUtil.isBlank(url) || StrUtil.isBlank(driver)) {
continue;
}
DataSourceProperty property = new DataSourceProperty();
property.setPoolName(name);
property.setUsername(username);
property.setPassword(password);
property.setUrl(url);
property.setDriverClassName(driver);
map.put(name, property);
}
return map;
}
};
}
use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project longmarch by yuyueqty.
the class DataSourceController method add.
@ApiOperation(value = "添加数据源")
@PostMapping("/add")
public Set<String> add(@Validated @RequestBody DataSourceDTO dto) {
DataSourceProperty dataSourceProperty = new DataSourceProperty();
BeanUtils.copyProperties(dto, dataSourceProperty);
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
ds.addDataSource(dto.getPoolName(), dataSource);
return ds.getDataSources().keySet();
}
use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project helio-starters by uncarbon97.
the class HelioDynamicDataSourceRegistry method containsDataSource.
/**
* 查询是否已存在数据源
*
* @param dataSourceName 数据源名称
* @param obtainIfAbsent 缺失时尝试从 IoC 容器中获取,并加入数据源缓存
* @return boolean
*/
public boolean containsDataSource(String dataSourceName, boolean obtainIfAbsent) {
/*
配置不存在则动态添加数据源,以懒加载的模式解决分布式场景的配置同步
为了保证数据完整性,配置后生成数据源缓存,后台便无法修改更换数据源,若一定要修改请迁移数据后重启服务或自行修改底层逻辑
*/
if (dynamicRoutingDataSource.getDataSources().containsKey(dataSourceName)) {
return true;
}
if (!obtainIfAbsent) {
return false;
}
boolean successFlag = false;
for (DataSourceDefinitionProvider provider : dataSourceDefinitionProviders) {
DataSourceDefinition dataSourceDefinition = provider.getDataSourceDefinition(dataSourceName);
if (dataSourceDefinition != null) {
// 存在非空结果,则拷贝数据源配置
DataSourceProperty dataSourceProperty = new DataSourceProperty();
BeanUtil.copyProperties(dataSourceDefinition, dataSourceProperty);
// 补充字段
dataSourceProperty.setPoolName(dataSourceDefinition.getName()).setType(HikariDataSource.class);
// 创建动态数据源
DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
// 添加新数据源
dynamicRoutingDataSource.addDataSource(dataSourceName, dataSource);
log.info("已动态添加新数据源 {}", dataSourceName);
successFlag = true;
break;
}
}
if (!successFlag) {
log.error("动态添加新数据源失败,原因:没有 DataSourceDefinitionProvider 提供 {} 的 DataSourceDefinition", dataSourceName);
}
return successFlag;
}
Aggregations