springboot + druid-spring-boot-starter + mysql 实现动态多数据源

1. 需要导入的 maven 依赖

      <dependency>

          <groupId>com.alibaba</groupId>

          <artifactId>druid-spring-boot-starter</artifactId>

          <version>1.1.10</version>

      </dependency>

2. 配置信息(*.yml)

# 数据源配置
spring:
  datasource:
    druid:
      master: # 主库数据源
        url: jdbc:mysql://localhost:3306/qi_e
        username: root
        password: 123456   #密码,可以配置密文密码
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.cj.jdbc.Driver
        filters: config,stat,wall,slf4j #stat: sql 信息监控统计,wall:防注入,slf4j:日志打印
        #connect-properties:  #自定义密码解密信息,需要配合自定义密码解密类
        #  key: test_public_keys  # 密码解密key 
        #  pwd: BljexasdfwdafeasdfaA==  # 加密密码
        #password-callback-class-name: com.xx.ss.xx.CustomPasswordDecrypt#自定义解密类路径
        initialSize: 5 # 初始连接数
        minIdle: 10    # 最小连接池数量
        maxActive: 20  # 最大连接池数量
        mxWait: 60000 # 配置获取连接等待超时的时间
        connectTimeout: 30000 # 配置连接超时时间
        socketTimeout: 60000  # 配置网络超时时间
        timeBetweenEvictionRunsMillis: 60000 d# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
        minEvictableIdleTimeMillis: 300000 # 配置一个连接在池中最小生存的时间,单位是毫秒
        maxEvictableIdleTimeMillis: 900000 # 配置一个连接在池中最大生存的时间,单位是毫秒
      slave: # 从库数据源
        url: jdbc:mysql://localhost:3306/qi_e2
        username: root
        password: 123456
        type: com.alibaba.druid.pool.DruidDataSource
        filters: config,stat,wall,slf4j #stat: sql 信息监控统计,wall:防注入,slf4j:日志打印
        # 密码加密方式1
        #connect-properties:  #自定义密码解密信息,需要配合自定义密码解密类
        #  key: test_public_keys  # 密码解密key 
        #  pwd: BljexasdfwdafeasdfaA==  # 加密密码
        #password-callback-class-name: com.xx.ss.xx.CustomPasswordDecrypt#自定义解密类路径
        driverClassName: com.mysql.cj.jdbc.Driver
        initialSize: 5 # 初始连接数
        minIdle: 10    # 最小连接池数量
        maxActive: 20  # 最大连接池数量
        mxWait: 60000 # 配置获取连接等待超时的时间
        connectTimeout: 30000 # 配置连接超时时间
        socketTimeout: 60000  # 配置网络超时时间
        timeBetweenEvictionRunsMillis: 60000 d# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
        minEvictableIdleTimeMillis: 300000 # 配置一个连接在池中最小生存的时间,单位是毫
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值