Spring Boot调用HTTP invoker远程服务出现Read time out异常如何解决
Spring Boot Http Invoker远程调用Read Timeout异常修复方案
异常信息:COULD NOT ACCESS HTTP INVOKER REMOTE SERVICE AT [service URL]; nested exception is java.net.SocketTimeoutException: Read Time out
该报错本质是调用远程服务时读取响应超时,你直接在XML中给Http Invoker配置项加readTimeout属性不生效,是因为该属性不属于HttpInvokerProxyFactoryBean本身,需要绑定到底层HTTP请求执行器中配置才会生效,以下是两种可行修复方案:
方案1:Spring Boot 推荐Java配置方式
直接通过配置类自定义HttpInvoker代理,绑定自定义超时参数的请求执行器:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean; import org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor; @Configuration public class HttpInvokerConfig { @Bean public SimpleHttpInvokerRequestExecutor httpInvokerRequestExecutor() { SimpleHttpInvokerRequestExecutor executor = new SimpleHttpInvokerRequestExecutor(); // 读取超时时间,单位毫秒,示例设为30秒,可按需调整 executor.setReadTimeout(30000); // 连接超时时间,单位毫秒,示例设为5秒 executor.setConnectTimeout(5000); return executor; } @Bean public YourRemoteService yourRemoteService(SimpleHttpInvokerRequestExecutor executor) { HttpInvokerProxyFactoryBean factoryBean = new HttpInvokerProxyFactoryBean(); // 替换为实际远程服务地址 factoryBean.setServiceUrl("你的远程服务URL"); // 替换为你自己的远程服务接口类 factoryBean.setServiceInterface(YourRemoteService.class); factoryBean.setHttpInvokerRequestExecutor(executor); factoryBean.afterPropertiesSet(); return (YourRemoteService) factoryBean.getObject(); } }
方案2:XML配置方式
需要先单独配置请求执行器再注入到Invoker代理bean中,不会再提示属性不存在:
<!-- 配置带超时参数的请求执行器 --> <bean id="httpInvokerRequestExecutor" class="org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor"> <property name="readTimeout" value="30000" /> <property name="connectTimeout" value="5000" /> </bean> <!-- 配置远程服务代理,注入上面的请求执行器 --> <bean id="yourRemoteService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="你的远程服务URL" /> <property name="serviceInterface" value="com.xxx.YourRemoteService" /> <property name="httpInvokerRequestExecutor" ref="httpInvokerRequestExecutor" /> </bean>
注意事项
- 超时时间单位均为毫秒,不建议设置过长避免请求堆积,可根据服务实际响应耗时灵活调整
- 若使用HttpClient作为底层请求实现,替换为
HttpComponentsHttpInvokerRequestExecutor配置对应参数即可,逻辑完全一致 - 调整超时参数后仍报错的话,需要排查远端服务是否正常运行、网络是否存在丢包、服务端口是否开放等问题
内容的提问来源于stack exchange,提问作者h_sew
相关产品推荐
相关产品推荐

