赞
踩
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>2.5.6</version>
</dependency>
一主多从方案
spring: datasource: dynamic: primary: master #设置默认的数据源或者数据源组,默认值即为master,如果你主从默认下主库的名称就是master可不定义此项。 datasource: master: username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://47.xxx.xx.xxx:3306/dynamic?characterEncoding=utf8&useSSL=false slave_1: username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://47.xxx.xx.xxx:3307/dynamic?characterEncoding=utf8&useSSL=false slave_2: username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://47.xxx.xx.xxx:3308/dynamic?characterEncoding=utf8&useSSL=false #......省略 #以上会配置一个默认库master,一个组slave下有两个子库slave_1,slave_2
多主多从方案
spring:
datasource:
dynamic:
datasource:
master_1:
master_2:
slave_1:
slave_2:
slave_3:
多数据库方案
spring:
datasource:
dynamic:
primary: mysql #记得设置一个默认数据源
datasource:
mysql:
oracle:
sqlserver:
h2:
@DS 可以注解在方法上和类上,同时存在方法注解优先于类上注解,强烈建议注解在service实现或mapper接口方法上。
注意从2.0.0 不再支持@DS空注解 ,您 必须 指明你所需要的数据库 组名 或者 具体某个数据库名称 。
注解 | 结果 |
---|---|
没有@DS | 默认数据源 |
@DS(“dsName”) | dsName可以为组名也可以为具体某个库的名称 |
注解可以使用在sevice 和 mapper 层, 可以用在方法个类上
springBoot 2.x默认是使用HikariCP的, 所以觉得HikariCP的可以不需要换Druid,因为国内用多还是写一下
**注意:**主从可以使用不同的数据库连接池,如master使用Druid监控,从库使用HikariCP。 如果不配置连接池type类型,默认是Druid优先于HikariCP。
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
spring: datasource: druid: stat-view-servlet: loginUsername: root loginPassword: root dynamic: datasource: master: username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://120.xxx.xxx.xxx:3316/aaaa?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai druid: #以下均为默认值 initial-size: 3 max-active: 8 min-idle: 2 max-wait: -1 min-evictable-idle-time-millis: 30000 max-evictable-idle-time-millis: 30000 time-between-eviction-runs-millis: 0 validation-query: select 1 validation-query-timeout: -1 test-on-borrow: false test-on-return: false test-while-idle: true pool-prepared-statements: true max-open-prepared-statements: 100 filters: stat,wall share-prepared-statements: true slave_1: username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://120.xx.xx.xxx:3317/aaaa?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai druid: #以下均为默认值 initial-size: 3 max-active: 8 min-idle: 2 max-wait: -1 min-evictable-idle-time-millis: 30000 max-evictable-idle-time-millis: 30000 time-between-eviction-runs-millis: 0 validation-query: select 1 validation-query-timeout: -1 test-on-borrow: false test-on-return: false test-while-idle: true pool-prepared-statements: true max-open-prepared-statements: 100 filters: stat,wall share-prepared-statements: true
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。