@SentinelRestTemplate集成RestTemplate空指针异常

实际这是一篇给出临时性解决方案的博文,因为这个问题有望在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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值