Skip to content

Commit

Permalink
Merge pull request #1114 from huaweicloud/master_locator
Browse files Browse the repository at this point in the history
[#1113] support load systemManage property sources for extension
  • Loading branch information
dantesun authored Nov 28, 2023
2 parents 332c41b + acb2070 commit ed12ff2
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 12 deletions.
4 changes: 4 additions & 0 deletions spring-cloud-huawei-nacos/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>spring-cloud-huawei-governance</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
* 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.huaweicloud.nacos;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.huaweicloud.nacos.config.SystemManagedPropertySourceLocator;

@Configuration
@ConditionalOnNacosDiscoveryEnabled
@ConditionalOnProperty(value = "spring.cloud.servicecomb.system.property.load.enabled", havingValue = "true",
matchIfMissing = true)
public class NacosAdaptersBootstrapConfiguration {
@Bean
public SystemManagedPropertySourceLocator servicecombNacosPropertySourceLocator(
NacosConfigManager nacosConfigManager) {
return new SystemManagedPropertySourceLocator(nacosConfigManager);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
* 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.huaweicloud.nacos;

public class NacosConst {
public static final String SERVICECOMB_NACOS_CONFIG_DATA_ID = "spring.cloud.servicecomb.nacos.config.dataId";

public static final String SERVICECOMB_NACOS_CONFIG_GROUP = "spring.cloud.servicecomb.nacos.config.group";

public static final String SERVICECOMB_NACOS_CONFIG_REFRESH = "spring.cloud.servicecomb.nacos.config.refresh";

public static final String SERVICECOMB_NACOS_CONFIG_TIMEOUT = "spring.cloud.servicecomb.nacos.config.timeout";

public static final String SERVICECOMB_NACOS_PROPERTY_SOURCE_NAME = "SERVICECOMB-NACOS";

public static final String SECURITY_CONFIG_DATA_ID_PREFIX = "cse-app-security-";

public static final String SECURITY_CONFIG_DATA_ID_SUFFIX = ".yaml";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
* 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.huaweicloud.nacos.config;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosPropertySourceRepository;
import com.alibaba.cloud.nacos.client.NacosPropertySource;
import com.alibaba.cloud.nacos.parser.NacosDataParserHandler;
import com.alibaba.nacos.api.config.ConfigService;
import com.huaweicloud.nacos.NacosConst;

@Order(0)
public class SystemManagedPropertySourceLocator implements PropertySourceLocator {
private static final Logger LOGGER = LoggerFactory.getLogger(SystemManagedPropertySourceLocator.class);

private final ConfigService configService;

private long timeout;

private String dataId;

private String group;

private boolean isRefresh;

public SystemManagedPropertySourceLocator(NacosConfigManager nacosConfigManager) {
this.configService = nacosConfigManager.getConfigService();
}

@Override
public PropertySource<?> locate(Environment environment) {
dataId = environment.getProperty(NacosConst.SERVICECOMB_NACOS_CONFIG_DATA_ID, String.class,
buildDefaultDataId(environment));
group = environment.getProperty(NacosConst.SERVICECOMB_NACOS_CONFIG_GROUP, String.class,
"cse-app-security-group");
// isRefresh is only true can achieve dynamic updates
isRefresh = environment.getProperty(NacosConst.SERVICECOMB_NACOS_CONFIG_REFRESH, boolean.class,
true);
timeout = environment.getProperty(NacosConst.SERVICECOMB_NACOS_CONFIG_TIMEOUT, Long.class, 3000L);
CompositePropertySource composite = new CompositePropertySource(NacosConst.SERVICECOMB_NACOS_PROPERTY_SOURCE_NAME);
loadServicecombNacosProperties(composite);
return composite;
}

private String buildDefaultDataId(Environment environment) {
String serviceName = environment.getProperty("spring.application.name", String.class, "");
return NacosConst.SECURITY_CONFIG_DATA_ID_PREFIX + serviceName + NacosConst.SECURITY_CONFIG_DATA_ID_SUFFIX;
}

private void loadServicecombNacosProperties(CompositePropertySource composite) {
if (StringUtils.isEmpty(dataId) || StringUtils.isEmpty(group)) {
return;
}
String fileExtension = NacosDataParserHandler.getInstance().getFileExtension(dataId);
NacosPropertySource propertySource = new NacosPropertySource(queryNacosPropertyData(fileExtension),
group, dataId, new Date(), isRefresh);
// add nacos listener
NacosPropertySourceRepository.collectNacosPropertySource(propertySource);
addFirstPropertySource(composite, propertySource);
}

private List<PropertySource<?>> queryNacosPropertyData(String fileExtension) {
try {
String data = configService.getConfig(dataId, group, timeout);
if (StringUtils.isEmpty(data)) {
LOGGER.warn("empty nacos configuration, dataId[{}], group[{}]", dataId, group);
return Collections.emptyList();
}
return NacosDataParserHandler.getInstance().parseNacosData(dataId, data, fileExtension);
} catch (Exception e) {
LOGGER.error("get data from Nacos failed, dataId {} ", dataId, e);
}
return Collections.emptyList();
}

private void addFirstPropertySource(final CompositePropertySource composite,
NacosPropertySource nacosPropertySource) {
if (null == nacosPropertySource || nacosPropertySource.getSource().isEmpty()) {
return;
}
composite.addFirstPropertySource(nacosPropertySource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,5 @@
## limitations under the License.
## ---------------------------------------------------------------------------

x-component-order: 100

spring:
cloud:
nacos:
config:
shared-configs:
- data-id: cse-app-security-${spring.application.name}.yaml
group: cse-app-security-group
refresh: true


org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.huaweicloud.nacos.NacosAdaptersBootstrapConfiguration

0 comments on commit ed12ff2

Please sign in to comment.