shardingsphere做了读写分离做了主从配置脱敏无效分析

news/2024/5/7 5:57:45/文章来源:https://blog.csdn.net/QQ70945934/article/details/126910290

软件版本信息

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

问题描述

整合SpringBoot框架的时候,做了主从读写分离,脱敏配置一直无效。其实我们项目是先使用了读写分离,还使用了Mybatis-plus,所以影响因素很多的。发现问题后,自己先搞了一个简单的demo,去掉Mybatis-plus和读写分离,发现脱敏是有用的。加上读写分离就失效了。所以应该是读写分离脱敏配置失效导致的。最后分析源码,找原因。

原因分析

通过源码分析,知道shardingsphere在注入配置信息的核心类是org.apache.shardingsphere.shardingjdbc.spring.boot.SpringBootConfiguration
代码如下:

@Configuration
@ComponentScan("org.apache.shardingsphere.spring.boot.converter")
@EnableConfigurationProperties({SpringBootShardingRuleConfigurationProperties.class,SpringBootMasterSlaveRuleConfigurationProperties.class, SpringBootEncryptRuleConfigurationProperties.class,SpringBootPropertiesConfigurationProperties.class, SpringBootShadowRuleConfigurationProperties.class})
@ConditionalOnProperty(prefix = "spring.shardingsphere", name = "enabled", havingValue = "true", matchIfMissing = true)
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@RequiredArgsConstructor
public class SpringBootConfiguration implements EnvironmentAware {private final SpringBootShardingRuleConfigurationProperties shardingRule;private final SpringBootMasterSlaveRuleConfigurationProperties masterSlaveRule;private final SpringBootEncryptRuleConfigurationProperties encryptRule;private final SpringBootShadowRuleConfigurationProperties shadowRule;private final SpringBootPropertiesConfigurationProperties props;private final Map<String, DataSource> dataSourceMap = new LinkedHashMap<>();private final String jndiName = "jndi-name";/*** Get sharding data source bean.** @return data source bean* @throws SQLException SQL exception*/@Bean@Conditional(ShardingRuleCondition.class)public DataSource shardingDataSource() throws SQLException {return ShardingDataSourceFactory.createDataSource(dataSourceMap, new ShardingRuleConfigurationYamlSwapper().swap(shardingRule), props.getProps());}/*** Get master-slave data source bean.** @return data source bean* @throws SQLException SQL exception*/@Bean@Conditional(MasterSlaveRuleCondition.class)public DataSource masterSlaveDataSource() throws SQLException {return MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, new MasterSlaveRuleConfigurationYamlSwapper().swap(masterSlaveRule), props.getProps());}/*** Get encrypt data source bean.** @return data source bean* @throws SQLException SQL exception*/@Bean@Conditional(EncryptRuleCondition.class)public DataSource encryptDataSource() throws SQLException {return EncryptDataSourceFactory.createDataSource(dataSourceMap.values().iterator().next(), new EncryptRuleConfigurationYamlSwapper().swap(encryptRule), props.getProps());}/*** Get shadow data source bean.** @return data source bean* @throws SQLException SQL exception*/@Bean@Conditional(ShadowRuleCondition.class)public DataSource shadowDataSource() throws SQLException {return ShadowDataSourceFactory.createDataSource(dataSourceMap, new ShadowRuleConfigurationYamlSwapper().swap(shadowRule), props.getProps());}
}    

从源码中会发现shardingsphere会根据配置不同注入不同的DataSource ,其中DataSource有如下几种:

  1. ShardingDataSource:分片的数据源;
  2. MasterSlaveDataSource:主从读写分离的数据源;
  3. EncryptDataSource:数据脱敏的数据源;
  4. ShadowDataSource:影子表数据源;

接下来咱们分析一下数据源的具体创建过程,因为本文只涉及到主从读写分离和数据脱敏相关,所以只分析这两个数据源的创建过程,其他两个各位大佬有兴趣自己去分析。

ShardingDataSource创建过程

其实创建数据源的过程的代码挺简单的,直接贴源码吧。

