Springcloud (2) client-side load Balancing Ribbon

Source: Internet
Author: User

Unlike Nginx, the Ribbon is the client load balancer, and Nginx is the server load balancer

Create a demo project for the Ribbon

First join the Ribbon dependency

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>

Modifying a configuration file application.properties
server.port=9000
Spring.application.name=ribbon-consumer
#自定义属性
# ribbon.listofservers is fixed, stores is written by himself
stores.ribbon.listofservers=http://localhost:8891,http://localhost:8892

Then customize a controller, the return value is the corresponding service
/**
* Interface for Request forwarding
* @autor Guojs
* @date 2018/7/31 17:07
*/
@RestController
public class Consumercontroller {

@Resource
Private Loadbalancerclient balancerclient;

@RequestMapping ("/consumer")
Public String Helloconsumer () {
Read the corresponding configuration
serviceinstance instance = Balancerclient.choose ("Stores");
This URI is after the load-balancing algorithm
Uri uri = uri.create (String.Format ("http://%s:%s", Instance.gethost (), Instance.getport ()));
return uri.tostring ();
}

}
Then start the previous Eureka cluster and two service providers, and then start this ribbon access Http://localhost:9000/consumer
You will find that the default is to use a polling load-balancing algorithm, one http://localhost:8891 at a time http://localhost:8892 polling.

To modify the load balancing algorithm:
1. Modify the configuration file
Stores.ribbon.nfloadbalancerruleclassname=com.netflix.loadbalancer
2. Write a bean in the main function to overwrite the default algorithm
@Bean
Public IRule Ribbonrule () {
return new Randomrule ();
}
The Ribbon after improvement
Introducing Eureka Dependencies
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
Simplifies configuration files and registers the Ribbon in Eureak
server.port=9000
Spring.application.name=ribbon-consumer
eureka.client.service-url.defaultzone=http://localhost:8877/eureka/,http://localhost:8898/eureka/


@RibbonClient (name = "Hello-server", configuration = Com.netflix.loadbalancer.RandomRule.class) (the first parameter is the service name, The second parameter is the load balancing algorithm, which is used to customize the load Balancing algorithm)
Declares a bean that is used to invoke the service
@Bean
@LoadBalanced
Public Resttemplate resttemplate () {
return new Resttemplate ();
}
Next look at the controller's modification, the original direct call to the service URL to invoke the service name, using the resttemplate just declared to invoke
@Resource
Private Resttemplate resttemplate;

@RequestMapping ("/consumer")
Public String Helloconsumer () {
Return resttemplate.getforentity ("Http://HELLO-SERVER/hello", String.class). GetBody ();
}
This modification solves the problem of hard-writing the service address in the configuration file, but there is no single point of failure, but in practice the Ribbon is not used in this way, and each Eureka client individually declares a ribbon (that is, a single client-unique ribbon).

The load balancing algorithm that comes with it:

Bestavailablerule Select a minimal concurrent request for the service to detect each service, if the service is hung, then ignore, select the service where the least requested

Availabilityfilteringrule
Weightedresponsetimerule  weighted polling? Assign a weight according to the corresponding time, the longer the corresponding time, the smaller the weight, the lower the likelihood of being selected.

Retryrule

Roundrobinrule

Randomrule Stochastic algorithm

Zoneavoidancerule

Springcloud (2) client-side load Balancing Ribbon

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.