springboot+mysql+jpa+sharding-jdbc+druid读写分离

介绍:https://github.com/apache/shardingsphere

数据库:主库(写)+2从库(读)

db_write(写数据库master)

db_read1(读数据库slave1)

db_read2(读数据库slave2)

 

pom.xml


    4.0.0
    com.zsk
    springboot-read-write-separate
    0.0.1-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.8.RELEASE
        
    

    
        UTF-8
        UTF-8
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-devtools
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            mysql
            mysql-connector-java
        
        
            org.projectlombok
            lombok
            provided
        
         
            com.alibaba
            druid-spring-boot-starter
            1.1.10
        

        
            io.shardingsphere
            sharding-jdbc-spring-boot-starter
            3.1.0.M1
        
        
        
            io.springfox
            springfox-swagger2
            2.7.0
        
        
            io.springfox
            springfox-swagger-ui
            2.7.0
        
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

application.yml

server:
  port: 8080
  
spring: 
  jpa:
    show-sql: false
    properties:
      hibernate:
        hbm2ddl:
          auto: none #update         
sharding:
  jdbc:
    dataSource:
      names: db-write,db-read1,db-read2
      # 配置主库
      db-write: #org.apache.tomcat.jdbc.pool.DataSource
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/db_write?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
        username: amsuser
        password: ams2018
        #最大连接数
        maxPoolSize: 20
      db-read1: # 配置第一个从库
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/db_read1?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
        username: amsuser
        password: ams2018
        maxPoolSize: 20
      db-read2: # 配置第二个从库
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/db_read2?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
        username: amsuser
        password: ams2018
        maxPoolSize: 20
    config:
      masterslave: # 配置读写分离
        load-balance-algorithm-type: round_robin # 配置从库选择策略random(随机)round_robin(轮循)
        name: db-write-read
        master-data-source-name: db-write
        slave-data-source-names: db-read1,db-read2
    props:
      sql: # 开启SQL显示,默认值: false,注意:仅配置读写分离时不会打印日志
        show: true
          

效果:

post http://127.0.0.1:8080/api/user 数据写入了db_write库

get http://127.0.0.1:8080/api/users 多次访问轮询读取db_read1库,db_read2库的数据

返回数据如下

[{"id":"bc2bc1ce-c278-11ea-8c98-8c1645da95ec","userName":"db_read1","password":"123456","createTime":"2020-07-10T06:43:59.000+0000"}]
[{"id":"bc2bc1ce-c278-11ea-8c98-8c1645da95ec","userName":"db_read2","password":"123456","createTime":"2020-07-10T06:43:59.000+0000"}]

完整代码demo:https://gitee.com/zhangsike/springboot-learn/tree/master/springboot-read-write-separate,注意运行项目之前先执行sql.sql

 

配置mysql从库监听同步主库数据

 

你可能感兴趣的:(springboot)