未验证 提交 7f6a863f 编写于 作者: J Juan Pan(Trista) 提交者: GitHub

Refactor yaml rule config for spring boot (#6937)

* Add YamlEncryptRuleSpringBootConfiguration

* Add YamlMasterSlaveRuleSpringBootConfiguration

* Add YamlShadowRuleSpringBootConfiguration

* Add YamlShardingRuleSpringBootConfiguration
......@@ -17,15 +17,18 @@
package org.apache.shardingsphere.encrypt.spring.boot;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.encrypt.algorithm.config.AlgorithmProvidedEncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.encrypt.spring.boot.algorithm.EncryptAlgorithmProvidedBeanRegistry;
import org.apache.shardingsphere.encrypt.spring.boot.condition.EncryptSpringBootCondition;
import org.apache.shardingsphere.encrypt.spring.boot.rule.YamlEncryptRuleSpringBootConfiguration;
import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.yaml.swapper.EncryptRuleAlgorithmProviderConfigurationYamlSwapper;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
......@@ -39,33 +42,25 @@ import java.util.Optional;
* Encrypt rule configuration for spring boot.
*/
@Configuration
@EnableConfigurationProperties(YamlEncryptRuleSpringBootConfiguration.class)
@ConditionalOnClass(YamlEncryptRuleConfiguration.class)
@Conditional(EncryptSpringBootCondition.class)
@RequiredArgsConstructor
public class EncryptRuleSpringBootConfiguration {
private final EncryptRuleAlgorithmProviderConfigurationYamlSwapper swapper = new EncryptRuleAlgorithmProviderConfigurationYamlSwapper();
/**
* Encrypt YAML rule spring boot configuration.
*
* @return YAML rule configuration
*/
@Bean
@ConfigurationProperties(prefix = "spring.shardingsphere.rules.encrypt")
public YamlEncryptRuleConfiguration encryptConfig() {
return new YamlEncryptRuleConfiguration();
}
private final YamlEncryptRuleSpringBootConfiguration yamlConfig;
/**
* Encrypt rule configuration for spring boot.
*
* @param yamlEncryptRuleConfiguration YAML encrypt rule configuration
* @param encryptors encryptors algorithm to map
* @return encrypt rule configuration
*/
@Bean
public RuleConfiguration encryptRuleConfiguration(final YamlEncryptRuleConfiguration yamlEncryptRuleConfiguration, final ObjectProvider<Map<String, EncryptAlgorithm>> encryptors) {
AlgorithmProvidedEncryptRuleConfiguration result = swapper.swapToObject(yamlEncryptRuleConfiguration);
public RuleConfiguration encryptRuleConfiguration(final ObjectProvider<Map<String, EncryptAlgorithm>> encryptors) {
AlgorithmProvidedEncryptRuleConfiguration result = swapper.swapToObject(yamlConfig.getEncrypt());
result.setEncryptors(Optional.ofNullable(encryptors.getIfAvailable()).orElse(Collections.emptyMap()));
return result;
}
......
......@@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.apache.shardingsphere.encrypt.spring.boot;
package org.apache.shardingsphere.encrypt.spring.boot.algorithm;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry;
......
/*
* 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.encrypt.spring.boot.rule;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* YAML encrypt rule spring boot configuration.
*/
@ConfigurationProperties(prefix = "spring.shardingsphere.rules")
@Getter
@Setter
public final class YamlEncryptRuleSpringBootConfiguration {
private YamlEncryptRuleConfiguration encrypt;
}
......@@ -17,15 +17,18 @@
package org.apache.shardingsphere.masterslave.spring.boot;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.masterslave.algorithm.config.AlgorithmProvidedMasterSlaveRuleConfiguration;
import org.apache.shardingsphere.masterslave.spi.MasterSlaveLoadBalanceAlgorithm;
import org.apache.shardingsphere.masterslave.spring.boot.algorithm.MasterSlaveAlgorithmProvidedBeanRegistry;
import org.apache.shardingsphere.masterslave.spring.boot.condition.MasterSlaveSpringBootCondition;
import org.apache.shardingsphere.masterslave.spring.boot.rule.YamlMasterSlaveRuleSpringBootConfiguration;
import org.apache.shardingsphere.masterslave.yaml.config.YamlMasterSlaveRuleConfiguration;
import org.apache.shardingsphere.masterslave.yaml.swapper.MasterSlaveRuleAlgorithmProviderConfigurationYamlSwapper;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
......@@ -39,34 +42,25 @@ import java.util.Optional;
* Rule spring boot configuration for master-slave.
*/
@Configuration
@EnableConfigurationProperties(YamlMasterSlaveRuleSpringBootConfiguration.class)
@ConditionalOnClass(YamlMasterSlaveRuleConfiguration.class)
@Conditional(MasterSlaveSpringBootCondition.class)
@RequiredArgsConstructor
public class MasterSlaveRuleSpringbootConfiguration {
private final MasterSlaveRuleAlgorithmProviderConfigurationYamlSwapper swapper = new MasterSlaveRuleAlgorithmProviderConfigurationYamlSwapper();
/**
* YAML rule spring boot configuration for master-slave.
*
* @return YAML rule configuration
*/
@Bean
@ConfigurationProperties(prefix = "spring.shardingsphere.rules.master-slave")
public YamlMasterSlaveRuleConfiguration masterSlaveConfig() {
return new YamlMasterSlaveRuleConfiguration();
}
private final YamlMasterSlaveRuleSpringBootConfiguration yamlConfig;
/**
* Master slave rule configuration for spring boot.
*
* @param yamlRuleConfiguration YAML master slave rule configuration
* @param loadBalanceAlgorithms load balance algorithms
* @return master slave rule configuration
*/
@Bean
public RuleConfiguration masterSlaveRuleConfiguration(final YamlMasterSlaveRuleConfiguration yamlRuleConfiguration,
final ObjectProvider<Map<String, MasterSlaveLoadBalanceAlgorithm>> loadBalanceAlgorithms) {
AlgorithmProvidedMasterSlaveRuleConfiguration result = swapper.swapToObject(yamlRuleConfiguration);
public RuleConfiguration masterSlaveRuleConfiguration(final ObjectProvider<Map<String, MasterSlaveLoadBalanceAlgorithm>> loadBalanceAlgorithms) {
AlgorithmProvidedMasterSlaveRuleConfiguration result = swapper.swapToObject(yamlConfig.getMasterSlave());
Map<String, MasterSlaveLoadBalanceAlgorithm> balanceAlgorithmMap = Optional.ofNullable(loadBalanceAlgorithms.getIfAvailable()).orElse(Collections.emptyMap());
result.setLoadBalanceAlgorithms(balanceAlgorithmMap);
return result;
......@@ -83,4 +77,3 @@ public class MasterSlaveRuleSpringbootConfiguration {
return new MasterSlaveAlgorithmProvidedBeanRegistry(environment);
}
}
......@@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.apache.shardingsphere.masterslave.spring.boot;
package org.apache.shardingsphere.masterslave.spring.boot.algorithm;
import org.apache.shardingsphere.masterslave.spi.MasterSlaveLoadBalanceAlgorithm;
import org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry;
......
/*
* 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.masterslave.spring.boot.rule;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.masterslave.yaml.config.YamlMasterSlaveRuleConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* YAML master-slave rule spring boot configuration.
*/
@ConfigurationProperties(prefix = "spring.shardingsphere.rules")
@Getter
@Setter
public final class YamlMasterSlaveRuleSpringBootConfiguration {
private YamlMasterSlaveRuleConfiguration masterSlave;
}
......@@ -17,12 +17,14 @@
package org.apache.shardingsphere.shadow.spring.boot;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.shadow.spring.boot.condition.ShadowSpringBootCondition;
import org.apache.shardingsphere.shadow.spring.boot.rule.YamlShadowRuleSpringBootConfiguration;
import org.apache.shardingsphere.shadow.yaml.config.YamlShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.yaml.swapper.ShadowRuleConfigurationYamlSwapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
......@@ -31,31 +33,23 @@ import org.springframework.context.annotation.Configuration;
* Shadow rule configuration for spring boot.
*/
@Configuration
@EnableConfigurationProperties(YamlShadowRuleSpringBootConfiguration.class)
@ConditionalOnClass(YamlShadowRuleConfiguration.class)
@Conditional(ShadowSpringBootCondition.class)
@RequiredArgsConstructor
public class ShadowRuleSpringBootConfiguration {
private final ShadowRuleConfigurationYamlSwapper swapper = new ShadowRuleConfigurationYamlSwapper();
/**
* Shadow YAML rule spring boot configuration.
*
* @return YAML rule configuration
*/
@Bean
@ConfigurationProperties(prefix = "spring.shardingsphere.rules.shadow")
public YamlShadowRuleConfiguration shadow() {
return new YamlShadowRuleConfiguration();
}
private final YamlShadowRuleSpringBootConfiguration yamlConfig;
/**
* Shadow rule configuration.
*
* @param yamlShadowRuleConfig YAML shadow rule configuration
* @return shadow rule configuration
*/
@Bean
public RuleConfiguration shadowRuleConfiguration(final YamlShadowRuleConfiguration yamlShadowRuleConfig) {
return swapper.swapToObject(yamlShadowRuleConfig);
public RuleConfiguration shadowRuleConfiguration() {
return swapper.swapToObject(yamlConfig.getShadow());
}
}
/*
* 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.shadow.spring.boot.rule;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.shadow.yaml.config.YamlShadowRuleConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* YAML shadow rule spring boot configuration.
*/
@ConfigurationProperties(prefix = "spring.shardingsphere.rules")
@Getter
@Setter
public final class YamlShadowRuleSpringBootConfiguration {
private YamlShadowRuleConfiguration shadow;
}
......@@ -17,16 +17,19 @@
package org.apache.shardingsphere.sharding.spring.boot;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.sharding.algorithm.config.AlgorithmProvidedShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
import org.apache.shardingsphere.sharding.spring.boot.algorithm.ShardingAlgorithmProvidedBeanRegistry;
import org.apache.shardingsphere.sharding.spring.boot.condition.ShardingSpringBootCondition;
import org.apache.shardingsphere.sharding.spring.boot.rule.YamlShardingRuleSpringBootConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.yaml.swapper.ShardingRuleAlgorithmProviderConfigurationYamlSwapper;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
......@@ -40,38 +43,29 @@ import java.util.Optional;
* Sharding rule configuration for spring boot.
*/
@Configuration
@EnableConfigurationProperties(YamlShardingRuleSpringBootConfiguration.class)
@ConditionalOnClass(YamlShardingRuleConfiguration.class)
@Conditional(ShardingSpringBootCondition.class)
@RequiredArgsConstructor
public class ShardingRuleSpringBootConfiguration {
private final ShardingRuleAlgorithmProviderConfigurationYamlSwapper swapper = new ShardingRuleAlgorithmProviderConfigurationYamlSwapper();
/**
* Create sharding YAML rule spring boot configuration bean.
*
* @return YAML rule configuration
*/
@Bean
@ConfigurationProperties(prefix = "spring.shardingsphere.rules.sharding")
public YamlShardingRuleConfiguration shardingConfig() {
return new YamlShardingRuleConfiguration();
}
private final YamlShardingRuleSpringBootConfiguration yamlConfig;
/**
* Create sharding rule configuration bean.
*
* @param yamlShardingRuleConfiguration YAML sharding rule configuration
* @param shardingAlgorithmProvider sharding algorithm provider
* @param keyGenerateAlgorithmProvider key generate algorithm provider
* @return sharding rule configuration
*/
@Bean
public RuleConfiguration shardingRuleConfiguration(final YamlShardingRuleConfiguration yamlShardingRuleConfiguration,
final ObjectProvider<Map<String, ShardingAlgorithm>> shardingAlgorithmProvider,
public RuleConfiguration shardingRuleConfiguration(final ObjectProvider<Map<String, ShardingAlgorithm>> shardingAlgorithmProvider,
final ObjectProvider<Map<String, KeyGenerateAlgorithm>> keyGenerateAlgorithmProvider) {
Map<String, ShardingAlgorithm> shardingAlgorithmMap = Optional.ofNullable(shardingAlgorithmProvider.getIfAvailable()).orElse(Collections.emptyMap());
Map<String, KeyGenerateAlgorithm> keyGenerateAlgorithmMap = Optional.ofNullable(keyGenerateAlgorithmProvider.getIfAvailable()).orElse(Collections.emptyMap());
AlgorithmProvidedShardingRuleConfiguration result = swapper.swapToObject(yamlShardingRuleConfiguration);
AlgorithmProvidedShardingRuleConfiguration result = swapper.swapToObject(yamlConfig.getSharding());
result.setShardingAlgorithms(shardingAlgorithmMap);
result.setKeyGenerators(keyGenerateAlgorithmMap);
return result;
......
/*
* 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.sharding.spring.boot.rule;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* YAML sharding rule spring boot configuration.
*/
@ConfigurationProperties(prefix = "spring.shardingsphere.rules")
@Getter
@Setter
public final class YamlShardingRuleSpringBootConfiguration {
private YamlShardingRuleConfiguration sharding;
}
......@@ -29,7 +29,7 @@ import java.util.Properties;
@ConfigurationProperties(prefix = "spring.shardingsphere")
@Getter
@Setter
public class SpringBootPropertiesConfiguration {
public final class SpringBootPropertiesConfiguration {
private Properties props = new Properties();
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册