package org.apache.shardingsphere.shardingjdbc.api;import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.core.rule.MasterSlaveRule;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.MasterSlaveDataSource;import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;/*** Master-slave data source factory.*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class MasterSlaveDataSourceFactory {/*** Create master-slave data source.** @param dataSourceMap data source map* @param masterSlaveRuleConfig master-slave rule configuration* @param props props* @return master-slave data source* @throws SQLException SQL exception*/public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, final Properties props) throws SQLException {return new MasterSlaveDataSource(dataSourceMap, new MasterSlaveRule(masterSlaveRuleConfig), props);}
}
/** Licensed to the Apache Software Foundation (ASF) under one or more* contributor license agreements.  See the NOTICE file distributed with* this work for additional information regarding copyright ownership.* The ASF licenses this file to You under the Apache License, Version 2.0* (the "License"); you may not use this file except in compliance with* the License.  You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource;import lombok.Getter;
import org.apache.shardingsphere.core.rule.MasterSlaveRule;
import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.AbstractDataSourceAdapter;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.MasterSlaveConnection;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.context.MasterSlaveRuntimeContext;
import org.apache.shardingsphere.spi.NewInstanceServiceLoader;
import org.apache.shardingsphere.underlying.route.decorator.RouteDecorator;import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;/*** Master-slave data source.*/
@Getter
public class MasterSlaveDataSource extends AbstractDataSourceAdapter {private final MasterSlaveRuntimeContext runtimeContext;static {NewInstanceServiceLoader.register(RouteDecorator.class);}public MasterSlaveDataSource(final Map<String, DataSource> dataSourceMap, final MasterSlaveRule masterSlaveRule, final Properties props) throws SQLException {super(dataSourceMap);runtimeContext = new MasterSlaveRuntimeContext(dataSourceMap, masterSlaveRule, props, getDatabaseType());}@Overridepublic final MasterSlaveConnection getConnection() {return new MasterSlaveConnection(getDataSourceMap(), runtimeContext);}
}

创建过程非常简单,从创建过程会发现,核心的属性有如下几个:

  1. dataSourceMap:主从数据源集合;
  2. masterSlaveRule:主从规则配置;
  3. props:额外的属性配置;

到这里会发现,主从数据源创建过程根本不会涉及到数据脱敏的配置信息。我又特意看了一下创建Connection和PreparedStatement也一样根本没有涉及到脱敏相关的配置,自然在使用了主从的时候就脱敏配置可能会失效,这里为啥是可能呢?后文再说。

EncryptDataSource创建过程

创建过程也是挺简单的,直接贴代码吧

@Getter
public class EncryptDataSource extends AbstractDataSourceAdapter {private final EncryptRuntimeContext runtimeContext;static {NewInstanceServiceLoader.register(SQLRewriteContextDecorator.class);NewInstanceServiceLoader.register(ResultProcessEngine.class);}public EncryptDataSource(final DataSource dataSource, final EncryptRule encryptRule, final Properties props) throws SQLException {super(dataSource);runtimeContext = new EncryptRuntimeContext(dataSource, encryptRule, props, getDatabaseType());}@Overridepublic final EncryptConnection getConnection() throws SQLException {return new EncryptConnection(getDataSource().getConnection(), runtimeContext);}/*** Get data source.** @return data source*/public DataSource getDataSource() {return getDataSourceMap().values().iterator().next();}
}

这里就不一样了,会发现有脱敏相关配置的关联,即:encryptRule 信息,所以配置单数据源的时候,脱敏配置是可以生效的。

小扩展

第一:遗留一个问题,那做分片的时候,脱敏有效吗?看官大佬,自己去看源码哈。

第二:解答一下,之前说配置了主从分离可能会导致脱敏失效;为什么说可能呢?回到最开始的那个源码类就知道答案了。因为shardingsphere创建数据源的过程中是有条件的,大家可以去看看条件。所以当配置读写分离和脱敏配置的时候,会同时注入两个数据源。所以可能会导致代码中引用错数据源DataSource对象,所以可能会失效。在这里还需要注意一个仔细就是:创建脱敏数据源对象的时候,shardingsphere是取的dataSourceMap的首个对象,是不是觉得代码在走钢丝,配置多个数据源的时候,可能出现莫名其妙的问题。这里只是我的见解哈,因为整个Java生态框架整合的时候,每个框架的具体仔细我不能保证都非常清楚,所以说可能。只是觉得作为框架,应该考虑更多一些,要么抛异常,不能想当然取第一个,这种做法就是我们在工作中做需求的时候的我认为。
数据源创建条件截图如下:
在这里插入图片描述
这两个类的实现代码如下:

