当前位置: 首页 > news >正文

网站视频下载方法网络营销优化推广公司

网站视频下载方法,网络营销优化推广公司,网页制作排行榜,如何设计动态网页Sharding JDBC自动配置的原理 与所有starter一样,shardingsphere-jdbc-core-spring-boot-starter也是通过SPI自动配置的原理实现分库分表配置加载,spring.factories文件中的自动配置类shardingsphere-jdbc-core-spring-boot-starter功不可没&#xff0c…

Sharding JDBC自动配置的原理

与所有starter一样,shardingsphere-jdbc-core-spring-boot-starter也是通过SPI自动配置的原理实现分库分表配置加载,spring.factories文件中的自动配置类shardingsphere-jdbc-core-spring-boot-starter功不可没,他主要是自动创建了模式bean、事务类型bean和数据源bean,配置加载的过程可以归纳如下:

创建模式bean
创建数据源bean
创建事务类型扫描bean
ShardingSphereAutoConfiguration
ModeConfiguration
ShardingSphereDataSource
TransactionTypeScanner

其中,创建数据源bean时会根据不同的模式创建不同的bean,本地模式直接从配置文件中加载,配置中心模式就从配置中心加载。ShardingSphereAutoConfiguration的实现如下:

@Configuration
@ComponentScan("org.apache.shardingsphere.spring.boot.converter")
@EnableConfigurationProperties(SpringBootPropertiesConfiguration.class)
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@RequiredArgsConstructor
public class ShardingSphereAutoConfiguration implements EnvironmentAware {private String databaseName;private final SpringBootPropertiesConfiguration props;private final Map<String, DataSource> dataSourceMap = new LinkedHashMap<>();/*** 模式配置** @return mode configuration*/@Beanpublic ModeConfiguration modeConfiguration() {return null == props.getMode() ? null : new YamlModeConfigurationSwapper().swapToObject(props.getMode());}/*** 单机模式:从本地配置中加载DataSource** @param rules rules configuration* @param modeConfig mode configuration* @return data source bean* @throws SQLException SQL exception*/@Bean@Conditional(LocalRulesCondition.class)@Autowired(required = false)public DataSource shardingSphereDataSource(final ObjectProvider<List<RuleConfiguration>> rules, final ObjectProvider<ModeConfiguration> modeConfig) throws SQLException {Collection<RuleConfiguration> ruleConfigs = Optional.ofNullable(rules.getIfAvailable()).orElseGet(Collections::emptyList);return ShardingSphereDataSourceFactory.createDataSource(databaseName, modeConfig.getIfAvailable(), dataSourceMap, ruleConfigs, props.getProps());}/*** 集群模式:从配置中心中加载DataSource** @param modeConfig mode configuration* @return data source bean* @throws SQLException SQL exception*/@Bean@ConditionalOnMissingBean(DataSource.class)public DataSource dataSource(final ModeConfiguration modeConfig) throws SQLException {return !dataSourceMap.isEmpty() ? ShardingSphereDataSourceFactory.createDataSource(databaseName, modeConfig, dataSourceMap, Collections.emptyList(), props.getProps()): ShardingSphereDataSourceFactory.createDataSource(databaseName, modeConfig);}/*** 事务类型扫描bean** @return transaction type scanner*/@Beanpublic TransactionTypeScanner transactionTypeScanner() {return new TransactionTypeScanner();}@Overridepublic final void setEnvironment(final Environment environment) {dataSourceMap.putAll(DataSourceMapSetter.getDataSourceMap(environment));databaseName = DatabaseNameSetter.getDatabaseName(environment);}
}

下面以单机模式出发点,理一下加载的过程。

@Bean
@Conditional(LocalRulesCondition.class)
@Autowired(required = false)
public DataSource shardingSphereDataSource(final ObjectProvider<List<RuleConfiguration>> rules, final ObjectProvider<ModeConfiguration> modeConfig) throws SQLException {Collection<RuleConfiguration> ruleConfigs = Optional.ofNullable(rules.getIfAvailable()).orElseGet(Collections::emptyList);// 通过ShardingSphereDataSourceFactory工厂创建数据源return ShardingSphereDataSourceFactory.createDataSource(databaseName, modeConfig.getIfAvailable(), dataSourceMap, ruleConfigs, props.getProps());
}

通过对源码的跟踪,可以发现,ShardingSphereDataSourceFactory.createDataSource创建数据源经历了如下的过程

创建数据源bean
new
创建上下文管理器
创建JDBC上下文
创建InstanceMetaData
根据mode调用
创建元数据上下文
创建数据库连接元数据
调用
加载分片规则
new
ShardingSphereAutoConfiguration
ShardingSphereDataSourceFactory.createDataSource
ShardingSphereDataSource
ShardingSphereDataSource.createContextManager
JDBCContext
InstanceMetaDataBuilderFactory.create
StandaloneContextManagerBuilder.build
MetaDataContextsFactory.create
ShardingSphereDatabasesFactory.create
ShardingSphereDatabase.create
DatabaseRulesBuilder.build
ShardingSphereDatabase

分片规则加载原理

分片规则、审计规则、key生成规则都是通过SPI的方式加载,自动配置类ShardingSphereAutoConfiguration中创建ShardingSphereDataSource的时候,会加载配置的分片规则,创建核心配置类ShardingRule,在ShardingRule的创建中会通过SPI的方式加载分片规则。加载的过程如下:

