當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Springboot2.x +JPA 集成 Apache ShardingSphere 分表+读写分离
生活随笔
收集整理的這篇文章主要介紹了
Springboot2.x +JPA 集成 Apache ShardingSphere 分表+读写分离
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
分庫分表背景:
數(shù)據(jù)庫性能瓶頸:主要分為按照業(yè)務(wù)來劃分或者按照數(shù)據(jù)量來劃分。
拆分方式:
水平拆分(每個(gè)表的結(jié)構(gòu)都一樣):訂單表數(shù)據(jù)量大,我們可以水平拆分 ,分成order表1、order表2、order表3 。。。
垂直拆分:一個(gè)多字段的表拆分成多個(gè)表
例如:order訂單表和oderItem訂單詳情表
一個(gè)訂單會(huì)購買多件商品,因此,訂單order表中會(huì)只有一條數(shù)據(jù),orderItem訂單項(xiàng)表會(huì)對(duì)應(yīng)這個(gè)訂單購買的多件商品。
文章目錄
- 一、基礎(chǔ)準(zhǔn)備
- 1. 技術(shù)選型
- 2. 搭建mysql主從復(fù)制服務(wù)器
- 1. 引入 Maven 依賴
- 2. 規(guī)則配置
- 3. 實(shí)體
- 4. 接口
- 5. 表結(jié)構(gòu)
- 6. 測(cè)試類
- 7. 完整pom
一、基礎(chǔ)準(zhǔn)備
1. 技術(shù)選型
| spring-boot | 2.4.3 |
| jpa | 2.4.3 |
| shardingsphere | 5.0.0-alpha |
| mysql | 5.7.3 |
| hikari | 3.4.5 |
2. 搭建mysql主從復(fù)制服務(wù)器
基于Docker的Mysql主從復(fù)制搭建_mysql5.7.x
1. 引入 Maven 依賴
<dependency><groupId>org.apache.shardingsphere</groupId><artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId><version>5.0.0-alpha</version></dependency>2. 規(guī)則配置
spring.shardingsphere.datasource.names=primary-ds,replica-ds-0spring.shardingsphere.datasource.common.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.common.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.common.username=root spring.shardingsphere.datasource.common.password=123456spring.shardingsphere.datasource.primary-ds.jdbc-url=jdbc:mysql://192.168.43.202:3339/ds0?serverTimezone=UTC&useSSL=false spring.shardingsphere.datasource.replica-ds-0.jdbc-url=jdbc:mysql://192.168.43.202:3340/ds0?serverTimezone=UTC&useSSL=falsespring.shardingsphere.rules.replica-query.data-sources.ds0.primary-data-source-name=primary-ds spring.shardingsphere.rules.replica-query.data-sources.ds0.replica-data-source-names=replica-ds-0 spring.shardingsphere.rules.replica-query.data-sources.ds0.load-balancer-name=round-robin# 負(fù)載均衡算法配置 spring.shardingsphere.rules.replica-query.load-balancers.round-robin.type=ROUND_ROBIN spring.shardingsphere.rules.replica-query.load-balancers.round-robin.props.default=0spring.shardingsphere.props.sql-show=true# 配置 t_order 表規(guī)則 spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=ds0.t_order_$->{0..1}# 配置分表策略 spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-column=order_id spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-algorithm-name=table-inline# 配置 分片算法 spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.type=INLINE spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.props.algorithm-expression=t_order_$->{order_id % 2}# 分布式序列策略配置 spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.column=order_id spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.key-generator-name=snowflake# 分布式序列算法配置 spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker-id=1233. 實(shí)體
package com.gblfy.distributedsharding.entity;import lombok.Data;import javax.persistence.*;@Data @Entity @Table(name = "t_order") public class OrderEntity {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long orderId;private Integer userId; }4. 接口
package com.gblfy.distributedsharding.mapper;import com.gblfy.distributedsharding.entity.OrderEntity; import org.springframework.data.jpa.repository.JpaRepository;import java.util.List;public interface OrderMapper extends JpaRepository<OrderEntity, Long> {OrderEntity findByOrderId(Long orderId);List<OrderEntity> findByUserId(Integer userId); }5. 表結(jié)構(gòu)
在master主庫執(zhí)行
CREATE DATABASE ds0; use ds0; CREATE TABLE `t_order_0` (`order_id` bigint(20) unsigned NOT NULL,`user_id` int(11) DEFAULT NULL,PRIMARY KEY (`order_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;CREATE TABLE `t_order_1` (`order_id` bigint(20) unsigned NOT NULL,`user_id` int(11) DEFAULT NULL,PRIMARY KEY (`order_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;6. 測(cè)試類
package com.gblfy.distributedsharding;import com.gblfy.distributedsharding.entity.OrderEntity; import com.gblfy.distributedsharding.mapper.OrderMapper; import org.apache.shardingsphere.infra.hint.HintManager; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.Random;@SpringBootTest class DistributedShardingApplicationTests {@Autowiredprivate OrderMapper orderMapper;@Testvoid insert() {for (int i = 0; i <20 ; i++) {OrderEntity entity = new OrderEntity();entity.setUserId(new Random().nextInt(999));orderMapper.save(entity);}}@Testvoid findByOrderId() {//在主庫負(fù)責(zé)增刪改查 從庫負(fù)責(zé)查詢 場景中 ,存在剛把數(shù)據(jù)寫入主庫中,// 為來得急同步從庫,因此會(huì)導(dǎo)致從庫沒有查詢的數(shù)據(jù),但實(shí)際,數(shù)據(jù)在主庫是存在的,// 從庫負(fù)責(zé)查詢只是為了環(huán)節(jié)主庫的數(shù)據(jù)庫查詢的壓力,因此,在特殊場景,需要從主庫// 查詢數(shù)據(jù),可以通過配置五主庫中查詢數(shù)據(jù)HintManager.getInstance().setPrimaryRouteOnly();orderMapper.findByOrderId(570271967295811584L);}@Testvoid findByUserId() {orderMapper.findByUserId(556);}@Testvoid updateByOrderId() {OrderEntity byOrderId = orderMapper.findByOrderId(570279923689172992L);byOrderId.setUserId(1000);orderMapper.save(byOrderId);} }7. 完整pom
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId><version>5.0.0-alpha</version></dependency>
從庫查詢
總結(jié)
以上是生活随笔為你收集整理的Springboot2.x +JPA 集成 Apache ShardingSphere 分表+读写分离的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring项目启动加载xml配置文件替
- 下一篇: SpringBoot2 集成 xxl-j