@LoadBalanced RestTemplate should support direct IP address and localhost requests

未关闭
#1,601 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@xzxiaoshan 已经在做这个了。

开始于 2025年11月20日。

  • #1602 来自 @xzxiaoshan —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
java, spring
领域
api, backend

调研方向

首先定位 @LoadBalanced RestTemplate 使用的负载均衡拦截器及其 service ID 解析路径。跟踪 localhost、IPv4 和带方括号的 IPv6 主机是如何分类的,然后验证直接调用会绕过服务发现,而服务名称仍保留负载均衡和 host:port ID。针对列出的 URL 形式添加专门的覆盖测试,并运行相关测试。

由索引模型根据 Issue 内容生成。

描述

waiting-for-triage

Describe the bug/feture
When using RestTemplate with @LoadBalanced annotation, it cannot make requests to direct addresses like IP addresses (http://192.168.1.110:8080) or localhost (http://localhost:8080). The load balancer interceptor attempts to resolve these through service discovery, causing request failures.

Expected Behavior
@LoadBalanced RestTemplate should intelligently distinguish between:

  • Direct requests - IP addresses and localhost calls that should bypass service discovery
  • Service discovery requests - Service names that should go through load balancing

Proposed Solution

Modify the load balancer detection logic to identify direct requests by these patterns:

  • localhost calls with explicit ports: http://localhost:8080
  • IP address calls with explicit ports: http://192.168.1.110:8080
  • IPv6 addresses with ports: http://[::1]:8080

Enhancement to Service ID Resolution

For service discovery compatibility:

Change service ID extraction from just host to host:port format
Schema (http/https) can be ignored in service ID resolution as it's not typically relevant for service discovery
This maintains backward compatibility while providing more precise service identification

Sample Code

@Autowired
private RestTemplate loadBalancedRestTemplate;

// Direct requests - should bypass service discovery
loadBalancedRestTemplate.getForObject("http://192.168.1.110:8080/api/test", String.class);
loadBalancedRestTemplate.getForObject("http://localhost:8080/api/test", String.class);
loadBalancedRestTemplate.getForObject("http://10.0.0.1:9000/service", String.class);
loadBalancedRestTemplate.getForObject("http://[::1]:8080/ipv6", String.class);

// Service discovery requests - should use load balancing
loadBalancedRestTemplate.getForObject("http://user-service/api/users", String.class);

Benefits

Single RestTemplate instance can handle both direct and service calls
Enhanced service ID resolution using host:port improves service discovery precision
Maintains full backward compatibility
Better developer experience without requiring multiple RestTemplate instances

Environment

Spring Cloud Commons version: [4.3.0]

主要语言
Java
星标
751
派生
767
平均合并
1 天 1 小时
30 天内合并 PR
17

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

spring-cloud/spring-cloud-commons 的其他 Issue

查看 spring-cloud/spring-cloud-commons 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。