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