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

配置中心

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

转载于:https://www.cnblogs.com/wingsless/p/11432501.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 引入依赖 在 `pom.xml` 中引入 `shardingsphere-jdbc-core` 依赖: ```xml <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>shardingsphere-jdbc-core</artifactId> <version>5.0.0-alpha</version> </dependency> ``` 2. 配置数据源 在 `application.yml` 中配置数据源: ```yaml spring: datasource: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8 username: root password: root sharding: jdbc: # 数据源列表 datasource: ds0: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/test0?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8 username: root password: root ds1: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8 username: root password: root # 分片规则配置 sharding: default-data-source: ds0 # 默认数据源 tables: user: actual-data-nodes: ds${0..1}.user_${0..1} # 实际数据节点 database-strategy: inline: sharding-column: id # 分片键 algorithm-expression: ds${id % 2} # 分库算法 table-strategy: inline: sharding-column: id # 分片键 algorithm-expression: user_${id % 2} # 分表算法 ``` 3. 编写代码 ```java @Service public class UserServiceImpl implements UserService { @Autowired private JdbcTemplate jdbcTemplate; @Override public void addUser(User user) { String sql = "INSERT INTO user (id, name) VALUES (?, ?)"; Object[] params = new Object[] { user.getId(), user.getName() }; int count = jdbcTemplate.update(sql, params); System.out.println("插入 " + count + " 条记录"); } @Override public List<User> getUsers() { String sql = "SELECT * FROM user"; return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class)); } } ``` 4. 测试 编写测试方法: ```java @SpringBootTest class UserServiceImplTest { @Autowired private UserService userService; @Test void addUser() { User user = new User(); user.setId(1L); user.setName("张三"); userService.addUser(user); } @Test void getUsers() { List<User> users = userService.getUsers(); System.out.println(users); } } ``` 执行测试方法,查看控制台输出和数据库表中的数据,验证分库分表是否成功实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值