ibatis/mybatis分庫工具包cobarclient的使用

jopen 10年前發布 | 64K 次閱讀 MyBatis3 持久層框架 MyBatis

cobar分為client和server:server是一個獨立的服務;client則是一個引用jar包。

github地址:https://github.com/alibaba/cobar、https://github.com/alibaba/cobarclient

本文主要記錄cobarclient的常用配置

我用的2.1.0版本,maven配置:

        <dependency>
            <groupId>com.alibaba.cobar</groupId>
            <artifactId>cobar-client</artifactId>
            <version>2.1.0-SNAPSHOT</version>
        </dependency>

spring的相關配置:

<?xml version="1.0" encoding="utf-8"?>
<beans default-autowire="byName"
    xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">

    <util:set id="shardSet" set-class="java.util.LinkedHashSet">
        <ref bean="shard1"/>
        <ref bean="shard2"/>
    </util:set>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">

        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />

        <property name="filters" value="stat" />

        <property name="maxActive" value="${jdbc.maxActive}" />
        <property name="initialSize" value="1" />
        <property name="maxWait" value="${jdbc.maxIdle}" />
        <property name="minIdle" value="1" />

        <property name="timeBetweenEvictionRunsMillis" value="3000" />
        <property name="minEvictableIdleTimeMillis" value="300000" />

        <property name="validationQuery" value="SELECT 'x'" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />

        <property name="poolPreparedStatements" value="true" />
        <property name="maxPoolPreparedStatementPerConnectionSize"
            value="20" />
    </bean>

    <bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">

        <property name="url" value="${jdbc1.url}" />
        <property name="username" value="${jdbc1.username}" />
        <property name="password" value="${jdbc1.password}" />

        <property name="filters" value="stat" />

        <property name="maxActive" value="${jdbc1.maxActive}" />
        <property name="initialSize" value="1" />
        <property name="maxWait" value="${jdbc1.maxIdle}" />
        <property name="minIdle" value="1" />

        <property name="timeBetweenEvictionRunsMillis" value="3000" />
        <property name="minEvictableIdleTimeMillis" value="300000" />

        <property name="validationQuery" value="SELECT 'x'" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />

        <property name="poolPreparedStatements" value="true" />
        <property name="maxPoolPreparedStatementPerConnectionSize"
            value="20" />
    </bean>

    <bean id="shard1" class="com.alibaba.cobarclient.Shard">
        <property name="id" value="shard1"></property>
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="shard2" class="com.alibaba.cobarclient.Shard">
        <property name="id" value="shard2"></property>
        <property name="dataSource" ref="dataSource1"></property>
    </bean>

    <util:map id="functionsMap">
        <entry key="hash" value-ref="hashFunction">
        </entry>
    </util:map>
    
    <bean id="hashFunction" class="com.raycloud.maijia.db.router.rules.support.HashFunction"/>

    <bean id="router" class="com.alibaba.cobarclient.config.SimpleRouterFactoryBean">
        <property name="configLocations">
            <list>
                <value>classpath:/dbrule/sqlaction-sharding-rules.xml</value>
                <value>classpath:/dbrule/ns-sharding-rules.xml</value>
            </list>
        </property>
        <property name="functions" ref="functionsMap"></property>
        <property name="shards" ref="shardSet"></property>
    </bean>

    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocations" value="classpath:/sqlmap-config.xml"></property>
    </bean>

    <bean id="sqlMapClientTemplate" class="com.alibaba.cobarclient.MysdalSqlMapClientTemplate">
        <property name="sqlMapClient" ref="sqlMapClient" />
        <property name="shards" ref="shardSet"></property>
        <property name="router" ref="router"></property>
    </bean>

    <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true" />

    <aop:aspectj-autoproxy proxy-target-class="true" />

    <bean id="txManager"
        class="com.alibaba.cobarclient.transaction.BestEffortMultiDataSourceTransactionManager">
        <property name="shards" ref="shardSet"></property>
    </bean>

</beans>

根據namespace的路由規則配置文件ns-sharding-rules.xml:

<rules>
  <rule>
    <namespace>sqlmap_shop_statistics</namespace>
    <shardingExpression><![CDATA[hash.apply(shopId)==0]]></shardingExpression>
    <shards>shard1</shards>
  </rule>
  <rule>
    <namespace>sqlmap_shop_statistics</namespace>
    <shardingExpression><![CDATA[hash.apply(shopId)==1]]></shardingExpression>
    <shards>shard2</shards>
  </rule>
</rules>

根據sqlmap的路由規則配置文件sqlaction-sharding-rules.xml:

<rules>
    <rule>
        <sqlmap>sqlmap_shop.queryShopDetailById</sqlmap>
        <shardingExpression><![CDATA[hash.apply(id)==0]]></shardingExpression>
        <shards>shard1</shards>
    </rule>
    <rule>
        <sqlmap>sqlmap_shop.queryShopDetailById</sqlmap>
        <shardingExpression><![CDATA[hash.apply(id)==1]]></shardingExpression>
        <shards>shard2</shards>
    </rule>
</rules>

注:這里的分片表達式也可以直接寫成:

<shardingExpression>true</shardingExpression>

如果一個查詢要查詢兩個片(不要有空格哦):

    <rule>
        <sqlmap>sqlmap_shop.queryShopDetailById</sqlmap>
        <shardingExpression><![CDATA[hash.apply(id)==1]]></shardingExpression>
        <shards>shard1,shard2</shards>
    </rule>

cobarclient最近更新有點慢,如果業務不是很復雜的話,可以用下,畢竟輕量級的還是比較方便移植或替代的。

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!