我的Spring多数据源

Spring 多数据源已经不是什么稀奇的事了,在实际应用中主从数据库就需要用到多数据源的配置与动态切换。在搜索引擎中都可以找到很多资料,各也有各的做法,本文也就不做过多的阐述其原理,只是介绍下目前我在项目中对于多数据源的使用情况,欢迎大家前来拍砖。

先特别感谢好友 Tony

首先,继承spring-jdbc 的 AbstractRoutingDataSource

public class DynamicDataSource extends AbstractRoutingDataSource {    
    protected Object determineCurrentLookupKey() {        
        return DataSourceHolder.getDataSources();    
    }
}

创建自己的DataSourceHolder

public class DataSourceHolder {    
    
    private static ThreadLocal dataSources = new ThreadLocal();    

    public static String getDataSources() {        
        String source = dataSources.get();        
        if (source == null) source = DataSourceType.MASTER;        
        return source;    
    }    
    public static void setDataSources(String source) {       
        dataSources.set(source);    
    }
}

当然还有DataSourceType

public class DataSourceType {    
    public static final String MASTER = "master";    
    public static final String SLAVE = "slaver";
}

到此为止,和所有搜索引擎中找到的资料一致,除了命名可能有些区别 _
那么接下来介绍一下不同的地方,网上也有很多资料是通过AOP来实现动态切换,比如:Spring 配置多数据源实现数据库读写分离 这篇文章中的介绍,就很完美的使用AOP实现了主从数据库的切换,而我的做法有一些大同小异,我使用了Java动态代理,先上代码。

先创建自己的Annotation:

从库注解

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Read {}

主库注解

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Write {}

为了以防万一,增加了OtherSource
而这个也只是为了防止在一个项目中有多个数据库的连接需求,比如:将用户信息独立成库,商品信息独立成库等

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface OtherSource {    
    String value();
}

以下则为真正重点——动态代理
继承MapperFactoryBean,实现InvocationHandler,在invoke之前,根据注解来实现动态数据源的切换

public class MapperFactoryBeanInvocation extends MapperFactoryBean implements InvocationHandler {    
    private T t;    
    public MapperFactoryBeanInvocation() {    }    
    public T getObject() throws Exception {        
        T t = super.getObject();        
        return this.getProxyObject(t);    
    }    
    private T getProxyObject(T t) {        
        this.t = t;        
        return (T) Proxy.newProxyInstance(t.getClass().getClassLoader(), t.getClass().getInterfaces(), this);    
    }    
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {        
        boolean isRead = true;        
        StringBuffer typeBuffer = new StringBuffer("");
        //获取是否有其他数据源
        OtherSource ortherSourceAnn = method.getAnnotation(OtherSource.class);        
        if (ortherSourceAnn != null) {            
            typeBuffer.append(ortherSourceAnn.value()).append("_");        
        }   
        //获取主从状态    
        Annotation ann_method = method.getAnnotation(Read.class);        
        if (ann_method == null) isRead = false;  
        if (isRead) typeBuffer.append(DataSourceType.SLAVE);        
        else typeBuffer.append(DataSourceType.MASTER);   
        //切换数据源    
        DataSourceHolder.setDataSources(typeBuffer.toString());        
        return method.invoke(this.t, args);    
    }
}

至此,Spring多数据源主从动态切换已经实现。
在实际使用过程中,对应的管理好多数据源配置,即可实现动态切换的目的

db.properties 配置

#jdbc driver
jdbc.driverClassName=com.mysql.jdbc.Driver
#master
design.master.jdbc.url=jdbc:mysql://192.168.1.101:3306/design_shop?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=true
design.master.jdbc.username=root
design.master.jdbc.password=111111
#slaver
design.slaver.jdbc.url=jdbc:mysql://192.168.1.102:3306/design_shop?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=true
design.slaver.jdbc.username=root
design.slaver.jdbc.password=111111
#master
shop.master.jdbc.url=jdbc:mysql://192.168.1.103:3306/shop_mall?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=true
shop.master.jdbc.username=root
shop.master.jdbc.password=a12345678
#slaver
shop.slaver.jdbc.url=jdbc:mysql://192.168.1.104:3306/shop_mall?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=true
shop.slaver.jdbc.username=root
shop.slaver.jdbc.password=a12345678
#c3p0 config
c3p0.initialPoolSize=8
c3p0.minPoolSize=5
c3p0.maxPoolSize=10
c3p0.acquireIncrement=10
c3p0.maxIdleTime=60
c3p0.idleConnectionTestPeriod=120
c3p0.maxStatements=100
c3p0.autoCommitOnClose=false
c3p0.testConnectionOnCheckout=false
c3p0.testConnectionOnCheckin=false
c3p0.preferredTestQuery=select now()

spring 配置

...
    
        
        
        
        
        
        
        
        
       
        
        
    



...

dataSource.xml 配置

...
    
        
            
            
        
    
    
        
            
            
        
    
    
        
            
            
        
    
    
        
            
            
        
    
    
        
                
                    
                        
                        
                        
                    
            
            
            
        
    
    
        
            
        

...

data_mapping.xml 配置
需要注意的是,此处class指向的是自己创建的MapperFactoryBeanInvocation动态代理


        
    

    
           
    

Mybatis Mapper

