Skip to content

Commit

Permalink
1.11.1
Browse files Browse the repository at this point in the history
  • Loading branch information
noear committed Nov 24, 2022
1 parent 6f3daea commit 91e9528
Show file tree
Hide file tree
Showing 25 changed files with 418 additions and 52 deletions.
1 change: 0 additions & 1 deletion __release/solon-base-bundle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<module>../../_solon_base/solon.config.yaml</module>

<module>../../_solon_base/solon.data</module>
<module>../../_solon_base/solon.data.datasource</module>
<module>../../_solon_base/solon.auth</module>
<module>../../_solon_base/solon.schedule</module>
<module>../../_solon_base/solon.validation</module>
Expand Down
2 changes: 2 additions & 0 deletions __release/solon-plugin-bundle2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

<module>../../_solon_plugin/luffy-solon-plugin</module>

<module>../../_solon_plugin/dynamic-datasource-solon-plugin</module>

<module>../../_solon_plugin/weed3-solon-plugin</module>
<module>../../_solon_plugin/wood-solon-plugin</module>

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
<relativePath>../../solon-parent/pom.xml</relativePath>
</parent>

<artifactId>solon.data.datasource</artifactId>
<artifactId>solon.data.dynamicds</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon</artifactId>
<artifactId>solon.data</artifactId>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.noear.solon.data.datasource.annotation;
package org.noear.solon.data.datasource.dynamic.annotation;

import org.noear.solon.annotation.Around;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.noear.solon.data.datasource.annotation;
package org.noear.solon.data.datasource.dynamic.annotation;

import org.noear.solon.core.aspect.Interceptor;
import org.noear.solon.core.aspect.Invocation;
import org.noear.solon.data.datasource.DynamicDsUtils;
import org.noear.solon.data.dynamicds.DynamicDsUtils;

