search
HomeJavajavaTutorialspring cloud client load balancing Ribbon

spring cloud client load balancing Ribbon

Jun 26, 2017 am 09:31 AM
cloudspringclientload

1. Load Balancing

Load Balance: Built on the existing network structure On top of that, it provides a cheap, effective and transparent method to expand the bandwidth of network devices and servers, increase throughput, enhance network data processing capabilities, and improve network flexibility and availability. What it means is to allocate execution to multiple operating units, such as Web servers, FTP servers, enterprise key application servers and other mission-critical servers, etc., so as to complete work tasks together.

1. Server-side load balancing: The client requests the load balancing server, and the load balancing server forwards the request to a server that actually provides services based on its own algorithm. The server will The response data is sent to the load balancing server, and the load balancing server finally returns the data to the client. (nginx)

2. Client load balancing: Client-based load balancing, simply put, is to set a scheduling algorithm in the client program and initiate a request to the server When , first execute the scheduling algorithm to calculate which server to initiate the request to, and then initiate the request to the server.

Features based on client load balancing:

  • is implemented by the client’s internal program and does not require additional investment in load balancer software and hardware.

  • The problem of unavailability of the business server needs to be solved within the program. Server failure has little transparency to the application.

  • The program needs to solve the problem of pressure overload on the business server.

2. Ribbon implements client load balancing

We use spring boot to test.

pom file:

<project>
  <modelversion>4.0.0</modelversion>
  <groupid>com.jalja.org</groupid>
  <artifactid>spring-consumer-server-ribbon</artifactid>
  <version>0.0.1-SNAPSHOT</version>
  
   <parent><groupid>org.springframework.boot</groupid><artifactid>spring-boot-starter-parent</artifactid><version>1.5.2.RELEASE</version></parent><properties><project.build.sourceencoding>UTF-8</project.build.sourceencoding><project.reporting.outputencoding>UTF-8</project.reporting.outputencoding><java.version>1.8</java.version></properties><dependencymanagement><dependencies><dependency><groupid>org.springframework.cloud</groupid><artifactid>spring-cloud-dependencies</artifactid><version>Camden.SR4</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencymanagement><dependencies> <dependency><groupid>org.springframework.cloud</groupid><artifactid>spring-cloud-starter-ribbon</artifactid></dependency><dependency><groupid>org.springframework.boot</groupid><artifactid>spring-boot-starter-web</artifactid></dependency></dependencies></project>

application.yml

stores:
  ribbon:
    listOfServers: www.baidu.com,www.jalja.org,www.163.com

Ribbon’s load balancing strategy

1. RoundRobinRule (polling mode) public class RoundRobinRule extends AbstractLoadBalancerRule roundRobin method polls to select the server Polls the index and selects the server corresponding to the index This strategy is also the default strategy of ribbon

SpringCloudRibbonApplication.java
   ="static"= loadBalancer.choose("stores"= URI.create(String.format("http://%s:%s" "static"

:80
:80
:80
:80
:80
:80

2. RandomRule (random strategy) public class RandomRule extends AbstractLoadBalancerRule Randomly select a server Randomly select index on index Server corresponding to the location.

Add to the configuration file application.yml

NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
stores:
  ribbon:
    listOfServers: www.baidu.com,www.jalja.org,www.163.org
    #随机
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

Add

    @Beanpublic IRule ribbonRule() {return new RandomRule();//这里配置策略,和配置文件对应}
# to SpringCloudRibbonApplication.java ##The result of executing 6 times:

http://www.baidu.com:80http://www.baidu.com:80http://www.baidu.com:80http://www.163.org:80http://www.baidu.com:80http://www.jalja.org:80

3. BestAvailableRule (concurrency) public class BestAvailableRule extends ClientConfigEnabledRoundRobinRule Select a minimum concurrency Requested server Examine the servers one by one. If the server is tripped, ignore it and select the server with the smallest ActiveRequestsCount

Add ## to the configuration file application.yml

#NFLoadBalancerRuleClassName: com.netflix.loadbalancer.BestAvailableRule

Add

@Beanpublic IRule ribbonRule() {return new BestAvailableRule();//这里配置策略,和配置文件对应}
to SpringCloudRibbonApplication.java. Result of executing 6 times:

http://www.baidu.com:80http://www.baidu.com:80http://www.baidu.com:80http://www.baidu.com:80http://www.baidu.com:80http://www.baidu.com:80

4. AvailabilityFilteringRule (server status) public class AvailabilityFilteringRule extends PredicateBasedRule Filter out those backend servers that are marked as circuit tripped because of continuous connection failures, and filter out those high Concurrent backend server (active connections exceed the configured threshold) Use an AvailabilityPredicate to include the logic of filtering the server. In fact, it is to check the running status of each server recorded in the status

5. WeightedResponseTimeRule (based on response time) public class WeightedResponseTimeRule extends RoundRobinRule Allocate a weight based on the response time. The longer the response time, the smaller the weight, and the possibility of being selected. The lower the sex. A background thread periodically reads the evaluation response time from status and calculates a weight for each server. The calculation of Weight is also relatively simple. Responsetime minus each server's own average responsetime is the weight of the server. When the operation is just started and no status is formed, the roubine strategy is used to select the server.


6. RetryRule (according to policy + retry) public class RetryRule extends AbstractLoadBalancerRule The on-machine retry mechanism for the selected load balancing strategy. When the server selection fails within a configured time period, it will always try to use subRule to select an available server


7, ZoneAvoidanceRule (Zone status + service status) public class ZoneAvoidanceRule extends PredicateBasedRule Compositely determines the performance of the zone where the server is located and the availability of the server to select the server. Use ZoneAvoidancePredicate and AvailabilityPredicate to determine whether to select a server. The former determines whether the running performance of a zone is available, and eliminates all servers in the unavailable zone. ), AvailabilityPredicate is used to filter out servers with too many connections.

The strategies 4, 5, 6, and 7 are used in the same way as above and will not be demonstrated here

The above is the detailed content of spring cloud client load balancing Ribbon. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)