Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
1610c6bf8e |
39
README.md
39
README.md
@ -26,4 +26,41 @@
|
|||||||
- Maven3.0+
|
- Maven3.0+
|
||||||
- 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`
|
@ -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
|
Loading…
Reference in New Issue
Block a user