1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

refactor Nacos ConfigService & NamingService

This commit is contained in:
fangjian0423
2019-11-11 11:38:27 +08:00
parent f31b48a01f
commit fa886418d7
25 changed files with 67 additions and 245 deletions

View File

@@ -44,11 +44,6 @@ public class NacosConfigAutoConfiguration {
return new NacosConfigProperties();
}
@Bean
public NacosConfigManager nacosConfigManager() {
return new NacosConfigManager();
}
@Bean
public NacosRefreshProperties nacosRefreshProperties() {
return new NacosRefreshProperties();
@@ -61,10 +56,10 @@ public class NacosConfigAutoConfiguration {
@Bean
public NacosContextRefresher nacosContextRefresher(
NacosConfigManager nacosConfigManager,
NacosConfigProperties configProperties,
NacosRefreshProperties nacosRefreshProperties,
NacosRefreshHistory refreshHistory) {
return new NacosContextRefresher(nacosRefreshProperties, refreshHistory,
nacosConfigManager.getConfigService());
configProperties.configServiceInstance());
}
}

View File

@@ -36,17 +36,10 @@ public class NacosConfigBootstrapConfiguration {
return new NacosConfigProperties();
}
@Bean
@ConditionalOnMissingBean
public NacosConfigManager nacosConfigManager() {
return new NacosConfigManager();
}
@Bean
public NacosPropertySourceLocator nacosPropertySourceLocator(
NacosConfigManager nacosConfigManager,
NacosConfigProperties nacosConfigProperties) {
return new NacosPropertySourceLocator(nacosConfigManager, nacosConfigProperties);
return new NacosPropertySourceLocator(nacosConfigProperties);
}
}

View File

@@ -1,53 +0,0 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* 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 com.alibaba.cloud.nacos;
import java.util.Objects;
import com.alibaba.cloud.nacos.diagnostics.analyzer.NacosConnectionFailureException;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
*/
public class NacosConfigManager {
private static ConfigService service = null;
@Autowired
private NacosConfigProperties properties;
public ConfigService getConfigService() {
if (Objects.isNull(service)) {
try {
service = NacosFactory
.createConfigService(properties.getConfigServiceProperties());
properties.initConfigService(service);
}
catch (NacosException e) {
throw new NacosConnectionFailureException(properties.getServerAddr(),
e.getMessage(), e);
}
}
return service;
}
}

View File

@@ -22,7 +22,10 @@ import java.util.Properties;
import javax.annotation.PostConstruct;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -190,7 +193,7 @@ public class NacosConfigProperties {
*/
private List<Config> extConfig;
private ConfigService configService;
private static ConfigService configService;
// todo sts support
@@ -355,18 +358,22 @@ public class NacosConfigProperties {
}
/**
* @see NacosConfigManager#getConfigService() .
* @return ConfigService
*/
@Deprecated
public ConfigService configServiceInstance() {
if (null == configService) {
try {
configService = NacosFactory
.createConfigService(getConfigServiceProperties());
}
catch (NacosException e) {
log.error("create naming service error!properties={},e=,", this, e);
return null;
}
}
return configService;
}
public void initConfigService(ConfigService configService) {
this.configService = configService;
}
public Properties getConfigServiceProperties() {
Properties properties = new Properties();
properties.put(SERVER_ADDR, Objects.toString(this.serverAddr, ""));

View File

@@ -18,7 +18,6 @@ package com.alibaba.cloud.nacos.client;
import java.util.List;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.cloud.nacos.NacosPropertySourceRepository;
import com.alibaba.cloud.nacos.parser.NacosDataParserHandler;
@@ -58,18 +57,14 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
private NacosConfigProperties nacosConfigProperties;
private NacosConfigManager nacosConfigManager;
public NacosPropertySourceLocator(NacosConfigManager nacosConfigManager,
NacosConfigProperties nacosConfigProperties) {
this.nacosConfigManager = nacosConfigManager;
public NacosPropertySourceLocator(NacosConfigProperties nacosConfigProperties) {
this.nacosConfigProperties = nacosConfigProperties;
}
@Override
public PropertySource<?> locate(Environment env) {
ConfigService configService = nacosConfigManager.getConfigService();
ConfigService configService = nacosConfigProperties.configServiceInstance();
if (null == configService) {
log.warn("no instance of config service found, can't load config from nacos");

View File

@@ -16,7 +16,6 @@
package com.alibaba.cloud.nacos.endpoint;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
@@ -40,9 +39,6 @@ public class NacosConfigEndpointAutoConfiguration {
@Autowired
private NacosConfigProperties nacosConfigProperties;
@Autowired
private NacosConfigManager nacosConfigManager;
@Autowired
private NacosRefreshHistory nacosRefreshHistory;
@@ -55,6 +51,7 @@ public class NacosConfigEndpointAutoConfiguration {
@Bean
public NacosConfigHealthIndicator nacosConfigHealthIndicator() {
return new NacosConfigHealthIndicator(nacosConfigManager.getConfigService());
return new NacosConfigHealthIndicator(
nacosConfigProperties.configServiceInstance());
}
}

View File

@@ -21,7 +21,6 @@ import java.util.Map;
import com.alibaba.cloud.nacos.NacosConfigAutoConfiguration;
import com.alibaba.cloud.nacos.NacosConfigBootstrapConfiguration;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
import com.alibaba.nacos.client.config.NacosConfigService;
@@ -85,9 +84,6 @@ public class NacosConfigEndpointTests {
@Autowired
private NacosConfigProperties properties;
@Autowired
private NacosConfigManager nacosConfigManager;
@Autowired
private NacosRefreshHistory refreshHistory;
@@ -104,7 +100,7 @@ public class NacosConfigEndpointTests {
Builder builder = new Builder();
NacosConfigHealthIndicator healthIndicator = new NacosConfigHealthIndicator(
nacosConfigManager.getConfigService());
properties.configServiceInstance());
healthIndicator.doHealthCheck(builder);
Builder builder1 = new Builder();