欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot多数据源配置

程序员文章站 2022-04-27 22:24:43
...

yml文件不多写

数据库名枚举

package com.springboot.db;

public enum DBName
{
    DBVv("dbvv"),
    IMM("imm");
    private String key;

    DBName(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }
}

用来切换数据库的方法

package com.springboot.db;

/**
 * FileName: DBContext
 * Description: 更改数据库方法配置
 */
public class DBContext
{
    private static ThreadLocal<String> dbName=new ThreadLocal<>();

    public static String getTargetDataSource(){
        return dbName.get();
    }

    public static void changeDB(String name){
        dbName.set(name);
    }

    //清除数据源名
    public static void clearDB(){
        dbName.remove();
    }
}

继承类实现动态切换

package com.springboot.db;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 * FileName: DynamicDataSource
 * Description:动态切换数据源
 */
public class DynamicDataSource extends AbstractRoutingDataSource
{

    @Override
    protected Object determineCurrentLookupKey() {
        return DBContext.getTargetDataSource();
    }
}

数据源配置

package com.springboot.db;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.HashMap;

/**
 * FileName: ContextConfig
 * Description: 数据源配置
 */
@SpringBootApplication
@ComponentScan(basePackages = "com.springboot.testdemo.*")
@MapperScan(basePackages = "com.springboot.testdemo.*.dao")
@EnableTransactionManagement
public class ContextConfig
{
    @ConfigurationProperties("spring.datasource.druid.dbvv")
    @Bean("dbvv.mysql")
    @Qualifier("dbvv.mysql")
    public DataSource mysqlDataSource(){
        return DruidDataSourceBuilder.create().build();
    }

    @ConfigurationProperties("spring.datasource.druid.imm")
    @Bean("imm.oracle")
    @Qualifier("imm.oracle")
    public DataSource oracleDataSource(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean("dynamicDataSource")
    @Qualifier("dynamicDataSource")
    @Primary
    public DataSource dynamicDataSource(){
        DynamicDataSource dataSource = new DynamicDataSource();
        HashMap<Object, Object> targetDataSource = new HashMap<>();
        targetDataSource.put(DBName.DBVv.getKey(),mysqlDataSource());
        targetDataSource.put(DBName.IMM.getKey(),oracleDataSource());
        dataSource.setTargetDataSources(targetDataSource);
        dataSource.setDefaultTargetDataSource(targetDataSource.get(DBName.DBVv.getKey()));
        return dataSource;
    }
}

配置数据库拦截器,Tomcat8默认使用nio的线程连接池处理请求,一个线程会串行依次的处理多个请求,那么给每个线程独特标识数据源的Threadlocal就会紊乱;拦截在每次请求进入业务层代码的时候,保证都是mysql数据库

package com.springboot.db;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * FileName: DataSourceInterceptor
 * Description: 数据库拦截器
 */
public class DataSourceInterceptor implements HandlerInterceptor
{

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        DBContext.clearDB();
        DBContext.changeDB(DBName.DBVv.getKey());
        return HandlerInterceptor.super.preHandle(request,response,handler);
    }
}

 

相关标签: springboot