Skip to content

Commit

Permalink
bugfix: fix configuration center can't read configuration using Sprin…
Browse files Browse the repository at this point in the history
…gCloudConfig (#2172)
  • Loading branch information
xingfudeshi committed Feb 5, 2020
1 parent b8a35d1 commit 1f22626
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 150 deletions.
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,14 +21,15 @@
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.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.CLIENT_RM_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CLIENT_TM_PREFIX;
Expand Down Expand Up @@ -108,7 +109,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

0 comments on commit 1f22626

Please sign in to comment.