From a7f839a20a6074d1f3d8b945d4ea90164ff5134a Mon Sep 17 00:00:00 2001 From: shedfreewu <49236872+shedfreewu@users.noreply.github.com> Date: Fri, 7 Nov 2025 10:30:11 +0800 Subject: [PATCH] fix: tsf gateway config support tsf-data-access. (#1745) Signed-off-by: Haotian Zhang <928016560@qq.com> --- CHANGELOG.md | 1 + .../gateway/context/GatewayConsulConfig.java | 280 ++++++++++++++++++ .../gateway/context/GatewayConsulRepo.java | 273 ++++------------- .../tsf/gateway/core/constant/PluginType.java | 4 +- .../gateway/core/model/GatewayAllResult.java | 14 +- .../core/model/GatewayAllResultTest.java | 4 +- 6 files changed, 358 insertions(+), 218 deletions(-) create mode 100644 spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/main/java/com/tencent/cloud/plugin/gateway/context/GatewayConsulConfig.java diff --git a/CHANGELOG.md b/CHANGELOG.md index eb830484a..3a2d9ffb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,3 +27,4 @@ - [test:add junit tests to sct-common.](https://github.com/Tencent/spring-cloud-tencent/pull/1730) - [test:add junit tests to sct-gw-plugin.](https://github.com/Tencent/spring-cloud-tencent/pull/1732) - [feat:compatible metadata transfer with TSF SDK version 1.x.](https://github.com/Tencent/spring-cloud-tencent/pull/1736) +- [fix: tsf gateway config support tsf-data-access.](https://github.com/Tencent/spring-cloud-tencent/pull/1745) diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/main/java/com/tencent/cloud/plugin/gateway/context/GatewayConsulConfig.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/main/java/com/tencent/cloud/plugin/gateway/context/GatewayConsulConfig.java new file mode 100644 index 000000000..fea6745d4 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/main/java/com/tencent/cloud/plugin/gateway/context/GatewayConsulConfig.java @@ -0,0 +1,280 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 Tencent. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * 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.tencent.cloud.plugin.gateway.context; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicBoolean; + +import com.ecwid.consul.v1.ConsulClient; +import com.ecwid.consul.v1.QueryParams; +import com.ecwid.consul.v1.Response; +import com.ecwid.consul.v1.kv.model.GetValue; +import com.tencent.cloud.common.util.JacksonUtils; +import com.tencent.polaris.plugins.configuration.connector.consul.ConsulConfigContext; +import com.tencent.tsf.gateway.core.constant.GatewayConstant; +import com.tencent.tsf.gateway.core.model.GatewayAllResult; +import com.tencent.tsf.gateway.core.model.GroupApiResult; +import com.tencent.tsf.gateway.core.model.GroupResult; +import com.tencent.tsf.gateway.core.model.PathRewriteResult; +import com.tencent.tsf.gateway.core.model.PathWildcardResult; +import com.tencent.tsf.gateway.core.model.PluginInstanceInfoResult; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import shade.polaris.org.apache.commons.io.IOUtils; + +public class GatewayConsulConfig { + + private static final Logger logger = LoggerFactory.getLogger(GatewayConsulConfig.class); + private final String keyPrefix; + private final String type; + private final AtomicBoolean isFirstLoad = new AtomicBoolean(true); + private final ConsulClient consulClient; + private final ConsulConfigContext consulConfigContext; + private final Runnable refreshAction; + private Long index = -1L; + private GatewayAllResult gatewayAllResult; + + public GatewayConsulConfig(String keyPrefix, String type, + ConsulClient consulClient, ConsulConfigContext consulConfigContext, Runnable refreshAction) { + this.keyPrefix = keyPrefix; + this.type = type; + this.consulClient = consulClient; + this.consulConfigContext = consulConfigContext; + this.refreshAction = refreshAction; + } + + public String getType() { + return type; + } + + public GatewayAllResult getGatewayAllResult() { + return gatewayAllResult; + } + + public void firstLoad() { + if (isFirstLoad.compareAndSet(true, false)) { + watch(true); + } + } + + public void watch() { + watch(false); + } + + public void watch(boolean isFirstLoad) { + try { + Response> watchResponse = consulClient.getKVValues(keyPrefix, + consulConfigContext.getAclToken(), new QueryParams(consulConfigContext.getWaitTime(), index)); + + if (watchResponse.getValue() == null) { + gatewayAllResult = loadResponseFromFile(); + if (!isFirstLoad) { + refreshAction.run(); + } + return; + } + + Long newIndex = watchResponse.getConsulIndex(); + if (logger.isDebugEnabled()) { + logger.debug("[watch] keyPrefix:{}, index: {}, newIndex: {}", keyPrefix, index, newIndex); + } + + if (newIndex != null && !Objects.equals(index, newIndex)) { + index = newIndex; + gatewayAllResult = parseGroupResponse(watchResponse); + if (!isFirstLoad) { + refreshAction.run(); + } + } + } + catch (Exception e) { + logger.error("Gateway plugin watch error.", e); + try { + Thread.sleep(consulConfigContext.getConsulErrorSleep()); + } + catch (Exception ex) { + logger.error("error in sleep, msg: " + e.getMessage()); + } + } + } + + private GatewayAllResult loadResponseFromFile() { + GroupResult groupResult = null; + GroupApiResult groupApiResult = new GroupApiResult(); + groupApiResult.setResult(new ArrayList<>()); + + PathRewriteResult pathRewriteResult = new PathRewriteResult(); + PathWildcardResult pathWildcardResult = null; + + PluginInstanceInfoResult pluginInstanceInfoResult = new PluginInstanceInfoResult(); + pluginInstanceInfoResult.setResult(new ArrayList<>()); + + switch (type) { + case GatewayConstant.GROUP_FILE_NAME: + groupResult = (GroupResult) readLocalRepo(GatewayConstant.GROUP_FILE_NAME, GroupResult.class); + break; + case GatewayConstant.API_FILE_NAME: + groupApiResult = (GroupApiResult) readLocalRepo(GatewayConstant.API_FILE_NAME, GroupApiResult.class); + break; + case GatewayConstant.PATH_REWRITE_FILE_NAME: + pathRewriteResult = (PathRewriteResult) readLocalRepo(GatewayConstant.PATH_REWRITE_FILE_NAME, PathRewriteResult.class); + break; + case GatewayConstant.PATH_WILDCARD_FILE_NAME: + pathWildcardResult = (PathWildcardResult) readLocalRepo(GatewayConstant.PATH_WILDCARD_FILE_NAME, PathWildcardResult.class); + break; + case GatewayConstant.PLUGIN_FILE_NAME: + pluginInstanceInfoResult = (PluginInstanceInfoResult) readLocalRepo(GatewayConstant.PLUGIN_FILE_NAME, PluginInstanceInfoResult.class); + break; + } + + return new GatewayAllResult(groupResult, groupApiResult, pathRewriteResult, pathWildcardResult, pluginInstanceInfoResult); + } + + private GatewayAllResult parseGroupResponse(Response> listResponse) { + GroupResult groupResult = null; + GroupApiResult groupApiResult = new GroupApiResult(); + groupApiResult.setResult(new ArrayList<>()); + + PathRewriteResult pathRewriteResult = new PathRewriteResult(); + PathWildcardResult pathWildcardResult = null; + + PluginInstanceInfoResult pluginInstanceInfoResult = new PluginInstanceInfoResult(); + pluginInstanceInfoResult.setResult(new ArrayList<>()); + + + for (GetValue getValue : listResponse.getValue()) { + String key = getValue.getKey(); + String[] keySplit = key.split("/"); + // format example: tsf_gateway/group-xxx/group/data + if (keySplit.length < 4) { + continue; + } + switch (keySplit[2]) { + case GatewayConstant.GROUP_FILE_NAME: + if (logger.isDebugEnabled()) { + logger.debug("[parseResponse] Received group data: {}", getValue.getDecodedValue()); + } + groupResult = JacksonUtils.deserialize(getValue.getDecodedValue(), GroupResult.class); + break; + case GatewayConstant.API_FILE_NAME: + if (logger.isDebugEnabled()) { + logger.debug("[parseResponse] Received api data: {}", getValue.getDecodedValue()); + } + GroupApiResult apiTemp = JacksonUtils.deserialize(getValue.getDecodedValue(), GroupApiResult.class); + groupApiResult.getResult().addAll(apiTemp.getResult()); + break; + case GatewayConstant.PATH_REWRITE_FILE_NAME: + if (logger.isDebugEnabled()) { + logger.debug("[parseResponse] Received path rewrite data: {}", getValue.getDecodedValue()); + } + pathRewriteResult = JacksonUtils.deserialize(getValue.getDecodedValue(), PathRewriteResult.class); + break; + case GatewayConstant.PATH_WILDCARD_FILE_NAME: + if (logger.isDebugEnabled()) { + logger.debug("[parseResponse] Received path wildcard data: {}", getValue.getDecodedValue()); + } + pathWildcardResult = JacksonUtils.deserialize(getValue.getDecodedValue(), PathWildcardResult.class); + break; + case GatewayConstant.PLUGIN_FILE_NAME: + if (logger.isDebugEnabled()) { + logger.debug("[parseResponse] Received path plugin data: {}", getValue.getDecodedValue()); + } + PluginInstanceInfoResult pluginTemp = JacksonUtils.deserialize(getValue.getDecodedValue(), PluginInstanceInfoResult.class); + pluginInstanceInfoResult.getResult().addAll(pluginTemp.getResult()); + break; + } + + } + switch (type) { + case GatewayConstant.GROUP_FILE_NAME: + saveAsFile(JacksonUtils.serialize2Json(groupResult), GatewayConstant.GROUP_FILE_NAME); + break; + case GatewayConstant.API_FILE_NAME: + saveAsFile(JacksonUtils.serialize2Json(groupApiResult), GatewayConstant.API_FILE_NAME); + break; + case GatewayConstant.PATH_REWRITE_FILE_NAME: + saveAsFile(JacksonUtils.serialize2Json(pathRewriteResult), GatewayConstant.PATH_REWRITE_FILE_NAME); + break; + case GatewayConstant.PATH_WILDCARD_FILE_NAME: + saveAsFile(JacksonUtils.serialize2Json(pathWildcardResult), GatewayConstant.PATH_WILDCARD_FILE_NAME); + break; + case GatewayConstant.PLUGIN_FILE_NAME: + saveAsFile(JacksonUtils.serialize2Json(pluginInstanceInfoResult), GatewayConstant.PLUGIN_FILE_NAME); + break; + } + + return new GatewayAllResult(groupResult, groupApiResult, pathRewriteResult, pathWildcardResult, pluginInstanceInfoResult); + } + + private void saveAsFile(String data, String type) { + try { + // 写入文件 + OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(getRepoStoreFile(type))); + writer.write(data); + writer.close(); + } + catch (Throwable t) { + logger.warn("[tsf-gateway] save as file occur exception.", t); + } + } + + private File getRepoStoreFile(String type) { + String filePath = GatewayConstant.GATEWAY_REPO_PREFIX + type + GatewayConstant.FILE_SUFFIX; + File file = new File(filePath); + try { + if (!file.exists()) { + file.getParentFile().mkdirs(); + file.createNewFile(); + } + } + catch (IOException e) { + logger.warn("[tsf-gateway] load group info from local file occur error. filePath: " + filePath, e); + } + return file; + } + + private Object readLocalRepo(String type, Class repoResultClazz) { + byte[] bytes; + try (FileInputStream fin = new FileInputStream(getRepoStoreFile(type)); InputStreamReader isr = new InputStreamReader(fin)) { + bytes = IOUtils.toByteArray(isr, "utf-8"); + if (bytes == null || bytes.length == 0) { + return null; + } + } + catch (IOException t) { + logger.warn("[readLocalRepo] read group info from file occur exception: {}", t.getMessage()); + return null; + } + try { + return JacksonUtils.deserialize(new String(bytes, StandardCharsets.UTF_8), repoResultClazz); + } + catch (Throwable t) { + logger.warn("[readLocalRepo] json serialize data to group occur exception: {}", t.getMessage()); + return null; + } + } +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/main/java/com/tencent/cloud/plugin/gateway/context/GatewayConsulRepo.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/main/java/com/tencent/cloud/plugin/gateway/context/GatewayConsulRepo.java index 4cd07f1e6..23ae205a1 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/main/java/com/tencent/cloud/plugin/gateway/context/GatewayConsulRepo.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/main/java/com/tencent/cloud/plugin/gateway/context/GatewayConsulRepo.java @@ -17,12 +17,6 @@ package com.tencent.cloud.plugin.gateway.context; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; import java.net.URI; import java.util.ArrayList; import java.util.Arrays; @@ -31,18 +25,13 @@ import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Objects; import java.util.Optional; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicLong; import com.ecwid.consul.v1.ConsulClient; import com.ecwid.consul.v1.ConsulRawClient; -import com.ecwid.consul.v1.QueryParams; -import com.ecwid.consul.v1.Response; -import com.ecwid.consul.v1.kv.model.GetValue; import com.tencent.cloud.common.util.JacksonUtils; import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import com.tencent.polaris.api.utils.CollectionUtils; @@ -54,6 +43,7 @@ import com.tencent.polaris.plugins.configuration.connector.consul.ConsulConfigCo import com.tencent.tsf.gateway.core.constant.AuthMode; import com.tencent.tsf.gateway.core.constant.GatewayConstant; import com.tencent.tsf.gateway.core.model.GatewayAllResult; +import com.tencent.tsf.gateway.core.model.GatewayResult; import com.tencent.tsf.gateway.core.model.Group; import com.tencent.tsf.gateway.core.model.GroupApi; import com.tencent.tsf.gateway.core.model.GroupApiResult; @@ -62,10 +52,8 @@ import com.tencent.tsf.gateway.core.model.GroupSecret; import com.tencent.tsf.gateway.core.model.PathRewriteResult; import com.tencent.tsf.gateway.core.model.PathWildcardResult; import com.tencent.tsf.gateway.core.model.PathWildcardRule; -import com.tencent.tsf.gateway.core.model.PluginInstanceInfoResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import shade.polaris.org.apache.commons.io.IOUtils; import org.springframework.cloud.gateway.event.RefreshRoutesEvent; import org.springframework.cloud.gateway.filter.FilterDefinition; @@ -84,11 +72,22 @@ public class GatewayConsulRepo { private final ContextGatewayPropertiesManager contextGatewayPropertiesManager; private final ApplicationEventPublisher publisher; - private final AtomicLong gatewayGroupIndex = new AtomicLong(-1); - private final AtomicLong commonPluginIndex = new AtomicLong(-1); + + private GatewayConsulConfig groupConfig; + + private GatewayConsulConfig apiConfig; + + private GatewayConsulConfig rewriteConfig; + + private GatewayConsulConfig wildcardConfig; + + private GatewayConsulConfig pluginConfig; + private ConsulClient consulClient; + private ConsulConfigContext consulConfigContext; - private final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2, new NamedThreadFactory("consul-gateway-watch", true)); + + private final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5, new NamedThreadFactory("consul-gateway-watch", true)); public GatewayConsulRepo(ContextGatewayProperties contextGatewayProperties, PolarisSDKContextManager polarisSDKContextManager, @@ -123,79 +122,52 @@ public class GatewayConsulRepo { consulClient = new ConsulClient(new ConsulRawClient(agentHost, agentPort)); initConsulConfigContext(connectorConfig); - Response> listResponse = consulClient.getKVValues("tsf_gateway/" + tsfGroupId, consulConfigContext.getAclToken()); - - gatewayGroupIndex.set(listResponse.getConsulIndex()); - if (listResponse.getValue() != null) { - refreshGatewayGroupConfig(parseGroupResponse(listResponse)); - } - else { - logger.info("try to load gateway group config from local file."); - refreshGatewayGroupConfig(loadResponseFromFile()); - } - - scheduledExecutorService.scheduleAtFixedRate(() -> { - try { - Response> watchResponse = consulClient.getKVValues("tsf_gateway/" + tsfGroupId, - consulConfigContext.getAclToken(), new QueryParams(consulConfigContext.getWaitTime(), gatewayGroupIndex.get())); - // 404 - if (watchResponse.getValue() == null) { - return; - } - // 200 - Long newIndex = watchResponse.getConsulIndex(); - if (logger.isDebugEnabled()) { - logger.debug("[watch group] index: {}, newIndex: {}", gatewayGroupIndex.get(), newIndex); - } - if (newIndex != null && !Objects.equals(gatewayGroupIndex.get(), newIndex)) { - gatewayGroupIndex.set(newIndex); - refreshGatewayGroupConfig(parseGroupResponse(watchResponse)); - this.publisher.publishEvent(new RefreshRoutesEvent(this)); - } - - } - catch (Exception e) { - logger.warn("Gateway config watch error.", e); - try { - Thread.sleep(consulConfigContext.getConsulErrorSleep()); - } - catch (Exception ex) { - logger.error("error in sleep, msg: " + e.getMessage()); - } - } - - }, consulConfigContext.getDelay(), consulConfigContext.getDelay(), TimeUnit.MILLISECONDS); + groupConfig = new GatewayConsulConfig("tsf_gateway/" + tsfGroupId + "/" + GatewayConstant.GROUP_FILE_NAME + "/data", + GatewayConstant.GROUP_FILE_NAME, consulClient, consulConfigContext, this::refreshAndPublishGroupConfig); + apiConfig = new GatewayConsulConfig("tsf_gateway/" + tsfGroupId + "/" + GatewayConstant.API_FILE_NAME + "/data", + GatewayConstant.API_FILE_NAME, consulClient, consulConfigContext, this::refreshAndPublishGroupConfig); + rewriteConfig = new GatewayConsulConfig("tsf_gateway/" + tsfGroupId + "/" + GatewayConstant.PATH_REWRITE_FILE_NAME + "/data", + GatewayConstant.PATH_REWRITE_FILE_NAME, consulClient, consulConfigContext, this::refreshAndPublishGroupConfig); + wildcardConfig = new GatewayConsulConfig("tsf_gateway/" + tsfGroupId + "/" + GatewayConstant.PATH_WILDCARD_FILE_NAME + "/data", + GatewayConstant.PATH_WILDCARD_FILE_NAME, consulClient, consulConfigContext, this::refreshAndPublishGroupConfig); + pluginConfig = new GatewayConsulConfig("tsf_gateway/common/plugin/data", + GatewayConstant.PLUGIN_FILE_NAME, consulClient, consulConfigContext, this::refreshPlugin); + + groupConfig.firstLoad(); + apiConfig.firstLoad(); + rewriteConfig.firstLoad(); + wildcardConfig.firstLoad(); + pluginConfig.firstLoad(); + + mergeAndRefreshGroupConfig(); + refreshPlugin(); + + scheduledExecutorService.scheduleAtFixedRate(groupConfig::watch, consulConfigContext.getDelay(), consulConfigContext.getDelay(), TimeUnit.MILLISECONDS); + scheduledExecutorService.scheduleAtFixedRate(apiConfig::watch, consulConfigContext.getDelay(), consulConfigContext.getDelay(), TimeUnit.MILLISECONDS); + scheduledExecutorService.scheduleAtFixedRate(rewriteConfig::watch, consulConfigContext.getDelay(), consulConfigContext.getDelay(), TimeUnit.MILLISECONDS); + scheduledExecutorService.scheduleAtFixedRate(wildcardConfig::watch, consulConfigContext.getDelay(), consulConfigContext.getDelay(), TimeUnit.MILLISECONDS); + scheduledExecutorService.scheduleAtFixedRate(pluginConfig::watch, consulConfigContext.getDelay(), consulConfigContext.getDelay(), TimeUnit.MILLISECONDS); + } + private void mergeAndRefreshGroupConfig() { + GatewayAllResult result = new GatewayAllResult( + groupConfig.getGatewayAllResult().getGroupResult(), + apiConfig.getGatewayAllResult().getGroupApiResult(), + rewriteConfig.getGatewayAllResult().getPathRewriteResult(), + wildcardConfig.getGatewayAllResult().getPathWildcardResult(), + pluginConfig.getGatewayAllResult().getPluginInstanceInfoResult()); + refreshGatewayGroupConfig(result); + } - scheduledExecutorService.scheduleAtFixedRate(() -> { - try { - Response> watchResponse = consulClient.getKVValues("tsf_gateway/common/plugin", - consulConfigContext.getAclToken(), new QueryParams(consulConfigContext.getWaitTime(), commonPluginIndex.get())); - // 404 - if (watchResponse.getValue() == null) { - return; - } - // 200 - Long newIndex = watchResponse.getConsulIndex(); - if (logger.isDebugEnabled()) { - logger.debug("[watch plugin] index: {}, newIndex: {}", commonPluginIndex.get(), newIndex); - } - if (newIndex != null && !Objects.equals(commonPluginIndex.get(), newIndex)) { - commonPluginIndex.set(listResponse.getConsulIndex()); - parsePluginResponse(watchResponse); - } - } - catch (Exception e) { - logger.error("Gateway plugin watch error.", e); - try { - Thread.sleep(consulConfigContext.getConsulErrorSleep()); - } - catch (Exception ex) { - logger.error("error in sleep, msg: " + e.getMessage()); - } - } - }, consulConfigContext.getDelay(), consulConfigContext.getDelay(), TimeUnit.MILLISECONDS); + private void refreshPlugin() { + contextGatewayProperties.setPlugins(Optional.of(pluginConfig.getGatewayAllResult()). + map(GatewayAllResult::getPluginInstanceInfoResult).map(GatewayResult::getResult).orElse(Collections.emptyList())); + contextGatewayPropertiesManager.refreshPlugins(contextGatewayProperties.getPlugins()); + } + private void refreshAndPublishGroupConfig() { + mergeAndRefreshGroupConfig(); + this.publisher.publishEvent(new RefreshRoutesEvent(this)); } private void initConsulConfigContext(ServerConnectorConfigImpl connectorConfig) { @@ -244,109 +216,6 @@ public class GatewayConsulRepo { } } - private void parsePluginResponse(Response> listResponse) { - - PluginInstanceInfoResult pluginInstanceInfoResult = new PluginInstanceInfoResult(); - pluginInstanceInfoResult.setResult(new ArrayList<>()); - - for (GetValue getValue : listResponse.getValue()) { - if (logger.isDebugEnabled()) { - logger.debug("[parseResponse] Received plugin data: {}", getValue.getDecodedValue()); - } - PluginInstanceInfoResult temp = JacksonUtils.deserialize(getValue.getDecodedValue(), PluginInstanceInfoResult.class); - pluginInstanceInfoResult.getResult().addAll(temp.getResult()); - } - - saveAsFile(JacksonUtils.serialize2Json(pluginInstanceInfoResult), GatewayConstant.PLUGIN_FILE_NAME); - - contextGatewayProperties.setPlugins(pluginInstanceInfoResult.getResult()); - contextGatewayPropertiesManager.refreshPlugins(contextGatewayProperties.getPlugins()); - - } - - private GatewayAllResult loadResponseFromFile() { - GroupResult groupResult = (GroupResult) readLocalRepo(GatewayConstant.GROUP_FILE_NAME, GroupResult.class); - GroupApiResult groupApiResult = (GroupApiResult) readLocalRepo(GatewayConstant.API_FILE_NAME, GroupApiResult.class); - PathRewriteResult pathRewriteResult = (PathRewriteResult) readLocalRepo(GatewayConstant.PATH_REWRITE_FILE_NAME, PathRewriteResult.class); - PathWildcardResult pathWildcardResult = (PathWildcardResult) readLocalRepo(GatewayConstant.PATH_WILDCARD_FILE_NAME, PathWildcardResult.class); - return new GatewayAllResult(groupResult, groupApiResult, pathRewriteResult, pathWildcardResult); - } - - private Object readLocalRepo(String type, Class repoResultClazz) { - byte[] bytes; - try (FileInputStream fin = new FileInputStream(getRepoStoreFile(type)); InputStreamReader isr = new InputStreamReader(fin)) { - bytes = IOUtils.toByteArray(isr, "utf-8"); - if (bytes == null || bytes.length == 0) { - return null; - } - } - catch (IOException t) { - logger.warn("[readLocalRepo] read group info from file occur exception: {}", t.getMessage()); - return null; - } - try { - return JacksonUtils.deserialize(new String(bytes, "utf-8"), repoResultClazz); - } - catch (Throwable t) { - logger.warn("[readLocalRepo] json serialize data to group occur exception: {}", t.getMessage()); - return null; - } - } - - private GatewayAllResult parseGroupResponse(Response> listResponse) { - GroupResult groupResult = null; - GroupApiResult groupApiResult = new GroupApiResult(); - groupApiResult.setResult(new ArrayList<>()); - - PathRewriteResult pathRewriteResult = new PathRewriteResult(); - PathWildcardResult pathWildcardResult = null; - - - for (GetValue getValue : listResponse.getValue()) { - String key = getValue.getKey(); - String[] keySplit = key.split("/"); - // format example: tsf_gateway/group-xxx/group/data - if (keySplit.length < 4) { - continue; - } - switch (keySplit[2]) { - case GatewayConstant.GROUP_FILE_NAME: - if (logger.isDebugEnabled()) { - logger.debug("[parseResponse] Received group data: {}", getValue.getDecodedValue()); - } - groupResult = JacksonUtils.deserialize(getValue.getDecodedValue(), GroupResult.class); - break; - case GatewayConstant.API_FILE_NAME: - if (logger.isDebugEnabled()) { - logger.debug("[parseResponse] Received api data: {}", getValue.getDecodedValue()); - } - GroupApiResult temp = JacksonUtils.deserialize(getValue.getDecodedValue(), GroupApiResult.class); - groupApiResult.getResult().addAll(temp.getResult()); - break; - case GatewayConstant.PATH_REWRITE_FILE_NAME: - if (logger.isDebugEnabled()) { - logger.debug("[parseResponse] Received path rewrite data: {}", getValue.getDecodedValue()); - } - pathRewriteResult = JacksonUtils.deserialize(getValue.getDecodedValue(), PathRewriteResult.class); - break; - case GatewayConstant.PATH_WILDCARD_FILE_NAME: - if (logger.isDebugEnabled()) { - logger.debug("[parseResponse] Received path wildcard data: {}", getValue.getDecodedValue()); - } - pathWildcardResult = JacksonUtils.deserialize(getValue.getDecodedValue(), PathWildcardResult.class); - break; - } - - } - - saveAsFile(JacksonUtils.serialize2Json(groupResult), GatewayConstant.GROUP_FILE_NAME); - saveAsFile(JacksonUtils.serialize2Json(groupApiResult), GatewayConstant.API_FILE_NAME); - saveAsFile(JacksonUtils.serialize2Json(pathRewriteResult), GatewayConstant.PATH_REWRITE_FILE_NAME); - saveAsFile(JacksonUtils.serialize2Json(pathWildcardResult), GatewayConstant.PATH_WILDCARD_FILE_NAME); - - return new GatewayAllResult(groupResult, groupApiResult, pathRewriteResult, pathWildcardResult); - } - private void refreshGatewayGroupConfig(GatewayAllResult gatewayAllResult) { GroupResult groupResult = gatewayAllResult.getGroupResult(); GroupApiResult groupApiResult = gatewayAllResult.getGroupApiResult(); @@ -459,33 +328,7 @@ public class GatewayConsulRepo { contextGatewayPropertiesManager.refreshGroupRoute(contextGatewayProperties.getGroups()); } - private void saveAsFile(String data, String type) { - try { - // 写入文件 - OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(getRepoStoreFile(type))); - writer.write(data); - writer.close(); - } - catch (Throwable t) { - logger.warn("[tsf-gateway] save as file occur exception.", t); - } - } - private File getRepoStoreFile(String type) { - String filePath = GatewayConstant.GATEWAY_REPO_PREFIX + type + GatewayConstant.FILE_SUFFIX; - File file = new File(filePath); - try { - if (!file.exists()) { - file.getParentFile().mkdirs(); - file.createNewFile(); - } - } - catch (IOException e) { - - logger.warn("[tsf-gateway] load group info from local file occur error. filePath: " + filePath, e); - } - return file; - } private RouteDefinition getRouteDefinition(Group group) { RouteDefinition routeDefinition = new RouteDefinition(); diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/main/java/com/tencent/tsf/gateway/core/constant/PluginType.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/main/java/com/tencent/tsf/gateway/core/constant/PluginType.java index 3558b7c3d..e16f514f4 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/main/java/com/tencent/tsf/gateway/core/constant/PluginType.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/main/java/com/tencent/tsf/gateway/core/constant/PluginType.java @@ -71,7 +71,9 @@ public enum PluginType { return pluginType; } } - logger.warn("Unknown plugin type exception, please upgrade your gateway sdk"); + if (logger.isDebugEnabled()) { + logger.debug("Unknown plugin type:{} exception, please upgrade your gateway sdk", name); + } return null; } diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/main/java/com/tencent/tsf/gateway/core/model/GatewayAllResult.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/main/java/com/tencent/tsf/gateway/core/model/GatewayAllResult.java index 964053bde..f191ccd3a 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/main/java/com/tencent/tsf/gateway/core/model/GatewayAllResult.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/main/java/com/tencent/tsf/gateway/core/model/GatewayAllResult.java @@ -28,12 +28,16 @@ public class GatewayAllResult { private PathWildcardResult pathWildcardResult; + private PluginInstanceInfoResult pluginInstanceInfoResult; + public GatewayAllResult(GroupResult groupResult, GroupApiResult groupApiResult, - PathRewriteResult pathRewriteResult, PathWildcardResult pathWildcardResult) { + PathRewriteResult pathRewriteResult, PathWildcardResult pathWildcardResult, + PluginInstanceInfoResult pluginInstanceInfoResult) { this.groupResult = groupResult; this.groupApiResult = groupApiResult; this.pathRewriteResult = pathRewriteResult; this.pathWildcardResult = pathWildcardResult; + this.pluginInstanceInfoResult = pluginInstanceInfoResult; } public GroupResult getGroupResult() { @@ -67,4 +71,12 @@ public class GatewayAllResult { public void setPathWildcardResult(PathWildcardResult pathWildcardResult) { this.pathWildcardResult = pathWildcardResult; } + + public PluginInstanceInfoResult getPluginInstanceInfoResult() { + return pluginInstanceInfoResult; + } + + public void setPluginInstanceInfoResult(PluginInstanceInfoResult pluginInstanceInfoResult) { + this.pluginInstanceInfoResult = pluginInstanceInfoResult; + } } diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/test/java/com/tencent/tsf/gateway/core/model/GatewayAllResultTest.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/test/java/com/tencent/tsf/gateway/core/model/GatewayAllResultTest.java index f072378a5..8abbc0880 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/test/java/com/tencent/tsf/gateway/core/model/GatewayAllResultTest.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-gateway-plugin/src/test/java/com/tencent/tsf/gateway/core/model/GatewayAllResultTest.java @@ -30,15 +30,17 @@ public class GatewayAllResultTest { @Test public void testGatewayAllResult() { - GatewayAllResult gatewayAllResult = new GatewayAllResult(null, null, null, null); + GatewayAllResult gatewayAllResult = new GatewayAllResult(null, null, null, null, null); gatewayAllResult.setGroupResult(new GroupResult()); gatewayAllResult.setGroupApiResult(new GroupApiResult()); gatewayAllResult.setPathRewriteResult(new PathRewriteResult()); gatewayAllResult.setPathWildcardResult(new PathWildcardResult()); + gatewayAllResult.setPluginInstanceInfoResult(new PluginInstanceInfoResult()); assertThat(gatewayAllResult.getGroupResult()).isNotNull(); assertThat(gatewayAllResult.getGroupApiResult()).isNotNull(); assertThat(gatewayAllResult.getPathRewriteResult()).isNotNull(); assertThat(gatewayAllResult.getPathWildcardResult()).isNotNull(); + assertThat(gatewayAllResult.getPluginInstanceInfoResult()).isNotNull(); } }