Location>code7788 >text

4. Spring Cloud Ribbon to achieve "load balancing" detailed configuration instructions

Popularity:674 ℃/2024-11-15 15:27:54

4. Spring Cloud Ribbon to achieve "load balancing" detailed configuration instructions

@

catalogs
  • 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

  1. 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.

εœ¨θΏ™ι‡Œζ’ε…₯图片描述

  1. 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:

  1. Start by selecting the EurekaServer, whichprioritization Selection of less loaded in the same areaServer Services.
  2. Then, based on the user-specified policy, select an address from the list of service registrations fetched from the Server
  3. 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

  1. Requirement: Change the default polling algorithm to a randomized algorithm RandomRule
  2. Browser input.http://localhost/member/consumer/get/1
  3. 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.

  1. 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 ClientamongRestTemplate 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)

  1. Browser input.http://localhost/member/consumer/get/1
  2. Observe that the services on port 10000/10002 that are accessed are randomized.

εœ¨θΏ™ι‡Œζ’ε…₯图片描述

4. Summary:

  1. Spring Cloud Ribbon is based on Netflix Ribbon implementation of a set of client-side , load balancing tools .
  2. LB (Load Balance) two kinds of classification:
    1. Centralized: LB
    2. In-process LB
  3. Ribbon Principles and Mechanisms
  1. Start by selecting the EurekaServer, whichprioritization Selection of less loaded in the same areaServer Services.
  2. Then, based on the user-specified policy, select an address from the list of service registrations fetched from the Server
  3. The Ribbon offers a variety of strategies: for example:Polling, randomization and weighting based on response time, etc. 。
  1. Ribbon common load balancing algorithms.
  2. Three points for configuring Spring Cloud Ribbon:
  1. Create a configuration that corresponds to the RibbonConfiguration 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.
  2. It is also necessary to add a line to the correspondingEureka ClientamongRestTemplate The location of the Http template is turned on by adding the@LoadBalanced Enable load balancingThe @LoadBalanced annotation annotates load balancing.
  3. It is also necessary to set the correspondingEureka Client The scene starter of thetaxonomic 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."

εœ¨θΏ™ι‡Œζ’ε…₯图片描述