在之前的文章中我詳細描述了如何利用ShardingSphere-JDBC進行分庫分表,同時也實現了簡單的精確分庫算法接口,詳情見下面的鏈接:
但是觀察一下配置文件,我現在只有兩張表的情況下就已經用了60行來做配置,如果說我在一個真實的系統中,那么配置文件的規模將是非常可觀的,這個時候配置中心的作用就很重要了。ShardingSphere支持主流的zookeeper和etcd等服務發現組件,作為最常用的,我用zookeeper來實現。
下面是關於zookeeper部分的配置:
# 名字隨便起一個
spring.shardingsphere.orchestration.name=spring_boot_ds_sharding
# 覆蓋配置中心的配置,以本地為准
spring.shardingsphere.orchestration.overwrite=true
spring.shardingsphere.orchestration.registry.type=zookeeper
# 名字隨便起一個,這是我們這個集群的名稱,其他的集群可以用這個也可以用自己單獨的,作為資源隔離
spring.shardingsphere.orchestration.registry.namespace=shardingsphere
spring.shardingsphere.orchestration.registry.server-lists=127.0.0.1:2181
服務啟動以后就會自動的將這些配置上傳到配置好的配置中心去,未來只需要修改配置中心就可以。
需要注意一點,官方文檔中沒有講明白gradle或者maven的配置,如果按照文檔上講得直接把相關的starter配置進去,啟動會報錯,這是因為jar包沖突導致的,我這里給出gradle的配置,maven的可以參考修改:
compile('org.apache.shardingsphere:sharding-jdbc-orchestration-spring-boot-starter:4.0.0-RC2')
{
exclude group: 'org.apache.curator', module: 'curator-framework'
}
compile('org.apache.shardingsphere:sharding-orchestration-reg-zookeeper-curator:4.0.0-RC2')
{
exclude group: 'org.apache.curator', module: 'curator-framework'
exclude group: 'org.apache.curator', module: 'curator-recipes'
exclude group: 'org.apache.curator', module: 'curator-client'
}
compile group: 'org.apache.curator', name: 'curator-framework', version: '2.10.0'
compile group: 'org.apache.curator', name: 'curator-recipes', version: '2.10.0'
我們可以在zkCli上看看上傳上去的配置信息:
至此我們就完成了配置中心的配置工作。在以后的篇幅中,我還會提及數據脫敏等其他內容,要是有時間的話,我還希望能夠深入源碼內部了解一下這個組件。