最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談Sharding-JDBC強制路由案例實戰(zhàn)

 更新時間:2023年07月27日 15:19:32   作者:碩風和煒  
本文主要介紹了淺談Sharding-JDBC強制路由案例實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

?? 一.Sharding-JDBC強制路由基本概念

在一些應用場景中,分片條件依賴于外部業(yè)務邏輯。因此需要提供一種通過在外部業(yè)務代碼中指定路由配置的一種方式,在ShardingSphere中叫做Hint。如果使用Hint指定了強制分片路由,那么SQL將會無視原有的分片邏輯,直接路由至指定的數(shù)據(jù)節(jié)點操作。ShardingSphere使用ThreadLocal管理分片鍵值進行Hint強制路由??梢酝ㄟ^編程的方式向HintManager中添加分片值,該分片值僅在當前線程內(nèi)生效。

Hint使用場景:

  • 數(shù)據(jù)分片操作,如果分片鍵沒有在SQL或數(shù)據(jù)表中,而是在業(yè)務邏輯代碼中
  • 讀寫分離操作,如果強制在主庫進行某些數(shù)據(jù)操作

Sharding-JDBC強制路由案例實戰(zhàn)官方指導手冊

?? 二.Sharding-JDBC強制路由案例實戰(zhàn)

2.1 數(shù)據(jù)環(huán)境準備

沒有創(chuàng)建過的同學們需要在 ljw_course_db1ljw_course_db2中創(chuàng)建 t_course表.

