spring cloud client load balancing Ribbon
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 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 "> <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();//这里配置策略,和配置文件对应}
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();//这里配置策略,和配置文件对应}
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics


![VMware Horizon Client cannot be opened [Fix]](https://img.php.cn/upload/article/000/887/227/170835607042441.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
VMware Horizon Client helps you access virtual desktops conveniently. However, sometimes the virtual desktop infrastructure may experience startup issues. This article discusses the solutions you can take when the VMware Horizon client fails to start successfully. Why won't my VMware Horizon client open? When configuring VDI, if the VMWareHorizon client is not open, an error may occur. Please confirm that your IT administrator has provided the correct URL and credentials. If everything is fine, follow the solutions mentioned in this guide to resolve the issue. Fix VMWareHorizon Client Not Opening If VMW is not opening on your Windows computer
![VMware Horizon client freezes or stalls while connecting [Fix]](https://img.php.cn/upload/article/000/887/227/170942987315391.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
When connecting to a VDI using the VMWareHorizon client, we may encounter situations where the application freezes during authentication or the connection blocks. This article will explore this issue and provide ways to resolve this situation. When the VMWareHorizon client experiences freezing or connection issues, there are a few things you can do to resolve the issue. Fix VMWareHorizon client freezes or gets stuck while connecting If VMWareHorizon client freezes or fails to connect on Windows 11/10, do the below mentioned solutions: Check network connection Restart Horizon client Check Horizon server status Clear client cache Fix Ho

In 2023, AI technology has become a hot topic and has a huge impact on various industries, especially in the programming field. People are increasingly aware of the importance of AI technology, and the Spring community is no exception. With the continuous advancement of GenAI (General Artificial Intelligence) technology, it has become crucial and urgent to simplify the creation of applications with AI functions. Against this background, "SpringAI" emerged, aiming to simplify the process of developing AI functional applications, making it simple and intuitive and avoiding unnecessary complexity. Through "SpringAI", developers can more easily build applications with AI functions, making them easier to use and operate.

As an industry leader, Spring+AI provides leading solutions for various industries through its powerful, flexible API and advanced functions. In this topic, we will delve into the application examples of Spring+AI in various fields. Each case will show how Spring+AI meets specific needs, achieves goals, and extends these LESSONSLEARNED to a wider range of applications. I hope this topic can inspire you to understand and utilize the infinite possibilities of Spring+AI more deeply. The Spring framework has a history of more than 20 years in the field of software development, and it has been 10 years since the Spring Boot 1.0 version was released. Now, no one can dispute that Spring

How to implement spring programmatic transactions: 1. Use TransactionTemplate; 2. Use TransactionCallback and TransactionCallbackWithoutResult; 3. Use Transactional annotations; 4. Use TransactionTemplate in combination with @Transactional; 5. Customize the transaction manager.

Keeping web servers load balanced is one of the key measures to prevent downtime. Using a load balancer is a reliable approach, with HAProxy being a highly regarded choice. Using HAProxy, you can accurately configure the load balancing method and support SSL passthrough to ensure the security of communication between the client and the server. It starts by exploring the importance of implementing SSL passthrough in HAProxy, followed by a detailed discussion of the steps required to implement this feature and an example for better understanding. What is SSL passthrough? Why is it important? As a load balancer, HAProxy accepts and distributes the load flowing to your web servers across configured servers. Load distribution is targeted to client devices and

MQTT (MessageQueuingTelemetryTransport) is a lightweight message transmission protocol commonly used for communication between IoT devices. PHP is a commonly used server-side programming language that can be used to develop MQTT clients. This article will introduce how to use PHP to develop an MQTT client and include the following content: Basic concepts of the MQTT protocol Selection and usage examples of the PHPMQTT client library: Using the PHPMQTT client to publish and

When many friends download files, they will first browse on the web page and then transfer to the client to download. But sometimes users will encounter the problem that the Baidu Netdisk webpage cannot start the client. In response to this problem, the editor has prepared a solution for you to solve the problem that the Baidu Netdisk webpage cannot start the client. Friends in need can refer to it. Solution: 1. Maybe Baidu Netdisk is not the latest version. Manually open the Baidu Netdisk client, click the settings button in the upper right corner, and then click version upgrade. If there is no update, the following prompt will appear. If there is an update, please follow the prompts to update. 2. The detection service program of Baidu Cloud Disk may be disabled. It is possible that we manually or use security software to automatically disable the detection service program of Baidu Cloud Disk. Please check it out
