Springboot+Shardingsphere实现读写分离

Apache ShardingSphere是一款开源的分布式数据库中间件组成的生态圈。它由Sharding-JDBC、Sharding-Proxy和Sharding-Sidecar(规划中)这3款相互独立的产品组成。 他们均提供标准化的数据分片、分布式事务和数据库治理功能,可适用于如Java同构、异构语言、容器、云原生等各种多样化的应用场景。

pom,注意springboot版本



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.4.2
         
    
    com.dcqc
    shardingspere
    0.0.1-SNAPSHOT
    shardingspere
    shardingspere
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.apache.shardingsphere
            sharding-jdbc-spring-boot-starter
            4.1.1
        
        
            org.apache.shardingsphere
            sharding-jdbc-spring-namespace
            4.0.0-RC1
        

        
            org.projectlombok
            lombok
            true
        
        
            io.springfox
            springfox-swagger-ui
            2.8.0
        
        
            com.alibaba
            fastjson
            1.2.23
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            mysql
            mysql-connector-java
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.1
        
    

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


application.yml

spring:
  shardingsphere:
    datasource:
      #配置真实数据源
      names: ds0,ds1,ds2
      ds0:
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:3306/quarant-db?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=UTC
        username: root
        password: admin
        type: com.zaxxer.hikari.HikariDataSource
        initialSize: 5
        minIdle: 10
        maxActive: 50
        maxWait: 60000
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        maxEvictableIdleTimeMillis: 900000
        validationQuery: SELECT 1 FROM DUAL
      ds1:
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://42.109.52.255:3307/quarant_db?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=UTC
        username: root
        password: admin
        type: com.zaxxer.hikari.HikariDataSource
        initialSize: 5
        minIdle: 10
        maxActive: 50
        maxWait: 60000
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        maxEvictableIdleTimeMillis: 900000
        validationQuery: SELECT 1 FROM DUAL
      ds2:
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://172.18.11.9:3316/quarant_db?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=UTC
        username: root
        password: Dcqc@1234
        type: com.zaxxer.hikari.HikariDataSource
        initialSize: 5
        minIdle: 10
        maxActive: 50
        maxWait: 60000
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        maxEvictableIdleTimeMillis: 900000
        validationQuery: SELECT 1 FROM DUAL
    sharding:
      master-slave-rules:
        master-test0:                                                          # 哪一个主节点
          master-datasource-name: ds0                                          # 指定主节点名字
          slave-data-source-names: ds0,ds1,ds2                                # 指定读节点名字,多个读节点用逗号分开
        # 配置从库选择策略,提供轮询与随机,这里选择用轮询
    masterslave:
      load-balance-algorithm-type: round_robin
    props:
      sql:
        show: true   # 日志显示SQL

server:
  port: 9092

controller

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dcqc.shardingspere.entity.QuarantineInfo;
import com.dcqc.shardingspere.service.QuarantineInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class QuarantController {
    private final QuarantineInfoService quarantineInfoService;

    @GetMapping("/update")
    public void update() {
        QuarantineInfo quarantineInfo = new QuarantineInfo();
        quarantineInfo.setName("石劲鹏");
        Boolean bb = quarantineInfoService.update(quarantineInfo, new LambdaQueryWrapper()
                .eq(QuarantineInfo::getId, 53));
    }

    @GetMapping("/query")
    public String query() {
        QuarantineInfo quarantineInfo = quarantineInfoService.getById(53);
        return JSON.toJSONString(quarantineInfo);
    }
}

你可能感兴趣的:(工具,spring,boot,java,数据库)