This is my 26th day of the August Genwen Challenge
This series code address: github.com/HashZhang/s…
We used Spring Cloud LoadBalancer as our client LoadBalancer, which is officially recommended by Spring Cloud.
Spring Cloud LoadBalancer background
Spring Cloud LoadBalancer is a client LoadBalancer similar to Ribbon, but Ribbon 2 is not compatible with Ribbon 1 because Ribbon 2 is in maintenance mode. So the Spring Cloud family bucket added Spring Cloud Loadbalancer as a new Loadbalancer in the Spring Cloud Commons project, and made forward compatibility. Even if your project continues to use the Spring Cloud Netflix suite (Ribbon, Eureka, Zuul, Hystrix, etc.) to make your project have these dependencies, you can simply configure Replace ribbon with Spring Cloud LoadBalancer.
Where is the load balancer used?
Internal microservice calls in Spring Cloud default to HTTP requests through the following three apis:
- RestTemplate: synchronizes HTTP APIS
- WebClient: Asynchronous and responsive HTTP API
- Tripartite client encapsulation, such as OpenFeign
If a spring-cloud-loadbalancer dependency is added to the project and the configuration is enabled, loadbalancer features are automatically added to the associated beans.
- For RestTemplate, it automatically checks all
@LoadBalanced
The annotated RestTemplate Bean adds the load balancer feature by adding the Interceptor. - For WebClient, it is created automatically
ReactorLoadBalancerExchangeFilterFunction
And we can do that by joiningReactorLoadBalancerExchangeFilterFunction
Load balancer features are added. - For three-party clients, we generally don’t need to configure anything extra.
Examples of these uses will be seen in our final test after the upgrade series.
Introduction to the Spring Cloud LoadBalancer architecture
Previous articles in this series mentioned NamedContextFactory, Spring Cloud LoadBalancer. This mechanism is also used here to implement different microservices using different Spring Cloud LoadBalancer configurations. The relevant core implementation is the @loadBalancerClient and @LoadBalancerClients annotations, And NamedContextFactory. LoadBalancerClientSpecification Specification, The realization of the NamedContextFactory LoadBalancerClientFactory. As shown below:
- Can be achieved by
loadbalancer.client.name
This property gets which microservice the Bean is currently being created for - You can see that the default configuration is
LoadBalancerClientConfiguration
, we can see that the main initialization of two beans:- ReactorLoadBalancer, because there are
@ConditionalOnMissingBean
So it can be replaced, so that’s our extension point - ServiceInstanceSupplier, the Supplier that provides instance information, as there is
@ConditionalOnMissingBean
So it can be replaced, so that’s our extension point
- ReactorLoadBalancer, because there are
- Specification for LoadBalancerSpecification, and then analyze its calls can be know, can get through it
@LoadBalancerClient
和@LoadBalancerClients
在LoadBalancerClientConfiguration
Based on additional specified configurations.
This section provides a brief overview of the use scenarios for Spring Cloud LoadBalancer, as well as the architectural design and extension points. In the next section, we’ll examine the Spring Cloud LoadBalancer source code in detail to understand how this works.
Wechat search “my programming meow” public account, a daily brush, easy to improve skills, won a variety of offers