分库分表之基于Shardingjdbc+docker+mysql主从架构实现读写分离(二)

        说明:如果实现了docker部署mysql并完成主从复制的话再继续,本篇文章主要说明springboot配置实现Shardingjdbc进行读写分离操作。

如果没实现docker部署mysql实现主从架构的话点击我

        Shardingjdbc配置介绍(版本:5.3.2)

application.yml配置设置连接池全局属性

spring:
  datasource:
    driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
    url: jdbc:shardingsphere:classpath:qiyu-db-sharding.yaml
    hikari:
      pool-name: qiyu-user-pool
      minimum-idle: 15
      idle-timeout: 60000
      maximum-pool-size: 300
      connection-init-sql: select 1
      connection-timeout: 4000
      max-lifetime: 60000

shardingjdbc读写分离配置(qiyu-db-sharding.yaml)

dataSources:
  user_master: ##新表,重建的分表
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    jdbcUrl: jdbc:mysql://192.168.1.128:8808/qiyu_live_user?useUnicode=true&characterEncoding=utf8
    username: root
    password: root

  user_slave0: ##新表,重建的分表
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    jdbcUrl: jdbc:mysql://192.168.1.128:8809/qiyu_live_user?useUnicode=true&characterEncoding=utf8
    username: root
    password: root


rules:
    - !READWRITE_SPLITTING
      dataSources:
        #读写分离配置
        user_ds:
         staticStrategy:
           writeDataSourceName: user_master
           readDataSourceNames:
              - user_slave0
    - !SINGLE
    #  不分表分分库的默认数据源
      defaultDataSource: user_ds
    - !SHARDING
      tables:
        t_user:
          actualDataNodes: user_ds.t_user_${(0..04).collect(){it.toString().padLeft(2,'0')}}
          tableStrategy:
            standard:
              #指定用于分表的列名。
              shardingColumn: user_id
              shardingAlgorithmName: t_user-inline

      #定义分表算法
      shardingAlgorithms:
        t_user-inline:
          type: INLINE
          props:
            #根据"user_id"对5取模的结果将作为分表的后缀,范围为00到04,对应五个表,并在左侧填充0。
            algorithm-expression: t_user_${(user_id % 5).toString().padLeft(2,'0')}
props:
  sql-show: true

同时这个hikari的数据源有点小bug,加个配置类

package com.laoyang.provider.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import javax.sql.DataSource;
import java.sql.Connection;

/**
 * @author:Kevin
 * @create: 2023-07-29 21:30
 * @Description:
 */
@Configuration
public class ShardingjdbcDatasourceAutoConnectionconfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(ShardingjdbcDatasourceAutoConnectionconfig.class);

    @Bean
    public ApplicationRunner runner(DataSource dataSource) {
        return args -> {
            LOGGER.info("dataSource: {}",dataSource);
            //手动触发下连接池的连接创建
            Connection connection = dataSource.getConnection();
        };
    }
}

运行:出现下图说明配置成功。

分库分表之基于Shardingjdbc+docker+mysql主从架构实现读写分离(二)_第1张图片

 

你可能感兴趣的:(docker,mysql,架构)