项目底层数据源引擎平滑切换

前言

随着数据量的增长以及业务的调整变更,我们需要选择合适的技术及存储引擎对数据进行归类,调整,达到高并发、秒响应、低延迟及可扩展对现有程序的改造升级

问题&现状

  1. 任务重,时间紧,人力不足,不能够重构业务
  2. 平滑切换,客户无感知
  3. 自建git服务器一套代码,要兼容不同分支发布

解决方案

1.对于问题一,我们要采用oop to aop 的变成思维的转变,即通过切面来完成,尽量不改动原来的业务逻辑,尤其是之前的老系统,多分组,一个改动,可能导致几十个bug 的,横空出世,太太可怕了吧
2. 第二个问题就是兼容,略带框架思维,走配置化和开关,不仅可以起到有效的解耦,而且还可以做到有效的补偿,有问题及时解决
3. 总体方案 切面 + 配置化

案例分享

直接更换数据库引擎 &根据标记做数据源切换

spring &Mybatis

案例一:基于包扫描的单数据源
1. xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

     <bean id="parentDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="maxActive" value="${windBird.maxActive}"/>
        <property name="initialSize" value="${windBird.initialSize}"/>
        <property name="maxWait" value="${windBird.maxWait}"/>
        <property name="minIdle" value="${windBird.minIdle}"/>
        <property name="timeBetweenEvictionRunsMillis" value="${windBird.timeBetweenEvictionRunsMillis}"/>
        <property name="minEvictableIdleTimeMillis" value="${windBird.minEvictableIdleTimeMillis}"/>
        <property name="validationQuery" value="${windBird.validationQuery}"/>
        <property name="testWhileIdle" value="${windBird.testWhileIdle}"/>
        <property name="testOnBorrow" value="${windBird.testOnBorrow}"/>
        <property name="testOnReturn" value="${windBird.testOnReturn}"/>
        <property name="poolPreparedStatements" value="${windBird.poolPreparedStatements}"/>
        <property name="maxOpenPreparedStatements" value="${windBird.maxOpenPreparedStatements}"/>
    </bean>

    <bean id="newDataSource" parent="parentDataSource">
        <property name="url">
            <value>${
   jdbc_new.url}</value>
        </property>
        <property name="username">
            <value>${
   jdbc_new.username}</value>
        </property>
        <property name="password">
            <value>${
   jdbc_new.password}</value>
        </property>
    </bean>
	
	<bean id="oldDataSource" parent="parentDataSource">
        <property name="url">
            <value>${
   jdbc_old.url}</value>
        </property>
        <property name="username">
            <value>${
   jdbc_old.username}</value>
        </property>
        <property name="password">
            <value>${
   jdbc_old.password}</value>
        </property>
    </bean>
<!--构造一个动态数据源 --> 
    <bean id="enhanceDynamicDataSource" class="com.windBird.service.impl.dataSource.enhanceDynamicDataSource"></bean>
<!--添加一个拦截器 -->  	
    <bean id="enhanceMapperInterceptor" class="com.windBird.service.impl.dataSource.EnhanceMapperInterceptor"></bean>
    <bean id="sqlSessionFactoryTestDataSource" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="enhanceDynamicDataSource"/>
        <property name="typeAliasesPackage" value="com.windBird.service.entity"/>
        <property name="mapperLocations">
            <list>
                <value>classpath:/sqlmap/test/*Mapper.xml</value>
                <value>classpath:/sqlmap/test/enhance/*Mapper.xml</value>
            </list>
        </property>
        <property name="plugins">
            <list>
                <ref bean="testMapperInterceptor"/>
            </list>
        </property>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryTestDataSource"/>
        <property name="basePackage" value="com.windBird.service.impl.test.dao"/>
        <property name="annotationClass" value="com.windBird.mybatis.MyBatisRepository"/>
    </bean>

    <bean id="txManagerTestHistory"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="enhanceDynamicDataSource"/>
		 <qualifier value="testHistory"/>
    </bean>
    <tx:annotation-driven transaction-manager="txManagerTestHistory"/>

</beans>

2. 构造的动态数据源
package com.wind.bird.service.impl.dataSource;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * @author windbird
 * @Date 2022/12/4 22:03
 * @ClassName EnhanceDynamicDataSource
 * @desc:  构造动态数据源
 */
public class EnhanceDynamicDataSource  extends AbstractRoutingDataSource {
   
    /**
     * 仅仅历史dao层切mapper
     */
    private static final ThreadLocal<Boolean> testReadMark = ThreadLocal.withInitial(() -> false);

    public static void setTestReadMark(Boolean Value){
   
        testReadMark.set(Value);
    }

    public static Boolean getTestReadMark(){
   
        return testReadMark.get();
    }


    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值