SpringBoot 整合 Sharding-Sphere + Druid 读写分离

Sharding-Sphere 文档地址:https://shardingsphere.apache.org/document/current/cn/manual/sharding-jdbc/configuration/config-yaml/#%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB-1

Maven依赖

        
        
        
            io.shardingsphere
            sharding-jdbc-spring-boot-starter
            3.1.0
        
        
        
            io.shardingsphere
            sharding-jdbc-spring-namespace
            3.1.0
        
        

读写分离properties(默认连接池)

# 配置真实数据源
sharding.jdbc.datasource.names=master1,slave0
# 主数据库
sharding.jdbc.datasource.master1.type=com.zaxxer.hikari.HikariDataSource
sharding.jdbc.datasource.master1.hikari.driver-class-name=com.mysql.cj.jdbc.Driver
sharding.jdbc.datasource.master1.jdbc-url=jdbc:mysql://localhost:3306/master1?characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai
sharding.jdbc.datasource.master1.username=root
sharding.jdbc.datasource.master1.password=123456
# 从数据库
sharding.jdbc.datasource.slave0.type=com.zaxxer.hikari.HikariDataSource
sharding.jdbc.datasource.slave0.hikari.driver-class-name=com.mysql.cj.jdbc.Driver
sharding.jdbc.datasource.slave0.jdbc-url=jdbc:mysql://localhost:3306/slave0?characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai
sharding.jdbc.datasource.slave0.username=root
sharding.jdbc.datasource.slave0.password=123456
# 配置读写分离
# 配置从库选择策略,提供轮询与随机,这里选择用轮询
sharding.jdbc.config.masterslave.load-balance-algorithm-type=round_robin
sharding.jdbc.config.masterslave.name=ms
sharding.jdbc.config.masterslave.master-data-source-name=master1
sharding.jdbc.config.masterslave.slave-data-source-names=slave0
# 开启SQL显示,默认值: false,注意:仅配置读写分离时不会打印日志
sharding.jdbc.config.props.sql.show=true
#设置为true,表示后发现的bean会覆盖之前相同名称的bean。
spring.main.allow-bean-definition-overriding=true

 读写分离yaml(默认连接池)

sharding:
  jdbc:
    datasource:
      names: master1,salve0 #主从数据源
      master1:
        type: com.zaxxer.hikari.HikariDataSource
        hikari:
          driver-class-name: com.mysql.cj.jdbc.Driver #数据库新的驱动,下面连接url一定要加区时
        jdbc-url: jdbc:mysql://localhost:3306/master1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
        username: root
        password: 123456
      salve0:
        type: com.zaxxer.hikari.HikariDataSource
        hikari:
          driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:3306/salve0?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
        username: root
        password: 123456
    config:  #读写分离配置
      masterslave:
        master-data-source-name: master1 #主数据库配置
        slave-data-source-names: salve0 #从数据库配置
        load-balance-algorithm-type: round_robin # 提供轮询与随机(random),这里选择用轮询,
        name: ms
      props:
        sql:
          show: true #打印sql日志
spring:
  main:
    allow-bean-definition-overriding: true #设置为true,表示后发现的bean会覆盖之前相同名称的bean。
  application:
    name: sharing-sphere-server2

Druid连接池 读写分离properties

# ++++++++++++++++++ shardingsphere【START】 ++++++++++++++++++
 
# 配置数据源 分别是 主数据库1个 从数据库1个
sharding.jdbc.datasource.names=master0,master0slave0
 
# 主第一个数据库
sharding.jdbc.datasource.master1.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.master1.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.master1.url=jdbc:mysql://localhost:3306/master1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true
sharding.jdbc.datasource.master0.username=root
sharding.jdbc.datasource.master0.password=XXXX
 
# 从第一个数据库
sharding.jdbc.datasource.slave0.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.slave0.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.slave0.url=jdbc:mysql://localhost:3306/slave0?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true
sharding.jdbc.datasource.master0slave0.username=root
sharding.jdbc.datasource.master0slave0.password=XXXX
 
 
# 读写分离配置
 
# 从库的读取规则为round_robin(轮询策略),除了轮询策略,还有支持random(随机策略)
sharding.jdbc.config.masterslave.load-balance-algorithm-type=round_robin
# 逻辑主从库名和实际主从库映射关系
# 主数据库0
sharding.jdbc.config.sharding.master-slave-rules.sdkcms.master-data-source-name=master1
# 从数据库0
sharding.jdbc.config.sharding.master-slave-rules.sdkcms.slave-data-source-names=slave0
 

 

Druid连接池 读写分离yaml

sharding:
  jdbc:
    datasource:
      names: master1,salve0 #主从数据源
      master1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver #数据库新的驱动,下面连接url一定要加区时
        url: jdbc:mysql://localhost:3306/master1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true
        username: root
        password: 123456
      salve0:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/salve0?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true
        username: root
        password: 123456
    config:  #读写分离配置
      masterslave:
        master-data-source-name: master1 #主数据库配置
        slave-data-source-names: salve0 #从数据库配置(多个以逗号隔开)
        load-balance-algorithm-type: round_robin # 提供轮询与随机(random),这里选择用轮询,
        name: ms
      props:
        sql:
          show: true #打印sql日志

 

你可能感兴趣的:(spring,cloud,alibaba,spring,boot)