分库分表(3) ---SpringBoot + ShardingSphere 实现读写分离

分库分表(3)---ShardingSphere实现读写分离

有关ShardingSphere概念前面写了两篇博客:

1、分库分表(1) --- 理论

2、 分库分表(2) --- ShardingSphere(理论)

下面就这个项目做个整体简单介绍,并在文章最下方附上项目Github地址

一、项目概述

1、技术架构

项目总体技术选型

SpringBoot2.0.6 + shardingsphere4.0.0-RC1 + Maven3.5.4  + MySQL + lombok(插件)

2、项目说明

场景 如果实际项目中Mysql是 Master-Slave (主从)部署的,那么数据保存到Master库,Master库数据同步数据到Slave库,数据读取到Slave库,

这样可以减缓数据库的压力。

3、数据库设计

我们这个项目中Mysql服务器并没有实现主从部署,而是同一个服务器建立两个库,一个当做Master库,一个当做Slave库。所以这里是不能实现的功能就是Master库

新增数据主动同步到Slave库。这样也更有利于我们测试看效果。

Master库

Slave库

从两幅图中可以看出,我这里在同一个服务器建两个数据库来模拟主从数据库。为了方便看测试效果,这里主从数据库中的数据是不一样的

二、核心代码

说明 完整的代码会放到GitHub上,这里只放一些核心代码。

1、pom.xml

    <properties>
        <java.version>1.8</java.version>
        <mybatis-spring-boot>2.0.1</mybatis-spring-boot>
        <druid>1.1.16</druid>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot}</version>
        </dependency>
        <!--mybatis驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--druid数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid}</version>
        </dependency>
        <!--shardingsphere最新版本-->
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.0.0-RC1</version>
        </dependency>
        <!--lombok实体工具-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

2、application.properties

server.port=8088
#指定mybatis信息
mybatis.config-location=classpath:mybatis-config.xml

spring.shardingsphere.datasource.names=master,slave0
# 数据源 主库
spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/master?characterEncoding=utf-8
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=123456
# 数据源 从库
spring.shardingsphere.datasource.slave0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave0.url=jdbc:mysql://localhost:3306/slave?characterEncoding=utf-8
spring.shardingsphere.datasource.slave0.username=root
spring.shardingsphere.datasource.slave0.password=123456

# 读写分离
spring.shardingsphere.masterslave.load-balance-algorithm-type=round_robin
spring.shardingsphere.masterslave.name=ms
spring.shardingsphere.masterslave.master-data-source-name=master
spring.shardingsphere.masterslave.slave-data-source-names=slave0
#打印sql
spring.shardingsphere.props.sql.show=true

Sharding-JDBC可以通过JavaYAMLSpring命名空间Spring Boot Starter四种方式配置,开发者可根据场景选择适合的配置方式。具体可以看官网。

3、UserController

@RestController
public class UserController {

    @Autowired
    private UserService userService;
    /**
     * @Description: 保存用户
     */
    @PostMapping("save-user")
    public Object saveUser() {
        return userService.saveOne(new User("小小", "女", 3));
    }
    /**
     * @Description: 获取用户列表
     */
    @GetMapping("list-user")
    public Object listUser() {
        return userService.list();
    }
}

三、测试验证

1、读数据

我们可以发现读取的数据是Slave库的数据。我们再来看控制台打印的SQL。可以看到读操作是Slave库。

2、写数据

请求

localhost:8088/save-user?name=小小&sex=女&age=3

查看Mater数据库

发现Master数据库已经多了一条数据了,再看控制台打印的SQL。

这个时候如果去看Slave库的话这条新增的数据是没有的,因为没有同步过去。

Github地址https://github.com/yudiandemingzi/spring-boot-sharding-sphere

参考

1、ShardingSphere中文文档

2、ShardingSphere官网

3、Shardingsphere Github库

 我相信,无论今后的道路多么坎坷,只要抓住今天,迟早会在奋斗中尝到人生的甘甜。抓住人生中的一分一秒,胜过虚度中的一月一年!(18)

原文地址:https://www.cnblogs.com/qdhxhz/p/11656205.html

时间: 2024-08-30 00:22:26

分库分表(3) ---SpringBoot + ShardingSphere 实现读写分离的相关文章

分库分表(6)--- SpringBoot+ShardingSphere实现分表+ 读写分离

分库分表(6)--- ShardingSphere实现分表+ 读写分离 有关分库分表前面写了五篇博客: 1.分库分表(1) --- 理论 2.分库分表(2) --- ShardingSphere(理论) 3.分库分表(3) ---SpringBoot + ShardingSphere实现读写分离 4.分库分表(4) ---SpringBoot + ShardingSphere 实现分表 5.分库分表(5) ---SpringBoot + ShardingSphere 实现分库分表 这篇博客通过S

分库分表(4) ---SpringBoot + ShardingSphere 实现分表

