Spring cloud Client Server Load balancer Ribbon, cloudribbon

Source: Internet
Author: User

Spring cloud Client Server Load balancer Ribbon, cloudribbon
I. Server Load balancer

Load Balancing: built on the existing network structure, 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. It means to allocate multiple operation units for execution, such as Web servers, FTP servers, enterprise key application servers, and other key task servers, so as to jointly complete tasks.

1. Server Load balancer: the client requests to the server Load balancer server. The Server Load balancer server forwards the request to a server that actually provides services based on its own algorithms, the server returns the response data to the Server Load balancer instance. (Nginx)

2. Customer Server Load balancer: Server Load balancer Based on the client. Simply put, a scheduling algorithm is set in the client program to initiate a request to the server, execute the scheduling algorithm to calculate the server to which the request is sent, and then send the request to the server.

Client-Based Load Balancing features:

  • Implemented by the internal program of the client, without the need for additional hardware and software investment of the Server Load balancer.
  • The program must solve the problem of Service Server unavailability, And the Server failure has little transparency to the application.
  • The program must solve the problem of overload of business servers.
Ii. Server Load balancer of the client using Ribbon

We use spring boot for testing.

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <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 policy

1. RoundRobinRule (polling mode) public class RoundRobinRule extends AbstractLoadBalancerRule roundRobin round robin server round-robin index selection. server where index is selected this policy is also the default ribbon Policy

SpringCloudRibbonApplication.java
@ SpringBootApplication @ EnableDiscoveryClient @ RestControllerpublic class SpringCloudRibbonApplication {public static void main (String [] args) {SpringApplication. run (SpringCloudRibbonApplication. class, args) ;}@ Autowired private LoadBalancerClient loadBalancer; @ RequestMapping (value = "static") public String staticRibbon () {ServiceInstance instance = loadBalancer. choose ("stores"); URI storesUri = URI. create (String. format ("http: // % s: % s", instance. getHost (), instance. getPort (); System. out. println (storesUri); return "static ";}}

Results of 6 consecutive requests:

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

2. RandomRule (random Policy) public class RandomRule extends AbstractLoadBalancerRule randomly selects a server randomly on the index, and selects the server at the corresponding location of the index.

Add in the configuration file application. yml

NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
Stores: ribbon: listOfServers: www.baidu.com, www.jalja.org, www.163.org # random NFLoadBalancerRuleClassName: com. netflix. loadbalancer. RandomRule

Add in SpringCloudRibbonApplication. java

@ Bean public IRule ribbonRule () {return new RandomRule (); // The Configuration Policy corresponds to the configuration file}

Result of 6 executions:

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 the server with the smallest number of concurrent requests to check the Server one by one. If the Server is tripped, ignore the error.

Add in the configuration file application. yml

NFLoadBalancerRuleClassName: com. netflix. loadbalancer. BestAvailableRule

Add in SpringCloudRibbonApplication. java

@ Bean public IRule ribbonRule () {return new BestAvailableRule (); // The Configuration Policy corresponds to the configuration file}

Result of 6 executions:

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 filters out backend servers marked as circuit tripped because of connection failure, and filter out those high-concurrency backend servers (active connections exceeds the configured threshold) to use an AvailabilityPredicate to include the logic for filtering the server. In fact, it is to check the running status of each server recorded in the status.

 

5. WeightedResponseTimeRule (based on the response time) public class WeightedResponseTimeRule extends RoundRobinRule allocates a weight according to the response time. The longer the corresponding time, the smaller the weight, the lower the probability of being selected. A background thread regularly reads the evaluation response time from the status and calculates a weight for each server. Weight calculation is also relatively simple. responsetime minus the average responsetime of each server is the Weight of the server. When statas is not formed at the beginning of the operation, select the server using the roubine policy.


6. RetryRule (based on policy + retry) public class RetryRule extends implements actloadbalancerrule to retry the mechanism on the selected Server Load balancer policy. If the server fails to be selected during a configuration period, you will always try to use subRule to select an available server.


7. ZoneAvoidanceRule (Zone status + service status) public class ZoneAvoidanceRule extends PredicateBasedRule composite judgment server region performance and server availability selection server use region and AvailabilityPredicate to determine whether to select a server, the previous one checks whether the Running Performance of a zone is available, removes all the servers that are unavailable, and AvailabilityPredicate filters out servers with too many connections.

 

The usage of these policies is the same as that described above.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.