/**
* 动态数据源切换
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.noear.solon.data.datasource;
package org.noear.solon.data.dynamicds;

import org.noear.solon.data.datasource.AbstractRoutingDataSource;

import javax.sql.DataSource;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;

Expand Down Expand Up @@ -31,25 +32,10 @@ public DynamicDataSource(Properties props) {
}

//::初始化
init(defSource, dataSourceMap, Boolean.parseBoolean(strictStr));
}



public DynamicDataSource(DataSource defaultTargetDataSource, Map<String, DataSource> targetDataSources) {
init(defaultTargetDataSource, targetDataSources, false);
}

public DynamicDataSource(DataSource defaultTargetDataSource, Map<String, DataSource> targetDataSources, boolean strict) {
init(defaultTargetDataSource, targetDataSources, strict);
}


/**
* 获取数据源集合
*/
public Map<String, DataSource> getDataSourceMap() {
return Collections.unmodifiableMap(targetDataSources);
setStrict(Boolean.parseBoolean(strictStr));
setTargetDataSources(dataSourceMap);
setDefaultTargetDataSource(defSource);
checkPropertiesSet();
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.noear.solon.data.datasource;
package org.noear.solon.data.dynamicds;

import org.noear.solon.Utils;
import org.noear.solon.core.Props;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.noear.solon.aspect.annotation.Service;
import org.noear.solon.data.datasource.DynamicDataSource;
import org.noear.solon.data.datasource.DynamicDsUtils;
import org.noear.solon.data.datasource.annotation.DynamicDs;
import org.noear.solon.data.dynamicds.DynamicDataSource;
import org.noear.solon.data.dynamicds.DynamicDsUtils;
import org.noear.solon.data.datasource.dynamic.annotation.DynamicDs;

import javax.sql.DataSource;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,37 @@
* @since 1.11
*/
public abstract class AbstractRoutingDataSource implements DataSource {
protected DataSource defaultTargetDataSource;
protected Map<String, DataSource> targetDataSources;
private DataSource defaultTargetDataSource;
private Map<String, DataSource> targetDataSources;

/**
* 严格模式(启用后在未匹配到指定数据源时候会抛出异常,不启用则使用默认数据源.)
* */
private boolean strict;

public void setTargetDataSources(Map<String, DataSource> targetDataSources) {
this.targetDataSources = targetDataSources;
}

public void setDefaultTargetDataSource(DataSource defaultTargetDataSource) {
this.defaultTargetDataSource = defaultTargetDataSource;
}

public void setStrict(boolean strict) {
this.strict = strict;
}

/**
* 初始化
* 检查属性设置
* */
protected void init(DataSource defaultDataSource, Map<String, DataSource> dataSourceMap, boolean strict) {
if (dataSourceMap == null || dataSourceMap.size() == 0) {
public void checkPropertiesSet(){
if (targetDataSources == null || targetDataSources.size() == 0) {
throw new IllegalArgumentException("Property 'targetDataSources' is required");
}

if (defaultDataSource == null) {
if (defaultTargetDataSource == null) {
throw new IllegalArgumentException("Property 'defaultTargetDataSource' is required");
}

this.strict = strict;
this.targetDataSources = dataSourceMap;
this.defaultTargetDataSource = defaultDataSource;
}

/**
Expand Down
58 changes: 58 additions & 0 deletions _solon_plugin/dynamic-datasource-solon-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

DynamicDataSource 使用示例

配置示例(致少要有:type, strict, default)

* type: 设定数据源实现类型(一般用链接池 hikari,druid 之类)
* strict: 设定严格模式(默认:false)。启用后在未匹配到指定数据源时候会抛出异常, 不启用则使用默认数据源.
* default: 动态数据源的默认源

```yaml
demo.ds.db_user:
type: "com.zaxxer.hikari.HikariDataSource"
strict: true
default:
jdbcUrl: "xxx" #属性名要与 type 类的属性对上
username: "xxx"
paasword: "xxx"
driverClassName: "xx"
db_user_2:
jdbcUrl: "xxx" #属性名要与 type 类的属性对上
username: "xxx"
paasword: "xxx"
driverClassName: "xx"
```
代码示使你
```java
//配置数据源 bean
@Configuration
public class Config {
@Bean("db_user")
public DataSource dsUser(@Inject("$demo.ds.db_user}") DynamicDataSource dataSource) {
return dataSource;
}
}

@Service
public class UserService{
@Db("db_user")
UserMapper userMapper;

@DynamicDs //使用 db_user 动态源内的 默认源
public void addUser(){
userMapper.inserUser();
}

@DynamicDs("db_user_1") //使用 db_user 动态源内的 db_user_1 源
public void getUserList(){
userMapper.selectUserList();
}

public void getUserList2(){
DynamicDsHolder.setCurrent("db_user_2"); //使用 db_user 动态源内的 db_user_2 源
userMapper.selectUserList();
}
}
```
30 changes: 30 additions & 0 deletions _solon_plugin/dynamic-datasource-solon-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.noear</groupId>
<artifactId>solon-parent</artifactId>
<version>1.11.1-M3</version>
<relativePath>../../solon-parent/pom.xml</relativePath>
</parent>

<artifactId>dynamic-datasource-solon-plugin</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon.data</artifactId>
</dependency>

<dependency>
<groupId>org.noear</groupId>
<artifactId>solon.aspect</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.noear.solon.data.datasource.dynamic;

import org.noear.solon.data.datasource.AbstractRoutingDataSource;

import javax.sql.DataSource;
import java.util.Map;
import java.util.Properties;

/**
* 动态数据源
*
* @author noear
* @since 1.11
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
public DynamicDataSource(Properties props) {
if (props == null || props.size() == 0) {
//缺少配置
throw new IllegalStateException("Missing dynamic data source configuration");
}

String strictStr = props.getProperty("strict", "false");
props.remove("strict");

Map<String, DataSource> dataSourceMap = DynamicDsUtils.buildDsMap(props);

//::获取默认数据源
DataSource defSource = dataSourceMap.get("default");

if (defSource == null) {
throw new IllegalStateException("Missing default data source configuration");
}

//::初始化
setStrict(Boolean.parseBoolean(strictStr));
setTargetDataSources(dataSourceMap);
setDefaultTargetDataSource(defSource);
checkPropertiesSet();
}


@Override
protected String determineCurrentKey() {
return DynamicDsUtils.getCurrent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.noear.solon.data.datasource.dynamic;

import org.noear.solon.Utils;
import org.noear.solon.core.Props;
import org.noear.solon.core.PropsConverter;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
* 动态数据源切换
*
* @author noear
* @since 1.11
*/
public class DynamicDsUtils {
static ThreadLocal<String> targetThreadLocal = new ThreadLocal<>();

/**
* 清除
*/
public static void clear() {
targetThreadLocal.remove();
}

/**
* 获取
*/
public static String getCurrent() {
return targetThreadLocal.get();
}

/**
* 设置
*/
public static void setCurrent(String dsBeanName) {
if (dsBeanName == null) {
targetThreadLocal.remove();
} else {
targetThreadLocal.set(dsBeanName);
}
}

/////////////////////////////

/**
* 构建数据源字典
*/
public static Map<String, DataSource> buildDsMap(Properties props) {
//::类型
String typeStr = props.getProperty("type");
if (Utils.isEmpty(typeStr)) {
//缺少类型配置
throw new IllegalStateException("Missing type configuration");
}
props.remove("type");

Class<?> typeClz = Utils.loadClass(typeStr);
if (typeClz == null || DataSource.class.isAssignableFrom(typeClz) == false) {
throw new IllegalStateException("Type configuration not is data source");
}


return buildDsMap(props, typeClz);
}

public static Map<String, DataSource> buildDsMap(Properties props, Class<?> typeClz) {
//::数据源构建
Props rootProps;
if (props instanceof Props) {
rootProps = ((Props) props);
} else {
rootProps = new Props();
rootProps.putAll(props);
}

Map<String, Props> groupProps = rootProps.getGroupedProp("");

if (groupProps.size() == 0) {
//缺少数据源配置
throw new IllegalStateException("Missing dynamic data source configuration");
}


Map<String, DataSource> dataSourceMap = new HashMap<>();
groupProps.forEach((key, prop) -> {
if (prop.size() > 1) {
//超过1个以上的,才可能是数据源属性
DataSource source = (DataSource) PropsConverter.global().convert(prop, typeClz);
dataSourceMap.put(key, source);
}
});

return dataSourceMap;
}
}
Loading

0 comments on commit 91e9528

Please sign in to comment.