怎么用sharding jdbc实现读写分离?
时间: 2023-09-01 07:11:09 浏览: 100
基于yml 配置方式 ,实现springBoot+sharding-jdbc+mybatis-plus 实现分库分表,读写分离,以及全局表,子表的配置
Sharding-JDBC 是一个基于 JDBC 的分库分表中间件,可以将 SQL 语句按照指定的规则路由到对应的数据库节点上。在 Sharding-JDBC 中实现读写分离的方法如下:
1. 配置数据源
在配置文件中配置数据源,包括主库和从库的信息。主库用于写操作,从库用于读操作。
```
spring.shardingsphere.datasource.names=master,slave
spring.shardingsphere.datasource.master.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.jdbc-url=jdbc:mysql://localhost:3306/master?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=root
spring.shardingsphere.datasource.slave.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave.jdbc-url=jdbc:mysql://localhost:3307/slave?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
spring.shardingsphere.datasource.slave.username=root
spring.shardingsphere.datasource.slave.password=root
```
2. 配置读写分离规则
在配置文件中配置读写分离规则,指定哪些 SQL 语句需要路由到从库。可以根据 SQL 类型、表名、字段等规则进行配置。
```
spring.shardingsphere.rules.master-slave.data-sources.names=master,slave
spring.shardingsphere.rules.master-slave.master-data-source-name=master
spring.shardingsphere.rules.master-slave.slave-data-source-names=slave
spring.shardingsphere.rules.master-slave.load-balance-algorithm-type=round_robin
spring.shardingsphere.rules.master-slave.auto-determine-data-source-type=true
spring.shardingsphere.rules.master-slave.master-slave-rules[0].data-source-name=ms
spring.shardingsphere.rules.master-slave.master-slave-rules[0].master-data-source-name=master
spring.shardingsphere.rules.master-slave.master-slave-rules[0].slave-data-source-names=slave
spring.shardingsphere.rules.master-slave.master-slave-rules[0].slave-data-source-selector-type=ROUND_ROBIN
spring.shardingsphere.rules.master-slave.master-slave-rules[0].slave-data-source-selector-props=test=ds_slave_0,ds_slave_1
spring.shardingsphere.rules.master-slave.master-slave-rules[0].load-balance-algorithm-type=ROUND_ROBIN
spring.shardingsphere.rules.master-slave.master-slave-rules[0].name=ms
spring.shardingsphere.rules.master-slave.master-slave-rules[0].type=MASTER_SLAVE
spring.shardingsphere.rules.master-slave.master-slave-rules[0].slave-data-source-names=slave
spring.shardingsphere.rules.master-slave.master-slave-rules[0].load-balance-algorithm-type=ROUND_ROBIN
spring.shardingsphere.rules.master-slave.master-slave-rules[0].slave-data-source-selector-type=ROUND_ROBIN
spring.shardingsphere.rules.master-slave.master-slave-rules[0].slave-data-source-selector-props=test=ds_slave_0,ds_slave_1
spring.shardingsphere.rules.master-slave.master-slave-rules[0].name=ms
spring.shardingsphere.rules.master-slave.master-slave-rules[0].type=MASTER_SLAVE
```
3. 配置数据源代理
在代码中使用数据源代理,将 SQL 语句路由到对应的数据库节点上。可以使用 ShardingDataSourceFactory 创建数据源,也可以使用 Spring Boot 集成的 ShardingSphereDataSource。
```
@Bean
public DataSource dataSource() throws SQLException {
return ShardingDataSourceFactory.createDataSource(dataSourceMap(), shardingRuleConfig(), new Properties());
}
```
4. 测试读写分离
测试时,可以使用 JdbcTemplate 执行 SQL 语句,观察 SQL 是否被正确路由到主库或从库。例如:
```
@Autowired
private JdbcTemplate jdbcTemplate;
public void testReadWrite() {
jdbcTemplate.execute("insert into user(name, age) values('test', 18)");
List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from user where age > 10");
System.out.println(list);
}
```
以上是 Sharding-JDBC 实现读写分离的基本步骤。在实际使用中,还需要根据具体的业务需求进行配置和调优。
阅读全文