栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

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

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【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依赖

		
        
            org.apache.shardingsphere
            shardingsphere-jdbc-core-spring-boot-starter
            5.0.0
        

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

结果展示:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/826151.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号