CREATE TABLE `t_course` (
  `cid` bigint(20) NOT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `corder_no` bigint(20) DEFAULT NULL,
  `cname` varchar(50) DEFAULT NULL,
  `brief` varchar(50) DEFAULT NULL,
  `price` double DEFAULT NULL,
  `status` int(11) DEFAULT NULL,
  PRIMARY KEY (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.2 創(chuàng)建SpringBoot程序 - 導入依賴

引入依賴 (注意: 在這里使用ShardingSphere4.1版本為案例進行強制路由的實戰(zhàn))

<?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">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>shardingjdbc-hint</artifactId>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/>
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-core-common</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.10</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.5.8</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.5</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>20.0</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.3 創(chuàng)建SpringBoot程序 - 實體類

與數(shù)據(jù)庫表中對應的實體類

@TableName("t_course")
@Data
@ToString
public class Course {
    @TableId(type = IdType.ASSIGN_ID)
    private Long cid;
    private Long userId;
    private Long corderNo;
    private String cname;
    private String brief;
    private double price;
    private int status;
}

2.4 創(chuàng)建SpringBoot程序 - 數(shù)據(jù)庫Mapper接口 & 主啟動類

編寫對應的數(shù)據(jù)庫持久層CourseMapper接口

@Repository
public interface CourseMapper extends BaseMapper<Course> {
}

@Repository注解的類需要交給我們的Spring容器進行管理,因此需要我們在主啟動類加上掃描接口的注解。

@SpringBootApplication
@MapperScan("com.ljw.mapper")
public class ShardingSphereApplication {
    public static void main(String[] args) {
        SpringApplication.run(ShardingSphereApplication.class,args);
    }
}

2.5 創(chuàng)建SpringBoot程序 - 自定義強制路由配置類

在自定義類中編寫分庫或分表路由策略,實現(xiàn)HintShardingAlgorithm接口,并且重寫doSharding方法

public class HintShardingAlgorithmConfig implements HintShardingAlgorithm<Long> {
    /**
     * 定義強制路由策略
     *      collection: 代表分片目標: 對哪些數(shù)據(jù)庫 表進行分片,比如做分庫路由,集合中就存儲著 db1.t_course, db1.t_course
     *      hintShardingValue: 代表分片值, 指的是使用者對分片鍵賦的值
     */
    @Override
    public Collection<String> doSharding(Collection<String> collection, HintShardingValue<Long> hintShardingValue) {
        // 結(jié)果收集
        Collection<String> result = new ArrayList<>();
        // 遍歷數(shù)據(jù)庫
        // collection: 代表分片目標: 對哪些數(shù)據(jù)庫 表進行分片,比如做分庫路由,集合中就存儲著 db1.t_course, db1.t_course
        for (String actualDb : collection) {
            // hintShardingValue: 代表分片值, 指的是使用者對分片鍵賦的值
            Collection<Long> values = hintShardingValue.getValues();
            for (Long value : values) {
                //分庫路由 判斷當前節(jié)點名稱結(jié)尾 是否與取模結(jié)果一致
                if(actualDb.endsWith(String.valueOf(value % 2 + 1))){
                    result.add(actualDb);
                }
            }
        }
        return result;
    }
}

2.6 創(chuàng)建SpringBoot程序 - 強制路由配置文件

配置讀寫分離相關配置的信息

# 應用名稱
spring.application.name=sharding-jdbc-hint
# 打印SQl
spring.shardingsphere.props.sql-show=true
# 定義多個數(shù)據(jù)源
spring.shardingsphere.datasource.names = db1,db2
# 數(shù)據(jù)源1
spring.shardingsphere.datasource.db1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.db1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.db1.jdbc-url=jdbc:mysql://192.168.10.132:3306/ljw_course_db1?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.shardingsphere.datasource.db1.username=root
spring.shardingsphere.datasource.db1.password=root
# 數(shù)據(jù)源2
spring.shardingsphere.datasource.db2.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.db2.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.db2.jdbc-url=jdbc:mysql://192.168.10.133:3306/ljw_course_db2?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.shardingsphere.datasource.db2.username=root
spring.shardingsphere.datasource.db2.password=root
# 配置默認數(shù)據(jù)源db1
spring.shardingsphere.sharding.default-data-source-name=db1
# Hint強制路由
# 使用t_course表測試強制路由到庫
spring.shardingsphere.sharding.tables.t_course.database-strategy.hint.algorithm-class-name=com.ljw.hint.HintShardingAlgorithmConfig
# 強制路由到指定表
spring.shardingsphere.sharding.tables.t_course.table-strategy.hint.algorithm-class-name=com.ljw.hint.HintShardingAlgorithmConfig
#配置數(shù)據(jù)節(jié)點 指定t_course邏輯表表的真實分布情況
spring.shardingsphere.sharding.tables.t_course.actual-data-nodes=db$->{1..2}.t_course_$->{1..2}

?? 三.Sharding-JDBC強制路由案例測試

先將我們數(shù)據(jù)庫中表中原來的數(shù)據(jù)都刪除掉,如果是新建的同學就不用了!

3.1 強制路由到庫測試

    @Autowired
    private CourseMapper courseMapper;
    //測試強制路由 到指定庫的插入操作
    @Test
    public void testInsertCourse(){
        //通過hintManager指定執(zhí)行策略的分片鍵的值
        HintManager hintManager = HintManager.getInstance();
        //強制路由到db2,此時我們需要傳入1,因為我們配置類的邏輯是vlaue%2+1->1%2+1
        hintManager.setDatabaseShardingValue(1L);
        for (int i = 0; i < 6; i++) {
            Course course = new Course();
            course.setUserId(1000L+i);
            course.setCname("Sharding-JDBC強制路由案例實戰(zhàn)");
            course.setBrief("全網(wǎng)Sharding-JDBC強制路由案例實戰(zhàn)最詳細講解!?。?);
            course.setPrice(8939.00);
            course.setStatus(0);
            courseMapper.insert(course);
        }
    }

運行結(jié)果:

3.2 強制路由到庫到表測試

@Autowired
    private CourseMapper courseMapper;
    @Test
    public void testHintDbToTable(){
        HintManager hintManager = HintManager.getInstance();
        //強制路由到指定庫 -> db2
        hintManager.addDatabaseShardingValue("t_course",1L);
        //強制路由到指定表 ->course1
        hintManager.addTableShardingValue("t_course",0L);
        List<Course> courses = courseMapper.selectList(null);
        courses.forEach(System.out::println);
    }

運行結(jié)果:

?? 四.總結(jié)

本篇文章主要講解了Sharding-JDBC強制路由案例實戰(zhàn),實操過程非常重要,大家一定要動手親自實踐一下,更多相關Sharding-JDBC強制路由內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java設計模式之23種設計模式詳解

    Java設計模式之23種設計模式詳解

    這篇文章主要介紹了Java設計模式之23種設計模式詳解,設計模式使代碼編制真正工程化,設計模式是軟件工程的基石,項目中合理的運用設計模式可以完美的解決很多問題,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • Java對象字段拷貝最佳實踐分享

    Java對象字段拷貝最佳實踐分享

    文章介紹了幾種常見的對象字段拷貝方法,包括手動set、BeanUtils.copyProperties、Lombok的@Builder和MapStruct,每種方法都有其優(yōu)缺點和適用場景,推薦使用MapStruct,因為它在編譯期生成代碼,性能最優(yōu),支持復雜對象映射,需要的朋友可以參考下
    2025-03-03
  • SpringBoot 快速實現(xiàn) api 加密的方法

    SpringBoot 快速實現(xiàn) api 加密的方法

    在項目中,為了保證數(shù)據(jù)的安全,我們常常會對傳遞的數(shù)據(jù)進行加密,常用的加密算法包括對稱加密(AES)和非對稱加密(RSA),本文給大家介紹SpringBoot 快速實現(xiàn) api 加密,感興趣的朋友一起看看吧
    2023-10-10
  • java IO實現(xiàn)電腦搜索、刪除功能的實例

    java IO實現(xiàn)電腦搜索、刪除功能的實例

    下面小編就為大家?guī)硪黄猨ava IO實現(xiàn)電腦搜索、刪除功能的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • Springboot在有參構(gòu)造方法類中使用@Value注解取值

    Springboot在有參構(gòu)造方法類中使用@Value注解取值

    這篇文章主要介紹了Springboot在有參構(gòu)造方法類中使用@Value注解取值,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • String字符串轉(zhuǎn)BigDecimal時,報NumberFormatException異常的解決

    String字符串轉(zhuǎn)BigDecimal時,報NumberFormatException異常的解決

    這篇文章主要介紹了String字符串轉(zhuǎn)BigDecimal時,報NumberFormatException異常的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 詳解MyBatis中主鍵回填的兩種實現(xiàn)方式

    詳解MyBatis中主鍵回填的兩種實現(xiàn)方式

    這篇文章主要介紹了詳解MyBatis中主鍵回填的兩種實現(xiàn)方式,主鍵回填其實是一個非常常見的需求,特別是在數(shù)據(jù)添加的過程中,我們經(jīng)常需要添加完數(shù)據(jù)之后,需要獲取剛剛添加的數(shù)據(jù) id,有興趣的可以參考一下
    2019-04-04
  • Java實現(xiàn)讀取CSV文件數(shù)據(jù)內(nèi)容(含逗號處理)

    Java實現(xiàn)讀取CSV文件數(shù)據(jù)內(nèi)容(含逗號處理)

    這篇文章主要為大家詳細介紹了如何使用Java實現(xiàn)讀取CSV文件數(shù)據(jù)內(nèi)容,包含了逗號處理,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2025-07-07
  • Mybatis如何一次性插入多條數(shù)據(jù)

    Mybatis如何一次性插入多條數(shù)據(jù)

    這篇文章主要介紹了Mybatis如何一次性插入多條數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • mybatis自動生成時如何設置不生成Example類詳解

    mybatis自動生成時如何設置不生成Example類詳解

    這篇文章主要給大家介紹了關于mybatis自動生成時如何設置不生成Example類的相關資料,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。
    2017-05-05

最新評論

彭山县| 韩城市| 临澧县| 安泽县| 确山县| 苍溪县| 乐至县| 博罗县| 大同市| 额敏县| 那曲县| 常山县| 河曲县| 罗定市| 建宁县| 罗甸县| 南雄市| 扬中市| 枣阳市| 巫山县| 于都县| 平武县| 潞西市| 舒城县| 手机| 高邑县| 固镇县| 淮安市| 鹤山市| 镇原县| 永昌县| 漠河县| 新乐市| 稻城县| 鄯善县| 扎赉特旗| 舞阳县| 龙南县| 柯坪县| 泸溪县| 明水县|