fix bugs
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* 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 com.baomidou.dynamic.datasource.creator.DataSourceProperty;
|
||||
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
|
||||
import com.baomidou.dynamic.datasource.provider.AbstractDataSourceProvider;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springblade.core.sharding.constant.ShardingConstant;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 分库分表数据源初始加载
|
||||
*
|
||||
* @author Chill
|
||||
*/
|
||||
@Slf4j
|
||||
public class ShardingDataSourceProvider extends AbstractDataSourceProvider {
|
||||
|
||||
private final String driverClassName;
|
||||
private final String url;
|
||||
private final String username;
|
||||
private final String password;
|
||||
private final DynamicDataSourceProperties dynamicDataSourceProperties;
|
||||
private final DataSource shardingSphereDataSource;
|
||||
|
||||
public ShardingDataSourceProvider(DefaultDataSourceCreator dataSourceCreator, DynamicDataSourceProperties dynamicDataSourceProperties, String driverClassName, String url, String username, String password, DataSource shardingSphereDataSource) {
|
||||
super(dataSourceCreator);
|
||||
this.dynamicDataSourceProperties = dynamicDataSourceProperties;
|
||||
this.driverClassName = driverClassName;
|
||||
this.url = url;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.shardingSphereDataSource = shardingSphereDataSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, DataSource> loadDataSources() {
|
||||
// 构建数据源集合
|
||||
Map<String, DataSourceProperty> map = new HashMap<>(16);
|
||||
// 构建主数据源
|
||||
DataSourceProperty masterProperty = new DataSourceProperty();
|
||||
masterProperty.setDriverClassName(driverClassName);
|
||||
masterProperty.setUrl(url);
|
||||
masterProperty.setUsername(username);
|
||||
masterProperty.setPassword(password);
|
||||
masterProperty.setDruid(dynamicDataSourceProperties.getDruid());
|
||||
map.put(dynamicDataSourceProperties.getPrimary(), masterProperty);
|
||||
// 构建yml数据源
|
||||
Map<String, DataSourceProperty> datasource = dynamicDataSourceProperties.getDatasource();
|
||||
if (!datasource.isEmpty()) {
|
||||
map.putAll(datasource);
|
||||
}
|
||||
// 构建分库分表数据源
|
||||
Map<String, DataSource> dataSourceMap = createDataSourceMap(map);
|
||||
dataSourceMap.put(ShardingConstant.SHARDING_DATASOURCE_KEY, shardingSphereDataSource);
|
||||
return dataSourceMap;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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 {
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
/**
|
||||
* 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.DynamicRoutingDataSource;
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import com.baomidou.dynamic.datasource.aop.DynamicDataSourceAnnotationAdvisor;
|
||||
import com.baomidou.dynamic.datasource.aop.DynamicDataSourceAnnotationInterceptor;
|
||||
import com.baomidou.dynamic.datasource.creator.DataSourceCreator;
|
||||
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
|
||||
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
|
||||
import com.baomidou.dynamic.datasource.event.DataSourceInitEvent;
|
||||
import com.baomidou.dynamic.datasource.event.EncDataSourceInitEvent;
|
||||
import com.baomidou.dynamic.datasource.processor.DsJakartaHeaderProcessor;
|
||||
import com.baomidou.dynamic.datasource.processor.DsJakartaSessionProcessor;
|
||||
import com.baomidou.dynamic.datasource.processor.DsProcessor;
|
||||
import com.baomidou.dynamic.datasource.processor.DsSpelExpressionProcessor;
|
||||
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
|
||||
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 lombok.AllArgsConstructor;
|
||||
import org.springblade.core.sharding.ShardingDataSourceProvider;
|
||||
import org.springblade.core.sharding.constant.ShardingConstant;
|
||||
import org.springblade.core.sharding.props.ShardingProperties;
|
||||
import org.springframework.beans.factory.SmartInitializingSingleton;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.*;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* ShardingSphere与DynamicDatasource配置类
|
||||
* 此配置类用于未开启租户模块数据库隔离功能的场景
|
||||
*
|
||||
* @author Chill
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties({DynamicDataSourceProperties.class, ShardingProperties.class})
|
||||
@AutoConfiguration(before = {DruidDataSourceAutoConfigure.class, DynamicDataSourceAutoConfiguration.class})
|
||||
@Import(value = {DynamicDataSourceCreatorAutoConfiguration.class})
|
||||
@ConditionalOnProperty(value = ShardingProperties.PREFIX + ".enabled", havingValue = "true")
|
||||
public class ShardingConfiguration {
|
||||
|
||||
@Lazy
|
||||
@Resource(name = "shardingSphereDataSource")
|
||||
private DataSource shardingSphereDataSource;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty(value = ShardingConstant.TENANT_DYNAMIC_DATASOURCE_PROP, havingValue = "false")
|
||||
public DataSourceInitEvent dataSourceInitEvent() {
|
||||
return new EncDataSourceInitEvent();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty(value = ShardingConstant.TENANT_DYNAMIC_DATASOURCE_PROP, havingValue = "false")
|
||||
public DefaultDataSourceCreator dataSourceCreator(List<DataSourceCreator> dataSourceCreators, DataSourceInitEvent dataSourceInitEvent, DynamicDataSourceProperties properties) {
|
||||
DefaultDataSourceCreator creator = new DefaultDataSourceCreator();
|
||||
creator.setCreators(dataSourceCreators);
|
||||
creator.setDataSourceInitEvent(dataSourceInitEvent);
|
||||
creator.setPublicKey(properties.getPublicKey());
|
||||
creator.setLazy(properties.getLazy());
|
||||
creator.setP6spy(properties.getP6spy());
|
||||
creator.setSeata(properties.getSeata());
|
||||
creator.setSeataMode(properties.getSeataMode());
|
||||
return creator;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty(value = ShardingConstant.TENANT_DYNAMIC_DATASOURCE_PROP, havingValue = "false")
|
||||
public DynamicDataSourceAnnotationInterceptor tenantDataSourceAnnotationInterceptor(DsProcessor dsProcessor, DynamicDataSourceProperties dynamicDataSourceProperties) {
|
||||
return new DynamicDataSourceAnnotationInterceptor(dynamicDataSourceProperties.getAop().getAllowedPublicOnly(), dsProcessor);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@Role(value = BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||
@ConditionalOnProperty(value = ShardingConstant.TENANT_DYNAMIC_DATASOURCE_PROP, havingValue = "false")
|
||||
public DynamicDataSourceAnnotationAdvisor dynamicDatasourceAnnotationAdvisor(DynamicDataSourceAnnotationInterceptor dynamicDataSourceAnnotationInterceptor, DynamicDataSourceProperties dynamicDataSourceProperties) {
|
||||
DynamicDataSourceAnnotationAdvisor advisor = new DynamicDataSourceAnnotationAdvisor(dynamicDataSourceAnnotationInterceptor, DS.class);
|
||||
advisor.setOrder(dynamicDataSourceProperties.getAop().getOrder());
|
||||
return advisor;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty(value = ShardingConstant.TENANT_DYNAMIC_DATASOURCE_PROP, havingValue = "false")
|
||||
public DsProcessor dsProcessor() {
|
||||
DsProcessor headerProcessor = new DsJakartaHeaderProcessor();
|
||||
DsProcessor sessionProcessor = new DsJakartaSessionProcessor();
|
||||
DsSpelExpressionProcessor spelExpressionProcessor = new DsSpelExpressionProcessor();
|
||||
headerProcessor.setNextProcessor(sessionProcessor);
|
||||
sessionProcessor.setNextProcessor(spelExpressionProcessor);
|
||||
return headerProcessor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义分库分表动态数据源加载逻辑
|
||||
*/
|
||||
@Bean
|
||||
@Primary
|
||||
@ConditionalOnProperty(value = ShardingConstant.TENANT_DYNAMIC_DATASOURCE_PROP, havingValue = "false")
|
||||
public DynamicDataSourceProvider dynamicDataSourceProvider(DefaultDataSourceCreator dataSourceCreator, DataSourceProperties dataSourceProperties, DynamicDataSourceProperties dynamicDataSourceProperties) {
|
||||
String driverClassName = dataSourceProperties.getDriverClassName();
|
||||
String url = dataSourceProperties.getUrl();
|
||||
String username = dataSourceProperties.getUsername();
|
||||
String password = dataSourceProperties.getPassword();
|
||||
DataSourceProperty master = dynamicDataSourceProperties.getDatasource().get(dynamicDataSourceProperties.getPrimary());
|
||||
if (master != null) {
|
||||
driverClassName = master.getDriverClassName();
|
||||
url = master.getUrl();
|
||||
username = master.getUsername();
|
||||
password = master.getPassword();
|
||||
}
|
||||
return new ShardingDataSourceProvider(dataSourceCreator, dynamicDataSourceProperties, driverClassName, url, username, password, shardingSphereDataSource);
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置分库分表动态数据源
|
||||
*/
|
||||
@Bean
|
||||
@Primary
|
||||
@ConditionalOnProperty(value = ShardingConstant.TENANT_DYNAMIC_DATASOURCE_PROP, havingValue = "false")
|
||||
public DataSource dataSource(List<DynamicDataSourceProvider> providers, DynamicDataSourceProperties dynamicDataSourceProperties) {
|
||||
DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource(providers);
|
||||
dataSource.setPrimary(dynamicDataSourceProperties.getPrimary());
|
||||
dataSource.setStrict(dynamicDataSourceProperties.getStrict());
|
||||
dataSource.setStrategy(dynamicDataSourceProperties.getStrategy());
|
||||
dataSource.setP6spy(dynamicDataSourceProperties.getP6spy());
|
||||
dataSource.setSeata(dynamicDataSourceProperties.getSeata());
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Order
|
||||
@AutoConfiguration
|
||||
@AllArgsConstructor
|
||||
@ConditionalOnProperty(value = ShardingConstant.TENANT_DYNAMIC_DATASOURCE_PROP, havingValue = "true")
|
||||
public static class ShardingSphereDataSourceConfiguration implements SmartInitializingSingleton {
|
||||
@Lazy
|
||||
@Resource(name = "shardingSphereDataSource")
|
||||
private final DataSource shardingSphereDataSource;
|
||||
|
||||
private final DataSource dataSource;
|
||||
|
||||
@Override
|
||||
public void afterSingletonsInstantiated() {
|
||||
if (shardingSphereDataSource != null) {
|
||||
// 获取储存的数据源集合
|
||||
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
|
||||
// 设置ShardingSphere数据源
|
||||
ds.addDataSource(ShardingConstant.SHARDING_DATASOURCE_KEY, shardingSphereDataSource);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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";
|
||||
|
||||
}
|
||||
@@ -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.processor;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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