fix:fix NullPointerException error when using RestTemplate with no label. (#586)

pull/589/head
Haotian Zhang 2 years ago committed by GitHub
parent 8eec5cc58c
commit 3525f53007
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -37,3 +37,4 @@
- [refactor:optimize project and code.](https://github.com/Tencent/spring-cloud-tencent/pull/571)
- [Optimize:change default dynamic config refresh type to reflect.](https://github.com/Tencent/spring-cloud-tencent/pull/575)
- [feat:optimize examples.](https://github.com/Tencent/spring-cloud-tencent/pull/578)
- [fix:fix NullPointerException error when using RestTemplate with no label.](https://github.com/Tencent/spring-cloud-tencent/pull/586)

@ -25,6 +25,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.tencent.cloud.common.constant.RouterConstants;
import com.tencent.cloud.common.metadata.MetadataContext;
@ -121,8 +122,12 @@ public class PolarisRouterServiceInstanceListSupplier extends DelegatingServiceI
Map<String, String> labelHeaderValuesMap = new HashMap<>();
try {
String labelHeaderValuesContent = labelHeaderValues.stream().findFirst().get();
labelHeaderValuesMap.putAll(JacksonUtils.deserialize2Map(URLDecoder.decode(labelHeaderValuesContent, UTF_8)));
Optional<String> labelHeaderValuesOptional = labelHeaderValues.stream().findFirst();
if (labelHeaderValuesOptional.isPresent()) {
String labelHeaderValuesContent = labelHeaderValuesOptional.get();
labelHeaderValuesMap.putAll(
JacksonUtils.deserialize2Map(URLDecoder.decode(labelHeaderValuesContent, UTF_8)));
}
}
catch (UnsupportedEncodingException e) {
throw new RuntimeException("unsupported charset exception " + UTF_8);

@ -50,11 +50,13 @@ import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
/**
* PolarisLoadBalancerInterceptor extends LoadBalancerInterceptor capabilities.
* Parses the label from the request and puts it into the RouterContext for routing.
*
*@author lepdou 2022-05-18
* @author lepdou, cheese8
*/
public class PolarisLoadBalancerInterceptor extends LoadBalancerInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisLoadBalancerInterceptor.class);
@ -132,18 +134,14 @@ public class PolarisLoadBalancerInterceptor extends LoadBalancerInterceptor {
labels.putAll(transitiveLabels);
// pass label by header
if (labels.size() == 0) {
request.getHeaders().set(RouterConstants.ROUTER_LABEL_HEADER, null);
return;
}
String encodedLabelsContent;
try {
String headerMetadataStr = URLEncoder.encode(JacksonUtils.serialize2Json(labels), "UTF-8");
request.getHeaders().set(RouterConstants.ROUTER_LABEL_HEADER, headerMetadataStr);
encodedLabelsContent = URLEncoder.encode(JacksonUtils.serialize2Json(labels), UTF_8);
}
catch (UnsupportedEncodingException e) {
LOGGER.error("Set header failed.", e);
throw new RuntimeException(e);
throw new RuntimeException("unsupported charset exception " + UTF_8);
}
request.getHeaders().set(RouterConstants.ROUTER_LABEL_HEADER, encodedLabelsContent);
}
private Map<String, String> getExpressionLabels(HttpRequest request, Set<String> labelKeys) {

@ -73,7 +73,7 @@
<revision>1.7.0-2021.0.3-SNAPSHOT</revision>
<!-- Dependencies -->
<polaris.version>1.7.4-SNAPSHOT</polaris.version>
<polaris.version>1.7.4</polaris.version>
<guava.version>31.0.1-jre</guava.version>
<logback.version>1.2.11</logback.version>
<mocktio.version>4.5.1</mocktio.version>

Loading…
Cancel
Save