Spring Boot 3.4.0 HTTP接口迁移后请求异常求助
Spring Boot 3.4.0 HTTP远程调用异常排查与解决需求
原Spring Boot 3.3.6实现代码
RestClient代理Bean配置
@Bean public RestRepositoryUser restRepositoryUser( @NonNull @Value("${api.url}") String apiUrl) throws URISyntaxException { RestClient restClient = RestClient.builder().baseUrl(new URI(apiUrl)).build(); HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder() .exchangeAdapter(RestClientAdapter.create(restClient)).build(); return factory.createClient(RestRepositoryGsUser.class); }
远程调用接口定义
public interface RestRepositoryUser { @GetExchange("/api/clientPf-data") public ResponseEntity<List<ClientPf>> findAllPortfolios( @RequestHeader("api-version") String version, @RequestHeader("api-requesterId") String requesterId, @RequestHeader("api-invocationContext") String invocationContext); }
服务层调用逻辑
@Log4j2 @Service @RequiredArgsConstructor public class ServicePortfolio { private final RestRepositoryUser restRepositoryPortfolio; public List<ClientPf> findAllPortfolios() { ResponseEntity<List<ClientPf>> response = restRepositoryPortfolio.findAllPortfolios("1.0", "maydaylocal", "maydaylocal"); log.info("FindAllPortfolios: response from api " + response.getStatusCode()); return response.getBody(); } }
迁移至Spring Boot 3.4.0后的配置与异常
添加的HTTP依赖
<dependency> <groupId>org.apache.httpcomponents.client5</groupId> <artifactId>httpclient5</artifactId> </dependency>
异常表现
- 首次GET请求返回401状态码,但响应体为空(正常情况下服务器会返回JSON格式的401响应)
- 后续请求出现超时
不同HTTP客户端测试结果
RestTemplate测试
- 使用http-components:请求可执行,但性能极差(加载小型JSON需15秒),频繁请求无超时
- 使用simple:请求可执行,但性能极差(加载小型JSON需17秒),频繁请求无超时
- 使用jdk:请求可执行,但性能极差(加载小型JSON需19秒),频繁请求无超时
RestClient测试
- 使用http-components:无法正常工作,频繁请求时超时
- 使用simple:无法正常工作,频繁请求时超时
- 使用jdk:无法正常工作,频繁请求时超时
需求
需要针对上述问题提供排查思路与解决方案。
内容的提问来源于stack exchange,提问作者BAUMERT Yoann
相关产品推荐
相关产品推荐

