1. mysql配置:

application-dubboService = spring-csc-campaign-agent-context.xml
kafka.topics.redisAccessLog = CDC_%s_REDIS_ACCESSLOG_CHG sharding.jdbc.datasource.sharding0.bu-id = 1-4HK8TL
sharding.jdbc.datasource.sharding1.bu-id = 1-4PH8TL
sharding.jdbc.datasource.sharding2.bu-id = 1-30TKK9
sharding.jdbc.datasource.sharding3.bu-id = 1-31DRXE5
sharding.jdbc.datasource.sharding4.bu-id = 1-30TKP7
sharding.jdbc.datasource.sharding5.bu-id = 1-30TKPA
sharding.jdbc.datasource.sharding6.bu-id = 1-30TKKQ
sharding.jdbc.datasource.sharding7.bu-id = 1-ASBI4Z spring.shardingsphere.datasource.names = sharding0,sharding1,sharding2,sharding3,sharding4,sharding5,sharding6,sharding7 #sit--CSC_MBHK_MEMBER spring.shardingsphere.datasource.sharding0.url = jdbc:mysql://10.95.35.226:3306/CSC_MBHK_CAMPAIGN?useUnicode=true&characterEncoding=utf-8&useCursorFetch=true&defaultFetchSize=100&allowMultiQueries=true
spring.shardingsphere.datasource.sharding0.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding0.username = cdcmbsit
spring.shardingsphere.datasource.sharding0.driverClassName = com.mysql.jdbc.Driver
spring.shardingsphere.datasource.sharding0.password = ENC(ZIeuW2I1txJcMPcxG4IjEPOS6uTzm9/O) spring.shardingsphere.datasource.sharding1.url = jdbc:mysql://10.95.35.226:3306/CDC_WTCPH_CAMPAIGN?useUnicode=true&characterEncoding=utf-8&useCursorFetch=true&defaultFetchSize=100&allowMultiQueries=true
spring.shardingsphere.datasource.sharding1.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding1.username = cdcmbphsit
spring.shardingsphere.datasource.sharding1.driverClassName = com.mysql.jdbc.Driver
spring.shardingsphere.datasource.sharding1.password = ENC(ZB1I2sYxgaasEeaccl7Ys/9JuydUdbSP) spring.shardingsphere.datasource.sharding2.url = jdbc:mysql://10.95.35.27:3306/CDC_WTCMY_CAMPAIGN?useUnicode=true&characterEncoding=utf-8&useCursorFetch=true&defaultFetchSize=100&allowMultiQueries=true
spring.shardingsphere.datasource.sharding2.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding2.username = u_cdc_wtcmy_app
spring.shardingsphere.datasource.sharding2.driverClassName = com.mysql.jdbc.Driver
spring.shardingsphere.datasource.sharding2.password = Asia4gCSuL&DA spring.shardingsphere.datasource.sharding3.url = jdbc:mysql://10.95.35.27:3306/CDC_WTCID_CAMPAIGN?useUnicode=true&characterEncoding=utf-8&useCursorFetch=true&defaultFetchSize=100&allowMultiQueries=true
spring.shardingsphere.datasource.sharding3.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding3.username = u_cdc_wtcid_app
spring.shardingsphere.datasource.sharding3.driverClassName = com.mysql.jdbc.Driver
spring.shardingsphere.datasource.sharding3.password = Asia4gCSuL&DA spring.shardingsphere.datasource.sharding4.url = jdbc:mysql://10.95.35.27:3306/CDC_WTCSG_CAMPAIGN?useUnicode=true&characterEncoding=utf-8&useCursorFetch=true&defaultFetchSize=100&allowMultiQueries=true
spring.shardingsphere.datasource.sharding4.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding4.username = u_cdc_wtcsg_app
spring.shardingsphere.datasource.sharding4.driverClassName = com.mysql.jdbc.Driver
spring.shardingsphere.datasource.sharding4.password = Asia4gCSuL&DA spring.shardingsphere.datasource.sharding5.url = jdbc:mysql://10.95.35.27:3306/CDC_WTCTH_CAMPAIGN?useUnicode=true&characterEncoding=utf-8&useCursorFetch=true&defaultFetchSize=100&allowMultiQueries=true
spring.shardingsphere.datasource.sharding5.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding5.username = u_cdc_wtcth_app
spring.shardingsphere.datasource.sharding5.driverClassName = com.mysql.jdbc.Driver
spring.shardingsphere.datasource.sharding5.password = Asia4gCSuL&DA spring.shardingsphere.datasource.sharding6.url = jdbc:mysql://10.95.35.27:3306/CDC_WTCTW_CAMPAIGN?useUnicode=true&characterEncoding=utf-8&useCursorFetch=true&defaultFetchSize=100&allowMultiQueries=true
spring.shardingsphere.datasource.sharding6.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding6.username = u_cdc_wtctw_app
spring.shardingsphere.datasource.sharding6.driverClassName = com.mysql.jdbc.Driver
spring.shardingsphere.datasource.sharding6.password = Asia4gCSuL&DA spring.shardingsphere.datasource.sharding7.url = jdbc:mysql://10.95.35.27:3306/CDC_WWHK_CAMPAIGN?useUnicode=true&characterEncoding=utf-8&useCursorFetch=true&defaultFetchSize=100&allowMultiQueries=true
spring.shardingsphere.datasource.sharding7.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding7.username = u_cdc_wwhk_app
spring.shardingsphere.datasource.sharding7.driverClassName = com.mysql.jdbc.Driver
spring.shardingsphere.datasource.sharding7.password = Asia4gCSuL&DA # 是否开启分表开关,生产要关闭false
sharding.table.switch = true # sharding hint, logic tables config
# notes: 需要配置所有的的逻辑表,不然就不能分库
sharding.hint.logic.tables= CUSTOMER_CAMPAIGN_TREATMENT,CUSTOMER_LOY_MKTG_OFFER,CUSTOMER_POS_SEG_LST,CUSTOMER_CAMPAIGN_CONTACT,CUSTOMER_LOY_CAM_SEG,CUSTOMER_CAMP_LD_WAVE,CUSTOMER_CAMPAIGN,CUSTOMER_CAMPAIGN_DCP,CUSTOMER_CAMPAIGN_GOAL,CUSTOMER_CAMPAIGN_OFFER,CUSTOMER_CAMPAIGN_OPTY,CUSTOMER_COMM_DTL,CUSTOMER_COMMUNICATION,CUSTOMER_MARKETING_OFFER,CUSTOMER_MARKETING_OFFER_PROD,CUSTOMER_MKOFFR_PROMO,CUSTOMER_POSTN,CUSTOMER_LOY_ATTRDEFN # 默认的hint分库算法
spring.shardingsphere.rules.sharding.default-database-strategy.hint.shardingalgorithm-name=byBuHintAlgorithm spring.shardingsphere.props.sql-show=true
# 配置分库的表,因为我们所有的表都需要分库,这里需要配置所有的表 spring.shardingsphere.rules.sharding.tables.CUSTOMER_CAMPAIGN_TREATMENT.actual-data-nodes = sharding$->{0..7}.CUSTOMER_CAMPAIGN_TREATMENT
spring.shardingsphere.rules.sharding.tables.CUSTOMER_LOY_MKTG_OFFER.actual-data-nodes = sharding$->{0..7}.CUSTOMER_LOY_MKTG_OFFER
spring.shardingsphere.rules.sharding.tables.CUSTOMER_POS_SEG_LST.actual-data-nodes = sharding$->{0..7}.CUSTOMER_POS_SEG_LST
spring.shardingsphere.rules.sharding.tables.CUSTOMER_CAMP_LD_WAVE.actual-data-nodes = sharding$->{0..7}.CUSTOMER_CAMP_LD_WAVE
spring.shardingsphere.rules.sharding.tables.CUSTOMER_CAMPAIGN.actual-data-nodes = sharding$->{0..7}.CUSTOMER_CAMPAIGN
spring.shardingsphere.rules.sharding.tables.CUSTOMER_CAMPAIGN_DCP.actual-data-nodes = sharding$->{0..7}.CUSTOMER_CAMPAIGN_DCP
spring.shardingsphere.rules.sharding.tables.CUSTOMER_CAMPAIGN_GOAL.actual-data-nodes = sharding$->{0..7}.CUSTOMER_CAMPAIGN_GOAL
spring.shardingsphere.rules.sharding.tables.CUSTOMER_CAMPAIGN_OFFER.actual-data-nodes = sharding$->{0..7}.CUSTOMER_CAMPAIGN_OFFER
spring.shardingsphere.rules.sharding.tables.CUSTOMER_CAMPAIGN_OPTY.actual-data-nodes = sharding$->{0..7}.CUSTOMER_CAMPAIGN_OPTY
spring.shardingsphere.rules.sharding.tables.CUSTOMER_COMM_DTL.actual-data-nodes = sharding$->{0..7}.CUSTOMER_COMM_DTL
spring.shardingsphere.rules.sharding.tables.CUSTOMER_COMMUNICATION.actual-data-nodes = sharding$->{0..7}.CUSTOMER_COMMUNICATION
spring.shardingsphere.rules.sharding.tables.CUSTOMER_MARKETING_OFFER.actual-data-nodes = sharding$->{0..7}.CUSTOMER_MARKETING_OFFER
spring.shardingsphere.rules.sharding.tables.CUSTOMER_MARKETING_OFFER_PROD.actual-data-nodes = sharding$->{0..7}.CUSTOMER_MARKETING_OFFER_PROD
spring.shardingsphere.rules.sharding.tables.CUSTOMER_MKOFFR_PROMO.actual-data-nodes = sharding$->{0..7}.CUSTOMER_MKOFFR_PROMO
spring.shardingsphere.rules.sharding.tables.CUSTOMER_POSTN.actual-data-nodes = sharding$->{0..7}.CUSTOMER_POSTN
spring.shardingsphere.rules.sharding.tables.CUSTOMER_LOY_ATTRDEFN.actual-data-nodes = sharding$->{0..7}.CUSTOMER_LOY_ATTRDEFN #分表配置
spring.shardingsphere.rules.sharding.tables.CUSTOMER_CAMPAIGN_CONTACT.logic-table=CUSTOMER_CAMPAIGN_CONTACT
spring.shardingsphere.rules.sharding.tables.CUSTOMER_CAMPAIGN_CONTACT.actual-data-nodes = sharding$->{0..1}.CUSTOMER_CAMPAIGN_CONTACT,sharding$->{2..7}.CUSTOMER_CAMPAIGN_CONTACT_$->{0..63},sharding$->{2..7}.CUSTOMER_CAMPAIGN_CONTACT spring.shardingsphere.rules.sharding.tables.CUSTOMER_CAMPAIGN_CONTACT.table-strategy.standard.sharding-column=CON_PER_ID
spring.shardingsphere.rules.sharding.tables.CUSTOMER_CAMPAIGN_CONTACT.table-strategy.standard.sharding-algorithm-name=hashModShardingAlgorithm spring.shardingsphere.rules.sharding.tables.CUSTOMER_LOY_CAM_SEG.logic-table=CUSTOMER_LOY_CAM_SEG
spring.shardingsphere.rules.sharding.tables.CUSTOMER_LOY_CAM_SEG.actual-data-nodes = sharding$->{0..1}.CUSTOMER_LOY_CAM_SEG,sharding$->{2..7}.CUSTOMER_LOY_CAM_SEG_$->{0..63},sharding$->{2..7}.CUSTOMER_LOY_CAM_SEG
spring.shardingsphere.rules.sharding.tables.CUSTOMER_LOY_CAM_SEG.table-strategy.standard.sharding-column=CONTACT_ID
spring.shardingsphere.rules.sharding.tables.CUSTOMER_LOY_CAM_SEG.table-strategy.standard.sharding-algorithm-name=hashModShardingAlgorithm # 分片算法配置
spring.shardingsphere.rules.sharding.sharding-algorithms.byBuHintAlgorithm.type= CLASS_BASED
spring.shardingsphere.rules.sharding.sharding-algorithms.byBuHintAlgorithm.props.strategy= HINT
spring.shardingsphere.rules.sharding.sharding-algorithms.byBuHintAlgorithm.props.algorithmClassName= com.aswatson.csc.campaign.conf.HintAlgorithm
spring.shardingsphere.rules.sharding.sharding-algorithms.byBuHintAlgorithm.props.buDbMapping = {"1-4HK8TL":"sharding0","1-4PH8TL":"sharding1","1-30TKK9":"sharding2","1-31DRXE5":"sharding3","1-30TKP7":"sharding4","1-30TKPA":"sharding5","1-30TKKQ":"sharding6","1-ASBI4Z":"sharding7"} # 分片算法配置
spring.shardingsphere.rules.sharding.sharding-algorithms.hashModShardingAlgorithm.type= CLASS_BASED
spring.shardingsphere.rules.sharding.sharding-algorithms.hashModShardingAlgorithm.props.strategy= STANDARD
spring.shardingsphere.rules.sharding.sharding-algorithms.hashModShardingAlgorithm.props.algorithmClassName= com.aswatson.csc.campaign.common.sharding.HashModShardingAlgorithm spring.main.allow-bean-definition-overriding = true

