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

jopen 9年前

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最近更新有点慢,如果业务不是很复杂的话,可以用下,毕竟轻量级的还是比较方便移植或替代的。