@LoadBalanced RestTemplate should support direct IP address and localhost requests

Đang mở
#1,601 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@xzxiaoshan đang làm issue này rồi.

Từ ngày 20/11/2025.

  • #1602 của @xzxiaoshan — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
java, spring
Lĩnh vực
api, backend

Hướng nghiên cứu

Bắt đầu bằng cách xác định load-balancer interceptor được @LoadBalanced RestTemplate sử dụng và đường dẫn phân giải service ID của nó. Theo dõi cách các host localhost, IPv4 và IPv6 được đặt trong dấu ngoặc vuông được phân loại, sau đó xác minh rằng các lệnh gọi trực tiếp bỏ qua discovery, trong khi tên dịch vụ vẫn giữ load balancing và các ID host:port. Bổ sung coverage tập trung cho các dạng URL được liệt kê và chạy các test liên quan.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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]

Ngôn ngữ chính
Java
Star
751
Fork
748
Merge trung bình
1 ngày 1 giờ
Pull request đã merge (30 ngày)
17

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của spring-cloud/spring-cloud-commons

Tất cả issue của spring-cloud/spring-cloud-commons

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.