-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1114 from huaweicloud/master_locator
[#1113] support load systemManage property sources for extension
- Loading branch information
Showing
5 changed files
with
186 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...huawei-nacos/src/main/java/com/huaweicloud/nacos/NacosAdaptersBootstrapConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
spring-cloud-huawei-nacos/src/main/java/com/huaweicloud/nacos/NacosConst.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
110 changes: 110 additions & 0 deletions
110
...-nacos/src/main/java/com/huaweicloud/nacos/config/SystemManagedPropertySourceLocator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters