當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Springboot2.x +JPA 集成 Apache ShardingSphere 读写分离
生活随笔
收集整理的這篇文章主要介紹了
Springboot2.x +JPA 集成 Apache ShardingSphere 读写分离
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
分庫分表背景:
數(shù)據(jù)庫性能瓶頸:主要分為按照業(yè)務(wù)來劃分或者按照數(shù)據(jù)量來劃分。
拆分方式:
水平拆分(每個表的結(jié)構(gòu)都一樣):訂單表數(shù)據(jù)量大,我們可以水平拆分 ,分成order表1、order表2、order表3 。。。
垂直拆分:一個多字段的表拆分成多個表
例如:order訂單表和oderItem訂單詳情表
一個訂單會購買多件商品,因此,訂單order表中會只有一條數(shù)據(jù),orderItem訂單項表會對應(yīng)這個訂單購買的多件商品。
文章目錄
- 一、基礎(chǔ)準(zhǔn)備
- 1. 技術(shù)選型
- 2. 搭建mysql主從復(fù)制服務(wù)器
- 1. 引入 Maven 依賴
- 2. 規(guī)則配置
- 3. 實(shí)體
- 4. 接口
- 5. 表結(jié)構(gòu)
- 6. 測試類
- 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ī)則配置
#主從數(shù)據(jù)庫配置 spring.shardingsphere.datasource.names=primary-ds,replica-ds-0#數(shù)據(jù)庫連接信息 spring.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&useUnicode=true&characterEncoding=UTF-8 spring.shardingsphere.datasource.replica-ds-0.jdbc-url=jdbc:mysql://192.168.43.202:3340/ds0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8#負(fù)載均衡策略 spring.shardingsphere.rules.replica-query.load-balancers.round-robin.type=ROUND_ROBIN spring.shardingsphere.rules.replica-query.data-sources.pr_ds.primary-data-source-name=primary-ds spring.shardingsphere.rules.replica-query.data-sources.pr_ds.replica-data-source-names=replica-ds-0#負(fù)載均衡算法配置 spring.shardingsphere.rules.replica-query.data-sources.pr_ds.load-balancer-name=round-robin spring.shardingsphere.rules.replica-query.load-balancers.round-robin.props.default=0#顯示sql spring.shardingsphere.props.sql-show=true3. 實(shí)體
package com.gblfy.distributedsharding.entity;import lombok.Data;import javax.persistence.*;@Data @Entity @Table(name = "t_order") public class OrderEntity {@Idprivate 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)
CREATE DATABASE ds0; use ds0; CREATE TABLE `t_order` (`order_id` bigint(20) unsigned NOT NULL,`user_id` int(11) DEFAULT NULL,PRIMARY KEY (`order_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;6. 測試類
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() {OrderEntity entity = new OrderEntity();entity.setOrderId(System.currentTimeMillis());entity.setUserId(new Random().nextInt(999));orderMapper.save(entity);}@Testvoid findByOrderId() {//在主庫負(fù)責(zé)增刪改查 從庫負(fù)責(zé)查詢 場景中 ,存在剛把數(shù)據(jù)寫入主庫中,// 為來得急同步從庫,因此會導(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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JobDataMap传递参数_02
- 下一篇: Navicat for MySQL中如何