springboot2.0和springboot2.1 +sharding-jdbc 轻量级读写分离配置,接以前的版本和源码

前置读写分离配置 https://blog.csdn.net/q18771811872/article/details/88636181

springboot项目创建教程 https://blog.csdn.net/q18771811872/article/details/88126835

接创建教程里面的demo项目 ,无缝衔接

当然sharding不知读写分离这么简单

直接上代码

pom.xml

<dependency>
            <groupId>io.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>3.1.0.M1</version>
        </dependency>

application.yml

sharding:
  jdbc:
    dataSource:
      names: db-test0,db-test1,db-test2
      # 配置主库
      db-test0: #org.apache.tomcat.jdbc.pool.DataSource
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/db-test?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
        username: root
        password: 123456
        #最大连接数
        maxPoolSize: 20
      db-test1: # 配置第一个从库
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/db-test1?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
        username: root
        password: 123456
        maxPoolSize: 20
      db-test2: # 配置第二个从库
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/db-test2?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
        username: root
        password: 123456
        maxPoolSize: 20
    config:
      masterslave: # 配置读写分离
        load-balance-algorithm-type: round_robin # 配置从库选择策略,提供轮询与随机,这里选择用轮询//random 随机 //round_robin 轮询
        name: db1s2
        master-data-source-name: db-test0
        slave-data-source-names: db-test1,db-test2
    props:
      sql: # 开启SQL显示,默认值: false,注意:仅配置读写分离时不会打印日志!!!
        show: true

一般配置完  maven包后,springboot 的默认数据库连接就会优选选择sharding的了 

springboot2.1说明:

2.1比较严格,所以呢,需要在配置文件添加一个属性

spring:
  main:
    allow-bean-definition-overriding: true

就可以正常启动了 

 

配置好后,直接重启 

直接贴了源码, 就不贴效果图了

 

前置读写分离配置 https://blog.csdn.net/q18771811872/article/details/88636181

springboot项目创建教程 https://blog.csdn.net/q18771811872/article/details/88126835

源码地址   https://gitee.com/wcccccccc/demo 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

筑基始,仙魔终

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值