首页 > 其他分享 >分库分表之第二篇

分库分表之第二篇

时间:2022-10-14 11:31:31浏览次数:54  
标签:分库 spring id sharding jdbc shardingsphere 分表 第二篇 order


分库分表之第二篇

2. Sharding-JDBC快速入门

2.1需求说明

使用Sharding-JDBC完成对订单表的水平分表,通过快速入门程序的开发,快速体验Sharding-JDBC的使用。人工创建两张表,t_order_1和t_order_2,这张表是订单表替换后的表,通过Shading-JDBC向订单表插入数据,按照一定的分片规则,主键为偶数的尽入t_order_1,另一部分数据进入t_order_2,通过Shading-Jdbc查询数据,根据SQL语句的内容从t_order_1或order_2查询数据。

2.2. 环境建设

2.2.1环境说明

操作系统:Win10数据库:MySQL-5.7.25 JDK:64位jdk1.8.0_201应用框架:spring-boot-2.1.3.RELEASE,Mybatis3.5.0 Sharding-JDBC:sharding-jdbc-spring-boot-starter-4.0 .0-RC1

2.2.2创建数据库

创建订单表

CREATE DATABASE`order_db`字符集'UTF8'COLLATE'utf8_general_ci'; ```在order_db中创建t_order_1,t_order_2表如果存在java DROP TABLE t_order_1; CREATE TABLE`t_order_1`(`order_id` BIGINT(20)非空注释'订单ID',`price`十进制(10,2)非空注释'订单价格',`user_id` BIGINT(20)非空注释“下一个单用户id”,“状态” varchar(50)字符集utf8集合utf8_general_ci NOT NULL COMMENT“订单状态”,主键(`order_id`)使用BTREE)引擎= InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = 如果存在表t_order_2; CREATE TABLE`t_order_2`(`order_id` BIGINT(20)非空注释'订单ID',`price`十进制(10,2)非空注释'订单价格',`user_id` BIGINT(20)非空注释'下一个单用户id',`status` varchar(50)字符集utf8集合utf8_general_ci NOT NULL COMMENT'订单状态',主键(`order_id`)使用BTREE 
)ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT =动态;

2.2.3约会maven依赖

sharding-jdbc和SpringBoot整合的Jar包:

<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding‐jdbc‐spring‐boot‐starter</artifactId>
<version>4.0.0‐RC1</version>
</dependency>

2.3 编写程序

2.3.1 分片规则配置

分片规则配置是sharding-jdbc进行分库分表操作的重要依据,配置内容包括 :数据源、主键生成策略等。
在application.properties中配置

server.port=56081
spring.application.name = sharding‐jdbc‐simple‐demo
server.servlet.context‐path = /sharding‐jdbc‐simple‐demo spring.http.encoding.enabled = true spring.http.encoding.charset = UTF‐8 spring.http.encoding.force = true
spring.main.allow‐bean‐definition‐overriding = true
mybatis.configuration.map‐underscore‐to‐camel‐case = true # 以下是分片规则配置
# 定义数据源
spring.shardingsphere.datasource.names = m1
spring.shardingsphere.datasource.m1.type = com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.m1.driver‐class‐name = com.mysql.jdbc.Driver spring.shardingsphere.datasource.m1.url = jdbc:mysql://localhost:3306/order_db?useUnicode=true spring.shardingsphere.datasource.m1.username = root spring.shardingsphere.datasource.m1.password = root
# 指定t_order表的数据分布情况,配置数据节点 spring.shardingsphere.sharding.tables.t_order.actual‐data‐nodes = m1.t_order_$‐>{1..2}
# 指定t_order表的主键生成策略为SNOWFLAKE spring.shardingsphere.sharding.tables.t_order.key‐generator.column=order_id spring.shardingsphere.sharding.tables.t_order.key‐generator.type=SNOWFLAKE
# 指定t_order表的分片策略,分片策略包括分片键和分片算法 spring.shardingsphere.sharding.tables.t_order.table‐strategy.inline.sharding‐column = order_id spring.shardingsphere.sharding.tables.t_order.table‐strategy.inline.algorithm‐expression = t_order_$‐>{order_id % 2 + 1}
# 打开sql输出日志 spring.shardingsphere.props.sql.show = true
swagger.enable = true
logging.level.root = info logging.level.org.springframework.web = info logging.level.com.itheima.dbsharding = debug logging.level.druid.sql =
  1. 首先定义数据源m1,并对m1进行实际的参数配置
  2. 指定t_order表的数据分布情况,它分布在m1.t_order_1、m1.t_order_2
  3. 指定t_order表的主键生成策略为SNOWFLAKE,SNOWFLAKE是一种分布式自增算法,保证id全局唯一
  4. 定义t_order分片策略,order_id为偶数的数据落在t_order_1,为奇数的落在t_order_2,分表策略的表达式为t_order_$->{order_id % 2 + 1}

2.3.2 数据操作

@Mapper
@Component
public interface OrderDao {
/**
* 新增订单
* @param price 订单价格 * @param userId 用户id * @param status 订单状态 * @return
*/
@Insert("insert into t_order(price,user_id,status) value(#{price},#{userId},#{status})")
int insertOrder(@Param("price") BigDecimal price, @Param("userId")Long userId, @Param("status")String status);
/**
* 根据id列表查询多个订单
* @param orderIds 订单id列表 * @return
*/
@Select({"<script>" + "select " +
"*"+
" from t_order t" +
" where t.order_id in " +
"<foreach collection='orderIds' item='id' open='(' separator=',' close=')'>" + " #{id} " +
"</foreach>"+
"</script>"})
List<Map> selectOrderbyIds(@Param("orderIds")List<Long> orderIds);
}

2.3.3 测试

编写单元测试 :

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ShardingJdbcSimpleDemoBootstrap.class}) public class OrderDaoTest {
@Autowired
private OrderDao orderDao;
@Test
public void testInsertOrder(){
for (int i = 0 ; i<10; i++){
orderDao.insertOrder(new BigDecimal((i+1)*5),1L,"WAIT_PAY");
}
}
@Test
public void testSelectOrderbyIds(){
List<Long> ids = new ArrayList<>(); ids.add(373771636085620736L); ids.add(373771635804602369L);
List<Map> maps = orderDao.selectOrderbyIds(ids); System.out.println(maps);
}
}

执行testInsertOrder:

分库分表之第二篇_spring


通过日志可以发现order_id为奇数的被插入到t_order_2表,为偶数的被插入到t_order_1表,达到预期目标。

执行testSelectOrderbyIds:

分库分表之第二篇_spring_02


通过日志可以发现,根据传入的order_id的奇偶不同,分片-JDBC分别去不同的表检索数据,达到预期目标。

2.4. 流程分析

通过日志分析,Sharding-JDBC在拿到用户要执行的sql之后干了那些事儿 :
(1)解析sql,获取片键值,在本例中是order_id
(2)Sharding-JDBC通过规则配置t_order_$->{order_id% 2 + 1},知道类当order_id为偶数时,应该往t_order_1表插数据,为奇数时,往t_order_2插数据。
(3)于是Sharding-JDBC根据order_id的值改写sql语句,改写后的SQL语句是真实所要执行的SQL语句。
(4)执行改写后的真实sql语句
(5)将所有真正执行sql的结果进行汇总合并,返回。

2.5 其他集成方式

Sharding-JDBC不仅可以与Spring boot良好集成,它还支持其他配置方式,共支持以下四种集成方式。
Spring Boot Yaml配置
定义application.yml,内容如下 :

server:
port: 56081
servlet:
context‐path: /sharding‐jdbc‐simple‐demo spring:
application:
name: sharding‐jdbc‐simple‐demo
http:
encoding:
enabled: true charset: utf‐8 force: true
main:
allow‐bean‐definition‐overriding: true
shardingsphere:
datasource:
names: m1
m1:
type: com.alibaba.druid.pool.DruidDataSource driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/order_db?useUnicode=true username: root
password: mysql
sharding:
tables:
t_order:
actualDataNodes: m1.t_order_$‐>{1..2} tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_$‐>{order_id % 2 + 1}
keyGenerator:
type: SNOWFLAKE
column: order_id
props: sql:
show: true
mybatis:
configuration: map‐underscore‐to‐camel‐case: true
swagger:
enable: true
logging:
level:
root: info
org.springframework.web: info
com.itheima.dbsharding: debug
druid.sql:

如果使用application.yml则需要屏蔽原来的application.properties文件。
Java配置
添加配置类 :

@Configuration
public class ShardingJdbcConfig {
// 定义数据源
Map<String, DataSource> createDataSourceMap() {
DruidDataSource dataSource1 = new DruidDataSource(); dataSource1.setDriverClassName("com.mysql.jdbc.Driver"); dataSource1.setUrl("jdbc:mysql://localhost:3306/order_db?useUnicode=true"); dataSource1.setUsername("root");
dataSource1.setPassword("root");
Map<String, DataSource> result = new HashMap<>(); result.put("m1", dataSource1);
return result;
}
// 定义主键生成策略
private static KeyGeneratorConfiguration getKeyGeneratorConfiguration() {
KeyGeneratorConfiguration result = new KeyGeneratorConfiguration("SNOWFLAKE","order_id");
return result;
}
// 定义t_order表的分片策略
TableRuleConfiguration getOrderTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration("t_order","m1.t_order_$‐> {1..2}");
result.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "t_order_$‐>{order_id % 2 + 1}"));
result.setKeyGeneratorConfig(getKeyGeneratorConfiguration()); return result;
}
// 定义sharding‐Jdbc数据源
@Bean
DataSource getShardingDataSource() throws SQLException {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration()); //spring.shardingsphere.props.sql.show = true
Properties properties = new Properties();
properties.put("sql.show","true");
return ShardingDataSourceFactory.createDataSource(createDataSourceMap(),
shardingRuleConfig,properties);
}
}

由于采用类配置类所以需要屏蔽原来application.properties文件中spring.shardingsphere开头的配置信息。还需要在SpringBoot启动类中屏蔽使用spring.shardingsphere配置项的类 :

@SpringBootApplication(exclude = {SpringBootConfiguration.class}) public class ShardingJdbcSimpleDemoBootstrap {....}

Spring命名空间配置 此方式使用xml方式配置,不推荐使用。

<?xml version="1.0" encoding="UTF‐8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema‐instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:sharding="http://shardingsphere.apache.org/schema/shardingsphere/sharding"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring‐beans.xsd
http://shardingsphere.apache.org/schema/shardingsphere/sharding
http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring‐context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring‐tx.xsd">
<context:annotation‐config />
<!‐‐定义多个数据源‐‐>
<bean id="m1" class="com.alibaba.druid.pool.DruidDataSource" destroy‐method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/order_db_1?useUnicode=true" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!‐‐定义分库策略‐‐>
<sharding:inline‐strategy id="tableShardingStrategy" sharding‐column="order_id" algorithm‐
expression="t_order_$‐>{order_id % 2 + 1}" />
<!‐‐定义主键生成策略‐‐>
<sharding:key‐generator id="orderKeyGenerator" type="SNOWFLAKE" column="order_id" />
<!‐‐定义sharding‐Jdbc数据源‐‐> <sharding:data‐source id="shardingDataSource">
<sharding:sharding‐rule data‐source‐names="m1">
<sharding:table‐rules>
<sharding:table‐rule logic‐table="t_order" table‐strategy‐ ref="tableShardingStrategy" key‐generator‐ref="orderKeyGenerator" />
</sharding:table‐rules>
</sharding:sharding‐rule>
</sharding:data‐source>
</beans>


标签:分库,spring,id,sharding,jdbc,shardingsphere,分表,第二篇,order
From: https://blog.51cto.com/u_15829196/5755975

相关文章

  • 我的第二篇随笔
    这是记录开博客原因的一篇随笔,是第二篇,第一篇是一个很简单的conda的,而那也是我决定开通博客的直接原因:这种很简单的事情我还总是忘记,总是在这种事上浪费时间实在是令人恼火......
  • 分库分表 Sharding: 4. 路由 / 映射:如何找到要操作的表
    4.   路由/映射:如何找到要操作的表4.1   实际表的分布种类基本表与实际表的映射关系,可分为均匀顺序分布,均匀轮询分布,仅均匀分布和自定义分布4种形式。现通过例......
  • 分库分表 Sharding: 5. 分片流程与 Sharding 核心问题
    5.   分片流程与Sharding核心问题5.1   Bee(JDBC部分)的转换流程Bee基于JDBC操作数据库的流程如下:1)使用实体Javabean(+条件表达式Condtion,对应SQL......
  • 分库分表 Sharding:7. Bee 对分片的优化
    7.   Bee对分片的优化7.1   对一库一表的优化最终路由到一库一表,即不会造成分片,因此Bee将其优化为单点操作。7.2   对一库多表的分页查询的优化对一库多表的......
  • 分库分表 Sharding:8. 主流的数据库中间件实现对比
    8.   主流的数据库中间件实现对比8.1   数据库代理与数据源代理典型的数据库中间件设计方案有2种:服务端代理(proxy:代理数据库)、客户端代理(datasource:代理数据......
  • 分库分表中间件 sharding
     任何一个技术的出现,都不是为了秀肌肉而产生的。  sharding jdbc 这个分库分表技术要解决的问题就是,随着数据量级的提升,物理硬件达到瓶颈,单表的性能优化也带来了瓶......
  • 分库分表
     这篇文章主要介绍分库分表,以及分库分表带来的问题、 ## 分库分表的几种形式 水平分库,本质是把相同的表放在不同的机器上。 垂直分库:本质是将多个表拆分到不同的机器......
  • 使用sharding 做分库分表以后,插入报错 Executing an update/delete query
    这个问题倘若没有 sharding,那就是在service层缺少了事务注解@Transaction这个问题具体看这里​ 我是跑测试类跑出来的问题,好像做分库分表,不能用测试类来测,只能通过 con......
  • 使用sharding做分库分表,使用jpa,发生的save不报错,数据库缺插不进去数据的问题
     先讲讲问题的诞生,我们项目起初没有引进 sharding分库,而是在项目上线前,才做的分库分表。也就是之前的业务都写好的,所以知道业务代码没有任何问题。 然后引入 sharding......
  • mybatis-plus还可以这样分表
    mybatis-plus还可以这样分表 为什么要分表Mysql是当前互联网系统中使用非常广泛的关系数据库,具有ACID的特性。但是mysql的单表性能会受到表中数据量的限制,主要原因......