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

issues87 修复在多个/多级SpringContext并存环境下,EventPublishingConfigService发送事件混乱,引发后续依赖事… #260

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import java.util.Properties;
import java.util.concurrent.ExecutorService;

import com.alibaba.nacos.spring.util.NacosUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;

import com.alibaba.nacos.api.NacosFactory;
Expand All @@ -46,9 +48,7 @@
* @since 0.1.0
*/
@SuppressWarnings("unchecked")
public class CacheableEventPublishingNacosServiceFactory implements NacosServiceFactory {

private static volatile CacheableEventPublishingNacosServiceFactory SINGLETON = new CacheableEventPublishingNacosServiceFactory();
public class CacheableEventPublishingNacosServiceFactory implements NacosServiceFactory, ApplicationContextAware {

private final Map<String, ConfigService> configServicesCache = new LinkedHashMap<String, ConfigService>(
2);
Expand All @@ -75,10 +75,6 @@ public CacheableEventPublishingNacosServiceFactory() {
createWorkerManager = Collections.unmodifiableMap(createWorkerManager);
}

public static CacheableEventPublishingNacosServiceFactory getSingleton() {
return SINGLETON;
}

@Override
public ConfigService createConfigService(Properties properties)
throws NacosException {
Expand Down Expand Up @@ -146,9 +142,9 @@ else if (o instanceof NamingMaintainService) {
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.context = (ConfigurableApplicationContext) applicationContext;
this.nacosConfigListenerExecutor = getSingleton().nacosConfigListenerExecutor == null
this.nacosConfigListenerExecutor = this.nacosConfigListenerExecutor == null
? getNacosConfigListenerExecutorIfPresent(applicationContext)
: getSingleton().nacosConfigListenerExecutor;
: this.nacosConfigListenerExecutor;
}

@Override
Expand Down Expand Up @@ -243,15 +239,16 @@ class ConfigCreateWorker extends AbstractCreateWorker<ConfigService> {
public ConfigService run(Properties properties, ConfigService service)
throws NacosException {
String cacheKey = identify(properties);
cacheKey += NacosUtils.SEPARATOR + context.getId() + NacosUtils.SEPARATOR;
ConfigService configService = configServicesCache.get(cacheKey);

if (configService == null) {
if (service == null) {
service = NacosFactory.createConfigService(properties);
}
configService = new EventPublishingConfigService(service, properties,
getSingleton().context,
getSingleton().nacosConfigListenerExecutor);
CacheableEventPublishingNacosServiceFactory.this.context,
CacheableEventPublishingNacosServiceFactory.this.nacosConfigListenerExecutor);
configServicesCache.put(cacheKey, configService);
}
return configService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,14 @@ public static void registerNacosConfigBeans(BeanDefinitionRegistry registry,
registerConfigServiceBeanBuilder(registry);

registerLoggingNacosConfigMetadataEventListener(registry);

registerCacheableEventPublishingNacosServiceFactory(registry,beanFactory);
}

private static void registerCacheableEventPublishingNacosServiceFactory(BeanDefinitionRegistry registry, BeanFactory beanFactory) {
registerInfrastructureBeanIfAbsent(registry,
NacosServiceFactory.BEAN_NAME,
CacheableEventPublishingNacosServiceFactory.class);
}

/**
Expand Down Expand Up @@ -491,21 +499,8 @@ public static Properties getGlobalPropertiesBean(BeanFactory beanFactory)
*/
public static NacosServiceFactory getNacosServiceFactoryBean(BeanFactory beanFactory)
throws NoSuchBeanDefinitionException {
if (null == beanFactory) {
return getNacosServiceFactoryBean();
}
ApplicationContextHolder applicationContextHolder = getApplicationContextHolder(
beanFactory);
CacheableEventPublishingNacosServiceFactory nacosServiceFactory = CacheableEventPublishingNacosServiceFactory
.getSingleton();
nacosServiceFactory
.setApplicationContext(applicationContextHolder.getApplicationContext());
return nacosServiceFactory;
}

public static NacosServiceFactory getNacosServiceFactoryBean()
throws NoSuchBeanDefinitionException {
return CacheableEventPublishingNacosServiceFactory.getSingleton();
return beanFactory.getBean(NacosServiceFactory.BEAN_NAME,
NacosServiceFactory.class);
}

public static ApplicationContextHolder getApplicationContextHolder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@
import java.util.HashMap;
import java.util.Map;

import com.alibaba.nacos.spring.factory.CacheableEventPublishingNacosServiceFactory;
import com.alibaba.nacos.spring.factory.NacosServiceFactory;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
Expand Down Expand Up @@ -84,6 +87,8 @@ public class NacosPropertySourcePostProcessorTest
+ "PATH = /My/Path";
@NacosInjected
private ConfigService configService;
@Autowired
private NacosServiceFactory nacosServiceFactory;

@Override
public void init(EmbeddedNacosHttpServer httpServer) {
Expand Down Expand Up @@ -179,7 +184,7 @@ private AnnotationConfigApplicationContext createContext(String dataId,
configService.publishConfig(dataId, groupId, content);

beanFactory.registerSingleton(CONFIG_SERVICE_BEAN_NAME, configService);

beanFactory.registerSingleton(NacosServiceFactory.BEAN_NAME,nacosServiceFactory);
context.register(AnnotationNacosInjectedBeanPostProcessor.class,
NacosPropertySourcePostProcessor.class, ConfigServiceBeanBuilder.class,
AnnotationNacosPropertySourceBuilder.class, TestConfiguration.class,
Expand Down