From 9d95dc24c0b4aa7e2cc92899f5c33a2956d35e05 Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Sat, 25 Apr 2020 18:02:18 +0800 Subject: [PATCH] =?UTF-8?q?add:=20gateway=E5=8F=AF=E4=BB=A5=E7=9B=91?= =?UTF-8?q?=E5=90=ACnacos-config=E4=B8=8B=E5=8F=91=E7=9A=84=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E4=BF=A1=E6=81=AF=E6=9B=B4=E6=96=B0=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../starter/nacos/NacosConfigureLoader.java | 50 ++++++++++++++++--- .../org/nutz/cloud/perca/RouteConfig.java | 38 +++++++++++++- .../org/nutz/cloud/perca/RouteFilter.java | 2 + .../org/nutz/cloud/perca/RouterMaster.java | 6 +++ 4 files changed, 89 insertions(+), 7 deletions(-) diff --git a/nutzboot-starter/nutzboot-starter-nacos-config/src/main/java/org/nutz/boot/starter/nacos/NacosConfigureLoader.java b/nutzboot-starter/nutzboot-starter-nacos-config/src/main/java/org/nutz/boot/starter/nacos/NacosConfigureLoader.java index 7e5099f5..d7739a03 100644 --- a/nutzboot-starter/nutzboot-starter-nacos-config/src/main/java/org/nutz/boot/starter/nacos/NacosConfigureLoader.java +++ b/nutzboot-starter/nutzboot-starter-nacos-config/src/main/java/org/nutz/boot/starter/nacos/NacosConfigureLoader.java @@ -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 recognize,only 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; + } } diff --git a/nutzcloud/nutzcloud-perca/src/main/java/org/nutz/cloud/perca/RouteConfig.java b/nutzcloud/nutzcloud-perca/src/main/java/org/nutz/cloud/perca/RouteConfig.java index 8a42d054..50a39e0e 100644 --- a/nutzcloud/nutzcloud-perca/src/main/java/org/nutz/cloud/perca/RouteConfig.java +++ b/nutzcloud/nutzcloud-perca/src/main/java/org/nutz/cloud/perca/RouteConfig.java @@ -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 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 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; + } + }); + } + } + + } diff --git a/nutzcloud/nutzcloud-perca/src/main/java/org/nutz/cloud/perca/RouteFilter.java b/nutzcloud/nutzcloud-perca/src/main/java/org/nutz/cloud/perca/RouteFilter.java index 8549691d..19b8165a 100644 --- a/nutzcloud/nutzcloud-perca/src/main/java/org/nutz/cloud/perca/RouteFilter.java +++ b/nutzcloud/nutzcloud-perca/src/main/java/org/nutz/cloud/perca/RouteFilter.java @@ -22,4 +22,6 @@ public interface RouteFilter { String getName(); String getType(); + + void close(); } diff --git a/nutzcloud/nutzcloud-perca/src/main/java/org/nutz/cloud/perca/RouterMaster.java b/nutzcloud/nutzcloud-perca/src/main/java/org/nutz/cloud/perca/RouterMaster.java index 2cadc37f..d39713ac 100644 --- a/nutzcloud/nutzcloud-perca/src/main/java/org/nutz/cloud/perca/RouterMaster.java +++ b/nutzcloud/nutzcloud-perca/src/main/java/org/nutz/cloud/perca/RouterMaster.java @@ -196,5 +196,11 @@ public class RouterMaster implements Comparable { return Integer.compare(priority, o.priority); } + + public void depose() { + for (RouteFilter filter : filters) { + filter.close(); + } + } }