One, load balancing
load Balance: built on the existing network structure, it provides an inexpensive and effective way to extend the bandwidth of network devices and servers, increase throughput, enhance network data processing capabilities, and improve network flexibility and availability. This means that it can be shared across multiple operating units, such as Web servers, FTP servers, enterprise critical application servers, and other mission-critical servers, to work together to accomplish tasks.
1, Server load Balancing : client requests to the load Balancer servers, the Load Balancer server according to its own algorithm transfer the request to a real service server, the server will respond to the Load Balancer server, the Load Balancer server finally return the data to the customer service side. (Nginx)
2, customer service load Balancing : Based on client-side load balancing, simply said in the client program, set up a scheduling algorithm, in the request to the server, the first execution of the scheduling algorithm to calculate which server to initiate the request, and then initiate the request to the server.
Based on the characteristics of client load balancing:
- Implemented by the client internal program, no additional load balancer hardware and software inputs are required.
- Internal procedures need to address the issue of unavailability of the business Server, and server failures are less transparent to the application.
- Internal procedures need to solve the problem of pressure overload of the business Server.
Second, the Ribbon to achieve client-side load Balancing
We use spring boot to test.
Pom file:
<Projectxmlns= "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
Load Balancing strategy for Ribbon
1. Roundrobinrule (polling mode) Public class Roundrobinrule extends Abstractloadbalancerrule roundrobin Polling Select Server Poll index, Select server for index location This policy is also the default policy for the Ribbon
Springcloudribbonapplication.java
@SpringBootApplication @enablediscoveryclient@restcontroller Public classspringcloudribbonapplication { Public Static voidMain (string[] args) {Springapplication.run (springcloudribbonapplication.class, args); } @AutowiredPrivateloadbalancerclient LoadBalancer; @RequestMapping (Value= "Static") PublicString 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"; }}
Continuous request 6 Execution results:
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 (Stochastic strategy) The public class Randomrule extends Abstractloadbalancerrule randomly selects a server at index random, and selects the server with the index corresponding to the location.
In configuration file application.yml join
NFLoadBalancerRuleClassName:com.netflix.loadbalancer.RandomRule
Stores: Ribbon: listofservers:www.baidu.com,www.jalja.org,www. 163. org #随机 NFLoadBalancerRuleClassName:com.netflix.loadbalancer.RandomRule
Join in the Springcloudribbonapplication.java
@Bean public IRule ribbonrule () { returnnew Randomrule (); // Here the configuration policy, and the configuration file corresponding }
6 results were performed:
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 (concurrent volume) Public class Bestavailablerule extends Clientconfigenabledroundrobinrule Select a minimum concurrent request for the server Examine the server individually, if the server is tripped, ignore it, and select the server activerequestscount the smallest
In configuration file application.yml join
NFLoadBalancerRuleClassName:com.netflix.loadbalancer.BestAvailableRule
Join in the Springcloudribbonapplication.java
@Bean Public IRule Ribbonrule () { returnnew bestavailablerule (); Here the configuration policy, and the configuration file corresponding }
6 results were performed:
http:// www.baidu.com:80 Http:// www.baidu.com:80 http: // www.baidu.com:80 http:// www.baidu.com:80 http:// www.baidu.com:80 http:// www.baidu.com :
4. Availabilityfilteringrule (server status) Public class Availabilityfilteringrule extends Predicatebasedrule filters out back-end servers that are flagged as circuit tripped because they have failed to connect , and filter out those highly concurrent backend servers (active connections over configured thresholds) using a availabilitypredicate to contain the logic for filtering the server, It's just checking the status of each server that is logged in the status.
5, Weightedresponsetimerule (according to response time) public class Weightedresponsetimerule extends Roundrobinrule According to the response time allocation of a weight, the longer the corresponding time, the smaller the weight, the less likely to be selected. A background thread periodically reads the evaluation response time from the status and computes 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. Use Roubine policy to select server when the Statas is not formed when it is first run.
6, Retryrule (according to policy + retry)public class Retryrule extends Abstractloadbalancerrule on the selected load Balancing policy machine retry mechanism. During a configuration time period when the server selection is unsuccessful, try to select an available server using the Subrule method
7, Zoneavoidancerule (Zone status + Service status)public class Zoneavoidancerule extends Predicatebasedrule compound to determine the performance of the server area and the availability of the server Select Server uses Zoneavoidancepredicate and availabilitypredicate to determine whether to select a server , the previous decision determines whether a zone's performance is available, rejects the unavailable zone (all servers), and availabilitypredicate is used to filter out servers with too many connections.
4, 5, 6, 7 These strategies are used in the same way as above. Not in the demo
Spring Cloud Client Load Balancer Ribbon