fix: fix multiple bugs in tsf. (#1746)

pull/1754/head
shedfreewu 1 month ago committed by Haotian Zhang
parent fb148f8393
commit f5811068ef

@ -18,6 +18,7 @@
package com.tencent.cloud.polaris.config; package com.tencent.cloud.polaris.config;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -163,12 +164,24 @@ public class ConfigurationModifier implements PolarisConfigurationConfigModifier
private void checkAddressAccessible(List<String> configAddresses) { private void checkAddressAccessible(List<String> configAddresses) {
// check address can connect // check address can connect
configAddresses.forEach(address -> { configAddresses.forEach(address -> {
String[] ipPort = address.split(":"); String[] ipPort;
// check ipv6 address, format: [ipv6_ip]:port
if (address.contains("[") && address.contains("]")) {
int lastColonIndex = address.lastIndexOf(':');
String port = address.substring(lastColonIndex + 1);
String ip = address.substring(1, lastColonIndex - 1);
ipPort = new String[]{ip, port};
}
else {
ipPort = address.split(":");
}
if (ipPort.length != 2) { if (ipPort.length != 2) {
throw new IllegalArgumentException("Config server address (" + address + ") is wrong, please check address like grpc://183.47.111.8:8091."); throw new IllegalArgumentException("Config server address (" + address + ") is wrong, please check address like grpc://183.47.111.8:8091.");
} }
LOGGER.info("[SCT] Check config server ipPort: {}", Arrays.asList(ipPort));
if (!AddressUtils.accessible(ipPort[0], Integer.parseInt(ipPort[1]), 3000)) { if (!AddressUtils.accessible(ipPort[0], Integer.parseInt(ipPort[1]), 3000)) {
String errMsg = "Config server address (" + address + ") can not be connected. Please check your config in bootstrap.yml" String errMsg = "Config server address (" + address + ") can not be connected. Please check your config in bootstrap.yml"
+ " with spring.cloud.polaris.address or spring.cloud.polaris.config.address."; + " with spring.cloud.polaris.address or spring.cloud.polaris.config.address.";

@ -175,11 +175,13 @@ public final class TsfTagUtils {
Map<String, String> tsfDisposableMetadata = new HashMap<>(tagSize); Map<String, String> tsfDisposableMetadata = new HashMap<>(tagSize);
if (CollectionUtils.isNotEmpty(tsfUserTagList)) { if (CollectionUtils.isNotEmpty(tsfUserTagList)) {
for (Tag tag : tsfUserTagList) { for (Tag tag : tsfUserTagList) {
if (Tag.ControlFlag.TRANSITIVE.equals(tag.getFlags())) { if (tag.getFlags() != null && tag.getFlags().contains(Tag.ControlFlag.TRANSITIVE)) {
tsfTransitiveMetadata.put(tag.getKey(), tag.getValue()); tsfTransitiveMetadata.put(tag.getKey(), tag.getValue());
} }
else {
tsfDisposableMetadata.put(tag.getKey(), tag.getValue()); tsfDisposableMetadata.put(tag.getKey(), tag.getValue());
} }
}
mergedTransitiveMetadata.putAll(tsfTransitiveMetadata); mergedTransitiveMetadata.putAll(tsfTransitiveMetadata);
mergedDisposableMetadata.putAll(tsfDisposableMetadata); mergedDisposableMetadata.putAll(tsfDisposableMetadata);
} }

@ -90,21 +90,27 @@ public class GatewayConsulConfig {
Response<List<GetValue>> watchResponse = consulClient.getKVValues(keyPrefix, Response<List<GetValue>> watchResponse = consulClient.getKVValues(keyPrefix,
consulConfigContext.getAclToken(), new QueryParams(consulConfigContext.getWaitTime(), index)); consulConfigContext.getAclToken(), new QueryParams(consulConfigContext.getWaitTime(), index));
if (watchResponse.getValue() == null) {
gatewayAllResult = loadResponseFromFile();
if (!isFirstLoad) {
refreshAction.run();
}
return;
}
Long newIndex = watchResponse.getConsulIndex(); Long newIndex = watchResponse.getConsulIndex();
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("[watch] keyPrefix:{}, index: {}, newIndex: {}", keyPrefix, index, newIndex); logger.debug("[watch] keyPrefix:{}, index: {}, newIndex: {}, response is empty:{}", keyPrefix, index, newIndex, watchResponse.getValue() == null);
} }
boolean change = false;
if (newIndex != null && !Objects.equals(index, newIndex)) { if (newIndex != null && !Objects.equals(index, newIndex)) {
change = true;
index = newIndex; index = newIndex;
}
if (watchResponse.getValue() == null) {
// only load from the cache file when the first load and response is empty.
if (isFirstLoad) {
gatewayAllResult = loadResponseFromFile();
logger.debug("[watch] loadResponseFromFile, keyPrefix: {}", keyPrefix);
}
return;
}
if (change) {
gatewayAllResult = parseGroupResponse(watchResponse); gatewayAllResult = parseGroupResponse(watchResponse);
if (!isFirstLoad) { if (!isFirstLoad) {
refreshAction.run(); refreshAction.run();

@ -311,16 +311,22 @@ public class GatewayConsulRepo {
} }
GroupContext groupContext = groups.get(wildcardRule.getGroupId()); GroupContext groupContext = groups.get(wildcardRule.getGroupId());
if (groupContext != null && groupContext.getRoutes() != null) {
groupContext.getRoutes().add(contextRoute); groupContext.getRoutes().add(contextRoute);
} }
else {
logger.warn("path wildcard rule {} not found in group {}", wildcardRule.getWildCardId(), wildcardRule.getGroupId());
}
}
} }
contextGatewayProperties.setGroups(groups); contextGatewayProperties.setGroups(groups);
contextGatewayProperties.setRoutes(routes); contextGatewayProperties.setRoutes(routes);
contextGatewayProperties.setPathRewrites(Optional.ofNullable(pathRewriteResult).map(PathRewriteResult::getResult) contextGatewayProperties.setPathRewrites(Optional.ofNullable(pathRewriteResult).map(PathRewriteResult::getResult)
.orElse(new ArrayList<>())); .orElse(new ArrayList<>()));
if (logger.isDebugEnabled()) {
logger.debug("Gateway config loaded. :{}", JacksonUtils.serialize2Json(contextGatewayProperties)); logger.debug("Gateway config loaded. :{}", JacksonUtils.serialize2Json(contextGatewayProperties));
}
contextGatewayPropertiesManager.setPathRewrites(contextGatewayProperties.getPathRewrites()); contextGatewayPropertiesManager.setPathRewrites(contextGatewayProperties.getPathRewrites());

@ -139,8 +139,8 @@ public final class PluginUtil {
if (path.charAt(0) != '/') { if (path.charAt(0) != '/') {
path = "/" + path; path = "/" + path;
} }
// apiPath is the path of downstream service.
String apiPath = StringUtils.substring(path, StringUtils.ordinalIndexOf(path, "/", 4)); String apiPath = path;
boolean matched = antPathMatcher.match(preTagName, apiPath); boolean matched = antPathMatcher.match(preTagName, apiPath);
if (matched) { if (matched) {
Map<String, String> pathTagMap = antPathMatcher.extractUriTemplateVariables(preTagName, apiPath); Map<String, String> pathTagMap = antPathMatcher.extractUriTemplateVariables(preTagName, apiPath);

@ -21,6 +21,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.common.tsf.TsfContextUtils; import com.tencent.cloud.common.tsf.TsfContextUtils;
import com.tencent.polaris.api.utils.IPAddressUtils;
import com.tencent.polaris.api.utils.StringUtils; import com.tencent.polaris.api.utils.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -164,7 +165,7 @@ public final class TsfCoreEnvironmentPostProcessor implements EnvironmentPostPro
defaultProperties.put("spring.cloud.polaris.config.enabled", "true"); defaultProperties.put("spring.cloud.polaris.config.enabled", "true");
defaultProperties.put("spring.cloud.polaris.config.internal-enabled", "false"); defaultProperties.put("spring.cloud.polaris.config.internal-enabled", "false");
defaultProperties.put("spring.cloud.polaris.config.data-source", "consul"); defaultProperties.put("spring.cloud.polaris.config.data-source", "consul");
defaultProperties.put("spring.cloud.polaris.config.address", "http://" + tsfConsulIp + ":" + tsfConsulPort); defaultProperties.put("spring.cloud.polaris.config.address", "http://" + IPAddressUtils.getIpCompatible(tsfConsulIp) + ":" + tsfConsulPort);
defaultProperties.put("spring.cloud.polaris.config.port", tsfConsulPort); defaultProperties.put("spring.cloud.polaris.config.port", tsfConsulPort);
defaultProperties.put("spring.cloud.polaris.config.token", tsfConsulToken); defaultProperties.put("spring.cloud.polaris.config.token", tsfConsulToken);
defaultProperties.put("spring.cloud.polaris.config.groups[0].namespace", "config"); defaultProperties.put("spring.cloud.polaris.config.groups[0].namespace", "config");

Loading…
Cancel
Save