java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot3 读写分离

SpringBoot3+ShardingJDBC5.5.0 读写分离配置的实现

作者:qq_22855851

本文主要介绍了SpringBoot3+ShardingJDBC5.5.0 读写分离配置的实现,最新版5.5.0支持SpringBoot3x现分享给大家,具有一定的参考价值,感兴趣的可以了解一下

网上自己找了很多教程但是配合SpringBoot3都无法使用,经过实验发现只有最新版5.5.0支持SpringBoot3x现在把相关读写分离配置分享给大家

相关springboot3的包这里就不多赘述,我使用的是MybatisPlus 3.5.5

首先导入相关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>

这里放一个完整的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>
            <!-- 对spring-boot版本统一管理-->
            <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>

第二部对springboot的application进行修改

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

那么接下来就是重点了,然后是sharding.yaml的相关配置,因为官方的文档比较碎片化,最后进行校验和处理得出的完整文档

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

这里我是用一个读库一个写库处理,数据库连接自己填,打印SQL可以开启可以看到读和写走到了不同的库,至于数据分片就不多赘述,我自己也没验证,不过估计在配置文件加入分片规则就可以做到,还有rules的下的配置一定要写SINGLE这个 不然它会报找不到表的错误,还有这个配置目前只有shardingjdbc 5.4.1和5.5.0可以用,其他版本并不通用 请不要混用
这里考虑到强制走写库的情况自己根据HintManager写了一个强制主库的注解和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;
    }
}

设置这个AOP后只要在service实现类方法上加上@Master注解就可以强制走读库处理了,其他情况安正常处理

到此这篇关于SpringBoot3+ShardingJDBC5.5.0 读写分离配置的实现的文章就介绍到这了,更多相关SpringBoot3 读写分离内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文