首页 > 其他分享 >springBoot 读写分离 事务

springBoot 读写分离 事务

时间:2024-04-19 16:27:09浏览次数:15  
标签:事务 jdbc slave springBoot shardingsphere 读写 master datasource 数据源

   ShardingSphere-JDBC 事务均用主库。

 

1.添加pom.xml

        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
            <version>5.1.1</version>
        </dependency>    

 

2.配置数据源

# 应用名称
spring.application.name=shardingjdbc-write-read-demo


# 打印SQl
spring.shardingsphere.props.sql-show=true


# 定义读写分离的数据源
spring.shardingsphere.datasource.names=master,slave


# 数据源1
spring.shardingsphere.datasource.master.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.jdbc-url=jdbc:mysql://192.168.10.132:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=root


# 数据源2
spring.shardingsphere.datasource.slave.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave.jdbc-url=jdbc:mysql://192.168.10.133:3306/test2?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.shardingsphere.datasource.slave.username=root
spring.shardingsphere.datasource.slave.password=root


#读写分离 主从关系
# 读写分离类型,如: Static,Dynamic, ms1 包含了 m1 和 s1
spring.shardingsphere.rules.readwrite-splitting.data-sources.ms1.type=Static


# 写数据源名称
spring.shardingsphere.rules.readwrite-splitting.data-sources.ms1.props.write-data-source-name=master


# 读数据源名称,多个从数据源用逗号分隔
spring.shardingsphere.rules.readwrite-splitting.data-sources.ms1.props.read-data-source-names=slave

 

3.测试在事务里 查询和新增

 

4.非事务运行

 

标签:事务,jdbc,slave,springBoot,shardingsphere,读写,master,datasource,数据源
From: https://www.cnblogs.com/entertain/p/18146214

相关文章

  • Springboot 下载模板
    文件路径在项目的resources下面  路径 resources/tempExcel/导入模板.xlsx后端代码:/***下载模板**@paramresponse*@throwsIOException*/@RepeatSubmit@PostMapping("/download/template")publicvoiddownloadTemplate(HttpServletResponseresponse)throws......
  • springBoot 读写分离
    1.pom.xml配置<!--读写分离api--><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId><version>4.0.0-RC1......
  • Springboot vue pdf/图片 加水印下载
    1:引入包<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.27</version></dependency>2前端代码:downloadFile1(fileUrl,filename){if(filename==null||fi......
  • springBoot 多数据源配置
    常规数据源#应用端口server:port:5555spring:datasource:username:rootpassword:ffjy1101url:jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=falsedriver-class-name:com.......
  • SQL事务隔离级别
    事务的隔离性是指:在并发环境中,并发的事务是相互隔离的,一个事务的执行不能被其它事务干扰。也就是说,不同的事务并发操作相同的数据时,每个事务都有各自完整的数据空间,即一个事务内部的操作及使用的数据对其他并发事务是隔离的,并发执行的各个事务之间不能相互干扰。在标准的......
  • stm32f103使用RT-Thread组件fal读写内部flash
    本次使用RT-Threadstudio编写,使用为5.02完整版,目的是将内部flash进行分区,可以直接在内部flash存储数据。一、功能配置首先是打开设置里的FAL组件,因为我这里不需要外部内存,SFUD驱动就没打开:然后是配置两个参数,一个在board.h里,定义BSP_USING_ON_CHIP_FLASH,一个是在stm32xxxx_hal_......
  • SpringBoot 3.1.10 整合Mybatis-plus(快速学会)
    一、简介1、什么是Mybatis-plus?Mybatis-Plus(简称MP)是一个Mybatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生。官网:https://baomidou.com/2、Mybatis-plus特性无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑损耗小:启动......
  • redis的事务
    回顾阳哥的redis视频,总结一下redis的事务。是什么可以一次执行多个命令,本质就是一组命令的集合,一个事务中的所有命令都会序列化,按顺序地串行化执行而不会被其它命令插入,不允许加塞。能干嘛一个队列中,一次性,顺序性,排他性的执行一系列命令。watch监控watch使得exec命令需要有......
  • mybatis-plus分页插件使用(springboot)
    1、添加依赖、、、2、自定义一个配置类importcom.baomidou.mybatisplus.annotation.DbType;importcom.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;importcom.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;importorg.sp......
  • SpringBoot项目 file not present 终局!
    在写一个文件上传接口时,从一个老项目里copy出来了一个接口,死活报错filenotpresent,参考如下步骤排查确保请求的httpheader里面的文件字段名和接口定义一致如果使用postman,则确定key和接口保持一致在保证一切都是对的情况下,检查下项目是否配置了CommonsMultipartResolv......