SpringBoot3+ShardingJDBC5.5.0 讀寫分離配置的實(shí)現(xiàn)
網(wǎng)上自己找了很多教程但是配合SpringBoot3都無(wú)法使用,經(jīng)過(guò)實(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
那么接下來(lái)就是重點(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ù)分片就不多贅述,我自己也沒(méi)驗(yàn)證,不過(guò)估計(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)文章希望大家以后多多支持腳本之家!
- spring boot使用sharding jdbc的配置方式
- 詳解springboot采用多數(shù)據(jù)源對(duì)JdbcTemplate配置的方法
- springboot2.0.0配置多數(shù)據(jù)源出現(xiàn)jdbcUrl is required with driverClassName的錯(cuò)誤
- SpringBoot多數(shù)據(jù)源配置詳細(xì)教程(JdbcTemplate、mybatis)
- 詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源
- springboot+springJdbc+postgresql 實(shí)現(xiàn)多數(shù)據(jù)源的配置
- springboot實(shí)現(xiàn)以代碼的方式配置sharding-jdbc水平分表
- SpringBoot?配置多個(gè)JdbcTemplate的實(shí)現(xiàn)步驟
- SpringBoot+MybatisPlus+jdbc連接池配置多數(shù)據(jù)源的實(shí)現(xiàn)
- Spring?JDBC配置與使用的實(shí)現(xiàn)
相關(guān)文章
shiro多驗(yàn)證登錄代碼實(shí)例及問(wèn)題解決
這篇文章主要介紹了shiro多驗(yàn)證登錄代碼實(shí)例及問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12SpringBoot實(shí)現(xiàn)國(guó)際化i18n詳解
國(guó)際化(Internationalization,簡(jiǎn)稱i18n)是指在軟件應(yīng)用中支持多種語(yǔ)言和文化的能力,本文將介紹如何在Spring?Boot應(yīng)用中實(shí)現(xiàn)國(guó)際化,需要的可以參考下2024-12-12關(guān)于MongoDB圖片URL存儲(chǔ)異常問(wèn)題以及解決
這篇文章主要介紹了關(guān)于MongoDB圖片URL存儲(chǔ)異常問(wèn)題以及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04SpringBoot2使用Jetty容器操作(替換默認(rèn)Tomcat)
這篇文章主要介紹了SpringBoot2使用Jetty容器操作(替換默認(rèn)Tomcat),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10struts2實(shí)現(xiàn)簡(jiǎn)單文件下載功能
這篇文章主要為大家詳細(xì)介紹了struts2實(shí)現(xiàn)簡(jiǎn)單文件下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01