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

[#1113] support load systemManage property sources for extension #1115

Merged
merged 1 commit into from
Nov 28, 2023
Merged
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
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 @@ -15,6 +15,8 @@
## See the License for the specific language governing permissions and
## limitations under the License.
## ---------------------------------------------------------------------------
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.huaweicloud.nacos.NacosAdaptersBootstrapConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.huaweicloud.nacos.NacosAdaptersAutoConfiguration,\
com.huaweicloud.nacos.graceful.NacosGracefulAutoConfiguration
29 changes: 0 additions & 29 deletions spring-cloud-huawei-nacos/src/main/resources/component.yaml

This file was deleted.

Loading