读写分离案例
背景:
面对日益增加的系统访问量,数据库的吞吐量面临着巨大瓶颈。对于同一时刻有大量并发读操作和较少写操作类型的应用系统来说,将数据库拆分为主库和从库,主库负责处理事务性的增删改操作,从库负责处理查询操作,能够有效的避免由数据更新导致的行锁,使得整个系统的查询性能得到极大的改善。
Sharding-JDBC介绍
Sharding-JDBC定位为轻量级Java框架,在java的JDBC层提供的额外服务。它使用客户端直连数据库,以jar包形式提供服务,无需额外部署和依赖,可理解为增强版的JDBC驱动,完全兼容JDBC和各种ORM框架
使用Sharding-JDBC可以在程序中轻松实现数据库读写分离。
* 适用于任何基于JDBC和ORM框架,如:JPA、Hibernate,Mybatis,Spring JDBC Template或直接使用JDBC。
* 支持任何第三方的数据库连接池,如:DBCP、C3P0、BoneCp、Druid、HikariCP等
* 支持任意实现JDBC规范的数据库。目前支持MySQL,Oracle,SQLServer、PostgreSQL以及任何遵循SQL92标准的数据库。
使用sharding-JDBC实现读写分离步骤:
1、导入Maven坐标
<dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>4.0.0-RC1</version>
</dependency>
2、在配置文件中配置读写分离规则
3、在配置文件中配置允许bean定义覆盖配置项
server: port: 8080 spring: shardingsphere: datasource: names: master,slave # 主数据源 master: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://192.168.138.100:3306/rw?characterEncoding=utf-8 username: root password: root # 从数据源 slave: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://192.168.138.101:3306/rw?characterEncoding=utf-8 username: root password: root masterslave: # 读写分离配置 load-balance-algorithm-type: round_robin # 最终的数据源名称 name: dataSource # 主库数据源名称 master-data-source-name: master # 从库数据源名称列表,多个逗号分隔 slave-data-source-names: slave props: sql: show: true #开启SQL显示,默认false main: allow-bean-definition-overriding: true mybatis-plus: configuration: #在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射 map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: id-type: ASSIGN_ID
断点测试增删改和查分别访问的是主库master还是从库slave
package com.itheima.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.itheima.entity.User; import com.itheima.service.UserService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.sql.DataSource; import java.util.List; @RestController @RequestMapping("/user") @Slf4j public class UserController { @Autowired private DataSource dataSource; @Autowired private UserService userService; @PostMapping public User save(User user){ userService.save(user); return user; } @DeleteMapping("/{id}") public void delete(@PathVariable Long id){ userService.removeById(id); } @PutMapping public User update(User user){ userService.updateById(user); return user; } @GetMapping("/{id}") public User getById(@PathVariable Long id){ User user = userService.getById(id); return user; } @GetMapping public List<User> list(User user){ LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(user.getId()!=null, User::getId, user.getId()); queryWrapper.eq(user.getName()!=null, User::getName, user.getName()); List<User> list = userService.list(queryWrapper); return list; } }
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)