背景

近期在项目中需要使用多数据源,其中有一些表的数据量比较大,需要对其进行分库分表;而其他数据表数据量比较正常,单表就可以。

项目中可能使用其他组的数据源数据,因此需要多数据源支持。

经过调研多数据源配置比较方便。在该项目中分库分表的策略比较简单,仅根据一个字段分就可以,因此分库分表方案选用比较流行方便易用的 sharding-jdbc

需要实现的目标是 根据学生姓名字段 student_name 进行分表,但是不需要分库。数据表是student_hist0 - student_hist9

引入 sharding-jdbc maven 依赖

<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>4.1.1</version>
</dependency>

数据源配置文件

spring:
application:
name: student-service-provider
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
defaultPropertyInclusion: non_null
deserialization:
FAIL_ON_UNKNOWN_PROPERTIES: false
#对返回的时间进行格式化
datasource:
hikari:
student:
url: jdbc:mysql://127.0.0.1:3306/student_service?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&useTimezone=true&serverTimezone=GMT%2
username: root
password: root123
log1:
url: jdbc:mysql://127.0.0.1:3306/log1?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: root123
log2:
url: jdbc:mysql://127.0.0.1:3306/log2?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: root123

配置多数据源代码

DataSourceProperties 数据源

import com.zaxxer.hikari.HikariDataSource;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration; @Data
@Configuration
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public class DataSourceProperties { private HikariDataSource student;
private HikariDataSource log1;
private HikariDataSource log2; }

DynamicDataSource 动态数据源

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource {

    /*
当前据源名称
*/
private static final ThreadLocal<String> dataSourceContextHolder = new ThreadLocal<>(); /*
设置数据源名称
*/
public static void setDataSourceName(String dataSourceName) {
dataSourceContextHolder.set(dataSourceName);
} /*
获取据源名称
*/
public static String getDataSourceName() {
return dataSourceContextHolder.get();
} /*
清除当数据源名称
*/
public static void clearDataSource() {
dataSourceContextHolder.remove();
} @Override
protected Object determineCurrentLookupKey() {
return getDataSourceName();
}
}

MultiDataSource 多数据源标记

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface MultiDataSource {
String value() default DataSourceConfig.DEFAULT_DATASOURCE_NAME;
}

重点来了,因为是根据表的某一个字段进行分表,该字段是一个字符串类型,因此需要想根据字符串的一致性hash码算出在哪张表上。在sharding-jdbc需要实现 PreciseShardingAlgorithm 类

例如:想要在student.student_hist 表中根据学生姓名进行分表,逻辑表是student_hist,真实表是 student_hist0 - student_hist9

DataSourceConfig.SHARD_MMS_DATASOURCE_TABLE_COUNT=10

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
import java.util.Collection; public class PreciseNodeIdShardingAlgorithm implements PreciseShardingAlgorithm<String> { @Override
public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
for (String tbnm : collection) {
if (tbnm.endsWith("hist" + (getHash(preciseShardingValue.getValue()) % DataSourceConfig.SHARD_MMS_DATASOURCE_TABLE_COUNT))) {
return tbnm;
}
}
throw new UnsupportedOperationException();
} private static int getHash(String str) {
final int p = 16777619;
int hash = (int) 2166136261L;
for (int i = 0; i < str.length(); i++)
hash = (hash ^ str.charAt(i)) * p;
hash += hash << 13;
hash ^= hash >> 7;
hash += hash << 3;
hash ^= hash >> 17;
hash += hash << 5; // 如果算出来的值为负数则取其绝对值
if (hash < 0)
hash = Math.abs(hash);
return hash;
}
}

多数据源装配 DataSourceConfig 。需要指定默认数据源,当不需要使用 分表的表时就使用默认的数据源,否则指定需要分表的数据源。

在配置分表策略时如果不需要分库,可以不进行设置 tableRuleConfiguration.setDatabaseShardingStrategyConfig();

