Java实现动态切换数据源

在一般的Java项目中,如果使用Spring去管理数据库连接信息,一般只能连接一个数据库,可是会有部分情况我们需要连接多个数据库,甚至还会存在不同的请求需要根据配置信息连接不同的数据库,比如:

在很多sass系统中,是采用分库部署的方式,就是不同账号连接的是不同的数据库,可是服务只有一个,所以就需要根据登录的账号信息动态切换数据源。

下面给出一个简单的demo,如果有需要大家可以根据自己的情况去改写。

我们知道,spring在执行sql的时候,是要先获取DataSource对象,然后getConnection获取连接,然后再执行excuteSql去真正执行sql语句。所以在sql执行之前将DataSource对象替换掉,就能动态切换数据源。

这里我们需要用到抽象类AbstractRoutingDataSource,我们需要继承这个类,并且重写determineTargetDataSource方法,这个方法会返回DataSource对象。

@Component
public class DynamicDataSource extends AbstractRoutingDataSource {
    public final static String JDBC_CONNECT_PARAM = "?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true";

    static RedissonClient redissonClient;
    @Autowired
    public void setRedisCache(RedissonClient redissonClient) {
        DynamicDataSource.redissonClient = redissonClient;
    }

    /**
     * 数据源MAP
     */
    public static final Map<String, DataSource> DATA_SOURCES_MAP = new ConcurrentHashMap<>(64);

    /**
     * 当前登录人
     */
    private static final ThreadLocal<String> LOGIN_NAME_KEY = new ThreadLocal<>();


    @Override
    protected Object determineCurrentLookupKey() {
        return LOGIN_NAME_KEY.get();
    }

    @Override
    @NonNull
    public DataSource determineTargetDataSource() {
        try {
            String loginName = LOGIN_NAME_KEY.get();
            DbConfigration dbConfigration = (DbConfigration)redissonClient.getMap("sites").get(loginName.toLowerCase());
            String host = dbConfigration.getHost();
            String dbName = dbConfigration.getDbName();
            String user = dbConfigration.getUsername();
            String password = dbConfigration.getPassword();
            String port = dbConfigration.getPort();
            String dbNameKey = host + dbName;
            if (DATA_SOURCES_MAP.get(dbNameKey) != null) {
                return DATA_SOURCES_MAP.get(dbNameKey);
            } else {
                HashMap<String, String> map = new HashMap<>();
                map.put("url", String.format("jdbc:mysql://%s:%s/%s%s",host,port,dbName,JDBC_CONNECT_PARAM));
                map.put("username", user);
                map.put("password", password);
                map.put("type", "com.zaxxer.hikari.HikariDataSource");
                map.put("driver-class-name", "com.mysql.cj.jdbc.Driver");
                // 创建连接池
                DataSource dataSource = DruidDataSourceFactory.createDataSource(map);
                DATA_SOURCES_MAP.put(dbNameKey, dataSource);
                return dataSource;
            }
        }catch (Exception e) {
            return null;
        }
    }

    @Override
    public void afterPropertiesSet() {}


    public static String getLoginNameKey() {
        return LOGIN_NAME_KEY.get();
    }

    public static void setLoginNameKey(String loginName) {
        clear();
        LOGIN_NAME_KEY.set(loginName);
    }

    /**
     * 清空缓存
     */
    public static void clearDataSourcesMap() {
        DATA_SOURCES_MAP.clear();
    }

    /**
     * 清空当前线程变量
     */
    public static void clear() {
        LOGIN_NAME_KEY.remove();
    }
}

例如,当前系统不同的登录账号需要访问不同的数据库

我们使用ThreadLocal对象存储当前请求的用户信息(可以去使用filter或者自定义aop去赋值),重写determineTargetDataSource方法的时候,就需要使用当前登录信息去获取DataSource对象,这里demo中是将数据库信息存放在redis中

我们写一个test,设置一下redis信息

@PostMapping(value = "/setDb")
    public void setDb() {
        DbConfigration zhangSanDb = new DbConfigration()
                .setHost("127.0.0.1")
                .setPort("3306")
                .setDbName("testdb")
                .setUsername("root")
                .setPassword("123456");
        redissonClient.getMap("sites").put("zhangsan",zhangSanDb);
    }

然后写个Test接口

@PostMapping(value = "/test")
    public Object test(@Param("name") String name) {
        DynamicDataSource.setLoginNameKey(name);
        return testService.test();
    }

这里我们将入参设置成ThreadLocal的值,传参为zhangsan

请求一下

执行成功

 

到此我们的功能就实现了

代码地址:https://gitee.com/chenxianchong1994/dynamic-jdbc

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值