java 实现运行时连接多data source数据源

  1. DynamicDataSource 继承 org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource,实现抽象方法 protected Object determineCurrentLookupKey()
    protected Object determineCurrentLookupKey() {
        return DatabaseContextHolder.getDatabaseType();
    }
    
    DatabaseContextHolder 中定义一个静态的 ThreadLocal<DatabaseType>contextHolder
    public class DatabaseContextHolder {
    private static final ThreadLocal<DatabaseType> contextHolder = new ThreadLocal<DatabaseType>();
    
    public static void setDatabaseType(DatabaseType databaseType) {
    
        contextHolder.set(databaseType);
    
    }
    
    public static DatabaseType getDatabaseType() {
    
        return (DatabaseType) contextHolder.get();
    
    }
    
    public static void clearDatabaseType() {
    
        contextHolder.remove();
    
    }
    

}

```
`DatabaseType` 是一个枚举,枚举了项目要连接的所有的数据库,

创建各个数据库的`DataSource`
```java
    com.alibaba.druid.pool.DruidDataSource dataSource = new com.alibaba.druid.pool.DruidDataSource()
    dataSource.setDriverClassName(env.getProperty("xxx.driverClassName"));
    dataSource.setUrl(env.getProperty("xxx.url"));
    dataSource.setUsername(env.getProperty("xxx.username"));
    dataSource.setPassword(env.getProperty("xxx.password"));

    dataSource.setInitialSize(Integer.parseInt(env.getProperty("datasource.initialSize")));
    dataSource.setMinIdle(Integer.parseInt(env.getProperty("datasource.minIdle")));
    dataSource.setMaxActive(Integer.parseInt(env.getProperty("datasource.maxActive")));      
    dataSource.setMaxWait(Long.parseLong(env.getProperty("datasource.maxWait")));
    dataSource.setRemoveAbandoned(Boolean.parseBoolean(env.getProperty("datasource.removeAbandoned")));
    dataSource.setRemoveAbandonedTimeout(Integer.parseInt(env.getProperty("datasource.removeAbandonedTimeout")));
    dataSource.setTimeBetweenEvictionRunsMillis(Long.parseLong(env.getProperty("datasource.timeBetweenEvictionRunsMillis")));
    dataSource.setMinEvictableIdleTimeMillis(Long.parseLong(env.getProperty("datasource.minEvictableIdleTimeMillis")));
    dataSource.setPoolPreparedStatements(Boolean.parseBoolean(env.getProperty("datasource.poolPreparedStatements")));
    dataSource.setMaxPoolPreparedStatementPerConnectionSize(Integer.parseInt(env.getProperty("datasource.maxPoolPreparedStatementPerConnectionSize")));
    dataSource.setValidationQuery(env.getProperty("datasource.validationQuery"));
```

`datasource` 设置`targetDataSources`
```java
 Map<Object, Object> targetDataSources = new HashMap<>();
 targetDataSources.put(DatabaseType.xxx, mailServerDataSource());
 datasource.setTargetDataSources(Map<Object,Object>)
 ```
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,833评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 32,456评论 18 399
  • 一. Java基础部分.................................................
    wy_sure阅读 9,283评论 0 11
  • 我们在做Web应用的时候,请求处理过程中发生错误是非常常见的情况。Spring Boot提供了一个默认的映射:/e...
    程序猿DD阅读 28,832评论 7 58
  • 我们的健康,取决于各个系统(组织和器官)的工作质量。而我们的寿命呢?则取决于各个系统的使用寿命,而它们的使用寿命则...
    健康小梅说阅读 3,843评论 0 0

友情链接更多精彩内容