Skip to content

Commit

Permalink
Separate nacos ConfigService initialization from normal properties be…
Browse files Browse the repository at this point in the history
…an. (#3784)
  • Loading branch information
chickenlj authored Jul 3, 2024
1 parent 1af2a95 commit 15ddb0f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ private String resolveNamespace() {
}
}

private void enrichNacosConfigProperties(Properties nacosConfigProperties) {
protected void enrichNacosConfigProperties(Properties nacosConfigProperties) {
if (environment == null) {
return;
}
Expand All @@ -602,7 +602,7 @@ private void enrichNacosConfigProperties(Properties nacosConfigProperties) {
String.valueOf(v)));
}

private String resolveKey(String key) {
protected String resolveKey(String key) {
Matcher matcher = PATTERN.matcher(key);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2013-2023 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
*
* https://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.configdata;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.alibaba.cloud.nacos.NacosConfigProperties;

/**
* Used for initialization of Nacos ConfigService.
*/
public class NacosConfigDataLoadProperties extends NacosConfigProperties {
private Map<String, String> config = new HashMap<>();

@Override
protected void enrichNacosConfigProperties(Properties nacosConfigProperties) {
config.forEach((k, v) -> nacosConfigProperties.putIfAbsent(resolveKey(k),
String.valueOf(v)));
}

Map<String, String> getConfig() {
return config;
}

void setConfig(Map<String, String> config) {
this.config = config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,12 @@ public class NacosConfigDataLocationResolver
* Prefix for Config Server imports.
*/
public static final String PREFIX = "nacos:";

private final Log log;

// support params

private static final String GROUP = "group";

// support params
private static final String REFRESH_ENABLED = "refreshEnabled";

private static final String PREFERENCE = "preference";
private final Log log;

public NacosConfigDataLocationResolver(DeferredLogFactory logFactory) {
this.log = logFactory.getLog(getClass());
Expand All @@ -85,22 +81,22 @@ protected NacosConfigProperties loadProperties(
BindHandler bindHandler = getBindHandler(context);

NacosConfigProperties nacosConfigProperties;
if (context.getBootstrapContext().isRegistered(NacosConfigProperties.class)) {
if (context.getBootstrapContext().isRegistered(NacosConfigDataLoadProperties.class)) {
nacosConfigProperties = context.getBootstrapContext()
.get(NacosConfigProperties.class);
.get(NacosConfigDataLoadProperties.class);
}
else {
nacosConfigProperties = binder
.bind("spring.cloud.nacos", Bindable.of(NacosConfigProperties.class),
.bind("spring.cloud.nacos", Bindable.of(NacosConfigDataLoadProperties.class),
bindHandler)
.map(properties -> binder
.bind(NacosConfigProperties.PREFIX,
.bind(NacosConfigDataLoadProperties.PREFIX,
Bindable.ofInstance(properties), bindHandler)
.orElse(properties))
.orElseGet(() -> binder
.bind(NacosConfigProperties.PREFIX,
Bindable.of(NacosConfigProperties.class), bindHandler)
.orElseGet(NacosConfigProperties::new));
.bind(NacosConfigDataLoadProperties.PREFIX,
Bindable.of(NacosConfigDataLoadProperties.class), bindHandler)
.orElseGet(NacosConfigDataLoadProperties::new));
}

return nacosConfigProperties;
Expand Down

0 comments on commit 15ddb0f

Please sign in to comment.