创建数据源bean
查询实例
通过SPI Class获取服务实例
Y:创建新的service实例
N:Map中获取service单例
创建分片规则
创建分片规则
创建核心分库分表规则
分片规则
Key生成规则
审计规则
通过工厂创建分片算法
获取注册的service
SPI方式获取service
通过工厂创建key生成算法
通过工厂创建审计算法
ShardingSphereAutoConfiguration
ShardingSphereDataSource
一系列加载...
DatabaseRulesBuilder.build
OrderedSPIRegistry.getRegisteredServices
ShardingSphereServiceLoader.getServiceInstances(spiClass)
带@SingletonSPI注解?
service.getClass().getDeclaredConstructor().newInstance()
SERVICES.getOrDefault(serviceInterface, Collections.emptyList())
ShardingRuleBuilder.build
new ShardingRule
ShardingAlgorithmFactory.newInstance
KeyGenerateAlgorithmFactory.newInstance
ShardingAuditAlgorithmFactory.newInstance
ShardingSphereAlgorithmFactory.createAlgorithm
TypedSPIRegistry.getRegisteredService
ShardingSphereServiceLoader.getServiceInstances(spiClass)

SPI核心实现类ShardingSphereServiceLoader中会将SPI接口进行Map缓存管理,需要时直接获取。如果Map中不存在,就通过反射的方式新建服务实例,具体实现源码如下:

public final class ShardingSphereServiceLoader {// 缓存service实例,// 缓存的Key,如://  org.apache.shardingsphere.sharding.spi.ShardingAlgorithm//  org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm//  org.apache.shardingsphere.transaction.spi.ShardingSphereTransactionManagerprivate static final Map<Class<?>, Collection<Object>> SERVICES = new ConcurrentHashMap<>();/*** 注册服务实例** @param 服务接口*/public static void register(final Class<?> serviceInterface) {if (!SERVICES.containsKey(serviceInterface)) {SERVICES.put(serviceInterface, load(serviceInterface));}}private static <T> Collection<Object> load(final Class<T> serviceInterface) {Collection<Object> result = new LinkedList<>();for (T each : ServiceLoader.load(serviceInterface)) {result.add(each);}return result;}/*** 获取服务实例* * @param 服务接口* @param <T> 服务类型* @return 服务实例*/public static <T> Collection<T> getServiceInstances(final Class<T> serviceInterface) {return null == serviceInterface.getAnnotation(SingletonSPI.class) ? createNewServiceInstances(serviceInterface) : getSingletonServiceInstances(serviceInterface);}@SneakyThrows(ReflectiveOperationException.class)@SuppressWarnings("unchecked")private static <T> Collection<T> createNewServiceInstances(final Class<T> serviceInterface) {if (!SERVICES.containsKey(serviceInterface)) {return Collections.emptyList();}Collection<Object> services = SERVICES.get(serviceInterface);if (services.isEmpty()) {return Collections.emptyList();}Collection<T> result = new LinkedList<>();for (Object each : services) {// 通过反射新建实例result.add((T) each.getClass().getDeclaredConstructor().newInstance());}return result;}@SuppressWarnings("unchecked")private static <T> Collection<T> getSingletonServiceInstances(final Class<T> serviceInterface) {return (Collection<T>) SERVICES.getOrDefault(serviceInterface, Collections.emptyList());}
}
http://www.zhongyajixie.com/news/30411.html

相关文章:

  • web前端就是做网站么天津网站优化公司
  • 做网站时管理员权限的页面微信推广朋友圈广告
  • 内江网站怎么做seo自己怎么开电商平台
  • 家居饰品网站建设论文网站的seo方案
  • 网站和系统的区别如何推广普通话
  • 建设完网站成功后需要注意什么最有效的网络推广方式和策略
  • 自适应网站 css上海搜索引擎优化1
  • wordpress自带的简码seo网站首页推广
  • 做app和做网站哪个容易百度网站网址是多少
  • 公司建设网站申请信用卡吗游戏代理加盟
  • 泰国做企业网站小程序开发需要哪些技术
  • 网站的创新点培训seo
  • 做企业内部网站要多久什么是网络软文营销
  • 公司网站建设项目详情百度seo是啥
  • 做机械一般做那个外贸网站太原做网站哪家好
  • 国内网站建设app推广
  • 成都专门做公司网站的公司宁波 seo整体优化
  • wordpress图片添加标签深圳快速seo排名优化
  • 实验室网站制作全能优化大师
  • 怎样免费做公司网站域名ip地址在线查询
  • 网站建设 asp 武汉爱网站关键词挖掘工具
  • 代做毕业设计找哪个网站百度关键词权重查询
  • 和龙市建设局网站关键词seo排名怎么选
  • 做oa好 还是做网站好谷歌优化排名怎么做
  • 河北建设工程信息网官网 费用中项网沧州seo推广
  • 黄石市城市建设档案馆网站产品推广介绍
  • 网站设计课程总结新闻头条新闻
  • 提供手机网站建设线上营销模式
  • 什么是网络营销渠道?重庆百度seo排名优化软件
  • 哪家企业做网站温州网站优化推广方案