欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot3+ShardingJDBC5.5.0 讀寫分離配置的實(shí)現(xiàn)

 更新時(shí)間:2024年08月09日 11:26:24   作者:qq_22855851  
本文主要介紹了SpringBoot3+ShardingJDBC5.5.0 讀寫分離配置的實(shí)現(xiàn),最新版5.5.0支持SpringBoot3x現(xiàn)分享給大家,具有一定的參考價(jià)值,感興趣的可以了解一下

網(wǎng)上自己找了很多教程但是配合SpringBoot3都無法使用,經(jīng)過實(shí)驗(yàn)發(fā)現(xiàn)只有最新版5.5.0支持SpringBoot3x現(xiàn)在把相關(guān)讀寫分離配置分享給大家

相關(guān)springboot3的包這里就不多贅述,我使用的是MybatisPlus 3.5.5

首先導(dǎo)入相關(guān)jar包

         <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>shardingsphere-jdbc</artifactId>
            <version>5.5.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.shardingsphere</groupId>
                    <artifactId>shardingsphere-test-util</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

這里放一個(gè)完整的pom給大家看看

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <groupId>com.yijun.sharding.jdbc</groupId>
    <artifactId>spring-boot3-sharding-jdbc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot3-sharding-jdbc</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>17</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>3.2.4</spring-boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.13.0</version>
        </dependency>
        <dependency>
            <groupId>jakarta.persistence</groupId>
            <artifactId>jakarta.persistence-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.5</version>
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis-spring</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>mybatis</artifactId>
                    <groupId>org.mybatis</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>shardingsphere-jdbc</artifactId>
            <version>5.5.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.shardingsphere</groupId>
                    <artifactId>shardingsphere-test-util</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <!-- 對(duì)spring-boot版本統(tǒng)一管理-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

第二部對(duì)springboot的application進(jìn)行修改

spring:
  main:
    allow-bean-definition-overriding: true
  application:
    name: ls-backend
  datasource:
    driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
    url: jdbc:shardingsphere:classpath:sharding.yaml
  servlet:
    multipart:
      max-file-size: 100MB
      max-request-size: 100MB
server:
  port: 8085
  servlet:
    context-path: /
    encoding:
      force-response: true


#Mybatis掃描
mybatis:
  config-location: classpath:/mybatis-config.xml

