Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: fix configuration center can't read configuration using SpringCloudConfig #2172

Merged
merged 31 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
15b9997
fix#2104 merge SpringUtils and SpringContextProvider
xingfudeshi Jan 12, 2020
90c3902
fix bean name
xingfudeshi Jan 12, 2020
6cccd91
change bean name
xingfudeshi Jan 12, 2020
509c5ea
add UT.
xingfudeshi Jan 12, 2020
227ca61
Merge branch 'develop' into fix_2104
xingfudeshi Jan 13, 2020
52b865b
Merge branch 'develop' into fix_2104
xingfudeshi Jan 14, 2020
d68424c
Merge branch 'develop' into fix_2104
lovepoem Jan 15, 2020
093070f
Merge branch 'develop' into fix_2104
xingfudeshi Jan 15, 2020
f838633
fix reviews.
xingfudeshi Jan 15, 2020
908a9fe
Merge branch 'develop' into fix_2104
xingfudeshi Jan 16, 2020
fa9f15d
Merge branch 'develop' into fix_2104
xingfudeshi Jan 16, 2020
545a339
Merge branch 'develop' into fix_2104
xingfudeshi Jan 18, 2020
7b3af9d
Merge branch 'develop' into fix_2104
xingfudeshi Jan 19, 2020
80552cd
Merge branch 'develop' into fix_2104
xingfudeshi Jan 19, 2020
5e51bff
Merge branch 'develop' into fix_2104
xingfudeshi Jan 22, 2020
95f3491
Merge branch 'develop' into fix_2104
xingfudeshi Jan 22, 2020
36b4714
optimize UT.
xingfudeshi Jan 23, 2020
fe45388
Merge branch 'develop' into fix_2104
xingfudeshi Jan 23, 2020
bcf845d
Merge branch 'develop' into fix_2104
xingfudeshi Jan 23, 2020
108e212
Merge branch 'develop' into fix_2104
xingfudeshi Jan 23, 2020
b7eefd8
Merge branch 'develop' into fix_2104
zjinlei Jan 23, 2020
cd2199f
add copyright comments.
xingfudeshi Jan 24, 2020
076b937
fix review.
xingfudeshi Jan 24, 2020
6d95e38
fix review.
xingfudeshi Jan 24, 2020
d8de41e
fix reviews.
xingfudeshi Jan 25, 2020
5548d3a
fix constant name.
xingfudeshi Jan 25, 2020
7c95b52
avoid duplicate operations.
xingfudeshi Jan 25, 2020
5a33799
Merge branch 'develop' into fix_2104
xingfudeshi Feb 4, 2020
70f24ba
Merge branch 'develop' into fix_2104
xingfudeshi Feb 5, 2020
7ab3df1
Merge branch 'develop' into fix_2104
xingfudeshi Feb 5, 2020
936dd36
Merge branch 'develop' into fix_2104
xingfudeshi Feb 5, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions common/src/main/java/io/seata/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,12 @@ public class Constants {
* default charset is utf-8
*/
public static final Charset DEFAULT_CHARSET = Charset.forName(DEFAULT_CHARSET_NAME);
/**
* The constant OBJECT_KEY_SPRING_APPLICATION_CONTEXT
*/
public static final String OBJECT_KEY_SPRING_APPLICATION_CONTEXT = "springApplicationContext";
/**
* The constant BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER
*/
public static final String BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER = "springApplicationContextProvider";
}
46 changes: 46 additions & 0 deletions common/src/main/java/io/seata/common/holder/ObjectHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.common.holder;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import io.seata.common.exception.ShouldNeverHappenException;

