芋道 Spring Boot 分库分表入门

点击上方“芋道源码”,选择“设为星标

做积极的人,而不是积极废人!

源码精品专栏

 

摘要: 原创出处 http://www.iocoder.cn/Spring-Boot/sharding-datasource/ 「芋道源码」欢迎转载,保留摘要,谢谢!

  • 1. 概述

  • 2. 分库分表

  • mybatis 配置内容

  • 3. 读写分离

  • 666. 彩蛋


1. 概述

因为市面上已经非常不错的分库分表的资料,所以艿艿就不在尴尬的瞎哔哔一些内容。推荐阅读两个资料:

  • 《Apache ShardingSphere 官方文档》

    ShardingSphere 是目前最好用的数据库中间件之一,很多时候,我们使用它来实现分库分表,或者读写分离。

    当然,它不仅仅能够提供上述两个功能,也能提供分布式事务、数据库治理。

  • 《为什么几乎所有的开源数据库中间件都是国内公司开源的?并且几乎都停止了更新?》

    这个是知乎上的一个讨论,适合我们来吃瓜,看看各路大神对这块的想法。

    生命不息,吃瓜不止。

目前,国内使用比较多的分库分表的中间件,主要有:

  • Apache ShardingSphere

  • Mycat

个人比较推荐使用 ShardingSphere ,主要有几个原因:

  • 在京东、当当等大型互联网公司落地使用,并且已经提供的有 100+ 企业的成功案例。

    关于 100+ 案例,并不是指的 100+ 公司采用,而是登记给 ShardingSphere 团队的公司数。实际肯定远超这个数字,毕竟大多数团队采用的话,都没去主动登记。

  • 社区强大,已经进入 Apache 孵化。并且有京东全职的开发团队,也有总共 88+ contributors 。

  • 功能完善,不仅仅提供分库分表、读写分离,也提供分布式事务、数据库治理等功能。

  • 代码质量非常高。项目负责人 张亮 简直是个代码质量狂魔!

    之前学习 Sharding-JDBC 时,尝试写过一套源码解析文章。代码简直易读到爆炸。

    亮哥自己也在某次采访中,提到如下内容:以工匠精神去雕琢细节。开放出去的源代码会在一定的范围内引起共鸣。一个值得研读开源项目,其代码必须经过雕琢,让其规范、一致、优雅、易懂,尽量将细节做到极致。通过代码质量给予使用者信心。

    所以呢,非常推荐胖友尝试去阅读下 ShardingSphere 。

可能会有胖友会提到 Mycat ,为什么不推荐使用它????? 默默不评价。如果在选型中考虑 Mycat 的话,推荐可以看看 dble 项目。

本文,我们会使用 ShardingSphere 的子项目 Sharding-JDBC 完成分库分表和读写分离的功能,会提供两个示例。如果胖友对 Sharding-JDBC 不是很了解,推荐先去阅读下 《Apache ShardingSphere 官方文档 —— 概览》 ,很简短。

2. 分库分表

示例代码对应仓库:lab-18-sharding-datasource-01 。

本小节,我们会使用 Sharding-JDBC 实现分库分表的功能。我们会将 orders 订单表,拆分到 2 个库,每个库 4 个订单表,一共 8 个表。库表的情况如下:

lab18_orders_0 库
  ├── orders_0
  └── orders_2
  └── orders_4
  └── orders_6
lab18_orders_1 库
  ├── orders_1
  └── orders_3
  └── orders_5
  └── orders_7
  • 偶数后缀的表,在 lab18_orders_0 库下。

  • 奇数后缀的表,在 lab18_orders_1 库下。

我们使用订单表上的 user_id 用户编号,进行分库分表的规则:

  • 首先,按照 index = user_id % 2 计算,将记录路由到 lab18_orders_${index} 库。

  • 然后,按照 index = user_id % 8 计算,将记录路由到 orders_${index} 表。

举个例子:

用户编号
1 lab18_orders_1 orders_1
2 lab18_orders_0 orders_2
3 lab18_orders_1 orders_3
4 lab18_orders_0 orders_4
5 lab18_orders_1 orders_5
6 lab18_orders_0 orders_6
7 lab18_orders_1 orders_7
8 lab18_orders_0 orders_8

考虑到部分表不需要分库分表,例如说 order_config 订单配置表,所以我们会配置路由到 lab18_orders_0 库下。

具体 ordersorder_config 两个表的创建语句,我们在 TODO 提供。

因为本文重心在于提供示例。胖友可以碰到不理解的地方,看看如下文档:

  • 《ShardingSphere > 概念 & 功能 > 数据分片》

  • 《ShardingSphere > 用户手册 > Sharding-JDBC > 使用手册 > 数据分片》

  • 《ShardingSphere > 用户手册 > Sharding-JDBC > 配置手册》

2.1 引入依赖

pom.xml 文件中,引入相关依赖。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>lab-18-sharding-datasource-01</artifactId>

    <dependencies>
        <!-- 实现对数据库连接池的自动化配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency> <!-- 本示例,我们使用 MySQL -->
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.48</version>
        </dependency>

        <!-- 实现对 MyBatis 的自动化配置 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

        <!-- 实现对 Sharding-JDBC 的自动化配置 -->
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.0.0-RC2</version>
        </dependency>

        <!-- 保证 Spring AOP 相关的依赖包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>

        <!-- 方便等会写单元测试 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>

具体每个依赖的作用,胖友自己认真看下艿艿添加的所有注释噢。

2.2 Application

创建 Application.java 类,代码如下:

// Application.java

@SpringBootApplication
@MapperScan(basePackages = "cn.iocoder.springboot.lab18.shardingdatasource.mapper")
public class Application {
}
  • 添加 @M

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Security 是一个强大且灵活的认证和授权框架,用于保护 Spring Boot 应用程序的安全性。下面是一个简单的入门指南,帮助你开始使用 Spring Security: 1. 添加 Spring Security 依赖:在你的项目的 `pom.xml` 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 创建一个 Spring Security 配置类:创建一个类并注解为 `@Configuration`,这个类将用于配置 Spring Security。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/public").permitAll() // 允许访问公共资源 .anyRequest().authenticated() // 其他请求需要认证 .and() .formLogin() // 启用表单登录 .and() .logout() // 启用注销功能 .and() .csrf().disable(); // 禁用 CSRF(跨站请求伪造)保护 } } ``` 在上述示例中,我们配置了一些基本的安全规则。可以自定义更多的规则来满足你的需求,比如配置自定义登录页面、添加用户角色等。 3. 添加用户认证:在上述配置类中,可以添加一个内存用户存储来进行简单的用户认证。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("admin") .password("{noop}password") // 使用 {noop} 前缀表示不加密密码 .roles("ADMIN"); } } ``` 在上述示例中,我们创建了一个用户名为 "admin"、密码为 "password"、角色为 "ADMIN" 的用户。 这只是 Spring Security 的入门指南,你可以进一步学习如何使用数据库存储用户信息、配置角色权限等。希望这些信息对你有所帮助!如果你有更多问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值