Browse Source

spring cloud 2021.x 源码阅读, OpenFeign + LoadBalancer + Nacos Discovery

hekai-study-v2021.x
Kai 1 year ago
parent
commit
6a8ebb2217
  1. 24
      pom.xml
  2. 6
      spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/loadbalancer/FeignBlockingLoadBalancerClient.java

24
pom.xml

@ -71,10 +71,10 @@
<target>1.8</target> <target>1.8</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <!-- <plugin>-->
<groupId>org.apache.maven.plugins</groupId> <!-- <groupId>org.apache.maven.plugins</groupId>-->
<artifactId>maven-checkstyle-plugin</artifactId> <!-- <artifactId>maven-checkstyle-plugin</artifactId>-->
</plugin> <!-- </plugin>-->
<plugin> <plugin>
<groupId>io.spring.javaformat</groupId> <groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId> <artifactId>spring-javaformat-maven-plugin</artifactId>
@ -91,14 +91,14 @@
</plugins> </plugins>
</build> </build>
<reporting> <!-- <reporting>-->
<plugins> <!-- <plugins>-->
<plugin> <!-- <plugin>-->
<groupId>org.apache.maven.plugins</groupId> <!-- <groupId>org.apache.maven.plugins</groupId>-->
<artifactId>maven-checkstyle-plugin</artifactId> <!-- <artifactId>maven-checkstyle-plugin</artifactId>-->
</plugin> <!-- </plugin>-->
</plugins> <!-- </plugins>-->
</reporting> <!-- </reporting>-->
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>

6
spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/loadbalancer/FeignBlockingLoadBalancerClient.java

@ -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);

Loading…
Cancel
Save