2.  分库的doSharding配置类:

package com.aswatson.csc.campaign.conf;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.Properties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingAlgorithm;
import org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingValue;
import org.springframework.context.annotation.Configuration; import java.util.Collection;
import java.util.Collections;
import java.util.Map; @Slf4j
@Configuration
public class HintAlgorithm implements HintShardingAlgorithm{ private Properties props;
private static Map shardingBuMapping = Collections.emptyMap(); public static void setShardingBuMapping(Map<String, String> shardingBuMapping) {
HintAlgorithm.shardingBuMapping = shardingBuMapping;
} @Override
public Collection<String> doSharding(Collection collection, HintShardingValue hintShardingValue) {
for (Object value : hintShardingValue.getValues()) {
String availableTargetName = String.valueOf(shardingBuMapping.get(value.toString()));
if (StringUtils.isNotEmpty(availableTargetName)) {
return Collections.singletonList(availableTargetName);
}
}
log.error("unknown sharding value and datasource mapping,sharding value={},all datasource mapping={}",
hintShardingValue, shardingBuMapping, new IllegalArgumentException());
return Collections.emptyList(); } @Override
public Properties getProps() {
return props;
} @Override public void init(Properties properties) {
this.props = properties;
initShardingBuMapping();
} private void initShardingBuMapping() {
String buDbMapping = props.getProperty("buDbMapping", "");
if (StringUtils.isEmpty(buDbMapping)) {
log.error("initShardingBuMapping fail because apollo config, buDbMapping not exist={}", buDbMapping,
new IllegalArgumentException());
}
JSONObject jsonObject = JSON.parseObject(buDbMapping);
shardingBuMapping = jsonObject.getInnerMap();
} }

