fix: fix PolarisRouterServiceInstanceListSupplier npe with reactive feign (#925)

pull/945/head
Shanyou Yu (Sean Yu) 2 years ago committed by GitHub
parent 5b56424aa5
commit 1f4a3bcb4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,3 +9,4 @@
- [feature: improve circuit breaker usage.](https://github.com/Tencent/spring-cloud-tencent/pull/916) - [feature: improve circuit breaker usage.](https://github.com/Tencent/spring-cloud-tencent/pull/916)
- [fix:fix nacos and consul registration.](https://github.com/Tencent/spring-cloud-tencent/pull/919) - [fix:fix nacos and consul registration.](https://github.com/Tencent/spring-cloud-tencent/pull/919)
- [Documentation content changes: add circuitbreaker readme.](https://github.com/Tencent/spring-cloud-tencent/pull/929) - [Documentation content changes: add circuitbreaker readme.](https://github.com/Tencent/spring-cloud-tencent/pull/929)
- [fix: fix PolarisRouterServiceInstanceListSupplier npe with reactive feign.](https://github.com/Tencent/spring-cloud-tencent/pull/925)

@ -97,6 +97,7 @@ public class PolarisRouterServiceInstanceListSupplier extends DelegatingServiceI
PolarisRouterContext routerContext = null; PolarisRouterContext routerContext = null;
DefaultRequestContext requestContext = (DefaultRequestContext) request.getContext(); DefaultRequestContext requestContext = (DefaultRequestContext) request.getContext();
if (requestContext != null) {
if (requestContext instanceof RequestDataContext) { if (requestContext instanceof RequestDataContext) {
routerContext = buildRouterContext(((RequestDataContext) requestContext).getClientRequest().getHeaders()); routerContext = buildRouterContext(((RequestDataContext) requestContext).getClientRequest().getHeaders());
} }
@ -104,6 +105,7 @@ public class PolarisRouterServiceInstanceListSupplier extends DelegatingServiceI
routerContext = buildRouterContext(((PolarisLoadBalancerRequest<?>) requestContext.getClientRequest()).getRequest() routerContext = buildRouterContext(((PolarisLoadBalancerRequest<?>) requestContext.getClientRequest()).getRequest()
.getHeaders()); .getHeaders());
} }
}
if (routerContext == null) { if (routerContext == null) {
// return all servers if router context is null. // return all servers if router context is null.

@ -38,7 +38,9 @@ import com.tencent.cloud.polaris.router.config.properties.PolarisRuleBasedRouter
import com.tencent.cloud.polaris.router.interceptor.MetadataRouterRequestInterceptor; import com.tencent.cloud.polaris.router.interceptor.MetadataRouterRequestInterceptor;
import com.tencent.cloud.polaris.router.interceptor.NearbyRouterRequestInterceptor; import com.tencent.cloud.polaris.router.interceptor.NearbyRouterRequestInterceptor;
import com.tencent.cloud.polaris.router.interceptor.RuleBasedRouterRequestInterceptor; import com.tencent.cloud.polaris.router.interceptor.RuleBasedRouterRequestInterceptor;
import com.tencent.cloud.polaris.router.resttemplate.PolarisLoadBalancerRequest;
import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor; import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor;
import com.tencent.cloud.polaris.router.spi.RouterResponseInterceptor;
import com.tencent.polaris.api.exception.PolarisException; import com.tencent.polaris.api.exception.PolarisException;
import com.tencent.polaris.api.pojo.DefaultInstance; import com.tencent.polaris.api.pojo.DefaultInstance;
import com.tencent.polaris.api.pojo.DefaultServiceInstances; import com.tencent.polaris.api.pojo.DefaultServiceInstances;
@ -63,6 +65,7 @@ import reactor.core.publisher.Flux;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.DefaultRequest; import org.springframework.cloud.client.loadbalancer.DefaultRequest;
import org.springframework.cloud.client.loadbalancer.DefaultRequestContext;
import org.springframework.cloud.client.loadbalancer.RequestData; import org.springframework.cloud.client.loadbalancer.RequestData;
import org.springframework.cloud.client.loadbalancer.RequestDataContext; import org.springframework.cloud.client.loadbalancer.RequestDataContext;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
@ -219,7 +222,7 @@ public class PolarisRouterServiceInstanceListSupplierTest {
setTransitiveMetadata(); setTransitiveMetadata();
PolarisRouterServiceInstanceListSupplier polarisSupplier = new PolarisRouterServiceInstanceListSupplier( PolarisRouterServiceInstanceListSupplier polarisSupplier = new PolarisRouterServiceInstanceListSupplier(
delegate, routerAPI, requestInterceptors, null); delegate, routerAPI, requestInterceptors, Collections.singletonList(new TestRouterResponseInterceptor()));
ProcessRoutersResponse assembleResponse = assembleProcessRoutersResponse(); ProcessRoutersResponse assembleResponse = assembleProcessRoutersResponse();
when(routerAPI.processRouters(any())).thenReturn(assembleResponse); when(routerAPI.processRouters(any())).thenReturn(assembleResponse);
@ -279,6 +282,30 @@ public class PolarisRouterServiceInstanceListSupplierTest {
} }
} }
@Test
public void testGet03() {
try (MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class)) {
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
.thenReturn(testCallerService);
MetadataContextHolder.set(new MetadataContext());
mockedApplicationContextAwareUtils.when(() -> delegate.get())
.thenReturn(assembleServers());
mockedApplicationContextAwareUtils.when(() -> routerAPI.processRouters(any()))
.thenReturn(new ProcessRoutersResponse(new DefaultServiceInstances(null, new ArrayList<>())));
PolarisRouterServiceInstanceListSupplier polarisSupplier = new PolarisRouterServiceInstanceListSupplier(
delegate, routerAPI, requestInterceptors, null);
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("/" + testCalleeService + "/users")
.header("k1", "v1")
.header(RouterConstant.ROUTER_LABEL_HEADER, "{\"k1\":\"v1\"}")
.queryParam("userid", "zhangsan")
.build();
DefaultRequestContext requestDataContext = new DefaultRequestContext(new PolarisLoadBalancerRequest(httpRequest, null), "blue");
DefaultRequest request = new DefaultRequest(requestDataContext);
assertThat(polarisSupplier.get(request).blockFirst().size()).isEqualTo(0);
}
}
private void setTransitiveMetadata() { private void setTransitiveMetadata() {
if (initTransitiveMetadata.compareAndSet(false, true)) { if (initTransitiveMetadata.compareAndSet(false, true)) {
// mock transitive metadata // mock transitive metadata
@ -325,4 +352,11 @@ public class PolarisRouterServiceInstanceListSupplierTest {
} }
return Flux.fromIterable(Collections.singletonList(servers)); return Flux.fromIterable(Collections.singletonList(servers));
} }
public class TestRouterResponseInterceptor implements RouterResponseInterceptor {
@Override
public void apply(ProcessRoutersResponse response, PolarisRouterContext routerContext) {
// do nothing
}
}
} }

Loading…
Cancel
Save