init
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springblade</groupId>
|
||||
<artifactId>BladeX-Tool</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>blade-starter-sharding</artifactId>
|
||||
<name>${project.artifactId}</name>
|
||||
<version>${project.parent.version}</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<module.name>org.springblade.blade.starter.sharding</module.name>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Blade -->
|
||||
<dependency>
|
||||
<groupId>org.springblade</groupId>
|
||||
<artifactId>blade-core-launch</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springblade</groupId>
|
||||
<artifactId>blade-starter-db-dynamic</artifactId>
|
||||
</dependency>
|
||||
<!-- sharding-jdbc -->
|
||||
<dependency>
|
||||
<groupId>org.apache.shardingsphere</groupId>
|
||||
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<!-- 指定版本避免 sharding-jdbc 依赖冲突 -->
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.antlr</groupId>
|
||||
<artifactId>antlr4-runtime</artifactId>
|
||||
</dependency>
|
||||
<!-- Auto -->
|
||||
<dependency>
|
||||
<groupId>org.springblade</groupId>
|
||||
<artifactId>blade-core-auto</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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.elasticjob.infra.yaml.representer;
|
||||
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.introspector.Property;
|
||||
import org.yaml.snakeyaml.nodes.NodeTuple;
|
||||
import org.yaml.snakeyaml.nodes.Tag;
|
||||
import org.yaml.snakeyaml.representer.Representer;
|
||||
|
||||
/**
|
||||
* ElasticJob YAML representer.
|
||||
*/
|
||||
public final class ElasticJobYamlRepresenter extends Representer {
|
||||
public ElasticJobYamlRepresenter(final DumperOptions options) {
|
||||
super(options);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NodeTuple representJavaBeanProperty(final Object javaBean, final Property property, final Object propertyValue, final Tag customTag) {
|
||||
return new DefaultYamlTupleProcessor().process(super.representJavaBeanProperty(javaBean, property, propertyValue, customTag));
|
||||
}
|
||||
}
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* 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.infra.util.yaml;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.apache.shardingsphere.infra.util.yaml.constructor.ShardingSphereYamlConstructor;
|
||||
import org.apache.shardingsphere.infra.util.yaml.representer.ShardingSphereYamlRepresenter;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.representer.Representer;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* YAML engine.
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public final class YamlEngine {
|
||||
|
||||
/**
|
||||
* Unmarshal YAML.
|
||||
*
|
||||
* @param yamlFile YAML file
|
||||
* @param classType class type
|
||||
* @param <T> type of class
|
||||
* @return object from YAML
|
||||
* @throws IOException IO Exception
|
||||
*/
|
||||
public static <T extends YamlConfiguration> T unmarshal(final File yamlFile, final Class<T> classType) throws IOException {
|
||||
try (
|
||||
FileInputStream fileInputStream = new FileInputStream(yamlFile);
|
||||
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream)) {
|
||||
return new Yaml(new ShardingSphereYamlConstructor(classType)).loadAs(inputStreamReader, classType);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmarshal YAML.
|
||||
*
|
||||
* @param yamlBytes YAML bytes
|
||||
* @param classType class type
|
||||
* @param <T> type of class
|
||||
* @return object from YAML
|
||||
* @throws IOException IO Exception
|
||||
*/
|
||||
public static <T extends YamlConfiguration> T unmarshal(final byte[] yamlBytes, final Class<T> classType) throws IOException {
|
||||
try (InputStream inputStream = new ByteArrayInputStream(yamlBytes)) {
|
||||
return new Yaml(new ShardingSphereYamlConstructor(classType)).loadAs(inputStream, classType);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmarshal YAML.
|
||||
*
|
||||
* @param yamlContent YAML content
|
||||
* @param classType class type
|
||||
* @param <T> type of class
|
||||
* @return object from YAML
|
||||
*/
|
||||
public static <T> T unmarshal(final String yamlContent, final Class<T> classType) {
|
||||
return new Yaml(new ShardingSphereYamlConstructor(classType)).loadAs(yamlContent, classType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmarshal YAML.
|
||||
*
|
||||
* @param yamlContent YAML content
|
||||
* @param classType class type
|
||||
* @param skipMissingProps true if missing properties should be skipped, false otherwise
|
||||
* @param <T> type of class
|
||||
* @return object from YAML
|
||||
*/
|
||||
public static <T> T unmarshal(final String yamlContent, final Class<T> classType, final boolean skipMissingProps) {
|
||||
Representer representer = new Representer(new DumperOptions());
|
||||
representer.getPropertyUtils().setSkipMissingProperties(skipMissingProps);
|
||||
return new Yaml(new ShardingSphereYamlConstructor(classType), representer).loadAs(yamlContent, classType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Marshal YAML.
|
||||
*
|
||||
* @param value object to be marshaled
|
||||
* @return YAML content
|
||||
*/
|
||||
public static String marshal(final Object value) {
|
||||
DumperOptions dumperOptions = new DumperOptions();
|
||||
dumperOptions.setLineBreak(DumperOptions.LineBreak.getPlatformLineBreak());
|
||||
if (value instanceof Collection) {
|
||||
return new Yaml(new ShardingSphereYamlRepresenter(dumperOptions), dumperOptions).dumpAs(value, null, DumperOptions.FlowStyle.BLOCK);
|
||||
}
|
||||
return new Yaml(new ShardingSphereYamlRepresenter(dumperOptions), dumperOptions).dumpAsMap(value);
|
||||
}
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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.infra.util.yaml.representer;
|
||||
|
||||
import org.apache.shardingsphere.infra.util.yaml.representer.processor.DefaultYamlTupleProcessor;
|
||||
import org.apache.shardingsphere.infra.util.yaml.representer.processor.ShardingSphereYamlTupleProcessor;
|
||||
import org.apache.shardingsphere.infra.util.yaml.representer.processor.ShardingSphereYamlTupleProcessorFactory;
|
||||
import org.apache.shardingsphere.infra.util.yaml.shortcuts.ShardingSphereYamlShortcutsFactory;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.introspector.Property;
|
||||
import org.yaml.snakeyaml.nodes.Node;
|
||||
import org.yaml.snakeyaml.nodes.NodeTuple;
|
||||
import org.yaml.snakeyaml.nodes.Tag;
|
||||
import org.yaml.snakeyaml.representer.Representer;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
* ShardingSphere YAML representer.
|
||||
*/
|
||||
public final class ShardingSphereYamlRepresenter extends Representer {
|
||||
|
||||
public ShardingSphereYamlRepresenter(DumperOptions options) {
|
||||
super(options);
|
||||
ShardingSphereYamlShortcutsFactory.getAllYamlShortcuts().forEach((key, value) -> addClassTag(value, new Tag(key)));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NodeTuple representJavaBeanProperty(final Object javaBean, final Property property, final Object propertyValue, final Tag customTag) {
|
||||
NodeTuple nodeTuple = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
|
||||
for (ShardingSphereYamlTupleProcessor each : ShardingSphereYamlTupleProcessorFactory.getAllInstances()) {
|
||||
if (property.getName().equals(each.getTupleName())) {
|
||||
return each.process(nodeTuple);
|
||||
}
|
||||
}
|
||||
return new DefaultYamlTupleProcessor().process(nodeTuple);
|
||||
}
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
@Override
|
||||
protected Node representMapping(final Tag tag, final Map<?, ?> mapping, final DumperOptions.FlowStyle flowStyle) {
|
||||
Map skippedEmptyValuesMapping = new LinkedHashMap<>(mapping.size(), 1);
|
||||
for (Entry<?, ?> entry : mapping.entrySet()) {
|
||||
if (entry.getValue() instanceof Collection && ((Collection) entry.getValue()).isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
if (entry.getValue() instanceof Map && ((Map) entry.getValue()).isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
skippedEmptyValuesMapping.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return super.representMapping(tag, skippedEmptyValuesMapping, flowStyle);
|
||||
}
|
||||
}
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* 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.spring.boot;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
|
||||
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
|
||||
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
|
||||
import org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlModeConfigurationSwapper;
|
||||
import org.apache.shardingsphere.spring.boot.datasource.DataSourceMapSetter;
|
||||
import org.apache.shardingsphere.spring.boot.prop.SpringBootPropertiesConfiguration;
|
||||
import org.apache.shardingsphere.spring.boot.rule.LocalRulesCondition;
|
||||
import org.apache.shardingsphere.spring.boot.schema.DatabaseNameSetter;
|
||||
import org.apache.shardingsphere.spring.transaction.TransactionTypeScanner;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Spring boot starter configuration.
|
||||
*/
|
||||
@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<>();
|
||||
|
||||
/**
|
||||
* Get mode configuration.
|
||||
*
|
||||
* @return mode configuration
|
||||
*/
|
||||
@Bean
|
||||
public ModeConfiguration modeConfiguration() {
|
||||
return null == props.getMode() ? null : new YamlModeConfigurationSwapper().swapToObject(props.getMode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get ShardingSphere data source bean.
|
||||
*
|
||||
* @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());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get data source bean from registry center.
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create transaction type scanner.
|
||||
*
|
||||
* @return transaction type scanner
|
||||
*/
|
||||
@Bean
|
||||
public TransactionTypeScanner transactionTypeScanner() {
|
||||
return new TransactionTypeScanner();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void setEnvironment(final Environment environment) {
|
||||
dataSourceMap.putAll(DataSourceMapSetter.getDataSourceMap(environment));
|
||||
databaseName = DatabaseNameSetter.getDatabaseName(environment);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* BladeX Commercial License Agreement
|
||||
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
|
||||
* <p>
|
||||
* Use of this software is governed by the Commercial License Agreement
|
||||
* obtained after purchasing a license from BladeX.
|
||||
* <p>
|
||||
* 1. This software is for development use only under a valid license
|
||||
* from BladeX.
|
||||
* <p>
|
||||
* 2. Redistribution of this software's source code to any third party
|
||||
* without a commercial license is strictly prohibited.
|
||||
* <p>
|
||||
* 3. Licensees may copyright their own code but cannot use segments
|
||||
* from this software for such purposes. Copyright of this software
|
||||
* remains with BladeX.
|
||||
* <p>
|
||||
* Using this software signifies agreement to this License, and the software
|
||||
* must not be used for illegal purposes.
|
||||
* <p>
|
||||
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
|
||||
* not liable for any claims arising from secondary or illegal development.
|
||||
* <p>
|
||||
* Author: Chill Zhuang (bladejava@qq.com)
|
||||
*/
|
||||
package org.springblade.core.sharding;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.constructor.Constructor;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* ShardingUtil
|
||||
*
|
||||
* @author Chill
|
||||
*/
|
||||
public class ShardingUtil {
|
||||
|
||||
@SneakyThrows
|
||||
public static DataSource createDataSource(String yamlConfig) {
|
||||
return YamlShardingSphereDataSourceFactory.createDataSource(yamlConfig.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* BladeX Commercial License Agreement
|
||||
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
|
||||
* <p>
|
||||
* Use of this software is governed by the Commercial License Agreement
|
||||
* obtained after purchasing a license from BladeX.
|
||||
* <p>
|
||||
* 1. This software is for development use only under a valid license
|
||||
* from BladeX.
|
||||
* <p>
|
||||
* 2. Redistribution of this software's source code to any third party
|
||||
* without a commercial license is strictly prohibited.
|
||||
* <p>
|
||||
* 3. Licensees may copyright their own code but cannot use segments
|
||||
* from this software for such purposes. Copyright of this software
|
||||
* remains with BladeX.
|
||||
* <p>
|
||||
* Using this software signifies agreement to this License, and the software
|
||||
* must not be used for illegal purposes.
|
||||
* <p>
|
||||
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
|
||||
* not liable for any claims arising from secondary or illegal development.
|
||||
* <p>
|
||||
* Author: Chill Zhuang (bladejava@qq.com)
|
||||
*/
|
||||
package org.springblade.core.sharding.annotation;
|
||||
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import org.springblade.core.sharding.constant.ShardingConstant;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 指定分库分表数据源切换
|
||||
*
|
||||
* @author Chill
|
||||
*/
|
||||
@DS(ShardingConstant.SHARDING_DATASOURCE_KEY)
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface ShardingDS {
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* BladeX Commercial License Agreement
|
||||
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
|
||||
* <p>
|
||||
* Use of this software is governed by the Commercial License Agreement
|
||||
* obtained after purchasing a license from BladeX.
|
||||
* <p>
|
||||
* 1. This software is for development use only under a valid license
|
||||
* from BladeX.
|
||||
* <p>
|
||||
* 2. Redistribution of this software's source code to any third party
|
||||
* without a commercial license is strictly prohibited.
|
||||
* <p>
|
||||
* 3. Licensees may copyright their own code but cannot use segments
|
||||
* from this software for such purposes. Copyright of this software
|
||||
* remains with BladeX.
|
||||
* <p>
|
||||
* Using this software signifies agreement to this License, and the software
|
||||
* must not be used for illegal purposes.
|
||||
* <p>
|
||||
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
|
||||
* not liable for any claims arising from secondary or illegal development.
|
||||
* <p>
|
||||
* Author: Chill Zhuang (bladejava@qq.com)
|
||||
*/
|
||||
package org.springblade.core.sharding.config;
|
||||
|
||||
import com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAutoConfiguration;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceCreatorAutoConfiguration;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springblade.core.db.dynamic.config.DynamicDataSourceConfiguration;
|
||||
import org.springblade.core.db.dynamic.processor.DynamicDataSourceProcessor;
|
||||
import org.springblade.core.sharding.processor.ShardingDataSourceProcessor;
|
||||
import org.springblade.core.sharding.props.ShardingProperties;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* ShardingSphere与DynamicDatasource配置类
|
||||
* 此配置类用于未开启租户模块数据库隔离功能的场景
|
||||
*
|
||||
* @author Chill
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties({DynamicDataSourceProperties.class, ShardingProperties.class})
|
||||
@AutoConfiguration(before = {DruidDataSourceAutoConfigure.class, DynamicDataSourceAutoConfiguration.class, DynamicDataSourceConfiguration.class})
|
||||
@Import(value = {DynamicDataSourceCreatorAutoConfiguration.class})
|
||||
@ConditionalOnProperty(value = ShardingProperties.PREFIX + ".enabled", havingValue = "true")
|
||||
public class ShardingConfiguration {
|
||||
|
||||
@Lazy
|
||||
@Resource(name = "shardingSphereDataSource")
|
||||
private DataSource shardingSphereDataSource;
|
||||
|
||||
/**
|
||||
* 自定义分库分表动态数据源加载逻辑
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public DynamicDataSourceProcessor dynamicShardingDataSourceProcessor() {
|
||||
return new ShardingDataSourceProcessor(shardingSphereDataSource);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* BladeX Commercial License Agreement
|
||||
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
|
||||
* <p>
|
||||
* Use of this software is governed by the Commercial License Agreement
|
||||
* obtained after purchasing a license from BladeX.
|
||||
* <p>
|
||||
* 1. This software is for development use only under a valid license
|
||||
* from BladeX.
|
||||
* <p>
|
||||
* 2. Redistribution of this software's source code to any third party
|
||||
* without a commercial license is strictly prohibited.
|
||||
* <p>
|
||||
* 3. Licensees may copyright their own code but cannot use segments
|
||||
* from this software for such purposes. Copyright of this software
|
||||
* remains with BladeX.
|
||||
* <p>
|
||||
* Using this software signifies agreement to this License, and the software
|
||||
* must not be used for illegal purposes.
|
||||
* <p>
|
||||
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
|
||||
* not liable for any claims arising from secondary or illegal development.
|
||||
* <p>
|
||||
* Author: Chill Zhuang (bladejava@qq.com)
|
||||
*/
|
||||
package org.springblade.core.sharding.constant;
|
||||
|
||||
/**
|
||||
* ShardingSphere常量
|
||||
*
|
||||
* @author Chill
|
||||
*/
|
||||
public interface ShardingConstant {
|
||||
/**
|
||||
* sharding数据源缓存名
|
||||
*/
|
||||
String SHARDING_DATASOURCE_KEY = "sharding";
|
||||
|
||||
/**
|
||||
* 租户动态数据源键
|
||||
*/
|
||||
String TENANT_DYNAMIC_DATASOURCE_PROP = "blade.tenant.dynamic-datasource";
|
||||
|
||||
}
|
||||
blade-starter-sharding/src/main/java/org/springblade/core/sharding/env/ShardingEnvPostProcessor.java
Vendored
+57
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* BladeX Commercial License Agreement
|
||||
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
|
||||
* <p>
|
||||
* Use of this software is governed by the Commercial License Agreement
|
||||
* obtained after purchasing a license from BladeX.
|
||||
* <p>
|
||||
* 1. This software is for development use only under a valid license
|
||||
* from BladeX.
|
||||
* <p>
|
||||
* 2. Redistribution of this software's source code to any third party
|
||||
* without a commercial license is strictly prohibited.
|
||||
* <p>
|
||||
* 3. Licensees may copyright their own code but cannot use segments
|
||||
* from this software for such purposes. Copyright of this software
|
||||
* remains with BladeX.
|
||||
* <p>
|
||||
* Using this software signifies agreement to this License, and the software
|
||||
* must not be used for illegal purposes.
|
||||
* <p>
|
||||
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
|
||||
* not liable for any claims arising from secondary or illegal development.
|
||||
* <p>
|
||||
* Author: Chill Zhuang (bladejava@qq.com)
|
||||
*/
|
||||
package org.springblade.core.sharding.env;
|
||||
|
||||
import org.springblade.core.auto.annotation.AutoEnvPostProcessor;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.env.EnvironmentPostProcessor;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
|
||||
/**
|
||||
* 初始化分库分表配置
|
||||
*
|
||||
* @author Chill
|
||||
*/
|
||||
@AutoEnvPostProcessor
|
||||
public class ShardingEnvPostProcessor implements EnvironmentPostProcessor, Ordered {
|
||||
|
||||
private static final String DYNAMIC_DATASOURCE_KEY = "spring.datasource.dynamic.enabled";
|
||||
|
||||
private static final String AUTOCONFIGURE_EXCLUDE_KEY = "spring.autoconfigure.exclude";
|
||||
|
||||
|
||||
@Override
|
||||
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
|
||||
environment.getSystemProperties().put(DYNAMIC_DATASOURCE_KEY, "false");
|
||||
environment.getSystemProperties().put(AUTOCONFIGURE_EXCLUDE_KEY, "com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return Ordered.LOWEST_PRECEDENCE;
|
||||
}
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* BladeX Commercial License Agreement
|
||||
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
|
||||
* <p>
|
||||
* Use of this software is governed by the Commercial License Agreement
|
||||
* obtained after purchasing a license from BladeX.
|
||||
* <p>
|
||||
* 1. This software is for development use only under a valid license
|
||||
* from BladeX.
|
||||
* <p>
|
||||
* 2. Redistribution of this software's source code to any third party
|
||||
* without a commercial license is strictly prohibited.
|
||||
* <p>
|
||||
* 3. Licensees may copyright their own code but cannot use segments
|
||||
* from this software for such purposes. Copyright of this software
|
||||
* remains with BladeX.
|
||||
* <p>
|
||||
* Using this software signifies agreement to this License, and the software
|
||||
* must not be used for illegal purposes.
|
||||
* <p>
|
||||
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
|
||||
* not liable for any claims arising from secondary or illegal development.
|
||||
* <p>
|
||||
* Author: Chill Zhuang (bladejava@qq.com)
|
||||
*/
|
||||
package org.springblade.core.sharding.processor;
|
||||
|
||||
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springblade.core.db.dynamic.creator.DynamicDataSourceProperty;
|
||||
import org.springblade.core.db.dynamic.processor.DynamicDataSourceProcessor;
|
||||
import org.springblade.core.sharding.constant.ShardingConstant;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Sharding动态数据源扩展
|
||||
*
|
||||
* @author BladeX
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class ShardingDataSourceProcessor implements DynamicDataSourceProcessor {
|
||||
|
||||
private final DataSource shardingSphereDataSource;
|
||||
|
||||
@Override
|
||||
public Map<String, DataSourceProperty> loadDataSourceProperties(Statement statement, DynamicDataSourceProperties dynamicDataSourceProperties) throws SQLException {
|
||||
Map<String, DataSourceProperty> map = new HashMap<>(16);
|
||||
|
||||
// 创建 Sharding数据源 对象
|
||||
DynamicDataSourceProperty shardingProperty = new DynamicDataSourceProperty();
|
||||
shardingProperty.setDataSource(shardingSphereDataSource);
|
||||
map.put(ShardingConstant.SHARDING_DATASOURCE_KEY, shardingProperty);
|
||||
|
||||
log.info("Sharding datasource processor loaded {} datasources", map.size());
|
||||
return map;
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* BladeX Commercial License Agreement
|
||||
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
|
||||
* <p>
|
||||
* Use of this software is governed by the Commercial License Agreement
|
||||
* obtained after purchasing a license from BladeX.
|
||||
* <p>
|
||||
* 1. This software is for development use only under a valid license
|
||||
* from BladeX.
|
||||
* <p>
|
||||
* 2. Redistribution of this software's source code to any third party
|
||||
* without a commercial license is strictly prohibited.
|
||||
* <p>
|
||||
* 3. Licensees may copyright their own code but cannot use segments
|
||||
* from this software for such purposes. Copyright of this software
|
||||
* remains with BladeX.
|
||||
* <p>
|
||||
* Using this software signifies agreement to this License, and the software
|
||||
* must not be used for illegal purposes.
|
||||
* <p>
|
||||
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
|
||||
* not liable for any claims arising from secondary or illegal development.
|
||||
* <p>
|
||||
* Author: Chill Zhuang (bladejava@qq.com)
|
||||
*/
|
||||
package org.springblade.core.sharding.props;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* 分库分表配置
|
||||
*
|
||||
* @author Chill
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@ConfigurationProperties(ShardingProperties.PREFIX)
|
||||
public class ShardingProperties {
|
||||
/**
|
||||
* 配置前缀
|
||||
*/
|
||||
public static final String PREFIX = "blade.sharding";
|
||||
|
||||
/**
|
||||
* 是否开启分库分表
|
||||
*/
|
||||
private Boolean enabled = Boolean.FALSE;
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user