SpringCloud多数据源实现
1、枚举多数据源-定义一一对应变量
/**
*
* 列出所有的数据源key(常用数据库名称来命名)
* 注意:
* 1)这里数据源与数据库是一对一的
* 2)DatabaseType中的变量名称就是数据库的名称
*/
public enum DatabaseType {
test1,test2
}
2、将数据源变量保存在线程变量中
/**
*
* 作用:1、保存一个线程安全的DatabaseType容器
*/
public class DatabaseContextHolder {
//将数据源枚举变量保存在线程变量中
private static final ThreadLocal<DatabaseType> contextHolder = new ThreadLocal<DatabaseType>();
public static void setDatabaseType(DatabaseType type) {
contextHolder.set(type);
}
public static DatabaseType getDatabaseType() {
return contextHolder.get();
}
}
3、动态数据源
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* 动态数据源(需要继承AbstractRoutingDataSource)
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
protected Object determineCurrentLookupKey() {
return DatabaseContextHolder.getDatabaseType();
}
}
4、数据源配置
数据源1:
test1.driverClassName=org.postgresql.Driver
test1.url=jdbc:postgresql://localhost:5432/test1
test.username=test1
test1.password=123456
test1.remove-abandoned=true
test1.remove-abandoned-timeout=120
test1.test-on-borrow=true
数据源2:
test2.driverClassName=org.postgresql.Driver
test2.url=jdbc:postgresql://localhost:5432/test2
test2.username=test2
test2.password=123456
test2.remove-abandoned=true
test2.remove-abandoned-timeout=120
test2.test-on-borrow=true
5、数据源初始化
/**
* springboot集成mybatis的基本入口
* 1)创建数据源(如果采用的是默认的tomcat-jdbc数据源,则不需要)
* 2)创建SqlSessionFactory
* 3)配置事务管理器,除非需要使用事务,否则不用配置
*
*/
@Configuration // 该注解类似于spring配置文件
@MapperScan(basePackages = "cn.test.mapper")
public class MyBatisConfig {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private Environment env;
@Bean
public DataSource test1DataSource() throws Exception {
Properties props = new Properties();
props.put(DruidDataSourceFactory.PROP_DRIVERCLASSNAME, env.getProperty("test1.driverClassName"));
props.put(DruidDataSourceFactory.PROP_URL, env.getProperty("test1.url"));
props.put(DruidDataSourceFactory.PROP_USERNAME, env.getProperty("test1.username"));
// 获取数据库密码
props.put(DruidDataSourceFactory.PROP_PASSWORD, env.getProperty("test1.password"));
props.put(DruidDataSourceFactory.PROP_REMOVEABANDONED, env.getProperty("test1.remove-abandoned"));
props.put(DruidDataSourceFactory.PROP_REMOVEABANDONEDTIMEOUT, env.getProperty("test1.remove-abandoned-timeout"));
props.put(DruidDataSourceFactory.PROP_TESTONBORROW, env.getProperty("test1.test-on-borrow"));
return DruidDataSourceFactory.createDataSource(props);
}
@Bean
public DataSource test2DataSource() throws Exception {
Properties props = new Properties();
props.put(DruidDataSourceFactory.PROP_DRIVERCLASSNAME, env.getProperty("test2.driverClassName"));
props.put(DruidDataSourceFactory.PROP_URL, env.getProperty("test2.url"));
props.put(DruidDataSourceFactory.PROP_USERNAME, env.getProperty("test2.username"));
// 获取数据库密码
props.put(DruidDataSourceFactory.PROP_PASSWORD, env.getProperty("test2.password"));
props.put(DruidDataSourceFactory.PROP_REMOVEABANDONED, env.getProperty("test2.remove-abandoned"));
props.put(DruidDataSourceFactory.PROP_REMOVEABANDONEDTIMEOUT, env.getProperty("test2.remove-abandoned-timeout"));
props.put(DruidDataSourceFactory.PROP_TESTONBORROW, env.getProperty("test2.test-on-borrow"));
return DruidDataSourceFactory.createDataSource(props);
}
/**
* @throws Exception
* @Primary 该注解表示在同一个接口有多个实现类可以注入的时候,默认选择哪一个,而不是让@autowire注解报错
* @Qualifier 根据名称进行注入,通常是在具有相同的多个类型的实例的一个注入(例如有多个DataSource类型的实例)
*/
@Bean
@Primary
public DynamicDataSource dataSource() throws Exception {
DataSource dataSource1 = test1DataSource();
DataSource dataSource2 = test2DataSource();
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DatabaseType.test1, dataSource1);
targetDataSources.put(DatabaseType.test2, dataSource2);
DynamicDataSource dataSource = new DynamicDataSource();
dataSource.setTargetDataSources(targetDataSources);// 该方法是AbstractRoutingDataSource的方法
dataSource.setDefaultTargetDataSource(dataSource1 );// 默认的datasource设置为dataSource 1
return dataSource;
}
/**
* 根据数据源创建SqlSessionFactory
*/
@Bean
public SqlSessionFactory sqlSessionFactory(DynamicDataSource ds) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(ds);// 指定数据源(这个必须有,否则报错)
// 加载配置mybatis-config
DefaultResourceLoader configResourceLoader = new DefaultResourceLoader();
Resource configResource = configResourceLoader.getResource("mybatis-config.xml");
bean.setConfigLocation(configResource);
// 添加mapper文件XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("classpath:cn/test/mapper/*.xml");
bean.setMapperLocations(resources);
return bean.getObject();
}
/**
* 配置事务管理器
*/
@Bean
public DataSourceTransactionManager transactionManager(DynamicDataSource dataSource) throws Exception {
return new DataSourceTransactionManager(dataSource);
}
}
6、
①查询test1的数据时,可以直接调用对应的mapper;(因为test1是默认数据源)
②查询test2的数据时,需要在调用之前指定数据源,如下代码:
DatabaseContextHolder.setDatabaseType(DatabaseType.test2);
③同时需要查询test1和test2的数据时,需要在调用之前指定正确的数据源。
SpringCloud多数据源实现的更多相关文章
- SpringBoot+SpringCloud+vue+Element开发项目——集成Druid数据源
添加依赖 pom.xml <!--druid--> <dependency> <groupId>com.alibaba</groupId> <ar ...
- SpringCloud里面切换数据源无效的问题
问题描述: 调用链:controller1的接口A->service1的方法A->service2的方法B 方法A开启了事务,且指定了数据库A的数据源 方法B也开启了事务,使用了默认的事务 ...
- SpringCloud微服务实战——搭建企业级开发框架(二十七):集成多数据源+Seata分布式事务+读写分离+分库分表
读写分离:为了确保数据库产品的稳定性,很多数据库拥有双机热备功能.也就是,第一台数据库服务器,是对外提供增删改业务的生产服务器:第二台数据库服务器,主要进行读的操作. 目前有多种方式实现读写分离,一种 ...
- 深入理解SpringCloud之配置刷新
我们知道在SpringCloud中,当配置变更时,我们通过访问http://xxxx/refresh,可以在不启动服务的情况下获取最新的配置,那么它是如何做到的呢,当我们更改数据库配置并刷新后,如何能 ...
- Fescar(Seata)-Springcloud流程分析-1阶段
Fescar是阿里18年开源的分布式事务的框架.Fescar的开源对分布式事务框架领域影响很大.作为开源大户,Fescar来自阿里的GTS,经历了好几次双十一的考验,一经开源便颇受关注.今天就来看了F ...
- SpringCloud(1)---基于RestTemplate微服务项目案例
基于RestTemplate微服务项目 在写SpringCloud搭建微服务之前,我想先搭建一个不通过springcloud只通过SpringBoot和Mybatis进行模块之间额通讯.然后在此基础上 ...
- SpringCloud(7)服务链路追踪Spring Cloud Sleuth
1.简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相应的依赖即可.本文主要讲述服务追踪组件zipki ...
- 基于idea的springcloud的helloworld项目搭建过程整理
Springcloud的搭建主要包括三个部分:服务注册中心.服务提供者.服务消费者.每一个部分都是一个springboot项目,它们通过配置文件(application.properties或appl ...
- SpringCloud系列十:SpringCloudConfig 高级配置(密钥加密处理(JCE)、KeyStore 加密处理、SpringCloudConfig 高可用机制、SpringCloudBus 服务总线)
1.概念:SpringCloudConfig 高级配置 2.具体内容 在 SpringCloudConfig 之中考虑到所有配置文件都暴露在远程仓库之中的安全性问题,所以提供有安全访问的处理机制,这样 ...
随机推荐
- lb的keepalive问题
lb的keepalive问题 0. keepalive 大家都很清楚他的用意了,就是为了减少3次握手,设置一个timeout,比如说20s ,在20s内不请求,连接还是保持着,这时候请求过来,不需要重 ...
- 【python(deap库)实现】GEAP 遗传算法/遗传编程 genetic programming +
目录 前言 1.优化问题的定义 单目标优化 多目标优化 2.个体编码 实数编码 二进制编码 序列编码(Permutation encoding) 粒子(Particles) 3 初始种群建立 一般族群 ...
- 虚拟机安装 Linux 最完整攻略
工作中如果你是Linux运维,或者程序员,一定经常需要一个Linux的环境来让你折腾.这个时候使用虚拟机对我们来说是一个不错的选择. 虚拟化技术目前主要有两种:一.原生架构,这种虚拟机产品直接安装在计 ...
- 5.1 Go函数定义
1 Go函数定义 Go函数是指:一段具有独立功能的代码,然后可以在程序中其他地方多次调用. Go分为自定义函数,系统函数. 函数可以将一个大的工作拆解成小的任务. 函数对用户隐藏了细节. Golang ...
- linux常用命令---centOS7的管理服务(针对yum安装的)
centOS7的管理服务(针对yum安装的)
- ionic + asp.net core webapi + keycloak实现前后端用户认证和自动生成客户端代码
概述 本文使用ionic/angular开发网页前台,asp.net core webapi开发restful service,使用keycloak保护前台页面和后台服务,并且利用open api自动 ...
- CDH6 高版本hbase+solr实现二级索引
之前的环境是单独下载的CDH组件包搭建的集群,但是因为hadoop版本过低导致漏洞无法修复,重新搭建高版本集群环境. 新集群环境: 主要组件:hadoop,hbase,zookeeper,Key-Va ...
- rasdaman介绍及安装
一.分布式介绍 Rasdaman中的主节点称为Rasdaman的主机,它充当中央Rasdaman请求分派器并且控制所有服务器进程.Rasdaman管理器接收客户机请求并将这些请求分配给服务器进程.服务 ...
- charles 抓包iOS模拟器 HTTPS请求
参考: https://www.jianshu.com/p/3bfae9ede35e https://www.jianshu.com/p/171046d9f4f9 https://www.jiansh ...
- POJ1661
题目链接:http://poj.org/problem?id=1661 解题思路: 离散化处理 + DP. 首先,纵坐标除了用来判断老鼠是否会摔死之外基本没用,主要考虑横坐标,只要求出在横坐标上必须走 ...