java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot配置数据库主从shardingsphere

SpringBoot如何配置数据库主从shardingsphere

作者:偷代码的猫

这篇文章主要介绍了SpringBoot如何配置数据库主从shardingsphere问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

SpringBoot配置数据库主从shardingsphere

spring:
  shardingsphere:
    props.sql.show: true
    datasource:
      names: master,slave1
      master:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.jdbc.Driver
        jdbc-url: jdbc:mysql://192.168.1.101:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&allowMultiQueries=true
        username: admin
        password: 123456
        minimum-idle: 10
        maximum-pool-size: 20
        idle-timeout: 300000
        max-lifetime: 900000
        connection-timeout: 30000
        connection-test-query: SELECT 1
      slave1:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.jdbc.Driver
        jdbc-url: jdbc:mysql://192.168.1.102:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&allowMultiQueries=true
        username: admin
        password: 123456
        minimum-idle: 10
        maximum-pool-size: 20
        idle-timeout: 300000
        max-lifetime: 900000
        connection-timeout: 30000
        connection-test-query: SELECT 1
    masterslave:
      name: us
      load-balance-algorithm-type: ROUND_ROBIN
      master-data-source-name: master
      slave-data-source-names:
        - slave1
        - master
  jackson:
    serialization:
      indent_output: true
  main:
    allow-bean-definition-overriding: true

1、SQL是否打印显示

props.sql.show: true

2、数据源名称

names: master,slave1

3、负载均衡算法

load-balance-algorithm-type: ROUND_ROBIN
 
ROUND_ROBIN:轮询
RANDOM:随机

4、主数据源名称

master-data-source-name: master

5、从数据源名称

slave-data-source-names:
    - slave1
    - master

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

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