forked from opensolon/solon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
418 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...data/datasource/annotation/DynamicDs.java → ...asource/dynamic/annotation/DynamicDs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...urce/annotation/DynamicDsInterceptor.java → ...amic/annotation/DynamicDsInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...solon/data/datasource/DynamicDsUtils.java → .../solon/data/dynamicds/DynamicDsUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
46 changes: 46 additions & 0 deletions
46
...solon-plugin/src/main/java/org/noear/solon/data/datasource/dynamic/DynamicDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...ce-solon-plugin/src/main/java/org/noear/solon/data/datasource/dynamic/DynamicDsUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.