public final class MasterSlaveRuleCondition extends SpringBootCondition {private static final String MASTER_SLAVE_NAME = "spring.shardingsphere.masterslave.name";@Overridepublic ConditionOutcome getMatchOutcome(final ConditionContext conditionContext, final AnnotatedTypeMetadata annotatedTypeMetadata) {return conditionContext.getEnvironment().containsProperty(MASTER_SLAVE_NAME)? ConditionOutcome.match() : ConditionOutcome.noMatch("Can't find ShardingSphere master-slave rule configuration in environment.");}
}
public final class EncryptRuleCondition extends SpringBootCondition {private static final String ENCRYPT_ENCRYPTORS_PREFIX = "spring.shardingsphere.encrypt.encryptors";private static final String ENCRYPT_TABLES_PREFIX = "spring.shardingsphere.encrypt.tables";@Overridepublic ConditionOutcome getMatchOutcome(final ConditionContext conditionContext, final AnnotatedTypeMetadata annotatedTypeMetadata) {boolean isEncrypt = PropertyUtil.containPropertyPrefix(conditionContext.getEnvironment(), ENCRYPT_ENCRYPTORS_PREFIX) && PropertyUtil.containPropertyPrefix(conditionContext.getEnvironment(), ENCRYPT_TABLES_PREFIX);return isEncrypt ? ConditionOutcome.match() : ConditionOutcome.noMatch("Can't find ShardingSphere encrypt rule configuration in environment.");}
}

通过条件源码会发现,条件重合是会发生的。

解决方案

知道原因了,就好解决了。初步的思路,在创建MasterSlaveDataSource对象的时候,给每个实际的数据源再套一层EncryptDataSource数据源,就应该可以解决问题。到这里就可以借助Spring给我们扩展点了,我使用的对象创建的前置方法代码如下:

@EnableConfigurationProperties(SpringBootCustomEncryptRuleConfigurationProperties.class)
@Configuration
@RequiredArgsConstructor
public class MasterSlaveEncryptDataSourceConfig implements BeanPostProcessor {private final SpringBootCustomEncryptRuleConfigurationProperties encryptRule;private final SpringBootPropertiesConfigurationProperties props;@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {if (bean instanceof MasterSlaveDataSource dataSource) {Map<String, DataSource> dataSourceMap = dataSource.getDataSourceMap();Map<String, DataSource> encryptDataSourceMap = new HashMap<>();dataSourceMap.forEach((k, v) -> {try {encryptDataSourceMap.put(k, EncryptDataSourceFactory.createDataSource(dataSourceMap.values().iterator().next(), new EncryptRuleConfigurationYamlSwapper().swap(encryptRule), props.getProps()));} catch (SQLException e) {e.printStackTrace();}});dataSource.getDataSourceMap().putAll(encryptDataSourceMap);return dataSource;} else {return bean;}}
}
@ConfigurationProperties(prefix = "spring.shardingsphere.custom-encrypt")
public class SpringBootCustomEncryptRuleConfigurationProperties extends YamlEncryptRuleConfiguration {}

有人可能会好奇哦,为啥需要后面这个类呢,仔细思考一下就知道了,如果使用了shardingsphere自带的脱敏配置类SpringBootEncryptRuleConfigurationProperties那就会创建一个EncryptDataSource数据源对象了,之前说过,注入多个数据源对象,可能会有副作用,所以自己搞一个脱敏配置类,配置信息也和自带分离,这样就不会注入自带的EncryptDataSource数据源对象了。

最后自己验证结果是没有问题的。

总结

