当前位置:   article > 正文

数据库拆分4--sharding-jdbc-spring-boot-starter自动装配启动过程

sharding-jdbc-spring-boot-starter

学习一下springboot是如何整合sharding-jdbc的。

添加依赖以后

  1. <dependency>
  2. <groupId>org.apache.shardingsphere</groupId>
  3. <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
  4. <version>4.1.1</version>
  5. </dependency>

由于springboot自动装配可知会自动加载这个类

org.apache.shardingsphere.shardingjdbc.spring.boot.SpringBootConfiguration

SpringBootConfiguration实现了接口EnvironmentAware 其主要属性如下:
  1. private final SpringBootShardingRuleConfigurationProperties shardingRule;
  2. private final SpringBootMasterSlaveRuleConfigurationProperties masterSlaveRule;
  3. private final SpringBootEncryptRuleConfigurationProperties encryptRule;
  4. private final SpringBootShadowRuleConfigurationProperties shadowRule;
  5. private final SpringBootPropertiesConfigurationProperties props;
  6. private final Map<String, DataSource> dataSourceMap = new LinkedHashMap();
  7. private final String jndiName = "jndi-name";

主要用来读取各种配置项,结合配置文件:

  1. spring.shardingsphere.datasource.names=wim-user,wim-order
  2. spring.shardingsphere.datasource.wim-user.type=com.alibaba.druid.pool.DruidDataSource
  3. spring.shardingsphere.datasource.wim-user.driver-class-name=com.mysql.cj.jdbc.Driver
  4. spring.shardingsphere.datasource.wim-user.url=jdbc:mysql://127.0.0.1:3306/wim-user?serverTimezone=UTC&useSSL=false
  5. spring.shardingsphere.datasource.wim-user.username=root
  6. spring.shardingsphere.datasource.wim-user.password=123456
  7. spring.shardingsphere.datasource.wim-order.type=com.alibaba.druid.pool.DruidDataSource
  8. spring.shardingsphere.datasource.wim-order.driver-class-name=com.mysql.cj.jdbc.Driver
  9. spring.shardingsphere.datasource.wim-order.url=jdbc:mysql://127.0.0.1:3306/wim-order?serverTimezone=UTC&useSSL=false
  10. spring.shardingsphere.datasource.wim-order.username=root
  11. spring.shardingsphere.datasource.wim-order.password=123456
  12. spring.shardingsphere.sharding.tables.user_t.actual-data-nodes=wim-user.user_t
  13. spring.shardingsphere.sharding.tables.log_t.actual-data-nodes=wim-user.log_t
  14. #spring.shardingsphere.sharding.tables.user_t.table-strategy.inline.sharding-column=user_id
  15. #spring.shardingsphere.sharding.tables.user_t.table-strategy.inline.algorithm-expression=user_t
  16. #
  17. #spring.shardingsphere.sharding.tables.log_t.table-strategy.inline.sharding-column=user_id
  18. #spring.shardingsphere.sharding.tables.log_t.table-strategy.inline.algorithm-expression=log_t
  19. spring.shardingsphere.sharding.default-data-source-name=wim-order
  20. spring.shardingsphere.props.sql.show=true
spring.shardingsphere.datasource前缀会自动解析成数据源 dataSourceMap
spring.shardingsphere.sharding前缀会自动解析成数据库分片配置 shardingRule
spring.shardingsphere.props前缀会解析成属性配置 props

其余几个配置

masterSlaveRule 读写分离配置
encryptRule加密配置
shadowRule影子表配置

在setEnvironment方法中实现数据源的配置项加载和初始化

  1. public final void setEnvironment(Environment environment) {
  2. String prefix = "spring.shardingsphere.datasource.";
  3. Iterator var3 = this.getDataSourceNames(environment, prefix).iterator();
  4. while(var3.hasNext()) {
  5. String each = (String)var3.next();
  6. try {
  7. this.dataSourceMap.put(each, this.getDataSource(environment, prefix, each));
  8. } catch (ReflectiveOperationException var6) {
  9. throw new ShardingSphereException("Can't find datasource type!", var6);
  10. } catch (NamingException var7) {
  11. throw new ShardingSphereException("Can't find JNDI datasource!", var7);
  12. }
  13. }
  14. }

