docs 📝 readme中补充多数据源配置指南

This commit is contained in:
施海荣 2023-02-15 10:01:35 +08:00
parent 971b91329f
commit 1610c6bf8e
2 changed files with 50 additions and 13 deletions

View File

@ -27,3 +27,40 @@
- MySQL8.0 - MySQL8.0
- redis6.0+ - redis6.0+
<br> <br>
## 多数据源配置指南
1. 在你需要用到多数据源的项目/模块中引入多数据源jar包的maven依赖
```xml
<dependency>
<groupId>com.cnbm</groupId>
<artifactId>ym-dynamic-datasource</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
```
2. 配置文件中,配置多数据源,注意必须以`dynamic.datasource`作为前缀如下slave1,slave2可以自己随意命名数据源不限于2个
```yml
dynamic:
datasource:
slave1:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
username: xxx
password: xxx
slave2:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/myysql?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
username: xxx
password: xxx
```
3. 在service实现中需要指定数据源的方法上面添加注解比如在login这个方法调用时指定用`slave1`这个数据源
```java
@DataSource("slave1")
public Result login(HttpServletRequest request, LoginParam loginParam) {
......
}
```
4. 验证
可以在日志中看到类似输出:`set datasource is slave1`

View File

@ -52,15 +52,15 @@ spring:
# maximum-pool-size: 10 # maximum-pool-size: 10
##多数据源的配置需要引用renren-dynamic-datasource ##多数据源的配置需要引用renren-dynamic-datasource
#dynamic: dynamic:
# datasource: datasource:
# slave1: slave1:
# driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:sqlserver://123456:1433;DatabaseName=renren_security url: jdbc:mysql://mysql.picaiba.com:30307/spc?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
# username: sa username: root
# password: 123456 password: 1qaz@WSX3edc$RFV
# slave2: slave2:
# driver-class-name: org.postgresql.Driver driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:postgresql://123456:5432/renren_security url: jdbc:mysql://mysql.picaiba.com:30307/spc?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
# username: postgres username: root
# password: 123456 password: 1qaz@WSX3edc$RFV