mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
refactor(nacos): Modify nacos service caching mechanism
This commit is contained in:
@@ -44,6 +44,11 @@ public class NacosConfigAutoConfiguration {
|
||||
return new NacosConfigProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NacosConfigManager nacosConfigManager() {
|
||||
return new NacosConfigManager();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NacosRefreshProperties nacosRefreshProperties() {
|
||||
return new NacosRefreshProperties();
|
||||
@@ -57,9 +62,10 @@ public class NacosConfigAutoConfiguration {
|
||||
@Bean
|
||||
public NacosContextRefresher nacosContextRefresher(
|
||||
NacosConfigProperties nacosConfigProperties,
|
||||
NacosConfigManager nacosConfigManager,
|
||||
NacosRefreshProperties nacosRefreshProperties,
|
||||
NacosRefreshHistory refreshHistory) {
|
||||
return new NacosContextRefresher(nacosRefreshProperties, refreshHistory,
|
||||
nacosConfigProperties.configServiceInstance());
|
||||
nacosConfigManager.getConfigService());
|
||||
}
|
||||
}
|
||||
|
@@ -36,6 +36,11 @@ public class NacosConfigBootstrapConfiguration {
|
||||
return new NacosConfigProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NacosConfigManager nacosConfigManager() {
|
||||
return new NacosConfigManager();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NacosPropertySourceLocator nacosPropertySourceLocator(
|
||||
NacosConfigProperties nacosConfigProperties) {
|
||||
|
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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 com.alibaba.cloud.nacos;
|
||||
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
|
||||
* @since
|
||||
*/
|
||||
public class NacosConfigManager implements ApplicationContextAware {
|
||||
|
||||
private ConfigService configService;
|
||||
|
||||
public ConfigService getConfigService() {
|
||||
return configService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
NacosConfigProperties properties = applicationContext.getBean(NacosConfigProperties.class);
|
||||
configService = properties.configServiceInstance();
|
||||
}
|
||||
}
|
@@ -184,7 +184,7 @@ public class NacosConfigProperties {
|
||||
*/
|
||||
private List<Config> extConfig;
|
||||
|
||||
private static ConfigService CONFIG_SERVICE;
|
||||
private ConfigService configService;
|
||||
|
||||
// todo sts support
|
||||
|
||||
@@ -400,10 +400,11 @@ public class NacosConfigProperties {
|
||||
+ refreshableDataids + '\'' + ", extConfig=" + extConfig + '}';
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public ConfigService configServiceInstance() {
|
||||
|
||||
if (null != CONFIG_SERVICE) {
|
||||
return CONFIG_SERVICE;
|
||||
if (null != configService) {
|
||||
return configService;
|
||||
}
|
||||
|
||||
Properties properties = new Properties();
|
||||
@@ -430,8 +431,8 @@ public class NacosConfigProperties {
|
||||
}
|
||||
|
||||
try {
|
||||
CONFIG_SERVICE = NacosFactory.createConfigService(properties);
|
||||
return CONFIG_SERVICE;
|
||||
configService = NacosFactory.createConfigService(properties);
|
||||
return configService;
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("create config service error!properties={},e=,", this, e);
|
||||
|
@@ -19,6 +19,7 @@ package com.alibaba.cloud.nacos.client;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosConfigManager;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
|
||||
@@ -54,14 +55,17 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
|
||||
private NacosConfigProperties nacosConfigProperties;
|
||||
|
||||
public NacosPropertySourceLocator(NacosConfigProperties nacosConfigProperties) {
|
||||
private NacosConfigManager nacosConfigManager;
|
||||
|
||||
public NacosPropertySourceLocator(NacosConfigManager nacosConfigManager, NacosConfigProperties nacosConfigProperties) {
|
||||
this.nacosConfigManager = nacosConfigManager;
|
||||
this.nacosConfigProperties = nacosConfigProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PropertySource<?> locate(Environment env) {
|
||||
|
||||
ConfigService configService = nacosConfigProperties.configServiceInstance();
|
||||
ConfigService configService = nacosConfigManager.getConfigService();
|
||||
|
||||
if (null == configService) {
|
||||
log.warn("no instance of config service found, can't load config from nacos");
|
||||
|
@@ -16,6 +16,7 @@
|
||||
|
||||
package com.alibaba.cloud.nacos.endpoint;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosConfigManager;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
@@ -39,6 +40,9 @@ public class NacosConfigEndpointAutoConfiguration {
|
||||
@Autowired
|
||||
private NacosConfigProperties nacosConfigProperties;
|
||||
|
||||
@Autowired
|
||||
private NacosConfigManager nacosConfigManager;
|
||||
|
||||
@Autowired
|
||||
private NacosRefreshHistory nacosRefreshHistory;
|
||||
|
||||
@@ -51,6 +55,6 @@ public class NacosConfigEndpointAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public NacosConfigHealthIndicator nacosConfigHealthIndicator() {
|
||||
return new NacosConfigHealthIndicator(nacosConfigProperties.configServiceInstance());
|
||||
return new NacosConfigHealthIndicator(nacosConfigManager.getConfigService());
|
||||
}
|
||||
}
|
||||
|
@@ -22,6 +22,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosConfigManager;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
@@ -83,6 +84,9 @@ public class NacosConfigEndpointTests {
|
||||
@Autowired
|
||||
private NacosConfigProperties properties;
|
||||
|
||||
@Autowired
|
||||
private NacosConfigManager nacosConfigManager;
|
||||
|
||||
@Autowired
|
||||
private NacosRefreshHistory refreshHistory;
|
||||
|
||||
@@ -99,7 +103,7 @@ public class NacosConfigEndpointTests {
|
||||
Builder builder = new Builder();
|
||||
|
||||
NacosConfigHealthIndicator healthIndicator = new NacosConfigHealthIndicator(
|
||||
properties.configServiceInstance());
|
||||
nacosConfigManager.getConfigService());
|
||||
healthIndicator.doHealthCheck(builder);
|
||||
|
||||
Builder builder1 = new Builder();
|
||||
|
Reference in New Issue
Block a user