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
相关产品推荐
相关产品推荐

