|
|
@ -15,7 +15,7 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.cloud.openfeign.loadbalancer; |
|
|
|
package org.springframework.cloud.openfeign.loadbalancer; |
|
|
|
|
|
|
|
// comment-by-kai 源码阅读 spring cloud 2021.x
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.net.URI; |
|
|
|
import java.net.URI; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
@ -94,6 +94,8 @@ public class FeignBlockingLoadBalancerClient implements Client { |
|
|
|
loadBalancerClientFactory.getInstances(serviceId, LoadBalancerLifecycle.class), |
|
|
|
loadBalancerClientFactory.getInstances(serviceId, LoadBalancerLifecycle.class), |
|
|
|
RequestDataContext.class, ResponseData.class, ServiceInstance.class); |
|
|
|
RequestDataContext.class, ResponseData.class, ServiceInstance.class); |
|
|
|
supportedLifecycleProcessors.forEach(lifecycle -> lifecycle.onStart(lbRequest)); |
|
|
|
supportedLifecycleProcessors.forEach(lifecycle -> lifecycle.onStart(lbRequest)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// loadBalancerClient根据serviceId,选择合适的ServiceInstance
|
|
|
|
ServiceInstance instance = loadBalancerClient.choose(serviceId, lbRequest); |
|
|
|
ServiceInstance instance = loadBalancerClient.choose(serviceId, lbRequest); |
|
|
|
org.springframework.cloud.client.loadbalancer.Response<ServiceInstance> lbResponse = new DefaultResponse( |
|
|
|
org.springframework.cloud.client.loadbalancer.Response<ServiceInstance> lbResponse = new DefaultResponse( |
|
|
|
instance); |
|
|
|
instance); |
|
|
@ -108,6 +110,8 @@ public class FeignBlockingLoadBalancerClient implements Client { |
|
|
|
return Response.builder().request(request).status(HttpStatus.SERVICE_UNAVAILABLE.value()) |
|
|
|
return Response.builder().request(request).status(HttpStatus.SERVICE_UNAVAILABLE.value()) |
|
|
|
.body(message, StandardCharsets.UTF_8).build(); |
|
|
|
.body(message, StandardCharsets.UTF_8).build(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 重构URL,替换serviceId为实际的通信地址
|
|
|
|
String reconstructedUrl = loadBalancerClient.reconstructURI(instance, originalUri).toString(); |
|
|
|
String reconstructedUrl = loadBalancerClient.reconstructURI(instance, originalUri).toString(); |
|
|
|
Request newRequest = buildRequest(request, reconstructedUrl); |
|
|
|
Request newRequest = buildRequest(request, reconstructedUrl); |
|
|
|
LoadBalancerProperties loadBalancerProperties = loadBalancerClientFactory.getProperties(serviceId); |
|
|
|
LoadBalancerProperties loadBalancerProperties = loadBalancerClientFactory.getProperties(serviceId); |
|
|
|