/**
* @author xingfudeshi@gmail.com
* The enum object holder
*/
public enum ObjectHolder {
/**
* singleton instance
*/
INSTANCE;
private static final int MAP_SIZE = 8;
private static final Map<String, Object> OBJECT_MAP = new ConcurrentHashMap<>(MAP_SIZE);

public Object getObject(String objectKey) {
return OBJECT_MAP.get(objectKey);
}

public <T> T getObject(Class<T> clasz) {
return clasz.cast(OBJECT_MAP.values().stream().filter(clasz::isInstance).findAny().orElseThrow(() -> new ShouldNeverHappenException("Can't find any object of class " + clasz.getName())));
}

public Object setObject(String objectKey, Object object) {
return OBJECT_MAP.putIfAbsent(objectKey, object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
*/
package io.seata.config.springcloud;

import org.springframework.context.annotation.Import;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.annotation.Import;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import({SpringContextProvider.class})
@Import({SpringApplicationContextProviderRegistrar.class})
public @interface EnableSeataSpringConfig {
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,20 @@
*/
package io.seata.config.springcloud;

import io.seata.common.holder.ObjectHolder;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;

public class SpringContextProvider implements ApplicationContextAware {
private static ApplicationContext applicationContext;
private static Environment environment;
import static io.seata.common.Constants.OBJECT_KEY_SPRING_APPLICATION_CONTEXT;

/**
* @author xingfudeshi@gmail.com
* The type spring application context provider
*/
public class SpringApplicationContextProvider implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextProvider.applicationContext = applicationContext;
SpringContextProvider.environment = applicationContext.getEnvironment();
}

public static Environment getEnvironment() {
return environment;
}

public static ApplicationContext getApplicationContext() {
return applicationContext;
ObjectHolder.INSTANCE.setObject(OBJECT_KEY_SPRING_APPLICATION_CONTEXT, applicationContext);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.config.springcloud;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import static io.seata.common.Constants.BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER;

/**
* @author xingfudeshi@gmail.com
* The type spring application context provider registrar
*/
public class SpringApplicationContextProviderRegistrar implements ImportBeanDefinitionRegistrar {

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
if (!registry.containsBeanDefinition(BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER)) {
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(SpringApplicationContextProvider.class).getBeanDefinition();
registry.registerBeanDefinition(BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER, beanDefinition);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

import java.util.Set;

import io.seata.common.holder.ObjectHolder;
import io.seata.common.util.StringUtils;
import io.seata.config.AbstractConfiguration;
import io.seata.config.ConfigurationChangeListener;
import org.springframework.context.ApplicationContext;

public class SpringCloudConfiguration extends AbstractConfiguration {

Expand Down Expand Up @@ -49,10 +51,11 @@ public String getTypeName() {

@Override
public String getConfig(String dataId, String defaultValue, long timeoutMills) {
if (null == SpringContextProvider.getEnvironment()) {
ApplicationContext applicationContext = ObjectHolder.INSTANCE.getObject(ApplicationContext.class);
if (null == applicationContext || null == applicationContext.getEnvironment()) {
return defaultValue;
}
String conf = SpringContextProvider.getEnvironment().getProperty(PREFIX + dataId);
String conf = applicationContext.getEnvironment().getProperty(PREFIX + dataId);
return StringUtils.isNotBlank(conf) ? conf : defaultValue;
}

Expand Down
2 changes: 1 addition & 1 deletion script/client/conf/registry.conf
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ registry {
}

config {
# file、nacos 、apollo、zk、consul、etcd3
# file、nacos 、apollo、zk、consul、etcd3、springCloudConfig
type = "file"

nacos {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import io.seata.spring.annotation.GlobalTransactionScanner;
import io.seata.spring.annotation.datasource.SeataDataSourceBeanPostProcessor;
import io.seata.spring.boot.autoconfigure.properties.SeataProperties;
import io.seata.spring.boot.autoconfigure.util.SpringUtils;
import io.seata.spring.boot.autoconfigure.provider.SpringApplicationContextProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand All @@ -29,6 +29,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import static io.seata.common.Constants.BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER;
import static io.seata.spring.annotation.datasource.AutoDataSourceProxyRegistrar.BEAN_NAME_SEATA_DATA_SOURCE_BEAN_POST_PROCESSOR;

/**
Expand All @@ -41,13 +42,14 @@
public class SeataAutoConfiguration {
private static final Logger LOGGER = LoggerFactory.getLogger(SeataAutoConfiguration.class);

@Bean
public SpringUtils springUtils() {
return new SpringUtils();
@Bean(BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER)
@ConditionalOnMissingBean(name = {BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER})
public SpringApplicationContextProvider springApplicationContextProvider() {
return new SpringApplicationContextProvider();
}

@Bean
@DependsOn({"springUtils"})
@DependsOn({BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER})
@ConditionalOnMissingBean(GlobalTransactionScanner.class)
public GlobalTransactionScanner globalTransactionScanner(SeataProperties seataProperties) {
if (LOGGER.isInfoEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@ConfigurationProperties(prefix = CONFIG_PREFIX)
public class ConfigProperties {
/**
* file, nacos, apollo, zk, consul, etcd3
* file, nacos, apollo, zk, consul, etcd3, springCloudConfig
*/
private String type = "file";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.spring.boot.autoconfigure.provider;

import io.seata.common.holder.ObjectHolder;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import static io.seata.common.Constants.OBJECT_KEY_SPRING_APPLICATION_CONTEXT;

/**
* @author xingfudeshi@gmail.com
* The type spring application context provider
*/
public class SpringApplicationContextProvider implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ObjectHolder.INSTANCE.setObject(OBJECT_KEY_SPRING_APPLICATION_CONTEXT, applicationContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@
import java.util.Optional;
import java.util.stream.Stream;

import io.seata.common.holder.ObjectHolder;
import io.seata.config.Configuration;
import io.seata.config.ExtConfigurationProvider;
import io.seata.spring.boot.autoconfigure.StarterConstants;
import io.seata.spring.boot.autoconfigure.util.SpringUtils;
import io.seata.spring.boot.autoconfigure.util.StringFormatUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.context.ApplicationContext;

import static io.seata.spring.boot.autoconfigure.StarterConstants.NORMALIZED_KEY_GROUPLIST;
import static io.seata.spring.boot.autoconfigure.StarterConstants.NORMALIZED_KEY_VGROUP_MAPPING;
Expand Down Expand Up @@ -97,7 +98,7 @@ private Object get(String dataId) throws IllegalAccessException {
String propertySuffix = getPropertySuffix(dataId);
Class propertyClass = getPropertyClass(getPropertyPrefix(dataId));
if (null != propertyClass) {
Object propertyObject = SpringUtils.getBean(propertyClass);
Object propertyObject = ObjectHolder.INSTANCE.getObject(ApplicationContext.class).getBean(propertyClass);
Optional<Field> fieldOptional = Stream.of(propertyObject.getClass().getDeclaredFields()).filter(
f -> f.getName().equalsIgnoreCase(propertySuffix)).findAny();
if (fieldOptional.isPresent()) {
Expand Down

This file was deleted.

Loading