add: gateway可以监听nacos-config下发的配置信息更新了

This commit is contained in:
Wendal Chen 2020-04-25 18:02:18 +08:00
parent f3d9958c9c
commit 9d95dc24c0
4 changed files with 89 additions and 7 deletions

View File

@ -1,9 +1,22 @@
package org.nutz.boot.starter.nacos;
import static com.alibaba.nacos.api.PropertyKeyConst.*;
import static com.alibaba.nacos.api.PropertyKeyConst.ACCESS_KEY;
import static com.alibaba.nacos.api.PropertyKeyConst.CLUSTER_NAME;
import static com.alibaba.nacos.api.PropertyKeyConst.CONFIG_LONG_POLL_TIMEOUT;
import static com.alibaba.nacos.api.PropertyKeyConst.CONFIG_RETRY_TIME;
import static com.alibaba.nacos.api.PropertyKeyConst.CONTEXT_PATH;
import static com.alibaba.nacos.api.PropertyKeyConst.ENABLE_REMOTE_SYNC_CONFIG;
import static com.alibaba.nacos.api.PropertyKeyConst.ENCODE;
import static com.alibaba.nacos.api.PropertyKeyConst.ENDPOINT;
import static com.alibaba.nacos.api.PropertyKeyConst.ENDPOINT_PORT;
import static com.alibaba.nacos.api.PropertyKeyConst.MAX_RETRY;
import static com.alibaba.nacos.api.PropertyKeyConst.NAMESPACE;
import static com.alibaba.nacos.api.PropertyKeyConst.SECRET_KEY;
import static com.alibaba.nacos.api.PropertyKeyConst.SERVER_ADDR;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.Executor;
import org.nutz.boot.AppContext;
import org.nutz.boot.annotation.PropDoc;
@ -120,16 +133,33 @@ public class NacosConfigureLoader extends PropertiesConfigureLoader {
throw Lang.makeThrow("nacos.config.data_type is not found or not recognizeonly json,xml and properties are support!");
}
}
protected String dataId;
protected String group;
@Override
public void init() throws Exception {
super.init();
String dataId = conf.get(NACOS_DATA_ID, conf.get("nutz.application.name", "nutzboot"));
String group = conf.get(NACOS_GROUP, "DEFAULT_GROUP");
String dataType = conf.get(NACOS_DATA_TYPE, "properties");
dataId = conf.get(NACOS_DATA_ID, conf.get("nutz.application.name", "nutzboot"));
group = conf.get(NACOS_GROUP, "DEFAULT_GROUP");
configService = NacosFactory.createConfigService(getNacosConfigProperties());
String configInfo = configService.getConfig(dataId, group, 5000);
log.debugf("get nacos config%s", configInfo);
String configInfo = configService.getConfigAndSignListener(dataId, group, 5000, new com.alibaba.nacos.api.config.listener.Listener() {
public Executor getExecutor() {
return null;
}
@Override
public void receiveConfigInfo(String configInfo) {
updateConfigString(configInfo);
}
});
updateConfigString(configInfo);
}
protected void updateConfigString(String configInfo) {
log.debugf("get nacos config%s", configInfo);
String dataType = conf.get(NACOS_DATA_TYPE, "properties");
if (Strings.isNotBlank(configInfo)) {
setConfig(configInfo, dataType, conf);
}
@ -163,4 +193,12 @@ public class NacosConfigureLoader extends PropertiesConfigureLoader {
public ConfigService getConfigService() {
return configService;
}
public String getDataId() {
return dataId;
}
public String getGroup() {
return group;
}
}

View File

@ -2,6 +2,7 @@ package org.nutz.cloud.perca;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Executor;
import org.nutz.boot.AppContext;
import org.nutz.ioc.Ioc;
@ -11,6 +12,10 @@ import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
@IocBean(create="init")
public class RouteConfig {
@ -31,7 +36,7 @@ public class RouteConfig {
return masters;
}
public void init() throws Exception {
public void reload() throws Exception {
List<RouterMaster> masters = new LinkedList<>();
for (String key : conf.getKeys()) {
if (key.startsWith("gw.") && key.endsWith(".filters")) {
@ -43,6 +48,37 @@ public class RouteConfig {
}
}
log.debugf("master count=%d", masters.size());
List<RouterMaster> oldMasters = this.masters;
this.masters = masters;
if (oldMasters != null && oldMasters.size() > 0) {
for (RouterMaster routerMaster : oldMasters) {
routerMaster.depose();
}
}
}
public void init() throws Exception {
reload();
if (ioc.has("nacosConfigService")) {
ConfigService cs = ioc.get(ConfigService.class, "nacosConfigService");
cs.addListener(conf.check("nacos.config.data-id"), conf.get("nacos.config.group", Constants.DEFAULT_GROUP), new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
try {
reload();
} catch (Exception e) {
log.error("fail to reload config!!!", e);
}
}
@Override
public Executor getExecutor() {
return null;
}
});
}
}
}

View File

@ -22,4 +22,6 @@ public interface RouteFilter {
String getName();
String getType();
void close();
}

View File

@ -196,5 +196,11 @@ public class RouterMaster implements Comparable<RouterMaster> {
return Integer.compare(priority, o.priority);
}
public void depose() {
for (RouteFilter filter : filters) {
filter.close();
}
}
}