“This is the 15th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.
Hello, I am Wukong.
So if you were to design a load balancing component, what would you do?
Here are a few factors to consider:
- How do I get and synchronize the server list? It involves interaction with the registry.
- How to split the load? The allocation strategy is involved.
- How do I intercept a client request and select a server to forward it? Request interception is involved.
With these questions in mind, let’s learn how to design a load balancer based on the principles of load balancing and the Ribbon architecture.
How the Ribbon intercepts requests
This article started with a question: How does a load balancer intercept a client request and then select a server to forward it?
With the Ribbon core components outlined above, we can draw a schematic of how the Ribbon intercepts requests.
Step 1: The Ribbon intercepts all restTemplates annotated with the @loadBalance annotation. RestTemplate is used to send HTTP requests.
Step 2: Add the Ribbon’s default LoadBalancerInterceptor to the execution logic of the RestTemplate. When the RestTemplate sends an HTTP request, the Ribbon intercepts it.
Step 3: After the interception, the Ribbon creates an ILoadBalancer instance.
Step 4: ILoadBalancer instance will use RibbonClientConfiguration complete automatic configuration. IRule, IPing, ServerList will be configured.
Step 5: The Ribbon selects a service from the list of services and forwards the request to the service.
About the author: Wu Kong, 8 years of experience in Internet development and architecture, explains distributed, architecture design, Java core technology with stories. Author of “JVM Performance Optimization Practice” column, open source “Spring Cloud Practice PassJava” project, public account: Wukong chat architecture. This article is available at www.passjava.cn