4. Spring Cloud Ribbon to achieve "load balancing" detailed configuration instructions
@
- 4. Spring Cloud Ribbon to achieve "load balancing" detailed configuration instructions
- preamble
-
1. Introduction to Ribbon
- 1.1 LB (Load Balance)
-
2. The Ribbon Principle
- 2.2 Ribbon mechanism
- 3. Spring Cloud Ribbon Implementation of Load Balancing Algorithm - Application Examples
- 4. Summary:
- 5. Finally:
preamble
- Corresponding to the previous study: πππππππππππChinaRainbowSea-CSDN Blog
- Corresponding to the next study: πππππππππππ
1. Introduction to Ribbon
What is Ribbon?
Cloud Ribbon is a client-side, load-balancing tool based on the Netflix Ribbon implementation.
The main function is to provide client load balancing algorithms and service invocation
The client component provides a series of well-defined configuration items such as "connection timeout, retry".
will connect to the specified service based on some rule (e.g., simple polling, random connection, etc.)
5. Programmers can easily implement load balancing by Ribbon's load balancing algorithm.
6. Bottom line: Ribbon: Load Balancing + RestTemplate Calls
Ribbon's official website address./Netflix/ribbon
Ribbon goes into maintenance?
Ribbon is currently in maintenance mode, the future replacement is Spring Cloud LoadBalancer.
1.1 LB (Load Balance)
LB Classification
- Centralized: LB
I.e., use of separate LB facilities (either hardware, e.g., F5, or software, e.g., Nginx) between the consumer and the provider of the service
It is the responsibility of the facility to forward access requests to the provider of the service via some policy.
- In-process LB
The LB logic is integrated into the consumer side, where the consumer service registry is informed of what addresses are available, and then selects from those addresses.
A suitable service address
Ribbon is an in-process L B. It is just a class library that is integrated into the consumer process and through which the consumer gets the address of the service provider.
Special Note: On the previous study: example-front member-consumer polling load access 10000/10002 The underlying is the Ribbon's default polling load algorithm.
2. The Ribbon Principle
Ribbon Architecture Diagram & Mechanisms
2.2 Ribbon mechanism
Ribbon mechanism:
- Start by selecting the EurekaServer, whichprioritization Selection of less loaded in the same areaServer Services.
- Then, based on the user-specified policy, select an address from the list of service registrations fetched from the Server
- The Ribbon provides a variety of strategies: for example:Polling, randomization and weighting based on response time, etc. γ
Ribbon Common Load Algorithms
3. Spring Cloud Ribbon Implementation of Load Balancing Algorithm - Application Examples
Needs analysis/illustration
- Requirement: Change the default polling algorithm to a randomized algorithm RandomRule
- Browser input.http://localhost/member/consumer/get/1
- Requirements The service on port 10000/10002 accessed is randomized, that is, set to:RandomRule stochastic load balancing
Actually: the so-called randomization is random on a small amount of data, but on a large amount of data, the randomization is essentially the same as theon average Pretty much.
- First: create member-service-consumer-80 com/rainbowsea/springcloud/config/, the configuration class of the random load balancing algorithm to configure thestochastic load balancing γ
package ;
import ;
import ;
import ;
import ;
@Configuration
public class RibbonRule {
@Bean // Configure to inject your own load balancing algorithm.
public IRule myRibbonRule() {
// Here the teacher returns the RandomRule, but you can specify something else.
//return new WeightedResponseTimeRule(); // The teacher returns a RandomRule.
return new RandomRule(); // Random algorithm
}
}
Note: It's also necessary to add a new parameter to the correspondingEureka Clientamong
RestTemplate
The location of the Http template is turned on by adding the@LoadBalanced
Enable load balancing
as well as in the correspondingEureka Client The scene starter is opened in the location of the scene starter by adding the
@RibbonClient
The note indicates which algorithm is used for load balancing under the Ribbon.
@RibbonClient(name = "MEMBER_SERVICE_PROVIDER_URL",configuration = ) // Specify the load balancing algorithm for the Ribbon
package ; import ; import ; import ; import ; import ; import ; // If the error: exclusion DataSourceAutoConfiguration auto-configuration //@SpringBootApplication(exclude = ) //@EnableEurekaClient Indicates that the item will be/module (in software)/programs Labeled as Eureka Client @EnableEurekaClient @SpringBootApplication @EnableDiscoveryClient // Enabling Service Discovery @RibbonClient(name = "MEMBER_SERVICE_PROVIDER_URL",configuration = ) // indicate clearly and with certainty Ribbon load balancing algorithms public class MemberConsumerApplication80 { public static void main(String[] args) { (, args); } }
beta (software)
- Browser input.http://localhost/member/consumer/get/1
- Observe that the services on port 10000/10002 that are accessed are randomized.
4. Summary:
- Spring Cloud Ribbon is based on Netflix Ribbon implementation of a set of client-side , load balancing tools .
- LB (Load Balance) two kinds of classification:
- Centralized: LB
- In-process LB
- Ribbon Principles and Mechanisms
- Start by selecting the EurekaServer, whichprioritization Selection of less loaded in the same areaServer Services.
- Then, based on the user-specified policy, select an address from the list of service registrations fetched from the Server
- The Ribbon offers a variety of strategies: for example:Polling, randomization and weighting based on response time, etc. γ
- Ribbon common load balancing algorithms.
- Three points for configuring Spring Cloud Ribbon:
- Create a configuration that corresponds to the Ribbon
Configuration class for load balancing algorithms (note the markup of the configuration class annotation @Configuration and the addition of '@Bean join ioc container management' at the corresponding method)
Configurationload balancing If you have a load balancing algorithm in the Ribbon, you can just new the corresponding load balancing algorithm in the Ribbon, e.g., RandomRule, RoundRobinRule.- It is also necessary to add a line to the correspondingEureka Clientamong
RestTemplate
The location of the Http template is turned on by adding the@LoadBalanced
Enable load balancingThe @LoadBalanced annotation annotates load balancing.
- It is also necessary to set the correspondingEureka Client The scene starter of the
taxonomic class
The position opens and is joined by the@RibbonClient
The note indicates which algorithm is used for load balancing under the Ribbon.@RibbonClient(name = "MEMBER_SERVICE_PROVIDER_URL",configuration = ) // Specify the load balancing algorithm for the Ribbon
5. Finally:
"In this final chapter, I would like to express my gratitude to each and every one of my readers. Your attention and responses have been a source of motivation for my creative endeavors, and I have drawn endless inspiration and courage from you. I will keep your encouragement in my heart and continue to struggle in other areas. Thanks to you all, we will always meet again at some point."