feat: support lane router (#1378)
Co-authored-by: Haotian Zhang <skyebefreeman@qq.com> Co-authored-by: andrew shan <45474304+andrewshan@users.noreply.github.com>pull/1379/head
parent
f5bb38c8c7
commit
360671217a
@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. 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.metadata.core;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
import com.tencent.cloud.common.util.JacksonUtils;
|
||||
import com.tencent.cloud.common.util.ReflectionUtils;
|
||||
import com.tencent.cloud.common.util.UrlUtils;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||
import com.tencent.cloud.rpc.enhancement.plugin.PluginOrderConstant;
|
||||
import com.tencent.polaris.metadata.core.MessageMetadataContainer;
|
||||
import com.tencent.polaris.metadata.core.MetadataType;
|
||||
import feign.Request;
|
||||
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_DISPOSABLE_METADATA;
|
||||
import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_METADATA;
|
||||
|
||||
/**
|
||||
* Pre EnhancedPlugin for feign to encode transfer metadata.
|
||||
*
|
||||
* @author Shedfree Wu
|
||||
*/
|
||||
public class EncodeTransferMedataFeignEnhancedPlugin implements EnhancedPlugin {
|
||||
@Override
|
||||
public EnhancedPluginType getType() {
|
||||
return EnhancedPluginType.Client.PRE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(EnhancedPluginContext context) throws Throwable {
|
||||
if (!(context.getOriginRequest() instanceof Request)) {
|
||||
return;
|
||||
}
|
||||
Request request = (Request) context.getOriginRequest();
|
||||
|
||||
// get metadata of current thread
|
||||
MetadataContext metadataContext = MetadataContextHolder.get();
|
||||
Map<String, String> customMetadata = metadataContext.getCustomMetadata();
|
||||
Map<String, String> disposableMetadata = metadataContext.getDisposableMetadata();
|
||||
Map<String, String> transHeaders = metadataContext.getTransHeadersKV();
|
||||
|
||||
MessageMetadataContainer calleeMessageMetadataContainer = metadataContext.getMetadataContainer(MetadataType.MESSAGE, false);
|
||||
Map<String, String> calleeTransitiveHeaders = calleeMessageMetadataContainer.getTransitiveHeaders();
|
||||
// currently only support transitive header from calleeMessageMetadataContainer
|
||||
this.buildHeaderMap(request, calleeTransitiveHeaders);
|
||||
|
||||
// build custom disposable metadata request header
|
||||
this.buildMetadataHeader(request, disposableMetadata, CUSTOM_DISPOSABLE_METADATA);
|
||||
|
||||
// process custom metadata
|
||||
this.buildMetadataHeader(request, customMetadata, CUSTOM_METADATA);
|
||||
|
||||
// set headers that need to be transmitted from the upstream
|
||||
this.buildTransmittedHeader(request, transHeaders);
|
||||
}
|
||||
|
||||
private void buildTransmittedHeader(Request request, Map<String, String> transHeaders) {
|
||||
if (!CollectionUtils.isEmpty(transHeaders)) {
|
||||
Map<String, Collection<String>> headers = getModifiableHeaders(request);
|
||||
transHeaders.entrySet().stream().forEach(entry -> {
|
||||
headers.remove(entry.getKey());
|
||||
headers.put(entry.getKey(), Arrays.asList(entry.getValue()));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set metadata into the request header for {@link Request} .
|
||||
* @param request instance of {@link Request}
|
||||
* @param metadata metadata map .
|
||||
* @param headerName target metadata http header name .
|
||||
*/
|
||||
private void buildMetadataHeader(Request request, Map<String, String> metadata, String headerName) {
|
||||
if (!CollectionUtils.isEmpty(metadata)) {
|
||||
buildHeaderMap(request, ImmutableMap.of(headerName, JacksonUtils.serialize2Json(metadata)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set headerMap into the request header for {@link Request} .
|
||||
* @param request instance of {@link Request}
|
||||
* @param headerMap header map .
|
||||
*/
|
||||
private void buildHeaderMap(Request request, Map<String, String> headerMap) {
|
||||
if (!CollectionUtils.isEmpty(headerMap)) {
|
||||
Map<String, Collection<String>> headers = getModifiableHeaders(request);
|
||||
headerMap.forEach((key, value) -> headers.put(key, Arrays.asList(UrlUtils.encode(value))));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The value obtained directly from the headers method is an unmodifiable map.
|
||||
* If the Feign client uses the URL, the original headers are unmodifiable.
|
||||
* @param request feign request
|
||||
* @return modifiable headers
|
||||
*/
|
||||
private Map<String, Collection<String>> getModifiableHeaders(Request request) {
|
||||
Map<String, Collection<String>> headers;
|
||||
headers = (Map<String, Collection<String>>) ReflectionUtils.getFieldValue(request, "headers");
|
||||
|
||||
if (!(headers instanceof LinkedHashMap)) {
|
||||
headers = new LinkedHashMap<>(headers);
|
||||
ReflectionUtils.setFieldValue(request, "headers", headers);
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return PluginOrderConstant.ClientPluginOrder.CONSUMER_TRANSFER_METADATA_PLUGIN_ORDER;
|
||||
}
|
||||
}
|
@ -1,102 +0,0 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. 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.metadata.core;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.cloud.common.constant.OrderConstant;
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
import com.tencent.cloud.common.util.JacksonUtils;
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
|
||||
import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_DISPOSABLE_METADATA;
|
||||
import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_METADATA;
|
||||
import static java.net.URLEncoder.encode;
|
||||
|
||||
/**
|
||||
* Interceptor used for adding the metadata in http headers from context when web client
|
||||
* is Feign.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class EncodeTransferMedataFeignInterceptor implements RequestInterceptor, Ordered {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(EncodeTransferMedataFeignInterceptor.class);
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return OrderConstant.Client.Feign.ENCODE_TRANSFER_METADATA_INTERCEPTOR_ORDER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(RequestTemplate requestTemplate) {
|
||||
// get metadata of current thread
|
||||
MetadataContext metadataContext = MetadataContextHolder.get();
|
||||
Map<String, String> customMetadata = metadataContext.getCustomMetadata();
|
||||
Map<String, String> disposableMetadata = metadataContext.getDisposableMetadata();
|
||||
Map<String, String> transHeaders = metadataContext.getTransHeadersKV();
|
||||
|
||||
this.buildMetadataHeader(requestTemplate, disposableMetadata, CUSTOM_DISPOSABLE_METADATA);
|
||||
|
||||
// process custom metadata
|
||||
this.buildMetadataHeader(requestTemplate, customMetadata, CUSTOM_METADATA);
|
||||
|
||||
// set headers that need to be transmitted from the upstream
|
||||
this.buildTransmittedHeader(requestTemplate, transHeaders);
|
||||
}
|
||||
|
||||
private void buildTransmittedHeader(RequestTemplate requestTemplate, Map<String, String> transHeaders) {
|
||||
if (!CollectionUtils.isEmpty(transHeaders)) {
|
||||
transHeaders.entrySet().stream().forEach(entry -> {
|
||||
requestTemplate.removeHeader(entry.getKey());
|
||||
requestTemplate.header(entry.getKey(), entry.getValue());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set metadata into the request header for {@link RestTemplate} .
|
||||
* @param requestTemplate instance of {@link RestTemplate}
|
||||
* @param metadata metadata map .
|
||||
* @param headerName target metadata http header name .
|
||||
*/
|
||||
private void buildMetadataHeader(RequestTemplate requestTemplate, Map<String, String> metadata, String headerName) {
|
||||
if (!CollectionUtils.isEmpty(metadata)) {
|
||||
String encodedMetadata = JacksonUtils.serialize2Json(metadata);
|
||||
requestTemplate.removeHeader(headerName);
|
||||
try {
|
||||
requestTemplate.header(headerName, encode(encodedMetadata, UTF_8));
|
||||
}
|
||||
catch (UnsupportedEncodingException e) {
|
||||
LOG.error("Set header failed.", e);
|
||||
requestTemplate.header(headerName, encodedMetadata);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. 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.metadata.provider;
|
||||
|
||||
import com.tencent.cloud.common.util.UrlUtils;
|
||||
import com.tencent.cloud.common.util.expresstion.SpringWebExpressionLabelUtils;
|
||||
import com.tencent.polaris.metadata.core.MessageMetadataContainer;
|
||||
import com.tencent.polaris.metadata.core.MetadataProvider;
|
||||
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
|
||||
/**
|
||||
* MetadataProvider used for Reactive.
|
||||
*
|
||||
* @author Shedfree Wu
|
||||
*/
|
||||
public class ReactiveMetadataProvider implements MetadataProvider {
|
||||
|
||||
private ServerHttpRequest serverHttpRequest;
|
||||
|
||||
private String callerIp;
|
||||
|
||||
public ReactiveMetadataProvider(ServerHttpRequest serverHttpRequest, String callerIp) {
|
||||
this.serverHttpRequest = serverHttpRequest;
|
||||
this.callerIp = callerIp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRawMetadataStringValue(String key) {
|
||||
switch (key) {
|
||||
case MessageMetadataContainer.LABEL_KEY_METHOD:
|
||||
return serverHttpRequest.getMethod().name();
|
||||
case MessageMetadataContainer.LABEL_KEY_PATH:
|
||||
return UrlUtils.decode(serverHttpRequest.getPath().toString());
|
||||
case MessageMetadataContainer.LABEL_KEY_CALLER_IP:
|
||||
return callerIp;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRawMetadataMapValue(String key, String mapKey) {
|
||||
switch (key) {
|
||||
case MessageMetadataContainer.LABEL_MAP_KEY_HEADER:
|
||||
return UrlUtils.decode(SpringWebExpressionLabelUtils.getHeaderValue(serverHttpRequest, mapKey, null));
|
||||
case MessageMetadataContainer.LABEL_MAP_KEY_COOKIE:
|
||||
return UrlUtils.decode(SpringWebExpressionLabelUtils.getCookieValue(serverHttpRequest, mapKey, null));
|
||||
case MessageMetadataContainer.LABEL_MAP_KEY_QUERY:
|
||||
return UrlUtils.decode(SpringWebExpressionLabelUtils.getQueryValue(serverHttpRequest, mapKey, null));
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. 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.metadata.provider;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import com.tencent.cloud.common.util.UrlUtils;
|
||||
import com.tencent.cloud.common.util.expresstion.ExpressionLabelUtils;
|
||||
import com.tencent.cloud.common.util.expresstion.ServletExpressionLabelUtils;
|
||||
import com.tencent.polaris.metadata.core.MessageMetadataContainer;
|
||||
import com.tencent.polaris.metadata.core.MetadataProvider;
|
||||
|
||||
|
||||
/**
|
||||
* MetadataProvider used for Servlet.
|
||||
*
|
||||
* @author Shedfree Wu
|
||||
*/
|
||||
public class ServletMetadataProvider implements MetadataProvider {
|
||||
|
||||
private HttpServletRequest httpServletRequest;
|
||||
|
||||
private String callerIp;
|
||||
|
||||
public ServletMetadataProvider(HttpServletRequest httpServletRequest, String callerIp) {
|
||||
this.httpServletRequest = httpServletRequest;
|
||||
this.callerIp = callerIp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRawMetadataStringValue(String key) {
|
||||
switch (key) {
|
||||
case MessageMetadataContainer.LABEL_KEY_METHOD:
|
||||
return httpServletRequest.getMethod();
|
||||
case MessageMetadataContainer.LABEL_KEY_PATH:
|
||||
return UrlUtils.decode(httpServletRequest.getRequestURI());
|
||||
case MessageMetadataContainer.LABEL_KEY_CALLER_IP:
|
||||
return callerIp;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRawMetadataMapValue(String key, String mapKey) {
|
||||
switch (key) {
|
||||
case MessageMetadataContainer.LABEL_MAP_KEY_HEADER:
|
||||
return UrlUtils.decode(httpServletRequest.getHeader(mapKey));
|
||||
case MessageMetadataContainer.LABEL_MAP_KEY_COOKIE:
|
||||
return UrlUtils.decode(ServletExpressionLabelUtils.getCookieValue(httpServletRequest.getCookies(), mapKey, null));
|
||||
case MessageMetadataContainer.LABEL_MAP_KEY_QUERY:
|
||||
return UrlUtils.decode(ExpressionLabelUtils.getQueryValue(httpServletRequest.getQueryString(), mapKey, null));
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. 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.metadata.provider;
|
||||
|
||||
import com.tencent.cloud.common.util.UrlUtils;
|
||||
import com.tencent.polaris.metadata.core.MessageMetadataContainer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.http.HttpCookie;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.mock.web.MockCookie;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link ReactiveMetadataProvider} and {@link ServletMetadataProvider}.
|
||||
*
|
||||
* @author quan, Shedfree Wu
|
||||
*/
|
||||
public class MetadataProviderTest {
|
||||
|
||||
private static final String notExistKey = "empty";
|
||||
|
||||
@Test
|
||||
public void testReactiveMetadataProvider() {
|
||||
String headerKey1 = "header1";
|
||||
String headerKey2 = "header2";
|
||||
String headerValue1 = "value1";
|
||||
String headerValue2 = "value2/test";
|
||||
String queryKey1 = "qk1";
|
||||
String queryKey2 = "qk2";
|
||||
String queryValue1 = "qv1";
|
||||
String queryValue2 = "qv2/test";
|
||||
String cookieKey1 = "ck1";
|
||||
String cookieKey2 = "ck2";
|
||||
String cookieValue1 = "cv1";
|
||||
String cookieValue2 = "cv2/test";
|
||||
String path = "/echo/test";
|
||||
String callerIp = "localhost";
|
||||
MockServerHttpRequest request = MockServerHttpRequest.get(path)
|
||||
.header(headerKey1, headerValue1)
|
||||
.header(headerKey2, UrlUtils.encode(headerValue2))
|
||||
.queryParam(queryKey1, queryValue1)
|
||||
.queryParam(queryKey2, UrlUtils.encode(queryValue2))
|
||||
.cookie(new HttpCookie(cookieKey1, cookieValue1))
|
||||
.cookie(new HttpCookie(cookieKey2, UrlUtils.encode(cookieValue2)))
|
||||
.build();
|
||||
|
||||
ReactiveMetadataProvider reactiveMetadataProvider = new ReactiveMetadataProvider(request, callerIp);
|
||||
assertThat(reactiveMetadataProvider.getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_HEADER, headerKey1)).isEqualTo(headerValue1);
|
||||
assertThat(reactiveMetadataProvider.getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_HEADER, headerKey2)).isEqualTo(headerValue2);
|
||||
// com.tencent.polaris.metadata.core.manager.ComposeMetadataProvider.getRawMetadataMapValue need return null when key don't exist
|
||||
assertThat(reactiveMetadataProvider.getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_HEADER, notExistKey)).isNull();
|
||||
|
||||
assertThat(reactiveMetadataProvider.getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_COOKIE, cookieKey1)).isEqualTo(cookieValue1);
|
||||
assertThat(reactiveMetadataProvider.getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_COOKIE, cookieKey2)).isEqualTo(cookieValue2);
|
||||
assertThat(reactiveMetadataProvider.getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_COOKIE, notExistKey)).isNull();
|
||||
|
||||
assertThat(reactiveMetadataProvider.getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_QUERY, queryKey1)).isEqualTo(queryValue1);
|
||||
assertThat(reactiveMetadataProvider.getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_QUERY, queryKey2)).isEqualTo(queryValue2);
|
||||
assertThat(reactiveMetadataProvider.getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_QUERY, notExistKey)).isNull();
|
||||
assertThat(reactiveMetadataProvider.getRawMetadataMapValue(notExistKey, queryKey1)).isNull();
|
||||
|
||||
assertThat(reactiveMetadataProvider.getRawMetadataStringValue(MessageMetadataContainer.LABEL_KEY_METHOD)).isEqualTo("GET");
|
||||
assertThat(reactiveMetadataProvider.getRawMetadataStringValue(MessageMetadataContainer.LABEL_KEY_PATH)).isEqualTo(path);
|
||||
assertThat(reactiveMetadataProvider.getRawMetadataStringValue(MessageMetadataContainer.LABEL_KEY_CALLER_IP)).isEqualTo(callerIp);
|
||||
assertThat(reactiveMetadataProvider.getRawMetadataStringValue(notExistKey)).isNull();
|
||||
|
||||
request = MockServerHttpRequest.get("/echo/" + UrlUtils.decode("a@b")).build();
|
||||
reactiveMetadataProvider = new ReactiveMetadataProvider(request, callerIp);
|
||||
assertThat(reactiveMetadataProvider.getRawMetadataStringValue(MessageMetadataContainer.LABEL_KEY_PATH)).isEqualTo("/echo/a@b");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServletMetadataProvider() {
|
||||
String headerKey1 = "header1";
|
||||
String headerKey2 = "header2";
|
||||
String headerValue1 = "value1";
|
||||
String headerValue2 = "value2/test";
|
||||
String queryKey1 = "qk1";
|
||||
String queryKey2 = "qk2";
|
||||
String queryValue1 = "qv1";
|
||||
String queryValue2 = "qv2/test";
|
||||
String cookieKey1 = "ck1";
|
||||
String cookieKey2 = "ck2";
|
||||
String cookieValue1 = "cv1";
|
||||
String cookieValue2 = "cv2/test";
|
||||
String path = "/echo/test";
|
||||
String callerIp = "localhost";
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader(headerKey1, headerValue1);
|
||||
request.addHeader(headerKey2, UrlUtils.encode(headerValue2));
|
||||
request.setCookies(new MockCookie(cookieKey1, cookieValue1), new MockCookie(cookieKey2, UrlUtils.encode(cookieValue2)));
|
||||
request.setMethod(HttpMethod.GET.name());
|
||||
request.setRequestURI(path);
|
||||
request.setQueryString(queryKey1 + "=" + queryValue1 + "&" + queryKey2 + "=" + UrlUtils.encode(queryValue2));
|
||||
|
||||
ServletMetadataProvider servletMetadataProvider = new ServletMetadataProvider(request, callerIp);
|
||||
assertThat(servletMetadataProvider.getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_HEADER, headerKey1)).isEqualTo(headerValue1);
|
||||
assertThat(servletMetadataProvider.getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_HEADER, headerKey2)).isEqualTo(headerValue2);
|
||||
// com.tencent.polaris.metadata.core.manager.ComposeMetadataProvider.getRawMetadataMapValue need return null when key don't exist
|
||||
assertThat(servletMetadataProvider.getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_HEADER, notExistKey)).isNull();
|
||||
|
||||
assertThat(servletMetadataProvider.getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_COOKIE, cookieKey1)).isEqualTo(cookieValue1);
|
||||
assertThat(servletMetadataProvider.getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_COOKIE, cookieKey2)).isEqualTo(cookieValue2);
|
||||
assertThat(servletMetadataProvider.getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_COOKIE, notExistKey)).isNull();
|
||||
|
||||
assertThat(servletMetadataProvider.getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_QUERY, queryKey1)).isEqualTo(queryValue1);
|
||||
assertThat(servletMetadataProvider.getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_QUERY, queryKey2)).isEqualTo(queryValue2);
|
||||
assertThat(servletMetadataProvider.getRawMetadataMapValue(MessageMetadataContainer.LABEL_MAP_KEY_QUERY, notExistKey)).isNull();
|
||||
assertThat(servletMetadataProvider.getRawMetadataMapValue(notExistKey, queryKey1)).isNull();
|
||||
|
||||
assertThat(servletMetadataProvider.getRawMetadataStringValue(MessageMetadataContainer.LABEL_KEY_METHOD)).isEqualTo("GET");
|
||||
assertThat(servletMetadataProvider.getRawMetadataStringValue(MessageMetadataContainer.LABEL_KEY_PATH)).isEqualTo(path);
|
||||
assertThat(servletMetadataProvider.getRawMetadataStringValue(MessageMetadataContainer.LABEL_KEY_CALLER_IP)).isEqualTo(callerIp);
|
||||
assertThat(servletMetadataProvider.getRawMetadataStringValue(notExistKey)).isNull();
|
||||
|
||||
request.setRequestURI("/echo/" + UrlUtils.decode("a@b"));
|
||||
assertThat(servletMetadataProvider.getRawMetadataStringValue(MessageMetadataContainer.LABEL_KEY_PATH)).isEqualTo("/echo/a@b");
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>spring-cloud-tencent-plugin-starters</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-cloud-starter-tencent-threadlocal-plugin</artifactId>
|
||||
<name>Spring Cloud Starter Tencent ThreadLocal plugin</name>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.tencent.polaris</groupId>
|
||||
<artifactId>polaris-threadlocal</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. 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.threadlocal;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.tencent.polaris.threadlocal.cross.RunnableWrapper;
|
||||
|
||||
import org.springframework.core.task.TaskExecutor;
|
||||
|
||||
public class TaskExecutorWrapper<T> implements TaskExecutor {
|
||||
|
||||
private final TaskExecutor taskExecutor;
|
||||
|
||||
private final Supplier<T> contextGetter;
|
||||
|
||||
private final Consumer<T> contextSetter;
|
||||
|
||||
public TaskExecutorWrapper(TaskExecutor taskExecutor, Supplier<T> contextGetter, Consumer<T> contextSetter) {
|
||||
this.taskExecutor = taskExecutor;
|
||||
this.contextGetter = contextGetter;
|
||||
this.contextSetter = contextSetter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Runnable command) {
|
||||
taskExecutor.execute(new RunnableWrapper<>(command, contextGetter, contextSetter));
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. 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.threadlocal;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Fail.fail;
|
||||
|
||||
/**
|
||||
* Test for {@link TaskExecutorWrapper}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class TaskExecutorWrapperTest {
|
||||
|
||||
private static final ThreadLocal<String> TEST_THREAD_LOCAL = new ThreadLocal<>();
|
||||
|
||||
private static final String TEST = "TEST";
|
||||
|
||||
@Test
|
||||
public void testExecute() {
|
||||
TEST_THREAD_LOCAL.set(TEST);
|
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||
executor.initialize();
|
||||
AtomicReference<Boolean> result = new AtomicReference<>(false);
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
TaskExecutorWrapper<String> taskExecutorWrapper = new TaskExecutorWrapper<>(
|
||||
executor, TEST_THREAD_LOCAL::get, TEST_THREAD_LOCAL::set);
|
||||
taskExecutorWrapper.execute(() -> {
|
||||
result.set(TEST.equals(TEST_THREAD_LOCAL.get()));
|
||||
latch.countDown();
|
||||
});
|
||||
try {
|
||||
latch.await();
|
||||
assertThat(result.get()).isTrue();
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue