利用ShardingSphere-JDBC实现分库分表--配置中心的实现

在之前的文章中我详细描述了如何利用ShardingSphere-JDBC进行分库分表,同时也实现了简单的精确分库算法接口,详情见下面的链接:

利用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上看看上传上去的配置信息:

利用ShardingSphere-JDBC实现分库分表--配置中心的实现

至此我们就完成了配置中心的配置工作。在以后的篇幅中,我还会提及数据脱敏等其他内容,要是有时间的话,我还希望能够深入源码内部了解一下这个组件。