spring的AbstractRoutingDataSource多数据源配置以及切换

1,首先是对于数据源的配置

<bean id="dataSource"
		class="com.howbuy.pa.framework.rdbms.datasourceRoute.DynamicDataSource">
		<property name="targetDataSources">
			<map>
				<entry key="master" value-ref="master"></entry>
				<entry key="slave" value-ref="slave"></entry>
			</map>
		</property>
		<property name="defaultTargetDataSource" ref="master"></property>
	</bean>

	<bean id="master" class="com.howbuy.common.db.HowbuyBasicDataSource3"
		destroy-method="close">
		<property name="hpsUrl" value="http://it-server.howbuy.domain:8060/hps/ps" />
		<property name="url" value="${master.ds.jdbcUrl}" />
		<property name="username" value="${master.ds.user}" />
		<!-- <property name="password" value="${master.ds.password}" /> -->
		<property name="password" value="DB.mring-coop.PassWord" />
		<property name="initialSize" value="${master.ds.initialSize}" />
		<property name="minIdle" value="${master.ds.minIdle}" />
		<property name="maxActive" value="${master.ds.maxActive}" />
		<property name="maxWait" value="${master.ds.maxWait}" />
		<property name="timeBetweenEvictionRunsMillis" value="${master.ds.timeBetweenEvictionRunsMillis}" />
		<property name="minEvictableIdleTimeMillis" value="${master.ds.minEvictableIdleTimeMillis}" />
		<property name="validationQuery" value="${master.ds.validationQuery}" />
		<property name="testWhileIdle" value="${master.ds.testWhileIdle}" />
		<property name="testOnBorrow" value="${master.ds.testOnBorrow}" />
		<property name="testOnReturn" value="${master.ds.testOnReturn}" />

	</bean>


	<bean id="slave" class="com.alibaba.druid.pool.DruidDataSource"
		destroy-method="close">
		<property name="url" value="${slave.ds.jdbcUrl}" />
		<property name="username" value="${slave.ds.user}" />
		<property name="password" value="${slave.ds.password}" />
		<property name="initialSize" value="${slave.ds.initialSize}" />
		<property name="minIdle" value="${slave.ds.minIdle}" />
		<property name="maxActive" value="${slave.ds.maxActive}" />
		<property name="maxWait" value="${slave.ds.maxWait}" />
		<property name="timeBetweenEvictionRunsMillis" value="${slave.ds.timeBetweenEvictionRunsMillis}" />
		<property name="minEvictableIdleTimeMillis" value="${slave.ds.minEvictableIdleTimeMillis}" />
		<property name="validationQuery" value="${slave.ds.validationQuery}" />
		<property name="testWhileIdle" value="${slave.ds.testWhileIdle}" />
		<property name="testOnBorrow" value="${slave.ds.testOnBorrow}" />
		<property name="testOnReturn" value="${slave.ds.testOnReturn}" />
	</bean>

配置中DynamicDataSource类是由自己新建 要求继承AbstractRoutingDataSource类:

public class DynamicDataSource extends AbstractRoutingDataSource
{

    public DynamicDataSource()
    {
    }

    protected Object determineCurrentLookupKey()
    {
        return DynamicDataSourceSwitch.getDataSource();
    }
}

determineCurrentLookupKey方法内就是数据源切换的规则,可以自定义

2、切换数据源

数据源的切换时在代码内手动去做,一般是在Dao前,通过切面或者某种方式

DynamicDataSourceSwitch.setDataSource("slave");

补充一下这个类

public class DynamicDataSourceSwitch
{

    public DynamicDataSourceSwitch()
    {
    }

    public static void setDataSource(String name)
    {
        holder.set(name);
    }

    public static String getDataSource()
    {
        return (String)holder.get();
    }

    public static final ThreadLocal holder = new ThreadLocal();

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用AbstractRoutingDataSource可以很方便地配置多数据源,具体步骤如下: 1.自定义动态数据源类DynamicDataSource,继承AbstractRoutingDataSource类,并实现determineCurrentLookupKey()方法,该方法返回当前数据源的key值。 2.在Spring配置文件中配置多个数据源,并将DynamicDataSource作为默认数据源。 3.使用AOP技术,在方法执行前动态切换数据源。 下面是一个简单的示例: ```java // 自定义动态数据源类 public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DbContextHolder.getDataSource(); } } // 数据源上下文,用于存储当前线程使用的数据源key值 public class DbContextHolder { private static final ThreadLocal<String> contextHolder = new ThreadLocal<>(); public static void setDataSource(String dataSource) { contextHolder.set(dataSource); } public static String getDataSource() { return contextHolder.get(); } public static void clearDataSource() { contextHolder.remove(); } } // 配置多个数据源 @Bean(name = "dataSource1") public DataSource dataSource1() { // ... } @Bean(name = "dataSource2") public DataSource dataSource2() { // ... } @Bean(name = "dynamicDataSource") public DynamicDataSource dynamicDataSource() { DynamicDataSource dataSource = new DynamicDataSource(); Map<Object, Object> targetDataSources = new HashMap<>(); targetDataSources.put("dataSource1", dataSource1()); targetDataSources.put("dataSource2", dataSource2()); dataSource.setTargetDataSources(targetDataSources); dataSource.setDefaultTargetDataSource(dataSource1()); return dataSource; } // 使用AOP动态切换数据源 @Aspect @Component public class DataSourceAspect { @Pointcut("@annotation(com.gnxk.datasource.annotation.DataSource)") public void dataSourcePointCut() { } @Before("dataSourcePointCut()") public void before(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); DataSource dataSource = signature.getMethod().getAnnotation(DataSource.class); if (dataSource == null) { DbContextHolder.setDataSource("dataSource1"); } else { DbContextHolder.setDataSource(dataSource.value()); } } @After("dataSourcePointCut()") public void after(JoinPoint joinPoint) { DbContextHolder.clearDataSource(); } } ``` 使用注解@DataSource("dataSource1")或@DataSource("dataSource2")来指定使用哪个数据源。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值