3.  分表的doSharding配置类:

package com.aswatson.csc.campaign.common.sharding;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService; import java.util.Arrays;
import java.util.Collection;
import java.util.Properties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shardingsphere.infra.hint.HintManager;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm; /**
* @Author Tim
* @Date 2022/6/30 11:52
*/
@Slf4j
public class HashModShardingAlgorithm implements StandardShardingAlgorithm { // 通过Apollo client获取是否需要分表开关,不需要分表,通过availableTargetNames 截取 "_num"前的表
public static String shardingTableSwitch = "sharding.table.switch";
public static String nameSpace = "cdc-common"; @Override
public String doSharding(Collection availableTargetNames, PreciseShardingValue shardingValue) {
Comparable value = shardingValue.getValue();
int hashValue = value.hashCode();
int availableTargetNamesLen = availableTargetNames.size();
// 因为我们配置了是否需要分表的开关,不分表就返回逻辑表,如果availableTargetNames不包含这个逻辑表就会抛异常,
// 因此我们配置这个分表的实际表,最后加了这个逻辑表
if (availableTargetNamesLen > 1) {
availableTargetNamesLen -= 1;
}
int targetNamesIdx = Math.abs(hashValue) % (availableTargetNamesLen);
Object[] availableTargetNamesArr = availableTargetNames.toArray(new Object[0]);
String tableName = String.valueOf(availableTargetNamesArr[targetNamesIdx]);
log.info("doSharding-tableName: {}", tableName);
return getActualTable(tableName);
} private String getActualTable(String tableName) {
Config config = ConfigService.getConfig(nameSpace);
// String tableShardingSwitchBuIds = config.getProperty(shardingTableSwitchBuIds, "");
boolean tableShardingSwitch = Boolean.parseBoolean(config.getProperty(shardingTableSwitch, "false"));
// String[] tableShardingSwitchBuIdArrays = StringUtils.split(tableShardingSwitchBuIds, ",");
if (tableShardingSwitch) {
return tableName;
}
String actualTable = tableName.replaceAll("\\d+","");
log.info("getActualTable-tableName: {}, reason: tableShardingSwitch|{}",
actualTable, tableShardingSwitch);
return actualTable.substring(0, actualTable.length()-1); } // todo
@Override
public Collection<String> doSharding(Collection collection, RangeShardingValue rangeShardingValue) {
return null;
} @Override
public Properties getProps() {
return null;
} @Override
public void init(Properties properties) { }
}