public interface AccountLogMapper {
...
   @SelectProvider(type=AccountLogSqlProvider.class, method="selectByExample")
   @Results({    
       @Result(column="log_id", property="logId", jdbcType=JdbcType.INTEGER, id=true), 
       @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
       @Result(column="user_money", property="userMoney", jdbcType=JdbcType.DECIMAL),
       @Result(column="frozen_money", property="frozenMoney", jdbcType=JdbcType.DECIMAL), 
       @Result(column="rank_points", property="rankPoints", jdbcType=JdbcType.INTEGER), 
       @Result(column="pay_points", property="payPoints", jdbcType=JdbcType.INTEGER), 
       @Result(column="change_time", property="changeTime", jdbcType=JdbcType.INTEGER), 
       @Result(column="change_desc", property="changeDesc", jdbcType=JdbcType.VARCHAR), 
       @Result(column="change_type", property="changeType", jdbcType=JdbcType.TINYINT)
  })
   //因为默认为一库主从,因此这个Mapper没有使用@OtherSource注解,而@Read则指向从库,对应dataSource.xml 中的 salver
   @Read
   List selectByExample(AccountLogExample example);
...
}
public interface MallWechatImageTextTempMapper {
...
    @SelectProvider(type=MallWechatImageTextTempSqlProvider.class, method="countByExample")
    //此处@OtherSource("shop")指向了shop_mall数据库  @Read 指向从库  所以对应 dataSource.xml 中的 shop_slaver
    @Read
    @OtherSource("shop")
    long countByExample(MallWechatImageTextTempExample example);
...
}

Spring Test
测试代码使用kotlin编写,基本和Java区别不是很大,函数式写起来更方便 _

@RunWith(SpringJUnit4ClassRunner::class)
@ContextConfiguration("classpath:appServer.xml")
class Test {    

    @Autowired    
    var mapper: AccountLogMapper? = null    
    @Autowired    
    var imageMapper: MallWechatImageTextTempMapper? = null  
  
    @Test    
    fun test() {        
        println("*********************************** test shop_mall mall_wechat_image_temp countByExample ***********************************************")        
        val count = imageMapper?.countByExample(MallWechatImageTextTempExample().apply {
            createCriteria().andTempSetCodeEqualTo("wechat_temp_1")            
            resultColumn = "tempid"            
            offset = 0            
            limit = 100}) ?: 0L        
        println(count)        
        assert(true)    
    }
   
    @Test    
    fun testMyselfDB() {        
        println("*********************************** test myself  design_shop account_log countByExample ***********************************************")        
        val list = mapper?.selectByExample(AccountLogExample()) ?: arrayListOf()        
        if (list.size > 0) {            
            assert(true)        
        } else {            
            assert(false)        
        }    
    }
}

好了,所有和动态数据源相关的代码与示例已经完成了,我们现在来看下实际运行效果:

*********************************** test shop_mall  mall_wechat_image_temp countByExample ***********************************************
02:31:56 [main] INFO  com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 10, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 1hge1619lmh41qu1kjm50|366ac49b, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 1hge1619lmh41qu1kjm50|366ac49b, idleConnectionTestPeriod -> 120, initialPoolSize -> 8, jdbcUrl -> jdbc:mysql://139.224.40.142:3306/mall?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=true, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 60, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 100, maxStatementsPerConnection -> 0, minPoolSize -> 5, numHelperThreads -> 3, preferredTestQuery -> select now(), privilegeSpawnedThreads -> false, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
02:31:56 [main] DEBUG com.design.shop.dao.MallWechatImageTextTempMapper.countByExample - ==>  Preparing: SELECT count(tempid) FROM comp_mall_wechat_imagetext_temp WHERE ((temp_set_code = ?)) LIMIT 0,100 
02:31:56 [main] DEBUG com.design.shop.dao.MallWechatImageTextTempMapper.countByExample - ==> Parameters: wechat_temp_1(String)
02:31:56 [main] DEBUG com.design.shop.dao.MallWechatImageTextTempMapper.countByExample - <==      Total: 1
4
*********************************** test myself  design_shop account_log countByExample ***********************************************
02:31:56 [main] INFO  com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 10, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 1hge1619lmh41qu1kjm50|5f0e9815, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 1hge1619lmh41qu1kjm50|5f0e9815, idleConnectionTestPeriod -> 120, initialPoolSize -> 8, jdbcUrl -> jdbc:mysql://127.0.0.1:3306/design_shop?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=true, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 60, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 100, maxStatementsPerConnection -> 0, minPoolSize -> 5, numHelperThreads -> 3, preferredTestQuery -> select now(), privilegeSpawnedThreads -> false, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
02:31:56 [main] DEBUG com.design.shop.dao.AccountLogMapper.selectByExample - ==>  Preparing: SELECT log_id, user_id, user_money, frozen_money, rank_points, pay_points, change_time, change_desc, change_type FROM des_account_log 
02:31:56 [main] DEBUG com.design.shop.dao.AccountLogMapper.selectByExample - ==> Parameters: 
02:31:56 [main] DEBUG com.design.shop.dao.AccountLogMapper.selectByExample - <==      Total: 35

2个不同的从库实现了动态切换,是不是有点暗爽?当然 Mybatis Mapper 是由 MybatisGenerator生成的,原版生成可不会带@Read @Write @OtherSource 等注解 也没有 offset、limit 分页属性,以上内容可以通过对MybatisGenerator做一些小改造,提供插件即可全部实现。在后续文章中会提及MybatisGenerator的插件制作,尽请期待 _

第一次发文,文笔还有没有介绍清楚的地方,还请海涵。

你可能感兴趣的:(我的Spring多数据源)