Sharding-Jdbc 配置主从读写分离模式

Sharding-Jdbc 配置主从模式

我们项目的读写分离的主从模式项目中一般可以分为多种方式来实现,可以手动在代码 Java 中配置多种数据源来实现读写分离的模式,我们也可以通过第三方的框架来实现读写分离例如我们的 Sharding-jdbc,也可以是我们的MyCat的来实现读写分离。

    Java 代码的方式配置多种数据源 Sharding-Jdbc MyCat

今天我们就来讲解一下如何使用 Sharding-jdbc 来实现读写分离。

使用 YMAL 方式来实现配置。

配置 master 数据源和 slave 数据源。我们这次是的方案是一主多从的方式来实现读写分离,mysql 的主从搭建我们这次就不说了。我们这次就是主要来实现 sharding-jdbc 的方式来实现读写分离

Sharding-jdbc 读写分离配置

我们首先要引入 MAVEN 依赖

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

我们这次使用的版本是比较新的版本。

配置数据源以及数据源的名字

spring:
  shardingsphere:
  	# 内存模式
    mode:
      type: Memory
    # 数据源配置
    datasource:
      names: master,slave
      # 主数据源
      master:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.jc.mysql.Driver
        jdbc-url: jdbc:mysql://localhost:3306/shardingjdbc?allowPublicKeyRetrieval=true
        username: root
        password: 123456
      # 从机数据源
      slave:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.jc.mysql.Driver
        jdbc-url: jdbc:mysql://localhost:3306/shardingjdbc?allowPublicKeyRetrieval=true
        username: root
        password: 123456

主要是配置数据源的链接地址和账户以及密码。

读写分离的策略

spring:
  shardingsphere:
    rules:
      readwrite-splitting:
        data-sources:
          mydatasource:
          	# 类型是静态还是动态
            type: Static
            props:
              # 写入数据
              write-data-source-name: master
              # 读数据
              read-data-source-names: slave
            # 使用的逻辑策略
            load-balancer-name: round-alg
        # 负载均衡策略
        load-balancers:
        	# 轮询
            round-alg:
              type: ROUND_ROBIN

这里的是配置写数据和读数据使用的数据源

完整配置

spring:
  shardingsphere:
    mode:
      type: Memory
    # 数据源配置
    datasource:
      names: master,slave
      # 主数据源
      master:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.jc.mysql.Driver
        jdbc-url: jdbc:mysql://localhost:3306/shardingjdbc?allowPublicKeyRetrieval=true
        username: root
        password: 123456
      # 从机数据源
      slave:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.jc.mysql.Driver
        jdbc-url: jdbc:mysql://localhost:3306/shardingjdbc?allowPublicKeyRetrieval=true
        username: root
        password: 123456
    rules:
      readwrite-splitting:
        data-sources:
          mydatasource:
          	# 类型是静态还是动态
            type: Static
            props:
              write-data-source-name: master
              read-data-source-names: slave
            # 使用的逻辑策略
            load-balancer-name: round-alg
        # 负载均衡策略
        load-balancers:
        	# 轮询
            round-alg:
              type: ROUND_ROBIN
            # 随机
            random-alg:
              type: RANDOM
            # 权重,我们在配置权重的时候必须配置各个 slave 的权重值,值的数据类型为 double
            weight-alg:
              type: WEIGHT
              props:
                slave: 1.0

    props:
      show-sql: true

结束

中间我在启动的时候遇到一个 mysql 的连接异常

Public Key Retrieval is not allowed

这种情况可以配置参数的时候设置连接参数 ?allowPublicKeyRetrieval=true

经验分享 程序员 微信小程序 职场和发展