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

[SCB-2041]nacos module support json/yaml/properties format #1882

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions dynamic-config/config-nacos/README.md
Expand Up @@ -17,11 +17,12 @@ JSON:
}

3.In base model,add info in microservice.yaml:
nacos:
config:
servicecomb:
nacos:
serverAddr: 127.0.0.1:8848
dataId: example
group: DEFAULT_GROUP
dataId: example.yaml
namespace: 6a39260c-b834-456c-b52b-2981fb437c59

4.Then add blow code and start base model,you will get properties(If properties on nacos has changed, you can also get new value):
@RestSchema(schemaId = "nacos")
Expand Down
Expand Up @@ -41,96 +41,96 @@
import com.netflix.config.WatchedUpdateResult;

public class NacosConfigurationSourceImpl implements ConfigCenterConfigurationSource {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigCenterConfigurationSource.class);
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigCenterConfigurationSource.class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This project using google code style, maybe you need to add it to your editor. For IDEA and eclipse, this file is in $java-chassis/etc folder.

Copy link
Contributor

@liubao68 liubao68 Jul 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems travis CI got some problem. You can first fix code style problem and submit a commit direct to this PR, the CI will be retriggerd . Because you used the wrong formatter, this makes it a bit more difficult review code in github.


private final Map<String, Object> valueCache = new ConcurrentHashMap<>();
private final Map<String, Object> valueCache = new ConcurrentHashMap<>();

private List<WatchedUpdateListener> listeners = new CopyOnWriteArrayList<>();
private List<WatchedUpdateListener> listeners = new CopyOnWriteArrayList<>();

private static final String SERVER_ADDR = "nacos.config.serverAddr";
private static final String SERVER_ADDR = "servicecomb.nacos.serverAddr";

public NacosConfigurationSourceImpl() {
}
public NacosConfigurationSourceImpl() {
}

private final UpdateHandler updateHandler = new UpdateHandler();

@Override
public boolean isValidSource(Configuration localConfiguration) {
if (localConfiguration.getProperty(SERVER_ADDR) == null) {
LOGGER.warn("Nacos configuration source is not configured!");
return false;
}
return true;
}

private final UpdateHandler updateHandler = new UpdateHandler();
@Override
public void init(Configuration localConfiguration) {
NacosConfig.setConcurrentCompositeConfiguration(localConfiguration);
init();
}

private void init() {
NacosClient nacosClient = new NacosClient(updateHandler);
nacosClient.refreshNacosConfig();
}

@Override
public boolean isValidSource(Configuration localConfiguration) {
if (localConfiguration.getProperty(SERVER_ADDR) == null) {
LOGGER.warn("Nacos configuration source is not configured!");
return false;
@Override
public void addUpdateListener(WatchedUpdateListener watchedUpdateListener) {
listeners.add(watchedUpdateListener);
}
return true;
}

@Override
public void init(Configuration localConfiguration) {
NacosConfig.setConcurrentCompositeConfiguration(localConfiguration);
init();
}

private void init() {
NacosClient nacosClient = new NacosClient(updateHandler);
nacosClient.refreshNacosConfig();
}

@Override
public void addUpdateListener(WatchedUpdateListener watchedUpdateListener) {
listeners.add(watchedUpdateListener);
}

@Override
public void removeUpdateListener(WatchedUpdateListener watchedUpdateListener) {
listeners.remove(watchedUpdateListener);
}

private void updateConfiguration(WatchedUpdateResult result) {
for (WatchedUpdateListener l : listeners) {
try {
l.updateConfiguration(result);
} catch (Throwable ex) {
LOGGER.error("Error in invoking WatchedUpdateListener", ex);
}

@Override
public void removeUpdateListener(WatchedUpdateListener watchedUpdateListener) {
listeners.remove(watchedUpdateListener);
}
}

@Override
public Map<String, Object> getCurrentData() throws Exception {
return valueCache;
}

public List<WatchedUpdateListener> getCurrentListeners() {
return listeners;
}

public class UpdateHandler {
public void handle(ConfigurationAction action, Map<String, Object> config) {
if (config == null || config.isEmpty()) {
return;
}
Map<String, Object> configuration = ConfigMapping.getConvertedMap(config);
if (CREATE.equals(action)) {
valueCache.putAll(configuration);

updateConfiguration(createIncremental(ImmutableMap.copyOf(configuration),
null,
null));
} else if (SET.equals(action)) {
valueCache.putAll(configuration);

updateConfiguration(createIncremental(null,
ImmutableMap.copyOf(configuration),
null));
} else if (DELETE.equals(action)) {
configuration.keySet().forEach(valueCache::remove);
updateConfiguration(createIncremental(null,
null,
ImmutableMap.copyOf(configuration)));
} else {
LOGGER.error("action: {} is invalid.", action.name());
return;
}
LOGGER.warn("Config value cache changed: action:{}; item:{}", action.name(), configuration.keySet());

private void updateConfiguration(WatchedUpdateResult result) {
for (WatchedUpdateListener l : listeners) {
try {
l.updateConfiguration(result);
} catch (Throwable ex) {
LOGGER.error("Error in invoking WatchedUpdateListener", ex);
}
}
}

@Override
public Map<String, Object> getCurrentData() throws Exception {
return valueCache;
}

public List<WatchedUpdateListener> getCurrentListeners() {
return listeners;
}

public class UpdateHandler {
public void handle(ConfigurationAction action, Map<String, Object> config) {
if (config == null || config.isEmpty()) {
return;
}
Map<String, Object> configuration = ConfigMapping.getConvertedMap(config);
if (CREATE.equals(action)) {
valueCache.putAll(configuration);

updateConfiguration(createIncremental(ImmutableMap.copyOf(configuration),
null,
null));
} else if (SET.equals(action)) {
valueCache.putAll(configuration);

updateConfiguration(createIncremental(null,
ImmutableMap.copyOf(configuration),
null));
} else if (DELETE.equals(action)) {
configuration.keySet().forEach(valueCache::remove);
updateConfiguration(createIncremental(null,
null,
ImmutableMap.copyOf(configuration)));
} else {
LOGGER.error("action: {} is invalid.", action.name());
return;
}
LOGGER.warn("Config value cache changed: action:{}; item:{}", action.name(), configuration.keySet());
}
}
}
}