#1550 流程调通
This commit is contained in:
parent
192610b4c9
commit
1877e7d307
@ -50,7 +50,12 @@ public enum ConfigType {
|
||||
/**
|
||||
* config type is "yaml"
|
||||
*/
|
||||
YAML("yaml");
|
||||
YAML("yaml"),
|
||||
|
||||
/**
|
||||
* config type is "yaml"
|
||||
*/
|
||||
YML("yml");
|
||||
|
||||
String type;
|
||||
|
||||
|
@ -116,6 +116,10 @@
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
@ -16,7 +16,9 @@
|
||||
package com.alibaba.nacos.client.config.impl;
|
||||
|
||||
import com.alibaba.nacos.api.common.Constants;
|
||||
import com.alibaba.nacos.api.config.ConfigType;
|
||||
import com.alibaba.nacos.api.config.listener.AbstractSharedListener;
|
||||
import com.alibaba.nacos.client.config.listener.impl.ConfigChangeListener;
|
||||
import com.alibaba.nacos.api.config.listener.Listener;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager;
|
||||
@ -60,6 +62,7 @@ public class CacheData {
|
||||
}
|
||||
|
||||
public void setContent(String newContent) {
|
||||
this.lastContent = this.content;
|
||||
this.content = newContent;
|
||||
this.md5 = getMd5String(content);
|
||||
}
|
||||
@ -188,6 +191,15 @@ public class CacheData {
|
||||
configFilterChainManager.doFilter(null, cr);
|
||||
String contentTmp = cr.getContent();
|
||||
listener.receiveConfigInfo(contentTmp);
|
||||
|
||||
if (listener instanceof ConfigChangeListener) {
|
||||
if (dataId.endsWith(ConfigType.YAML.getType()) || dataId.endsWith(ConfigType.YML.getType())
|
||||
|| dataId.endsWith(ConfigType.PROPERTIES.getType())) {
|
||||
// compare lastContent and content
|
||||
ConfigChangeEvent event = new ConfigChangeEvent(dataId, lastContent, content);
|
||||
((ConfigChangeListener)listener).receiveConfigChange(event);
|
||||
}
|
||||
}
|
||||
listenerWrap.lastCallMd5 = md5;
|
||||
LOGGER.info("[{}] [notify-ok] dataId={}, group={}, md5={}, listener={} ", name, dataId, group, md5,
|
||||
listener);
|
||||
@ -279,6 +291,7 @@ public class CacheData {
|
||||
* last modify time
|
||||
*/
|
||||
private volatile long localConfigLastModified;
|
||||
private volatile String lastContent;
|
||||
private volatile String content;
|
||||
private int taskId;
|
||||
private volatile boolean isInitializing = true;
|
||||
|
@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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.alibaba.nacos.client.config.impl;
|
||||
|
||||
import com.alibaba.nacos.api.config.ConfigType;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* ConfigChangeEvent
|
||||
*
|
||||
* @author rushsky518
|
||||
*/
|
||||
public class ConfigChangeEvent {
|
||||
private Map<String, ConfigChangeItem> result;
|
||||
|
||||
public ConfigChangeEvent(String dataId, String oldContent, String content) {
|
||||
init(dataId, oldContent, content);
|
||||
}
|
||||
|
||||
public ConfigChangeItem getChangeItem(String key) {
|
||||
return result.get(key);
|
||||
}
|
||||
|
||||
public Collection<ConfigChangeItem> getChangeItems() {
|
||||
return result.values();
|
||||
}
|
||||
|
||||
private void init(String dataId, String oldContent, String content) {
|
||||
result = new HashMap<String, ConfigChangeItem>(32);
|
||||
|
||||
if (dataId.endsWith(ConfigType.PROPERTIES.getType())) {
|
||||
Properties oldProp = new Properties();
|
||||
Properties newProp = new Properties();
|
||||
try {
|
||||
oldProp.load(new StringReader(oldContent));
|
||||
newProp.load(new StringReader(content));
|
||||
filterData(oldProp, newProp);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if (dataId.endsWith(ConfigType.YML.getType()) || dataId.endsWith(ConfigType.YAML.getType())) {
|
||||
Yaml oldYaml = new Yaml();
|
||||
Yaml newYaml = new Yaml();
|
||||
Map<String, Object> oldMap = oldYaml.load(oldContent);
|
||||
Map<String, Object> newMap = newYaml.load(content);
|
||||
|
||||
filterData(oldMap, newMap);
|
||||
}
|
||||
}
|
||||
|
||||
private void filterData(Map oldMap, Map newMap) {
|
||||
for (Iterator<Map.Entry<String, Object>> entryItr = oldMap.entrySet().iterator(); entryItr.hasNext();) {
|
||||
Map.Entry<String, Object> e = entryItr.next();
|
||||
ConfigChangeItem cci = null;
|
||||
if (newMap.containsKey(e.getKey())) {
|
||||
if (e.getValue().equals(newMap.get(e.getKey()))) {
|
||||
continue;
|
||||
}
|
||||
cci = new ConfigChangeItem(e.getKey(), e.getValue().toString(), newMap.get(e.getKey()).toString());
|
||||
cci.setType(ConfigChangeItem.PropertyChangeType.MODIFIED);
|
||||
} else {
|
||||
cci = new ConfigChangeItem(e.getKey(), e.getValue().toString(), null);
|
||||
cci.setType(ConfigChangeItem.PropertyChangeType.DELETED);
|
||||
}
|
||||
|
||||
result.put(e.getKey(), cci);
|
||||
}
|
||||
|
||||
for (Iterator<Map.Entry<String, Object>> entryItr = newMap.entrySet().iterator(); entryItr.hasNext();) {
|
||||
Map.Entry<String, Object> e = entryItr.next();
|
||||
if (!oldMap.containsKey(e.getKey())) {
|
||||
ConfigChangeItem cci = new ConfigChangeItem(e.getKey(), null, e.getValue().toString());
|
||||
cci.setType(ConfigChangeItem.PropertyChangeType.ADDED);
|
||||
result.put(e.getKey(), cci);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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.alibaba.nacos.client.config.impl;
|
||||
|
||||
/**
|
||||
* ConfigChangeItem
|
||||
*
|
||||
* @author rushsky518
|
||||
*/
|
||||
public class ConfigChangeItem {
|
||||
private String key;
|
||||
private String oldValue;
|
||||
private String newValue;
|
||||
|
||||
private PropertyChangeType type;
|
||||
public enum PropertyChangeType {
|
||||
/** add */
|
||||
ADDED,
|
||||
/** modified */
|
||||
MODIFIED,
|
||||
/** deleted */
|
||||
DELETED
|
||||
}
|
||||
|
||||
public ConfigChangeItem(String key, String oldValue, String newValue) {
|
||||
this.key = key;
|
||||
this.oldValue = oldValue;
|
||||
this.newValue = newValue;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getOldValue() {
|
||||
return oldValue;
|
||||
}
|
||||
|
||||
public void setOldValue(String oldValue) {
|
||||
this.oldValue = oldValue;
|
||||
}
|
||||
|
||||
public String getNewValue() {
|
||||
return newValue;
|
||||
}
|
||||
|
||||
public void setNewValue(String newValue) {
|
||||
this.newValue = newValue;
|
||||
}
|
||||
|
||||
public PropertyChangeType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(PropertyChangeType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ConfigChangeItem{" +
|
||||
"key='" + key + '\'' +
|
||||
", oldValue='" + oldValue + '\'' +
|
||||
", newValue='" + newValue + '\'' +
|
||||
", type=" + type +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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.alibaba.nacos.client.config.listener.impl;
|
||||
|
||||
import com.alibaba.nacos.api.config.listener.AbstractListener;
|
||||
import com.alibaba.nacos.client.config.impl.ConfigChangeEvent;
|
||||
|
||||
/**
|
||||
* ConfigChangeListener
|
||||
*
|
||||
* @author rushsky518
|
||||
*/
|
||||
@SuppressWarnings("PMD.AbstractClassShouldStartWithAbstractNamingRule")
|
||||
public abstract class ConfigChangeListener extends AbstractListener {
|
||||
/**
|
||||
* handle config change
|
||||
* @param event
|
||||
*/
|
||||
public abstract void receiveConfigChange(final ConfigChangeEvent event);
|
||||
|
||||
@Override
|
||||
public void receiveConfigInfo(final String configInfo) {}
|
||||
}
|
||||
|
@ -38,3 +38,9 @@ server.tomcat.basedir=
|
||||
#nacos.security.ignore.urls=/**
|
||||
|
||||
nacos.security.ignore.urls=/,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/login,/v1/console/health/**,/v1/cs/**,/v1/ns/**,/v1/cmdb/**,/actuator/**,/v1/console/server/**
|
||||
|
||||
spring.datasource.platform=mysql
|
||||
db.num=1
|
||||
db.url.0=jdbc:mysql://localhost:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
|
||||
db.user=root
|
||||
db.password=123456
|
||||
|
@ -16,11 +16,12 @@
|
||||
package com.alibaba.nacos.example;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.locks.LockSupport;
|
||||
|
||||
import com.alibaba.nacos.api.NacosFactory;
|
||||
import com.alibaba.nacos.client.config.impl.ConfigChangeEvent;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.config.listener.Listener;
|
||||
import com.alibaba.nacos.client.config.listener.impl.ConfigChangeListener;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
|
||||
/**
|
||||
@ -32,39 +33,36 @@ public class ConfigExample {
|
||||
|
||||
public static void main(String[] args) throws NacosException, InterruptedException {
|
||||
String serverAddr = "localhost";
|
||||
String dataId = "test";
|
||||
String group = "DEFAULT_GROUP";
|
||||
String dataId = "redis.properties";
|
||||
String group = "multi-data-ids";
|
||||
Properties properties = new Properties();
|
||||
properties.put("serverAddr", serverAddr);
|
||||
ConfigService configService = NacosFactory.createConfigService(properties);
|
||||
String content = configService.getConfig(dataId, group, 5000);
|
||||
System.out.println(content);
|
||||
configService.addListener(dataId, group, new Listener() {
|
||||
@Override
|
||||
public void receiveConfigInfo(String configInfo) {
|
||||
System.out.println("receive:" + configInfo);
|
||||
}
|
||||
configService.addListener(dataId, group, new ConfigChangeListener() {
|
||||
|
||||
@Override
|
||||
public Executor getExecutor() {
|
||||
return null;
|
||||
public void receiveConfigChange(final ConfigChangeEvent event) {
|
||||
System.out.println(event.getChangeItems());
|
||||
}
|
||||
});
|
||||
|
||||
boolean isPublishOk = configService.publishConfig(dataId, group, "content");
|
||||
System.out.println(isPublishOk);
|
||||
|
||||
Thread.sleep(3000);
|
||||
content = configService.getConfig(dataId, group, 5000);
|
||||
System.out.println(content);
|
||||
|
||||
boolean isRemoveOk = configService.removeConfig(dataId, group);
|
||||
System.out.println(isRemoveOk);
|
||||
Thread.sleep(3000);
|
||||
|
||||
content = configService.getConfig(dataId, group, 5000);
|
||||
System.out.println(content);
|
||||
Thread.sleep(300000);
|
||||
LockSupport.park();
|
||||
// boolean isPublishOk = configService.publishConfig(dataId, group, "content");
|
||||
// System.out.println(isPublishOk);
|
||||
//
|
||||
// Thread.sleep(3000);
|
||||
// content = configService.getConfig(dataId, group, 5000);
|
||||
// System.out.println(content);
|
||||
//
|
||||
// boolean isRemoveOk = configService.removeConfig(dataId, group);
|
||||
// System.out.println(isRemoveOk);
|
||||
// Thread.sleep(3000);
|
||||
//
|
||||
// content = configService.getConfig(dataId, group, 5000);
|
||||
// System.out.println(content);
|
||||
// Thread.sleep(300000);
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user