从源码中,可以学习到如何如下:

  1. 学习了@Conditional注解一种用法和使用场景;
  2. 使用了工厂设计模式,可以借鉴使用场景和思路;
  3. 使用了装饰模式,使用场景加强一个类的功能;原生DataSource ===> EncryptDataSource ===> MasterSlaveDataSource
  4. 使用Spring框架的高级扩展点,去完成我们实际的开发任务。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_9392.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

阿里国际、ebay测评自养号,如何看待自己产品的销量?

作为全球知名的跨境电商平台&#xff0c;阿里国际、eBay 吸引了越来越多的跨境电商加入其中。对于商家来说&#xff0c;随时关注自己店铺里面产品的销售情况&#xff0c;可以更好的了解自家店铺的经营情况&#xff0c;发现其中的问题及时改进&#xff0c;从而更好增加产品的销售…

Framework启动流程——千字图文详解

Android底层是linux系统&#xff0c;因而在开机时仍然是运行天字第一号进程inti&#xff0c;读取init.rc来创建第一个Dalvik进程zygote&#xff0c;下面是个Framework启动的整体图 &#xff1a; 简单来说就是&#xff1a; Android启动过程包含从Linux内核加载到Home应用程序…

第六章:面向对象编程(基础部分)

目录 一、类与对象 1.定义&#xff1a; 2.案例&#xff1a;使用面向对象解决养猫问题 3.对象的内存&#xff1a; 4.属性的概念&#xff1a; 5.如何创建对象&#xff1a; 6.访问对象&#xff1a; 7.对象分配机制&#xff1a; 8.java创建对象的流程分析&#xff1a; 9.练…

Doris最全使用手册

目录 一&#xff1a;doris基础介绍 1.1 doris介绍 1.1.1 定义 1.1.2 具体的业务场景包括 1.1.3 Doris关键特性 二&#xff1a;Doris与其它数据库比较 三&#xff1a;底层索引与读写流程 3.1 Doris整体架构 3.2 Doris存储设计目标 四&#xff1a;数据划分&#xff08;…

前端笔试题记录(二)-代码输出题

1.将数组中的子对象加入到对应的父对象中&#xff0c;也就是parent_id为id 的&#xff0c;就将其加入进去。 function listToTree(list) {let map {};list.forEach(item > {if (!map[item.id]) {map[item.id] item;}});list.forEach(item > {if (item.parent_id ! 0) …

java基于SpringBoot+Vued的小区物业管理系统 elementui 前后端分离

系统管理也都将通过计算机进行整体智能化操作,对于小区物业管理系统所牵扯的管理及数据保存都是非常多的,例如管理员&#xff1a;首页、个人中心、用户管理、员工管理、业主信息管理、费用信息管理、楼房信息管理、报修信息管理、车位信息管理、停车信息管理、投诉编号管理、公…

C++实现一个线程池

一、为什么使用线程池 大家都知道C支持多线程开发&#xff0c;也就是支持多个任务并行运行&#xff0c;我们也知道线程的生命周期中包括创建、就绪、运行、阻塞、销毁等阶段&#xff0c;所以如果要执行的任务很多&#xff0c;每个任务都需要一个线程的话&#xff0c;那么频繁的…

在 WebKit(Safari) 中执行 Cypress 测试用例

在 WebKit(Safari) 中执行 Cypress 测试用例这篇博客介绍了我们如何在 WebKit(Safari) 中执行 Cypress 测试用例 在 ** 版本 10.8.0** ** ,** cypress 团队引入了对 WebKit(Safari) 浏览器的支持 赛普拉斯有 实验 支持 Safari 的浏览器引擎 WebKit。使用 WebKit 测试您的应用程…

面试操作系统 - 第 1 部分

面试操作系统 - 第 1 部分 从来没有在课堂上或非 CS 部门认真对待过 OS 科目(比如我)。不用担心,我支持你!You after reading the article 在这一系列文章中,我将尝试提供尽可能多的相关内容以及操作系统面试中需要的内容,但尽可能使质量达到最佳。 注意:- 本文的某些部…

介绍 Preact Signals

