Fix the issue of repeatedly passing one-time key 。

pull/430/head
misselvexu 3 years ago
parent af672367c5
commit 506b3267ea

@ -36,6 +36,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.lang.NonNull;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;
@ -53,8 +54,8 @@ public class DecodeTransferMetadataServletFilter extends OncePerRequestFilter {
private static final Logger LOG = LoggerFactory.getLogger(DecodeTransferMetadataServletFilter.class);
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, FilterChain filterChain)
protected void doFilterInternal(@NonNull HttpServletRequest httpServletRequest,
@NonNull HttpServletResponse httpServletResponse, FilterChain filterChain)
throws ServletException, IOException {
Map<String, String> internalTransitiveMetadata = getInternalTransitiveMetadata(httpServletRequest);
Map<String, String> customTransitiveMetadata = CustomTransitiveMetadataResolver.resolve(httpServletRequest);

@ -35,7 +35,7 @@ public final class MetadataConstant {
/**
* internal metadata disposable status key.
*/
public static final String INTERNAL_METADATA_DISPOSABLE = "internal-disposable-status";
public static final String INTERNAL_METADATA_DISPOSABLE = "internal-metadata-disposable-status";
/**
* Order of filter, interceptor, ...

@ -22,6 +22,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
@ -31,12 +32,9 @@ import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
import static com.tencent.cloud.common.constant.MetadataConstant.INTERNAL_METADATA_DISPOSABLE;
import static com.tencent.cloud.common.util.JacksonUtils.deserialize2Map;
import static com.tencent.cloud.common.util.JacksonUtils.serialize2Json;
import static java.net.URLDecoder.decode;
import static java.net.URLEncoder.encode;
/**
* Metadata Context Holder.
@ -103,44 +101,52 @@ public final class MetadataContextHolder {
// Save transitive metadata to ThreadLocal.
if (!CollectionUtils.isEmpty(dynamicTransitiveMetadata)) {
// processing disposable keys
if (dynamicTransitiveMetadata.containsKey(INTERNAL_METADATA_DISPOSABLE)) {
String disposableKeyStatus = dynamicTransitiveMetadata.get(INTERNAL_METADATA_DISPOSABLE);
try {
if (StringUtils.hasText(disposableKeyStatus)) {
Map<String, String> keyStatus = deserialize2Map(decode(disposableKeyStatus, UTF_8));
Iterator<Map.Entry<String, String>> it = keyStatus.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
String key = entry.getKey();
boolean status = Boolean.parseBoolean(entry.getValue());
if (!status) {
keyStatus.put(key, "true");
}
else {
// removed disposable key
dynamicTransitiveMetadata.remove(key);
it.remove();
}
// Check local static metadata .
Map<String, String> localTransitives = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
String localDisposable = localTransitives.get(INTERNAL_METADATA_DISPOSABLE);
Map<String, Boolean> localValidDisposables = new HashMap<>();
if (StringUtils.hasText(localDisposable)) {
Set<String> disposables = deserialize2Map(localDisposable).keySet();
for (String disposable : disposables) {
localValidDisposables.put(disposable, localTransitives.containsKey(disposable));
}
}
// processing disposable keys .
String disposableKeyStatus = dynamicTransitiveMetadata.get(INTERNAL_METADATA_DISPOSABLE);
if (StringUtils.hasText(disposableKeyStatus)) {
Map<String, String> keyStatus = deserialize2Map(disposableKeyStatus);
Iterator<Map.Entry<String, String>> it = keyStatus.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
String key = entry.getKey();
boolean status = Boolean.parseBoolean(entry.getValue());
if (!localValidDisposables.containsKey(key)) {
if (!status) {
keyStatus.put(key, "true");
}
else {
// removed disposable key
dynamicTransitiveMetadata.remove(key);
it.remove();
}
// reset
dynamicTransitiveMetadata.put(INTERNAL_METADATA_DISPOSABLE, encode(serialize2Json(keyStatus), UTF_8));
}
else {
// removed disposable key
dynamicTransitiveMetadata.remove(key);
keyStatus.put(key, "false");
}
}
catch (Exception e) {
LOGGER.error("Runtime system does not support utf-8 coding.", e);
}
// reset
dynamicTransitiveMetadata.put(INTERNAL_METADATA_DISPOSABLE, serialize2Json(keyStatus));
}
Map<String, String> staticTransitiveMetadata =
metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
Map<String, String> staticTransitiveMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
Map<String, String> mergedTransitiveMetadata = new HashMap<>();
mergedTransitiveMetadata.putAll(staticTransitiveMetadata);
mergedTransitiveMetadata.putAll(dynamicTransitiveMetadata);
metadataContext.putFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE,
Collections.unmodifiableMap(mergedTransitiveMetadata));
metadataContext.putFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE, Collections.unmodifiableMap(mergedTransitiveMetadata));
}
MetadataContextHolder.set(metadataContext);
}

@ -18,7 +18,6 @@
package com.tencent.cloud.common.metadata;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@ -33,10 +32,8 @@ import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
import static com.tencent.cloud.common.constant.MetadataConstant.INTERNAL_METADATA_DISPOSABLE;
import static com.tencent.cloud.common.util.JacksonUtils.serialize2Json;
import static java.net.URLEncoder.encode;
/**
* manage metadata from env/config file/custom spi.
@ -203,12 +200,7 @@ public class StaticMetadataManager {
mergedTransitiveMetadataResult.putAll(customSPITransitiveMetadata);
// append disposable status
try {
mergedTransitiveMetadataResult.put(INTERNAL_METADATA_DISPOSABLE, encode(serialize2Json(disposableStatusMetadata), UTF_8));
}
catch (UnsupportedEncodingException e) {
LOGGER.error("Runtime system does not support utf-8 coding.", e);
}
mergedTransitiveMetadataResult.put(INTERNAL_METADATA_DISPOSABLE, serialize2Json(disposableStatusMetadata));
this.mergedStaticTransitiveMetadata = Collections.unmodifiableMap(mergedTransitiveMetadataResult);
}

@ -44,12 +44,12 @@ public class MetadataCalleeController {
@Value("${server.port:0}")
private int port;
private final MetadataCallee2Service metadataCallee2Service;
private final MetadataCalleeService2 metadataCalleeService2;
private final RestTemplate restTemplate;
public MetadataCalleeController(MetadataCallee2Service metadataCallee2Service, RestTemplate restTemplate) {
this.metadataCallee2Service = metadataCallee2Service;
public MetadataCalleeController(MetadataCalleeService2 metadataCalleeService2, RestTemplate restTemplate) {
this.metadataCalleeService2 = metadataCalleeService2;
this.restTemplate = restTemplate;
}
@ -66,7 +66,13 @@ public class MetadataCalleeController {
"http://MetadataCalleeService2/metadata/service/callee2/info",
Map.class);
calleeMetadata.forEach((key, value) -> {
LOG.info("Callee2 Metadata (Key-Value): {} : {}", key, value);
LOG.info("RestTemplate Callee2 Metadata (Key-Value): {} : {}", key, value);
});
// Call remote service with Feign
Map<String, String> calleeMetadata2 = metadataCalleeService2.info();
calleeMetadata2.forEach((key, value) -> {
LOG.info("Feign Callee2 Metadata (Key-Value): {} : {}", key, value);
});
// Get Custom Metadata From Context

@ -28,8 +28,8 @@ import org.springframework.web.bind.annotation.GetMapping;
* @author Palmer Xu
*/
@FeignClient(value = "MetadataCalleeService2",
fallback = MetadataCallee2ServiceFallback.class)
public interface MetadataCallee2Service {
fallback = MetadataCalleeService2Fallback.class)
public interface MetadataCalleeService2 {
/**
* Get information of callee.

@ -29,7 +29,7 @@ import org.springframework.stereotype.Component;
* @author Palmer Xu
*/
@Component
public class MetadataCallee2ServiceFallback implements MetadataCallee2Service {
public class MetadataCalleeService2Fallback implements MetadataCalleeService2 {
@Override
public Map<String, String> info() {

@ -19,9 +19,14 @@ spring:
CUSTOM-METADATA-KEY-LOCAL-2: CUSTOM-VALUE-LOCAL-2
# Example: transitive
CUSTOM-METADATA-KEY-TRANSITIVE-2: CUSTOM-VALUE-TRANSITIVE-2
# Example: disposable
# CUSTOM-METADATA-KEY-DISPOSABLE: CUSTOM-VALUE-DISPOSABLE-CALLEE
# Assigned which metadata key-value will be passed along the link
transitive:
- CUSTOM-METADATA-KEY-TRANSITIVE-2
# - CUSTOM-METADATA-KEY-DISPOSABLE
disposable:
- CUSTOM-METADATA-KEY-DISPOSABLE
management:
endpoints:

@ -19,7 +19,7 @@ spring:
content:
# Example: intransitive
CUSTOM-METADATA-KEY-LOCAL: CUSTOM-VALUE-LOCAL
#
# Example: disposable
CUSTOM-METADATA-KEY-DISPOSABLE: CUSTOM-VALUE-DISPOSABLE
# Example: transitive
CUSTOM-METADATA-KEY-TRANSITIVE: CUSTOM-VALUE-TRANSITIVE

Loading…
Cancel
Save