mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
feat: add "Load Balance" for multi dataSource
This commit is contained in:
parent
023600319b
commit
6d88d16f02
@ -188,9 +188,8 @@ mybatis-flex:
|
|||||||
在多租户等某些场景下,我们可能需要用到动态的添加新的数据源,此时可以通过如下的方式进行添加。
|
在多租户等某些场景下,我们可能需要用到动态的添加新的数据源,此时可以通过如下的方式进行添加。
|
||||||
|
|
||||||
```java
|
```java
|
||||||
FlexDataSource flexDataSource = (FlexDataSource) FlexGlobalConfig
|
FlexDataSource flexDataSource = FlexGlobalConfig.getDefaultConfig()
|
||||||
.getDefaultConfig().getConfiguration()
|
.getDataSource();
|
||||||
.getEnvironment().getDataSource();
|
|
||||||
|
|
||||||
//新的数据源
|
//新的数据源
|
||||||
HikariDataSource newDataSource = new HikariDataSource();
|
HikariDataSource newDataSource = new HikariDataSource();
|
||||||
@ -208,11 +207,53 @@ public class DataSourceInitListener implements ApplicationListener<ContextRefres
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||||
FlexDataSource dataSource = (FlexDataSource) FlexGlobalConfig.getDefaultConfig()
|
FlexDataSource dataSource = FlexGlobalConfig.getDefaultConfig()
|
||||||
.getConfiguration().getEnvironment().getDataSource();
|
.getDataSource();
|
||||||
|
|
||||||
dataSource.addDataSource("....", new DruidDataSource("..."));
|
dataSource.addDataSource("....", new DruidDataSource("..."));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## 多数据源负载均衡 <Badge type="tip" text="^1.5.4" />
|
||||||
|
|
||||||
|
数据源负载均衡指的是:在进行数据查询的时候,随机使用一个数据源。 这是的在高并发的场景下,起到负载的效果。
|
||||||
|
|
||||||
|
假设多数据源配置如下:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
mybatis-flex:
|
||||||
|
datasource:
|
||||||
|
ds1:
|
||||||
|
type: druid
|
||||||
|
url: jdbc:mysql://127.0.0.1:3306/db
|
||||||
|
username: root
|
||||||
|
password: 123456
|
||||||
|
asyncInit: true
|
||||||
|
ds2:
|
||||||
|
type: com.your.datasource.type2
|
||||||
|
url: jdbc:mysql://127.0.0.1:3306/db2
|
||||||
|
username: root
|
||||||
|
password: 123456
|
||||||
|
other:
|
||||||
|
type: com.your.datasource.type2
|
||||||
|
url: jdbc:mysql://127.0.0.1:3306/db2
|
||||||
|
username: root
|
||||||
|
password: 123456
|
||||||
|
```
|
||||||
|
|
||||||
|
以上配置了三个数据源,分别为:`ds1`、`ds2`、`other`,假设我们想负载均衡使用 `ds1`、`ds2` ,那么代码如下:
|
||||||
|
|
||||||
|
```java 2,6
|
||||||
|
try{
|
||||||
|
DataSourceKey.use("ds*");
|
||||||
|
List<Row> rows = Db.selectAll("tb_account");
|
||||||
|
System.out.println(rows);
|
||||||
|
}finally{
|
||||||
|
DataSourceKey.clear();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`DataSourceKey.use("ds*")` 中的 `ds*` 指的是使用 `ds` 开头的任意一个数据源。`ds*` 必须以 "`*`" 结尾,
|
||||||
|
中间不能有空格,比如 "`ds *`" 中间有空格是不行的。
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user