1. 什么是 Signals? Signals 是用来处理状态的一种方式,它参考自 SolidJS,吸收了其大部分的优点。无论应用多么复杂,它都能保证快速响应。 Signals 的独特之处在于状态更改会以最有效的方式来自动更新组件和 UI。 Signals 基于自动状态绑定和依赖跟踪提供了出色的工效,并具…

手写RPC框架Feign

Feign原理实现手写RPC框架Feign什么是RPCFeign注入原理EnableFeignClientsFeign调用入口手写Feign手写RPC框架Feign 阅读本文你可获得&#xff1a; 1、RPC原理 2、feign注入原理 3、如何手写feign框架 4、动态代码设计模式应用场景 什么是RPC RPC&#xff1a;远程过程调用&…

基于ssm的校运会管理系统设计与实现-计算机毕业设计源码+LW文档

开发语言&#xff1a;Java 框架&#xff1a;ssm JDK版本&#xff1a;JDK1.8 服务器&#xff1a;tomcat7 数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09; 数据库工具&#xff1a;Navicat11 开发软件&#xff1a;eclipse/myeclipse/idea Maven包&#xff1a;…

CSS基础篇---01选择器、字体与文本样式

CSDN话题挑战赛第2期 参赛话题&#xff1a;学习笔记 文章目录 1.初识CSS CSS引入方式 2.选择器 标签&#xff08;元素&#xff09;选择器 class选择器 id选择器 通配符选择器 案例演示 3.字体样式 字体大小 字体粗细 字体样式 字体类型 字体系列 层叠性 font复…

VM关闭虚拟机之后,连接不上前一天设置的静态ip

错误场景&#xff1a; 问题原因&#xff1a; centos7 默认的网卡 ens33 在虚拟机启动时&#xff0c;没有自动启动。导致设置的静态ip不生效&#xff0c;故连接不上。 解决方案&#xff1a; 查看托管是否启动&#xff0c;倘若网卡没有启动很可能是没有加入托管。 查看托管是否…

【强化学习】《动手学强化学习》动态规划算法

【强化学习】《动手学强化学习》动态规划算法一、基本思想二、悬崖漫步环境三、策略迭代算法3.1 策略评估3.2 策略提升3.3 悬崖漫步环境下的策略迭代四、价值迭代算法一、基本思想 动态规划算法在计算机专业课中是特别重要的思想&#xff0c;将待求问题分解成若干个子问题&…

Springboot 集成kafka

一、创建项目并导入pom依赖 <dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId> </dependency> 二、修改application.yml配置 1. producer 生产端的配置 spring:#重要提示:kafka配置,该…

Redis介绍和安装

Redis介绍 Redis是一个开源的、基于Key-Value(键-值&#xff09;存储的NoSQL数据库。Redis因其丰富的数据结构、极快的速度、齐全的功能而为人所知&#xff0c;它是目前内存数据库方面的事实标准&#xff0c;是目前使用广泛的开源缓存中间件。 Redis特点 结构丰富&#xff0…

CS231a课程笔记:Lecture2 Camera Models

关于齐次坐标&#xff1a;(15条消息) 为什么要引入齐次坐标&#xff0c;齐次坐标的意义&#xff08;一&#xff09;_追求卓越583的博客-CSDN博客_齐次坐标的意义(15条消息) 为什么要引入齐次坐标&#xff0c;齐次坐标的意义&#xff08;二&#xff09;_追求卓越583的博客-CSDN博…

DNS 解析流程

一、背景 最近&#xff0c;在S3协议项目中调研通过DNS域名解析处理流量负载均衡问题。原来对dns也有一些粗浅的了解&#xff0c;知道通过DNS可以将域名转换为IP地址&#xff0c;也可以做负载均衡。但是DNS的解析流程以及缓存等机制&#xff0c;只是一知半解。正好&#xff0c;…

windows安装nginx并设置开机自启动

在macOS和linux中使用nginx我早已经轻车熟路。突然切到windows的环境中&#xff0c;我反而不会用了。 之前写了《windows使用nginx探索笔记》内容比较冗长&#xff0c;所以本文尽量精简一下。 环境 操作系统&#xff1a;windows 2008R2 Datacenter 已经安装的软件&#xff1…