You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kubernetes同命名空间Pod间REST服务调用遇302重定向问题

Pod间调用Actuator返回302重定向的解决方法

你遇到的302响应是因为目标Pod的服务把HTTP请求强制重定向到了HTTPS端口(8443),和Egress规则无关,以下是几种解决思路:

1. 直接改用HTTPS调用

把请求URL的协议从http改成https,端口替换为8443:

final String otherPodEndpoint = "https://" + v1Pod.getStatus().getPodIP() + ":8443/[app_name]/actuator/health";

注意:如果目标服务使用自签名SSL证书,需要给RestTemplate配置信任该证书的逻辑,否则会出现SSL握手失败的错误。

2. 关闭目标服务的HTTPS重定向

如果不需要强制HTTPS访问,修改目标服务的配置:

  • 若使用Spring Boot,在application.yml或application.properties中添加:
server.http.redirect-to-https=false
  • 检查目标服务是否存在自定义过滤器(Filter)或WebMvc配置,移除其中HTTP转HTTPS的重定向逻辑。

3. 配置RestTemplate自动跟随重定向

保持原HTTP调用的前提下,让RestTemplate自动处理302跳转。默认RestTemplate不开启自动重定向,需手动配置:

// 使用SimpleClientHttpRequestFactory开启重定向
RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setFollowRedirects(true);
restTemplate.setRequestFactory(requestFactory);

// 或者使用Apache HttpComponents实现更灵活的重定向策略
CloseableHttpClient httpClient = HttpClientBuilder.create()
        .setRedirectStrategy(new LaxRedirectStrategy())
        .build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);

内容的提问来源于stack exchange,提问作者orion_kid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 23:42:39