【Sharding Sphere、Spring Boot】Spring Boot整合Sharding Sphere Jdbc(5.0.0):读写分离

一、Sharding Sphere基本介绍

官网链接推荐:跳转官网 | 5.0.0中文文档 | 读写分离介绍

二、一主一从Docker环境搭建

【Docker、Mysql】Docker搭建Mysql主从复制(一主一从)

三、Spring Boot整合Sharding Sphere Jdbc实现读写分离

pom.xml依赖

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

application.yml配置


# ShardingSphere 读写分离
spring:
  main:
    allow-bean-definition-overriding: true
  shardingsphere:
    datasource:
      # 数据源
      names: master,slave1
      # 主库:添加、更新以及删除数据操作所使用的数据库,目前仅支持单主库。
      master:
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:7007/cyun_log?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
        username: root
        password: root
        type: com.zaxxer.hikari.HikariDataSource
      slave1:
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:7008/cyun_log?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
        username: root
        password: root
        type: com.zaxxer.hikari.HikariDataSource
    # 使用模式,支持三种模式:内存模式、单机模式、集群模式
    mode:
      type: Memory
    props:
      # 打印sql
      sql-show: true
    # 规则
    rules:
      readwrite-splitting:
        data-sources:
          master-slave:
            # 写数据源名称
            write-data-source-name: master
            # 读数据源名称,多个从数据源用逗号分隔
            read-data-source-names: slave1,master
            # 负载均衡算法名称
            load-balancer-name: round-robin
        # 负载均衡
        load-balancers:
          round-robin:
            # 负载均衡算法属性配置
            props:
              default: 0
            # 负载均衡算法类型:ROUND_ROBIN(轮询算法),RANDOM(随机算法)
            type: ROUND_ROBIN

结果展示:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值