Spring-cloud-ribbon Load Balancing

Source: Internet
Author: User

Ribbon

The Ribbon is a load balancer based on HTTP and TCP clients. The feign also uses the Ribbon, which is followed by an introduction to the use of feign.

The ribbon can poll for access in a ribbonserverlist server-side list configured through the client to achieve a balanced load.

When the ribbon is used in conjunction with Eureka, Ribbonserverlist is discoveryenabledniwsserverlist rewritten to get a list of the server from the Eureka Registry. It also replaces iping with niwsdiscoveryping, which delegates responsibilities to Eureka to determine whether the server has been started.

Let's take a look at how the Ribbon is used to invoke the service and achieve a balanced load on the client.

Preparatory work
    • Start the service registry in Chapter-9-1-1: Eureka-server
    • Start the service provider in Chapter-9-1-1: Compute-service
    • Modify the Server-port in Compute-service to 2223, and then start a service provider: Compute-service

Visit Now: http://localhost:1111/

Alt

You can see that the Compute-service service has two units running:

    • 192.168.21.101:compute-service:2222
    • 192.168.21.101:compute-service:2223
Consumer with ribbon for client load Balancing

Build a basic spring boot project and include the following in Pom.xml:

123456789101112131415161718192021222324252627282930313233343536373839 <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.3.5.release</version> <relativepath/> <!--lookup parent from repository to</parent> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-ribbon</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>brixton.release</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement>

In the application main class, the @EnableDiscoveryClient Discovery service capability is added through annotations. Creates a Resttemplate instance and @LoadBalanced opens the load balancing capability with annotations.

12345678910111213141516  @s Pringbootapplication @EnableDiscoveryClient public class ribbonapplication {   @Bean @LoadBalanced resttemplate () { return new resttemplate (); }  public static void main (string[] args) { Springapplication.run (Ribbonapplication.class, args);}  }

Create ConsumerController COMPUTE-SERVICE An add service to consume. The service is invoked by direct resttemplate, and the value of 10 + 20 is computed.

123456789101112 @RestController public class Consumercontroller { @Autowiredresttemplate resttemplate; @RequestMapping (value = "/add", method = Requestmethod.get) Public string add(@RequestParam ("param") string a) {return Resttemplate.getforentity ("http://COMPUTE-SERVICE/add?a=" +a, String.class). GetBody (); }

Q will come to the parameter a, stitching on the link, load balanced to two compute-service, "Compute-service" is the registration node in Eureka

application.propertiesConfiguring the Eureka Service Registry in

12345 spring.application.name=ribbon-consumerserver.port=3333 eureka.client.serviceurl.defaultzone=http://localhost:1111/eureka/

Launch the app and visit two times: http://localhost:3333/add?a=111

Then, open the Compute-service's two service providers, output a log content similar to the following:

    • The port is 2222 the service-provider log:
1 2016-06-02 11:16:26.787 INFO 90014---[io-2222-exec-10] com.didispace.web.ComputeController:/add, host : 192.168.21.101, Service_id:compute-service, result:30
    • The port is 2223 the service-provider log:
1 2016-06-02 11:19:41.241 INFO 90122---[nio-2223-exec-1] com.didispace.web.ComputeController:/add, host : 192.168.21.101, Service_id:compute-service, result:30

As you can see, the two Compute-service service ports that were previously started were called once. Here we have already implemented a balanced load of service calls through the Ribbon at the client.

Spring-cloud-ribbon Load Balancing

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.