springboot+dynamic-datasource多数据源配置动态切换

官方教程

https://github.com/baomidou/dynamic-datasource-spring-boot-starter
https://www.kancloud.cn/tracy5546/dynamic-datasource/2344619

一、引入依赖

导入数据库驱动,数据库连接池(非必须),数据库操作框架。

      
            org.postgresql
            postgresql
        
        
            com.alibaba
            druid-spring-boot-starter
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
        
        
            mysql
            mysql-connector-java
        
   
            com.baomidou
            dynamic-datasource-spring-boot-starter
        

二、配置文件

# 开发环境配置
spring:
  profiles: dev
  autoconfigure:
    exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
  datasource:
    dynamic:
      druid:
        initial-size: 10
        # 初始化大小,最小,最大
        min-idle: 20
        maxActive: 500
        # 配置获取连接等待超时的时间
        maxWait: 60000
        # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
        timeBetweenEvictionRunsMillis: 60000
        # 配置一个连接在池中最小生存的时间,单位是毫秒
        minEvictableIdleTimeMillis: 300000
        testWhileIdle: true
        testOnBorrow: true
        validation-query: SELECT 1
        testOnReturn: false
        # 打开PSCache,并且指定每个连接上PSCache的大小
        poolPreparedStatements: true
        maxPoolPreparedStatementPerConnectionSize: 20
        filters: stat,wall
        filter:
          wall:
            config:
              multi-statement-allow: true
              none-base-statement-allow: true
            enabled: true
        # 配置DruidStatFilter
        web-stat-filter:
          enabled: true
          url-pattern: "/*"
          exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
        # 配置DruidStatViewServlet
        stat-view-servlet:
          enabled: true
          url-pattern: "/druid/*"
          # IP白名单(没有配置或者为空,则允许所有访问)
          allow: #127.0.0.1,192.168.163.1
          # IP黑名单 (存在共同时,deny优先于allow)
          deny: #192.168.1.73
          #  禁用HTML页面上的“Reset All”功能
          reset-enable: false
          # 登录名
          login-username: admin
          # 登录密码

          login-password: 111111
        query-timeout: 36000
      primary: master
      strict: false
      datasource:
        master:
          url: jdbc:postgresql://x.xx.xx.xx:5432/x
          username: xxxx
          password: xxxx
          driver-class-name: org.postgresql.Driver
        db1:
          url: jdbc:mysql://x.x.x.x:3306/x?useUnicode=true&characterEncoding=utf-8
          username: xx
          password: xx
          driver-class-name: com.mysql.cj.jdbc.Driver
        db2:
          url: jdbc:mysql://x.x.x.x:3306/x?useUnicode=true&characterEncoding=utf-8
          username: xx
          password: xx
          driver-class-name: com.mysql.cj.jdbc.Driver
        db3:
          url: jdbc:mysql://xx.xx.xx.xx:3306/xx?useUnicode=true&characterEncoding=utf-8
          username: xxx
          password: xxx
          driver-class-name: com.mysql.cj.jdbc.Driver

三、使用

默认使用master数据源,如需切换在调用查询的方法上增加@DS("数据库")

@DS("db1")

import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class xxxxServiceImpl implements DfsGantryTransactionService {
    @Autowired
    xxxxDao xxxxDao;

    @Override
    @DS("db1")
    public Listxxxx> select() {
        return xxxxDao.selectList();
    }
}

四、动态切换

可以根据传入参数(配置文件配置的db1,db2,db3)动态切换数据源

@DS("#dataBaseName")//使用spel从参数获取
public List selectSpelByKey(String dataBaseName) {
	return userMapper.selectUsers();
}

五、加密配置数据库账号密码

import com.baomidou.dynamic.datasource.toolkit.CryptoUtils;

public class Demo {

    public static void main(String[] args) throws Exception {
        String password = "123456";
        //使用默认的publicKey ,建议还是使用下面的自定义
        String encodePassword = CryptoUtils.encrypt(password);
        System.out.println(encodePassword);
    }

        //自定义publicKey
    public static void main(String[] args) throws Exception {
        String[] arr = CryptoUtils.genKeyPair(512);
        System.out.println("privateKey:  " + arr[0]);
        System.out.println("publicKey:  " + arr[1]);
        System.out.println("url:  " + CryptoUtils.encrypt(arr[0], "jdbc:mysql://127.0.0.1:3306/order"));
        System.out.println("username:  " + CryptoUtils.encrypt(arr[0], "root"));
        System.out.println("password:  " + CryptoUtils.encrypt(arr[0], "123456"));
    }
}

六、手动切换数据源

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;

@Service
public class UserServiceImpl implements UserService {

  @Autowired
  private JdbcTemplate jdbcTemplate;

  public List selectAll() {
    DynamicDataSourceContextHolder.push("slave");//手动切换
    return  jdbcTemplate.queryForList("select * from user");
  }
 
}

你可能感兴趣的:(spring,boot,postgresql,spring)