Fix the issue of repeatedly passing one-time key 。

pull/430/head
misselvexu 3 years ago
parent 130e79b7d8
commit 500d7d63dd

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

@ -53,7 +53,7 @@ public final class MetadataConstant {
/** /**
* internal metadata disposable status key. * 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, ... * Order of filter, interceptor, ...

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

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

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

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

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

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

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

Loading…
Cancel
Save