Cobar Client的使用

http://blog.csdn.net/wxwzy738/article/details/17265577


cobar client是出自阿里的产品,cobar client只需要引入jar包即可,不需要建立服务。下面的地址是cobar client的帮助文档

http://code.alibabatech.com/docs/cobarclient/zh/

http://www.kuqin.com/system-analysis/20120212/318089.html

分库分表都是在Cobar产品里面的,Cobar分为两类,分别是Cobar Client和Cobar Server,根据业务的需要进行选择,Cobar Server是一组独立的(Stand Alone)的Server集群,Cobar Client就是第三方的Java包,他就直接嵌入到应用上面去。然后他的拆分规则都是直接写到应用的配置文件里面的。这是阿里自主开发的,没有用到外面的一些开源的工具

用到的数据库创建语句:

[sql]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. create database dbtest1;  
  2. create database dbtest2;  
  3. create database dbtest3;  
  4.   
  5. //分别在这三个数据库中创建下面的表:  
  6. CREATE TABLE `cont` (  
  7.   `id` bigint(20) NOT NULL AUTO_INCREMENT,  
  8.   `taobaoId` bigint(20) DEFAULT '0',  
  9.   `namevarchar(20) DEFAULT '',  
  10.   `upd_time` datetime DEFAULT NULL,  
  11.   PRIMARY KEY (`id`)  
  12. ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;  

cobar是扩展ibatis的 SqlMapClientTemplate的功能,对分布在多个数据库的表进行路由读写的开源软件。但是不支持单库的多表操作。

以下贴出一些关键的代码,详细的可以下载功能进行运行查看:

工程截图:


下面列举一个简单的功能,就spring与ibatis进行整合,在mysql数据库上面进行操作,对cont表进行添加和查询的功能操作:

简单看下service,dao的文件代码

Service:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public interface ContService {  
  2.     /** 
  3.      * 基本插入 
  4.      *  
  5.      * @return 
  6.      */  
  7.     public Long addCont(Cont cont);  
  8.   
  9.     /** 
  10.      * 根据主键查询 
  11.      */  
  12.     public Cont getContByKey(Long id);  
  13.   
  14.     /** 
  15.      * 根据条件查询 
  16.      *  
  17.      * @param contQuery 
  18.      *            查询条件 
  19.      * @return 
  20.      */  
  21.     public List<Cont> getContList(ContQuery contQuery);  
  22. }  

ServiceImpl:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. @Service("contService")  
  2. public class ContServiceImpl implements ContService {  
  3.   
  4.     private static final Log log = LogFactory.getLog(ContServiceImpl.class);  
  5.   
  6.     @Resource  
  7.     ContDAO contDAO;  
  8.   
  9.     /** 
  10.      * 插入数据库 
  11.      *  
  12.      * @return 
  13.      */  
  14.     public Long addCont(Cont cont) {  
  15.         try {  
  16.             return contDAO.addCont(cont);  
  17.         } catch (SQLException e) {  
  18.             log.error("dao addCont error.:" + e.getMessage(), e);  
  19.         }  
  20.         return 0L;  
  21.     }  
  22.   
  23.     /** 
  24.      * 根据主键查找 
  25.      */  
  26.     public Cont getContByKey(Long id) {  
  27.         try {  
  28.             return contDAO.getContByKey(id);  
  29.         } catch (SQLException e) {  
  30.             log.error("dao getContbyKey error.:" + e.getMessage(), e);  
  31.         }  
  32.         return null;  
  33.     }  
  34.   
  35.   
  36.     public List<Cont> getContList(ContQuery contQuery) {  
  37.         try {  
  38.             return contDAO.getContList(contQuery);  
  39.         } catch (SQLException e) {  
  40.             log.error("get Cont list error." + e.getMessage(), e);  
  41.         }  
  42.         return Collections.emptyList();  
  43.     }  
  44.   
  45. }  

Dao:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. @Repository  
  2. public class ContDAO {  
  3.   
  4.     @Resource  
  5.     SqlMapClientTemplate sqlMapClientTemplate;  
  6.   
  7.     public Long addCont(Cont cont) throws SQLException {  
  8.         return (Long) this.sqlMapClientTemplate.insert("Cont.insertCont", cont);  
  9.     }  
  10.   
  11.     /** 
  12.      * 根据主键查找 
  13.      *  
  14.      * @throws SQLException 
  15.      */  
  16.     public Cont getContByKey(Long id) throws SQLException {  
  17.         Map<String, Object> params = new HashMap<String, Object>();  
  18.         params.put("id", id);  
  19.         Cont result = (Cont) this.sqlMapClientTemplate.queryForObject(  
  20.                 "Cont.getContByKey", params);  
  21.         return result;  
  22.     }  
  23.   
  24.     @SuppressWarnings("unchecked")  
  25.     public List<Cont> getContList(ContQuery contQuery) throws SQLException {  
  26.         if (contQuery.getFields() != null && contQuery.getFields() != "") {  
  27.             return (List<Cont>) this.sqlMapClientTemplate.queryForList(  
  28.                     "Cont.getContListFields", contQuery);  
  29.         }  
  30.         return (List<Cont>) this.sqlMapClientTemplate.queryForList(  
  31.                 "Cont.getContList", contQuery);  
  32.     }  
  33.   
  34. }  


1、下面applicationContext.xmlspring配置文件是针对没有在cobar的情况下进行的常规配置:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.        xmlns:aop="http://www.springframework.org/schema/aop"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  7.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd  
  9.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"  
  10.        default-lazy-init="false">  
  11.   
  12.     <description>Spring公共配置</description>  
  13.     <context:component-scan base-package="com.hj.cobar"></context:component-scan>  
  14.       
  15.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  16.        <property name="location">  
  17.          <value>application.development.properties</value>  
  18.        </property>  
  19.     </bean>  
  20.       
  21.     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
  22.         <!-- 基本属性 url、user、password -->  
  23.         <property name="url" value="${jdbc_url}"/>  
  24.         <property name="username" value="${jdbc_user}"/>  
  25.         <property name="password" value="${jdbc_password}"/>  
  26.   
  27.         <!-- 配置初始化大小、最小、最大 -->  
  28.         <property name="initialSize" value="1"/>  
  29.         <property name="minIdle" value="1"/>  
  30.         <property name="maxActive" value="20"/>  
  31.   
  32.         <!-- 配置获取连接等待超时的时间 -->  
  33.         <property name="maxWait" value="60000"/>  
  34.   
  35.         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  
  36.         <property name="timeBetweenEvictionRunsMillis" value="60000"/>  
  37.   
  38.         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  
  39.         <property name="minEvictableIdleTimeMillis" value="300000"/>  
  40.   
  41.         <property name="validationQuery" value="SELECT 'x'"/>  
  42.         <property name="testWhileIdle" value="true"/>  
  43.         <property name="testOnBorrow" value="false"/>  
  44.         <property name="testOnReturn" value="false"/>  
  45.   
  46.         <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->  
  47.         <property name="poolPreparedStatements" value="false"/>  
  48.         <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>  
  49.   
  50.         <!-- 配置监控统计拦截的filters -->  
  51.         <property name="filters" value="stat"/>  
  52.     </bean>  
  53.       
  54.      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  55.         <property name="dataSource" ref="dataSource"/>  
  56.     </bean>  
  57.       
  58.     <!-- 使用annotation定义事务 -->  
  59.     <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>  
  60.       
  61.     <!-- 配置ibatis的SqlMapClient -->  
  62.     <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  
  63.         <property name="dataSource" ref="dataSource" />    
  64.         <property name="configLocation">  
  65.             <value>classpath:/sqlmap-config.xml</value>  
  66.         </property>  
  67.     </bean>  
  68.       
  69.     <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">  
  70.         <property name="sqlMapClient" ref="sqlMapClient" />    
  71.     </bean>  
  72.       
  73. </beans>  

2、下面是在applicationContext.xml使用cobar的配置,一些细节可参考阿里的cobar的帮助文档:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.        xmlns:aop="http://www.springframework.org/schema/aop"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  7.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd  
  9.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"  
  10.        default-lazy-init="false">  
  11.   
  12.     <description>Spring公共配置</description>  
  13.     <context:component-scan base-package="com.hj.cobar"></context:component-scan>  
  14.       
  15.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  16.        <property name="location">  
  17.          <value>application.development.properties</value>  
  18.        </property>  
  19.     </bean>  
  20.       
  21. <!-- 配置数据源开始 -->  
  22.     <bean id="dataSources" class="com.alibaba.cobar.client.datasources.DefaultCobarDataSourceService">  
  23.         <property name="dataSourceDescriptors">  
  24.             <set>  
  25.                 <bean class="com.alibaba.cobar.client.datasources.CobarDataSourceDescriptor">  
  26.                     <property name="identity" value="partition0"/>  
  27.                     <property name="targetDataSource" ref="dataSource0"/>  
  28.                     <property name="targetDetectorDataSource" ref="dataSource0"/>  
  29.                 </bean>  
  30.                 <bean class="com.alibaba.cobar.client.datasources.CobarDataSourceDescriptor">  
  31.                     <property name="identity" value="partition1"/>  
  32.                     <property name="targetDataSource" ref="dataSource1"/>  
  33.                     <property name="targetDetectorDataSource" ref="dataSource1"/>  
  34.                 </bean>  
  35.                 <bean class="com.alibaba.cobar.client.datasources.CobarDataSourceDescriptor">  
  36.                     <property name="identity" value="partition2"/>  
  37.                     <property name="targetDataSource" ref="dataSource2"/>  
  38.                     <property name="targetDetectorDataSource" ref="dataSource2"/>  
  39.                 </bean>  
  40.             </set>  
  41.         </property>  
  42.         <property name="haDataSourceCreator">  
  43.             <bean class="com.alibaba.cobar.client.datasources.ha.FailoverHotSwapDataSourceCreator">  
  44.                 <property name="detectingSql" value="update cobarha set timeflag=CURRENT_TIMESTAMP()"/>  
  45.             </bean>  
  46.         </property>  
  47.     </bean>  
  48.       
  49.     <!-- 数据源0 -->  
  50.     <bean id="dataSource0" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">   
  51.         <property name="url" value="${jdbc0.url}" />  
  52.         <property name="username" value="${jdbc0.username}" />  
  53.         <property name="password" value="${jdbc0.password}" />  
  54.         <property name="filters" value="config" />  
  55.         <property name="maxActive" value="5" />  
  56.         <property name="initialSize" value="5" />  
  57.         <property name="maxWait" value="1" />  
  58.         <property name="minIdle" value="5" />  
  59.         <property name="timeBetweenEvictionRunsMillis" value="3000" />  
  60.         <property name="minEvictableIdleTimeMillis" value="300000" />  
  61.         <property name="validationQuery" value="SELECT 'x'" />  
  62.         <property name="testWhileIdle" value="true" />  
  63.         <property name="testOnBorrow" value="false" />  
  64.         <property name="testOnReturn" value="false" />  
  65.         <property name="poolPreparedStatements" value="true" />  
  66.         <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />  
  67.         <!-- <property name="connectionProperties" value="config.decrypt=true" /> -->  
  68.     </bean>  
  69.       
  70.     <!-- 数据源1 -->  
  71.     <bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">   
  72.         <property name="url" value="${jdbc1.url}" />  
  73.         <property name="username" value="${jdbc1.username}" />  
  74.         <property name="password" value="${jdbc1.password}" />  
  75.         <property name="filters" value="config" />  
  76.         <property name="maxActive" value="5" />  
  77.         <property name="initialSize" value="5" />  
  78.         <property name="maxWait" value="1" />  
  79.         <property name="minIdle" value="5" />  
  80.         <property name="timeBetweenEvictionRunsMillis" value="3000" />  
  81.         <property name="minEvictableIdleTimeMillis" value="300000" />  
  82.         <property name="validationQuery" value="SELECT 'x'" />  
  83.         <property name="testWhileIdle" value="true" />  
  84.         <property name="testOnBorrow" value="false" />  
  85.         <property name="testOnReturn" value="false" />  
  86.         <property name="poolPreparedStatements" value="true" />  
  87.         <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />  
  88.     </bean>  
  89.       
  90.     <!-- 数据源2 -->  
  91.     <bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">   
  92.         <property name="url" value="${jdbc2.url}" />  
  93.         <property name="username" value="${jdbc2.username}" />  
  94.         <property name="password" value="${jdbc2.password}" />  
  95.         <property name="filters" value="config" />  
  96.         <property name="maxActive" value="5" />  
  97.         <property name="initialSize" value="5" />  
  98.         <property name="maxWait" value="1" />  
  99.         <property name="minIdle" value="5" />  
  100.         <property name="timeBetweenEvictionRunsMillis" value="3000" />  
  101.         <property name="minEvictableIdleTimeMillis" value="300000" />  
  102.         <property name="validationQuery" value="SELECT 'x'" />  
  103.         <property name="testWhileIdle" value="true" />  
  104.         <property name="testOnBorrow" value="false" />  
  105.         <property name="testOnReturn" value="false" />  
  106.         <property name="poolPreparedStatements" value="true" />  
  107.         <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />  
  108.     </bean>  
  109. <!-- 配置数据源结束 -->    
  110.       
  111. <!-- 配置路由规则开始 -->  
  112.     <bean id="hashFunction" class="com.hj.cobar.dao.router.HashFunction"></bean>  
  113.     <bean id="internalRouter"  
  114.         class="com.alibaba.cobar.client.router.config.CobarInteralRouterXmlFactoryBean">  
  115.         <!-- functionsMap是在使用自定义路由规则函数的时候使用 -->  
  116.         <property name="functionsMap">  
  117.             <map>  
  118.                 <entry key="hash" value-ref="hashFunction"></entry>  
  119.             </map>  
  120.         </property>  
  121.         <property name="configLocations">  
  122.             <list>  
  123.                 <value>classpath:/dbRule/sharding-rules-on-namespace.xml</value>  
  124.             </list>  
  125.         </property>  
  126.     </bean>  
  127. <!-- 配置路由规则结束 -->    
  128.     
  129.     <!-- 事务配置 -->  
  130.     <bean id="transactionManager" class="com.alibaba.cobar.client.transaction.MultipleDataSourcesTransactionManager">  
  131.         <property name="cobarDataSourceService" ref="dataSources"/>  
  132.     </bean>  
  133.       
  134.     <!-- 使用annotation定义事务 -->  
  135.     <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>  
  136.       
  137.     <!--  iBatis SQL map定义。                                                    -->  
  138.     <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  
  139.         <!-- 这里配置的dataSource0为默认的数据源,如果找不到数据库的话则到该数据源中查找 -->  
  140.         <property name="dataSource" ref="dataSource0" />    
  141.         <property name="configLocation">  
  142.             <value>classpath:/sqlmap-config.xml</value>  
  143.         </property>  
  144.     </bean>  
  145.       
  146.     <!-- 工程里一定要使用此工程模板,不能再使用ibatis原生的api,不然有的情况会不经过cobar的过滤 -->  
  147.     <bean id="sqlMapClientTemplate" class="com.alibaba.cobar.client.CobarSqlMapClientTemplate">  
  148.         <property name="sqlMapClient" ref="sqlMapClient" />  
  149.         <property name="cobarDataSourceService" ref="dataSources" />  
  150.         <property name="router" ref="internalRouter" />  
  151.         <property name="sqlAuditor">  
  152.             <bean class="com.alibaba.cobar.client.audit.SimpleSqlAuditor" />  
  153.         </property>  
  154.         <property name="profileLongTimeRunningSql" value="true" />  
  155.         <property name="longTimeRunningSqlIntervalThreshold" value="3600000" />  
  156.     </bean>  
  157.       
  158. </beans>  

3、在sharding-rules-on-namespace.xml配置cobar的路由规则,因为此路由规则是使用自定义的路由规则,所以还要写一个自定义的规则函数类

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <rules>  
  2.   
  3.     <rule>  
  4.         <namespace>Cont</namespace>  
  5.         <!--   
  6.                表达式如果不使用自定义路由规则函数,而是直接使用   taobaoId%2==0这种的话就不用在文件  
  7.                中配置<property name="functionsMap">中了  
  8.         -->  
  9.         <shardingExpression>hash.apply(taobaoId) == 0</shardingExpression>  
  10.         <shards>partition0</shards>  
  11.     </rule>  
  12.     <rule>  
  13.         <namespace>Cont</namespace>  
  14.         <shardingExpression>hash.apply(taobaoId) == 1</shardingExpression>  
  15.         <shards>partition1</shards>  
  16.     </rule>  
  17.     <rule>  
  18.         <namespace>Cont</namespace>  
  19.         <shardingExpression>hash.apply(taobaoId) == 2</shardingExpression>  
  20.         <shards>partition2</shards>  
  21.     </rule>  
  22.       
  23. </rules>  

4、自定义的路由规则函数类

hash的一种做法是对taobaoId进行md5加密,然后取前几位(我们这里取前两位),然后就可以将不同的taobaoId哈希到不同的用户表(cont_xx)中了。

通过这个技巧,我们可以将不同的taobaoId分散到256中用户表中,分别是cont_00,user_01 ...... cont_ff。因为taobaoId是数字且递增,根据md5的算法,可以将用户数据几乎很均匀的分别到不同的cont表中。

但是这里有个问题是,如果我们的系统的数据越来越多,势必单张表的数据量越来越大,而且根据这种算法无法扩展表,这又会回到单表量大的问题。

下面只是简单的用求余的方法来返回值

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * 根据某种自定义的hash算法来进行散列,并根据散列的值进行路由 
  3.  *  常见的水平切分规则有: 
  4.     基于范围的切分, 比如 memberId > 10000 and memberId < 20000 
  5.     基于模数的切分, 比如 memberId%128==1 或者 memberId%128==2 或者... 
  6.     基于哈希(hashing)的切分, 比如hashing(memberId)==someValue等 
  7.  * @author hj 
  8.  * 
  9.  */  
  10. public class HashFunction{  
  11.       
  12.     /** 
  13.      * 对三个数据库进行散列分布 
  14.      * 1、返回其他值,没有在配置文件中配置的,如负数等,在默认数据库中查找 
  15.      * 2、比如现在配置文件中配置有三个结果进行散列,如果返回为0,那么apply方法只调用一次,如果返回为2, 
  16.      *   那么apply方法就会被调用三次,也就是每次是按照配置文件的顺序依次的调用方法进行判断结果,而不会缓存方法返回值进行判断 
  17.      * @param taobaoId 
  18.      * @return 
  19.      */  
  20.     public int apply(Long taobaoId) {  
  21.         //先从缓存获取 没有则查询数据库  
  22.         //input 可能是taobaoId,拿taobaoId到缓存里去查用户的DB坐标信息。然后把库的编号输出  
  23.         System.out.println("taobaoId:"+taobaoId);  
  24.         int result = (int)(taobaoId % 3);  
  25.         System.out.println("在第"+(result + 1)+"个数据库中");  
  26.         return result;  
  27.     }  
  28.       
  29.     /** 
  30.            注:只调用一次 
  31.         taobaoId:3354 
  32.         在第1个数据库中 
  33.        
  34.              注:调用了三次 
  35.         taobaoId:7043 
  36.         在第3个数据库中 
  37.         taobaoId:7043 
  38.         在第3个数据库中 
  39.         taobaoId:7043 
  40.         在第3个数据库中 
  41.      */  
  42.   
  43. }  

5、下面是对service的单元测试

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * cobar的单元测试 
  3.  */  
  4. public class AppTest extends TestCase{  
  5.     ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
  6.     ContService contService = (ContService) context.getBean("contService");  
  7.       
  8.     /** 
  9.      * 没有使用对象查询直接使用基本类型则到默认的数据源中去查找数据 
  10.      */  
  11.     public void test1(){  
  12.         Cont cont = contService.getContByKey(2L);  
  13.         System.out.println(cont);  
  14.           
  15.     }  
  16.       
  17.     /** 
  18.      * 测试添加 
  19.      */  
  20.     public void test2(){  
  21.         Cont cont = new Cont();  
  22.         cont.setName("gd");  
  23.         Long taobaoId = new Long(new Random().nextInt(10000));  
  24.         System.out.println("#"+taobaoId);  
  25.         cont.setTaobaoId(taobaoId);  
  26.         contService.addCont(cont);  
  27.     }  
  28.       
  29.     /** 
  30.      * 测试使用对象包含taobaoId属性的进行查找 
  31.      * 使用这种方式可以根据taobaoId分库查找 
  32.      */  
  33.     public void test3(){  
  34.         ContQuery contQuery = new ContQuery();  
  35.         contQuery.setTaobaoId(2809L);  
  36.         List<Cont> list = contService.getContList(contQuery);  
  37.         if(list != null){  
  38.             System.out.println(list.get(0));  
  39.         }  
  40.     }  
  41. }  

工程下载地址: http://download.csdn.net/detail/wxwzy738/6698067
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Cobar连接PostgreSQL,您需要进行以下配置: 1. 在Cobar的配置文件中,添加一个新的数据源配置,指向您的PostgreSQL数据库。例如: ``` <dataNode name="pgdb"> <dataHost host="localhost" name="postgresql" maxCon="1000" minCon="10" balance="random" writeType="jdbc"> <dataSource class="com.alibaba.druid.pool.DruidDataSource" type="postgresql"> <property name="url">jdbc:postgresql://localhost:5432/mydb</property> <property name="username">postgres</property> <property name="password">mypassword</property> </dataSource> </dataHost> <database name="mydb" /> </dataNode> ``` 2. 确保您的Cobar和PostgreSQL数据库是在同一台服务器上运行,并且PostgreSQL数据库已经启动。 3. 您需要安装PostgreSQL JDBC驱动程序,以便Cobar可以使用它来连接到PostgreSQL数据库。 4. 在Cobar的规则文件中,将您希望在PostgreSQL数据库上执行的SQL语句路由到您在第1步中定义的数据源上。例如: ``` <rule name="pgdb_rule"> <columns>id</columns> <algorithm>mod</algorithm> <tableRule name="pgdb_table_0" rule="pgdb_rule"> <rule> <value>0</value> <dataNode>pgdb</dataNode> </rule> </tableRule> </rule> ``` 在这个例子中,如果SQL语句中的"id"列的值被模数为0,则它会被路由到名为"pgdb"的数据源上。 5. 最后,您需要在Cobar的启动脚本中,添加PostgreSQL JDBC驱动程序的路径到CLASSPATH环境变量中。例如: ``` export CLASSPATH=$CLASSPATH:/path/to/postgresql-jdbc-driver.jar ``` 希望这些步骤可以帮助您成功地配置Cobar连接到PostgreSQL数据库。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值