import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.StandardShardingStrategyConfiguration;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager; import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties; @Configuration
public class DataSourceConfig { public static final String DEFAULT_DATASOURCE_NAME = "defaultDataSource";
public static final String MMS_DATASOURCE_NAME = "mmsDatasource";
public static final String SHARD_MMS_DATASOURCE_NAME = "shardMmsDatasource"; public static int SHARD_MMS_DATASOURCE_TABLE_COUNT = 10; @Autowired
private DataSourceProperties properties; @Primary
@Bean(name = "dynamicDataSource")
public DataSource dynamicDataSource() {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
// 默认数据源
dynamicDataSource.setDefaultTargetDataSource(properties.getMms());
// 配置多数据源
Map<Object, Object> dsMap = new HashMap();
dsMap.put(DEFAULT_DATASOURCE_NAME, properties.getStudent());
dsMap.put(MMS_DATASOURCE_NAME, properties.getStudent());
dsMap.put(SHARD_MMS_DATASOURCE_NAME, buildShardDatasources());
dynamicDataSource.setTargetDataSources(dsMap);
return dynamicDataSource;
} public DataSource buildShardDatasources() {
// 配置多数据源
Map<String, DataSource> dsMap = new HashMap();
dsMap.put("shardMms", properties.getMms());
TableRuleConfiguration stuHisTableRuleConfig = new TableRuleConfiguration("student_hist", "shardMms.student_hist${0.." + (SHARD_MMS_DATASOURCE_TABLE_COUNT - 1) + "}");
// tableRuleConfiguration.setDatabaseShardingStrategyConfig();
stuHisTableRuleConfig.setTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("student_name", new PreciseNodeIdShardingAlgorithm())); ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getTableRuleConfigs().add(stuHisTableRuleConfig);
try {
Properties properties = new Properties();
properties.setProperty("sql.show", "true");
return ShardingDataSourceFactory.createDataSource(dsMap, shardingRuleConfig, properties);
} catch (SQLException throwables) {
throwables.printStackTrace();
throw new IllegalArgumentException();
}
} @Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dynamicDataSource());
} }

多数据源切换 DataSourceAspect ,需要使用多数据源切换时,需要在service方法上使用标注方法 MultiDataSource 并指定数据源。

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import java.lang.reflect.Method; @Aspect
@Configuration
@Slf4j
@Order(1)
public class DataSourceAspect { //切入点,service 中所有注解方法
@Pointcut("execution(* com.huitong..service..*.*(..)) && @annotation(com.huitong.app.config.datasource.MultiDataSource)")
public void dataSourceAspect() {
} @Around("dataSourceAspect()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
MultiDataSource ds = method.getAnnotation(MultiDataSource.class);
if (ds != null) {
DynamicDataSource.setDataSourceName(ds.value());
}
try {
return joinPoint.proceed();
} finally {
DynamicDataSource.clearDataSource();
}
}
}

参考文献:

在多数据源中对部分数据表使用shardingsphere进行分库分表的更多相关文章

  1. 分库分表(7)--- SpringBoot+ShardingSphere实现分库分表 + 读写分离

    分库分表(7)--- ShardingSphere实现分库分表+读写分离 有关分库分表前面写了六篇博客: 1.分库分表(1) --- 理论 2.分库分表(2) --- ShardingSphere(理 ...

  2. 分库分表(5) ---SpringBoot + ShardingSphere 实现分库分表

    分库分表(5)--- ShardingSphere实现分库分表 有关分库分表前面写了四篇博客: 1.分库分表(1) --- 理论 2.分库分表(2) --- ShardingSphere(理论) 3. ...

  3. Springboot2.x + ShardingSphere 实现分库分表

    之前一篇文章中我们讲了基于Mysql8的读写分离(文末有链接),这次来说说分库分表的实现过程. 概念解析 垂直分片 按照业务拆分的方式称为垂直分片,又称为纵向拆分,它的核心理念是专库专用. 在拆分之前 ...

  4. 【分库分表】sharding-jdbc实践—分库分表入门

    一.准备工作 1.准备三个数据库:db0.db1.db2 2.每个数据库新建两个订单表:t_order_0.t_order_1 DROP TABLE IF EXISTS `t_order_x`; CR ...

  5. 数据量大了一定要分表,分库分表组件Sharding-JDBC入门与项目实战

    最近项目中不少表的数据量越来越大,并且导致了一些数据库的性能问题.因此想借助一些分库分表的中间件,实现自动化分库分表实现.调研下来,发现Sharding-JDBC目前成熟度最高并且应用最广的Java分 ...

  6. SpringCloud微服务实战——搭建企业级开发框架(二十七):集成多数据源+Seata分布式事务+读写分离+分库分表

    读写分离:为了确保数据库产品的稳定性,很多数据库拥有双机热备功能.也就是,第一台数据库服务器,是对外提供增删改业务的生产服务器:第二台数据库服务器,主要进行读的操作. 目前有多种方式实现读写分离,一种 ...

  7. 【大数据和云计算技术社区】分库分表技术演进&最佳实践笔记

    1.需求背景 移动互联网时代,海量的用户每天产生海量的数量,这些海量数据远不是一张表能Hold住的.比如 用户表:支付宝8亿,微信10亿.CITIC对公140万,对私8700万. 订单表:美团每天几千 ...

  8. CRL快速开发框架系列教程十一(大数据分库分表解决方案)

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

  9. 解读分库分表中间件Sharding-JDBC

    [编者按]数据库分库分表从互联网时代开启至今,一直是热门话题.在NoSQL横行的今天,关系型数据库凭借其稳定.查询灵活.兼容等特性,仍被大多数公司作为首选数据库.因此,合理采用分库分表技术应对海量数据 ...

随机推荐

  1. 在 Golang 中实现一个简单的Http中间件

    本文主要针对Golang的内置库 net/http 做了简单的扩展,通过添加中间件的形式实现了管道(Pipeline)模式,这样的好处是各模块之间是低耦合的,符合单一职责原则,可以很灵活的通过中间件的 ...

  2. Java学习常用链接

    最全的Jenkins插件开发教程 最最最全的Jenkins插件开发教程_邪恶八进制-CSDN博客_jenkins插件开发 代理FQ工具Shadow socks https://www.blog-chi ...

  3. 一文彻底弄懂cookie、session、token

    前言 作为一个JAVA开发,之前有好几次出去面试,面试官都问我,JAVAWeb掌握的怎么样,我当时就不知道怎么回答,Web,日常开发中用的是什么?今天我们来说说JAVAWeb最应该掌握的三个内容. 发 ...

  4. shell的图形排列

    目录 一.矩形 二.直角三角形 三.倒直角三角形 四.靠右的直角三角形 五.等腰三角形 六.平行四边形 七.等腰梯形 八.菱形 九.可变动菱形 一.矩形 二.直角三角形 三.倒直角三角形 四.靠右的直 ...

  5. postman之get请求

    get请求:

  6. Android NDK 直播推流与引流

    本篇介绍一下直播技术中推流与引流的简单实现. 1.流媒体服务器测试 首先利用快直播 app (其他支持 RTMP 推流与引流的 app 亦可)和 ffplay.exe 对流媒体服务器进行测试. 快直播 ...

  7. NodeJS 中的事件循环,读了这篇就全懂了

    事件循环是 NodeJS 处理非阻塞 I/O 操作的和核心机制.NodeJS 的事件循环脱胎于 libuv 的事件循环,因此,要搞清楚 NodeJS 的事件循环,还需要先了解 libuv 的事件循环是 ...

  8. Linux线程属性总结(一)

    线程属性标识符:pthread_attr_t 包含在 pthread.h 头文件中. [c] view plaincopy //线程属性结构如下: typedef struct { int       ...

  9. mac 软件意外退出

    大概率的原因是软件签名问题. 先安装 xcode xcode-select --install 然后签名 sudo codesign --force --deep --sign - 文件位置(直接将应 ...

  10. DVWA靶场之CSRF(跨站请求伪造)通关

    Low: 服务器就看了password_new与password_conf是否相同,没有其他的验证 重新构造一个html页面,(自己假装自己是受害者,ip是靶场ip非本地ip) 1 <img s ...