mybatis-plus:
  mapper-locations: classpath:/mapper/*Mapper.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImp

那么接下來就是重點(diǎn)了,然后是sharding.yaml的相關(guān)配置,因?yàn)楣俜降奈臋n比較碎片化,最后進(jìn)行校驗(yàn)和處理得出的完整文檔

dataSources:
  ds_1:
    driverClassName: com.mysql.cj.jdbc.Driver
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    jdbcUrl: 
    username: 
    password: 
  ds_2:
    driverClassName: com.mysql.cj.jdbc.Driver
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    jdbcUrl: 
    username: 
    password: 
rules:
  - !SINGLE
    tables:
      - "*.*"
    defaultDataSource: ds_1
  - !READWRITE_SPLITTING
    dataSources:
      readwrite_ds:
        writeDataSourceName: ds_1
        readDataSourceNames:
          - ds_2
        transactionalReadQueryStrategy: PRIMARY
        loadBalancerName: random
    loadBalancers:
      random:
        type: RANDOM
props:
  sql-show: true

這里我是用一個(gè)讀庫(kù)一個(gè)寫庫(kù)處理,數(shù)據(jù)庫(kù)連接自己填,打印SQL可以開啟可以看到讀和寫走到了不同的庫(kù),至于數(shù)據(jù)分片就不多贅述,我自己也沒驗(yàn)證,不過估計(jì)在配置文件加入分片規(guī)則就可以做到,還有rules的下的配置一定要寫SINGLE這個(gè) 不然它會(huì)報(bào)找不到表的錯(cuò)誤,還有這個(gè)配置目前只有shardingjdbc 5.4.1和5.5.0可以用,其他版本并不通用 請(qǐng)不要混用
這里考慮到強(qiáng)制走寫庫(kù)的情況自己根據(jù)HintManager寫了一個(gè)強(qiáng)制主庫(kù)的注解和AOP如下:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)public @interface Master {}
@Slf4j
@Component
@Aspect
public class ShardingAop {

    @Around("execution(* com.yijun..service.impl.*.*(..))")
    public Object master(ProceedingJoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        Object ret = null;
        log.info(joinPoint.toShortString());
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        Master  master = method.getAnnotation(Master.class);
        HintManager hintManager = null;
        try {
            if (Objects.nonNull(master)) {
                HintManager.clear();
                hintManager = HintManager.getInstance();
                hintManager.setWriteRouteOnly();
            }
            ret = joinPoint.proceed(args);
        }catch (Exception ex){
            log.error("exception error",ex);
        }catch (Throwable ex2){
            log.error("Throwable",ex2);
        }finally {
            if (Objects.nonNull(master) && Objects.nonNull(hintManager)) {
                hintManager.close();
            }
        }
        return ret;
    }
}

設(shè)置這個(gè)AOP后只要在service實(shí)現(xiàn)類方法上加上@Master注解就可以強(qiáng)制走讀庫(kù)處理了,其他情況安正常處理

到此這篇關(guān)于SpringBoot3+ShardingJDBC5.5.0 讀寫分離配置的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot3 讀寫分離內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 打造一款代碼命名工具的詳細(xì)教程

    打造一款代碼命名工具的詳細(xì)教程

    這篇文章主要介紹了來,我們一起打造一款代碼命名工具,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • java8 Math新增方法介紹

    java8 Math新增方法介紹

    這篇文章主要介紹了java8 Math新增方法介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Mabitis中的#與$符號(hào)區(qū)別及用法介紹

    Mabitis中的#與$符號(hào)區(qū)別及用法介紹

    這篇文章主要介紹了Mabitis中的#與$符號(hào)區(qū)別,需要的朋友可以參考下
    2017-02-02
  • springboot項(xiàng)目中controller層與前端的參數(shù)傳遞方式

    springboot項(xiàng)目中controller層與前端的參數(shù)傳遞方式

    這篇文章主要介紹了springboot項(xiàng)目中controller層與前端的參數(shù)傳遞方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Mybatis逆向工程時(shí)失敗問題及解決

    Mybatis逆向工程時(shí)失敗問題及解決

    這篇文章主要介紹了Mybatis逆向工程時(shí)失敗問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • SpringCloud協(xié)同開發(fā)實(shí)現(xiàn)方法淺析

    SpringCloud協(xié)同開發(fā)實(shí)現(xiàn)方法淺析

    好幾個(gè)人同時(shí)開發(fā)同一個(gè)服務(wù)上的不同模塊,導(dǎo)致你需要調(diào)試的接口總是被路由到別人的服務(wù)上,非常影響調(diào)試的效率,而且人越多越難受,總是請(qǐng)求不到自己的服務(wù),這篇文章主要介紹了SpringCloud協(xié)同開發(fā)實(shí)現(xiàn)方法
    2022-12-12
  • Java手動(dòng)方式創(chuàng)建枚舉類示例

    Java手動(dòng)方式創(chuàng)建枚舉類示例

    這篇文章主要介紹了Java手動(dòng)方式創(chuàng)建枚舉類,結(jié)合實(shí)例形式分析了java創(chuàng)建枚舉類的方法及相關(guān)操作技巧,需要的朋友可以參考下
    2019-08-08
  • Springboot中@RequestParam和@PathVariable的用法與區(qū)別詳解

    Springboot中@RequestParam和@PathVariable的用法與區(qū)別詳解

    這篇文章主要介紹了Springboot中@RequestParam和@PathVariable的用法與區(qū)別詳解,RESTful API設(shè)計(jì)的最佳實(shí)踐是使用路徑參數(shù)來標(biāo)識(shí)一個(gè)或多個(gè)特定資源,而使用查詢參數(shù)來對(duì)這些資源進(jìn)行排序/過濾,需要的朋友可以參考下
    2024-01-01
  • Spring MVC請(qǐng)求參數(shù)接收的全面總結(jié)教程

    Spring MVC請(qǐng)求參數(shù)接收的全面總結(jié)教程

    這篇文章主要給大家總結(jié)介紹了關(guān)于Spring MVC請(qǐng)求參數(shù)接收的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • ObjectInputStream 和 ObjectOutputStream 介紹_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    ObjectInputStream 和 ObjectOutputStream 介紹_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    ObjectInputStream 和 ObjectOutputStream 的作用是,對(duì)基本數(shù)據(jù)和對(duì)象進(jìn)行序列化操作支持。本文給大家詳細(xì)介紹了ObjectInputStream 和 ObjectOutputStream的相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧
    2017-05-05

最新評(píng)論