diff --git a/CHANGELOG.md b/CHANGELOG.md index dbacc3555..705db065e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,3 +6,4 @@ - [Optimize: Maybe remove Chinese characters](https://github.com/Tencent/spring-cloud-tencent/pull/608) - [Bugfix: fix feign report call result error when using feign direct call](https://github.com/Tencent/spring-cloud-tencent/pull/622) - [Feature: support spring-retry router](https://github.com/Tencent/spring-cloud-tencent/pull/633) +- [Bugfix: fix throw npe when router context is null](https://github.com/Tencent/spring-cloud-tencent/pull/634) diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java index 63be917cc..3de268ce6 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java @@ -59,11 +59,11 @@ import static com.tencent.cloud.common.constant.ContextConstant.UTF_8; /** * Service routing entrance. - * + *

* Rule routing needs to rely on request parameters for server filtering. * The interface cannot obtain the context object of the request granularity, * so the routing capability cannot be achieved through ServerListFilter. - * + *

* And {@link PolarisRouterServiceInstanceListSupplier#get(Request)} provides the ability to pass in http headers, * so routing capabilities are implemented through IRule. * @@ -95,21 +95,23 @@ public class PolarisRouterServiceInstanceListSupplier extends DelegatingServiceI Flux> allServers = getDelegate().get(); // 2. filter by router + PolarisRouterContext routerContext = null; + DefaultRequestContext requestContext = (DefaultRequestContext) request.getContext(); - PolarisRouterContext key; if (requestContext instanceof RequestDataContext) { - key = buildRouterContext(((RequestDataContext) requestContext).getClientRequest().getHeaders()); + routerContext = buildRouterContext(((RequestDataContext) requestContext).getClientRequest().getHeaders()); } else if (requestContext.getClientRequest() instanceof PolarisLoadBalancerRequest) { - key = buildRouterContext(((PolarisLoadBalancerRequest) requestContext.getClientRequest()).getRequest() + routerContext = buildRouterContext(((PolarisLoadBalancerRequest) requestContext.getClientRequest()).getRequest() .getHeaders()); } - else { + + if (routerContext == null) { // return all servers if router context is null. return allServers; } - return doRouter(allServers, key); + return doRouter(allServers, routerContext); } //set method to public for unit test