实际这是一篇给出临时性解决方案的博文,因为这个问题有望在Spring Cloud Alibaba2.2.3版本以后解决掉,https://github.com/alibaba/spring-cloud-alibaba/issues/1346,这里也介绍了这个Bug的相关情况。
在使用RestTemplate的时候,一般情况下遇到网络故障或服务超时会报出例如:java.net.SocketTimeoutException: Read timed out, java.net.SocketException: connetct time out 这种通信异常的报错,但是当使用了@SentinelRestTemplate注解后,开启了Sentinel的限流降级以后,SentinelProtectInterceptor拦截器中com.alibaba.cloud.sentinel.custom.SentinelProtectInterceptor.intercept(HttpRequest, byte[], ClientHttpRequestExecution) 方法,Catch方法中,会吞掉错误的异常抛出,使得response返回null, 这个时候,RestTemplate调用的Response变为null,致使程序会报出 java.lang.NullPointerException的错误。
Entry hostEntry = null;
Entry hostWithPathEntry = null;
ClientHttpResponse response = null;
try {
hostEntry = SphU.entry(hostResource, EntryType.OUT);
if (entryWithPath) {
hostWithPathEntry = SphU.entry(hostWithPathResource, EntryType.OUT);
}
response = execution.execute(request, body);
if (this.restTemplate.getErrorHandler().hasError(response)) {
Tracer.trace(
new IllegalStateException("RestTemplate ErrorHandler has error"));
}
}
catch (Throwable e) {
if (!BlockException.isBlockException(e)) {
Tracer.trace(e);
// 这里会吞噬掉错误不再像外部抛出
}
else {
return handleBlockException(request, body, execution, (BlockException) e);

最低0.47元/天 解锁文章
703

被折叠的 条评论
为什么被折叠?



