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

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

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

Shardingjdbc配置介绍(版本:5.3.2)

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

cs 复制代码
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)

cs 复制代码
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,加个配置类

java 复制代码
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();
        };
    }
}

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

相关推荐
csdn_aspnet9 小时前
Docker MySql 5.7、MySql8.x 进阶配置
mysql·docker·容器
怕浪猫10 小时前
全球 3D 网络地球的实时通信节点,来看看这四种交互地球,满足你对物理互联网的所有好奇
面试·架构·github
wflynn12 小时前
GitHub 今日推荐|kiri:clone3 系统调用直达容器,2.5 毫秒启动无 runc 中间层
docker·rust·kubernetes·开源·github·containers
ZhengEnCi13 小时前
项目越写越乱?先别改架构:用一次目录重排 + 一份可复算的体检脚本把乱治住
python·架构·ai编程
方便面不加香菜16 小时前
Docker Volume -- 对照总结及综合实战案例操作演示
运维·docker·容器
wy_hhxx16 小时前
《Agent设计模式 图解可复用智能体架构》上篇内容整理
设计模式·架构·ai agent
城北徐宫17 小时前
MySQL数据类型整理
数据库·mysql
用户76155938363817 小时前
如何绕过 Rust 孤儿规则
架构
HelloWorld00117 小时前
面对不可逆外部支付回调:基于 TCC 与最大努力通知保证分布式资产终态一致性
架构
你三大爷17 小时前
Seata全局锁泄漏:从上游PR-backport到生产零事故发布实战
架构