shardingsphere 5.1.1 分库分表的更多相关文章

  1. 分库分表(2) --- ShardingSphere(理论)

    ShardingSphere---理论 ShardingSphere在中小企业需要分库分表的时候用的会比较多,因为它维护成本低,不需要额外增派人手;而且目前社区也还一直在开发和维护,还算是比较活跃. ...

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

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

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

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

  4. 分库分表(6)--- SpringBoot+ShardingSphere实现分表+ 读写分离

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

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

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

  6. mysql 分库分表 ~ ShardingSphere生态圈

    一  简介   Apache ShardingSphere是一款开源的分布式数据库中间件组成的生态圈二 成员包含   Sharding-JDBC是一款轻量级的Java框架,在JDBC层提供上述核心功能 ...

  7. 分库分表利器——sharding-sphere

    背景 得不到的东西让你彻夜难眠,没有尝试过的技术让我跃跃欲试. 本着杀鸡焉用牛刀的准则,我们倡导够用就行,不跟风,不盲从. 所以,结果就是我们一直没有真正使用分库分表.曾经好几次,感觉没有分库分表(起 ...

  8. 分库分表(3) ---SpringBoot + ShardingSphere 实现读写分离

    分库分表(3)---ShardingSphere实现读写分离 有关ShardingSphere概念前面写了两篇博客: 1.分库分表(1) --- 理论 2. 分库分表(2) --- ShardingS ...

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

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

  10. 分库分表框架ShardingSphere入门学习1

    背景 传统的将数据集中存储至单一数据节点的解决方案,在性能.可用性和运维成本这三方面已经难于满足互联网的海量数据场景. 从性能方面来说,由于关系型数据库大多采用 B+ 树类型的索引,在数据量超过阈值的 ...

随机推荐

  1. 【Android】实现连接SQLite并尝试进行增删改查

  2. Android学习day04【Button】

    出现的一些小状况: 小状况 报错,代码显示运行成功 但是无法在模拟机上显示 原因是没有在包含应有id 其二是关于设置背景颜色中 关于background与backgroundTint的区别 //这是b ...

  3. 什么是oa软件?oa软件能做什么?

    什么是OA软件? OA软件是办公自动化软件(Office Automation Software)的简称,是一种通过计算机技术,对办公业务流程进行管理和自动化处理的软件系统.它主要用于协调和管理企业内 ...

  4. 如何在Web应用中添加一个JavaScript Excel查看器

    前言 在现代的Web应用开发中,Excel文件的处理和展示是一项常见的需求.为了提供更好的用户体验和功能,经常需要在Web应用中添加一个JavaScript Excel查看器,小编今天将为大家展示如何 ...

  5. Python 潮流周刊第 30 期(摘要)

    本周刊由 Python猫 出品,精心筛选国内外的 250+ 信息源,为你挑选最值得分享的文章.教程.开源项目.软件工具.播客和视频.热门话题等内容.愿景:帮助所有读者精进 Python 技术,并增长职 ...

  6. 创建一个循环写入数据有事务提交的oracle函数示例

    /*创建函数*/create or replace function fnc_testtempInfo(startDate IN varchar2, endDate in varchar2) retu ...

  7. MySQL InnoDB加锁规则分析

    1.  基础知识回顾 1.索引的有序性,索引本身就是有序的 2.InnoDB中间隙锁的唯一目的是防止其他事务插入间隙.间隙锁可以共存.一个事务取得的间隙锁并不会阻止另一个事务取得同一间隙上的间隙锁.共 ...

  8. 从零玩转Websocket实时通讯服务之前后端分离版本

    前言 公司项目需要用到消息提示,那么WebSocket它来了经过我面向百度的学习,废话不多说直接开干. 后端搭建 一.依赖导入 <dependency> <groupId>or ...

  9. Vulntarget-b-wp

    Vulntarget-b 环境配置 centos7 用户 密码 root root vulntarget root 宝塔Linux面板http://192.168.0.104:8888/045b276 ...

  10. 让 sdk 包静默升级的 SAO 操作,你见过几种?

    拓展阅读 让 sdk 包静默升级的 SAO 操作,你见过几种? 业务背景 有时候为业务方提供了基础的 sdk 包,为了保证稳定性,一般都是 release 包. 但是每一次升级都非常痛苦,也不可能写一 ...