此时dataSourceMap中含有所有配置的数据源配置项,容器启动时具体使用何种数据源由相应配置项来决定

  1. public final class ShardingRuleCondition extends SpringBootCondition {
  2. public ShardingRuleCondition() {
  3. }
  4. public ConditionOutcome getMatchOutcome(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
  5. boolean isMasterSlaveRule = (new MasterSlaveRuleCondition()).getMatchOutcome(conditionContext, annotatedTypeMetadata).isMatch();
  6. boolean isEncryptRule = (new EncryptRuleCondition()).getMatchOutcome(conditionContext, annotatedTypeMetadata).isMatch();
  7. boolean isShadow = (new ShadowRuleCondition()).getMatchOutcome(conditionContext, annotatedTypeMetadata).isMatch();
  8. return !isMasterSlaveRule && !isEncryptRule && !isShadow ? ConditionOutcome.match() : ConditionOutcome.noMatch("Have found master-slave or encrypt rule in environment");
  9. }
  10. }

如果有spring.shardingsphere.masterslave.name加载主从数据源模式

如果有spring.shardingsphere.encrypt.encryptors加载加密数据源模式

如果有spring.shardingsphere.shadow加载主从数据源模式其他则加载影子库数据源模式

由于我们只配置了分片规则,因此会初始化shardingDataSource

  1. @Bean
  2. @Conditional({ShardingRuleCondition.class})
  3. public DataSource shardingDataSource() throws SQLException {
  4. return ShardingDataSourceFactory.createDataSource(this.dataSourceMap, (new ShardingRuleConfigurationYamlSwapper()).swap(this.shardingRule), this.props.getProps());
  5. }

因此容器中最终的数据源对象为ShardingDataSource

  1. public static DataSource createDataSource(Map<String, DataSource> dataSourceMap, ShardingRuleConfiguration shardingRuleConfig, Properties props) throws SQLException {
  2. return new ShardingDataSource(dataSourceMap, new ShardingRule(shardingRuleConfig, dataSourceMap.keySet()), props);
  3. }

ShardingDataSource数据源中通过ShardingRuntimeContext对象来实现上下文对象保存和传递以及功能增强

  1. private final ShardingRuntimeContext runtimeContext;
  2. public ShardingDataSource(Map<String, DataSource> dataSourceMap, ShardingRule shardingRule, Properties props) throws SQLException {
  3. super(dataSourceMap);
  4. this.checkDataSourceType(dataSourceMap);
  5. this.runtimeContext = new ShardingRuntimeContext(dataSourceMap, shardingRule, props, this.getDatabaseType());
  6. }
ShardingRuntimeContext中主要包括 缓存的数据源元数据信息以及事务管理器
  1. private final CachedDatabaseMetaData cachedDatabaseMetaData;
  2. private final ShardingTransactionManagerEngine shardingTransactionManagerEngine;
  3. public ShardingRuntimeContext(Map<String, DataSource> dataSourceMap, ShardingRule shardingRule, Properties props, DatabaseType databaseType) throws SQLException {
  4. super(dataSourceMap, shardingRule, props, databaseType);
  5. this.cachedDatabaseMetaData = this.createCachedDatabaseMetaData(dataSourceMap);
  6. this.shardingTransactionManagerEngine = new ShardingTransactionManagerEngine();
  7. this.shardingTransactionManagerEngine.init(databaseType, dataSourceMap);
  8. }

 应用启动后加载的数据库元数据相关信息

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/757030
推荐阅读
相关标签
  

闽ICP备14008679号