Home > Java > Java Tutorial > body text

spring cloud client load balancing Ribbon

巴扎黑
Release: 2017-06-26 09:31:34
Original
994 people have browsed it

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:


  4.0.0
  com.jalja.org
  spring-consumer-server-ribbon
  0.0.1-SNAPSHOT
  
   org.springframework.bootspring-boot-starter-parent1.5.2.RELEASEUTF-8UTF-81.8org.springframework.cloudspring-cloud-dependenciesCamden.SR4pomimport org.springframework.cloudspring-cloud-starter-ribbonorg.springframework.bootspring-boot-starter-web
Copy after login

application.yml

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

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
Copy after login
   ="static"= loadBalancer.choose("stores"= URI.create(String.format("http://%s:%s" "static"
Copy after login

: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
Copy after login
stores:
  ribbon:
    listOfServers: www.baidu.com,www.jalja.org,www.163.org
    #随机
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
Copy after login

Add

    @Beanpublic IRule ribbonRule() {return new RandomRule();//这里配置策略,和配置文件对应}
Copy after login
# 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
Copy after login

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();//这里配置策略,和配置文件对应}
Copy after login
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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!