1. 数据源 DataSource

CobarClient 的 DataSource 分为三层

ICobarDataSourceService: 封装了多个 DataSourceDescriptor, 以及 HA 包装的 IHADataSourceCreator

DataSourceDescriptor: 封装了 identity, 主 targetDataSource 以及 备 standbyDataSource

DataSource: 实际数据库 DataSource

 

2. 路由 Router

CobarClient 的路由规则依赖于 iBatis, 它主要由 SqlMap 的 id 和路由字段共同决定最终的datasource, 例如

<rules>
  <rule>
    <namespace>com.alibaba.cobar.client.entities.Offer</namespace>
    <shardingExpression>mod.apply(memberId)==1</shardingExpression>
    <shards>partition1</shards>
  </rule>
  <rule>
    <namespace>com.alibaba.cobar.client.entities.Offer</namespace>
    <shardingExpression>mod.apply(memberId)==2</shardingExpression>
    <shards>partition2</shards>
  </rule>
</rules>

 

表示对于 SqlMap 中的 namespace 为 Offer 的 SQL, 将会根据参数的 memberId 字段来获取 shard, shard 即为 DataSourceDescriptor 的 identity

 

3. HA 

CobarClient 的 HA 分为主动及被动方式, 主动方式默认开启, 即开一个线程定期探测主数据源是否可用, 如果不可用, 则切换为备数据源, 即切换 DataSourceDescriptor 的主备数据源

被动方式默认关闭, 被动方式是在每次执行查询前检查, 这样会降低效率, 如果检查失败, 则会切换主备.

 

4. 数据聚合

数据聚合主要还是依赖客户端查询完以后的人工聚合, 例如

select count(*), companyId from offer group by companyId

 将会对多个数据源执行 SQL, 并最终返回多个结果集, 最后依赖用户手动聚合 companyId

posted on 2014-10-22 12:23  ZimZz  阅读(3605)  评论(0编辑  收藏  举报