springboot结合sharding-jdbc4.0版本实现读写分离(整合druid+mybatis)

 

sharding-jdbc具体使用方式

先在pom.xml中引入依赖

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

这里用的是sharding-jdbc-spring-boot-starter
需要注意的是,此时druid不能用spring-boot-starter版本的,需要用正常的包:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.22</version>
</dependency>

不然启动会报错找不到url

配置文件

spring:
  
  shardingsphere:
    datasource:
      names: master,slave0
      master:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/test
        username: root
        password: 123456

        #connection-init-sqls: set names utf8mb4;
        #initial-size: 10
        #max-active: 100
        #min-idle: 10
        #max-wait: 60000
        #pool-prepared-statements: true
        #max-pool-prepared-statement-per-connection-size: 20
        #time-between-eviction-runs-millis: 60000
        #min-evictable-idle-time-millis: 300000
        #test-while-idle: true
        #test-on-borrow: false
        #test-on-return: false

        #filter:
          #stat:
            #log-slow-sql: true
            #slow-sql-millis: 1000
            #merge-sql: false

          #slf4j:
            #enabled: true
            #statement-executable-sql-log-enable: true
            #statement-log-enabled: false
      slave0:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/test
        username: root
        password: 123456

        #connection-init-sqls: set names utf8mb4;
        #initial-size: 10
        #max-active: 100
        #min-idle: 10
        #max-wait: 60000
        #pool-prepared-statements: true
        #max-pool-prepared-statement-per-connection-size: 20
        #time-between-eviction-runs-millis: 60000
        #min-evictable-idle-time-millis: 300000
        #test-while-idle: true
        #test-on-borrow: false
        #test-on-return: false

        #filter:
          #stat:
            #log-slow-sql: true
            #slow-sql-millis: 1000
            #merge-sql: false

          #slf4j:
            #enabled: true
            #statement-executable-sql-log-enable: true
            #statement-log-enabled: false

    masterslave:
      load-balance-algorithm-type: round_robin
      name: ms
      master-data-source-name: master
      slave-data-source-names: slave0,slave1

    props:
      sql:
        show: true

版权声明:本文为qq_41816640原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。