分库分表(4)--- ShardingSphere实现分表 有关分库分表前面写了三篇博客: 1.分库分表(1) --- 理论 2.分库分表(2) --- ShardingSphere(理论) 3.分库分表(3) ---SpringBoot + ShardingSphere实现读写分离 这篇博客通过ShardingSphere实现分表不分库,并在文章最下方附上项目Github地址. 一.项目概述 1.技术架构 项目总体技术选型 SpringBoot2.0.6 + shardingsphere4.0

分库分表(5) ---SpringBoot + ShardingSphere 实现分库分表

分库分表(5)--- ShardingSphere实现分库分表 有关分库分表前面写了四篇博客: 1.分库分表(1) --- 理论 2.分库分表(2) --- ShardingSphere(理论) 3.分库分表(3) ---SpringBoot + ShardingSphere实现读写分离 4.分库分表(4) ---SpringBoot + ShardingSphere 实现分表 这篇博客通过ShardingSphere实现分库分表,并在文章最下方附上项目Github地址. 一.项目概述 1.技术

SpringBoot使用Sharding-JDBC读写分离

摘要: 本文介绍SpringBoot使用当当Sharding-JDBC进行读写分离. 1.有关Sharding-JDBC 本文还是基于当当网Sharding-Jdbc的依赖,与上一篇使用Sharding-Jdbc进行分库分表依赖一致,并且本文大致内容与上一篇文章相似,建议先查看我的另一篇在查看这篇会简单许多,传送门<SpringBoot使用Sharding-JDBC分库分表>. 本文介绍SpringBoot使用当当Sharding-JDBC进行读写分离. 作为一个开发者,有一个学习的氛围跟一个

SpringBoot+Mybatis+Sharding-JDBC实现分库分表

项目里面一直用Sharding-JDBC,今天整理一下,就当温故而知新了,也是稳固而知新了. 一.整体介绍 项目采用的框架是SpringBoot+Mybatis+Sharding-JDBC,采用的是properties的形式: 分为两个数据库sharding_0,sharding_1.每个库三个表,t_user_00,t_user_01,t_user_02: 分库策略:age % 2 = 0的数据存储到sharding_0 ,为1的数据存储到sharding_1: 分表策略:user_id %

SpringBoot使用sharding-jdbc分库分表

一.前言 一般来说,随着业务的发展数据库的数据量会越来越多,当单表数据超过上千万时执行一些查询sql语句就会遇到性能问题.一开始可以用主从复制读写分离来减轻db压力,但是后面还是要用分库分表把数据进行水平拆分和垂直拆分. 实现分库分表目前我知道的方式有两种,第一种是使用mycat中间件实现,第二种是使用sharding-jdbc实现.相比较而言,sharding-jdbc引入一个jar包即可使用更轻量级一些,它们之间的优缺点这里也不做比较,有兴趣的可以自己搜索相关资料. 不清楚分库分表原理的可以

分库分表(2) --- ShardingSphere(理论)

ShardingSphere---理论 ShardingSphere在中小企业需要分库分表的时候用的会比较多,因为它维护成本低,不需要额外增派人手;而且目前社区也还一直在开发和维护,还算是比较活跃. 但是中大型公司一般会选择选用 Mycat 这类 proxy 层方案,因为可能大公司系统和项目非常多,团队很大,人员充足,那么最好是专门弄个人来研究和维护 Mycat, 然后大量项目直接透明使用即可. 一.ShardingSphere概念 1.概念 ShardingSphere是一套开源的分布式数据库

Mycat读写分离和分库分表配置

Mycat是一个开源的分布式数据库系统,不同于oracle和mysql,Mycat并没有存储引擎,但是Mycat实现了mysql协议,前段用户可以把它当做一个Proxy.其核心功能是分表分库,即将一个大表水平分割为N个小表,存储在后端mysql存储引擎里面.最新版本的Mycat不仅支持mysql,还可以支持MS SqlServer,Oracle,DB2等关系型数据库,而且还支持MongoDB这种NoSQL.Mycat对调用者屏蔽了后端存储具体实现. Mycat的原理是先拦截用户的SQL语句并做分

DB层面上的设计 分库分表 读写分离 集群化 负载均衡

第1章  引言 随着互联网应用的广泛普及,海量数据的存储和访问成为了系统设计的瓶颈问题.对于一个大型的 互联网应用,每天几十亿的PV无疑对数据库造成了相当高的负载.对于系统的稳定性和扩展性造成了极大的问题.通过数据切分来提高网站性能,横向扩展数据层 已经成为架构研发人员首选的方式.水平切分数据库,可以降低单台机器的负载,同时最大限度的降低了了宕机造成的损失.通过负载均衡策略,有效的降低了单台 机器的访问负载,降低了宕机的可能性:通过集群方案,解决了数据库宕机带来的单点数据库不能访问的问题:通过读