The most suitable Ribbon tutorial for beginners

Keywords: Programming Spring Maven Apache xml

What is Ribbon


Ribbon is a customer service load balancing tool based on HTTP and TCP, which is implemented based on Netflix Ribbon.

It is not deployed independently like Spring Cloud service registry, configuration center and API gateway, but it exists in almost every Spring Cloud microservice. Including the declarative service call provided by Feign is also based on the Ribbon implementation.

Ribbon provides many load balancing algorithms by default, such as polling, random, and so on. Even include custom load balancing algorithm.


What problems did the Ribbon solve


Ribbon provides a load balancing solution for microservices.


Difference of different load balancing schemes


At present, the mainstream load balancing schemes in the industry can be divided into two categories:

  • Centralized load balancing (server load balancing), that is, independent load balancing facilities (such as hardware, such as F5, or software, such as nginx) are used between the consumer and the provider, which is responsible for forwarding access requests to the provider through a certain strategy;
  • In process load balancing (client load balancing) integrates the load balancing logic into the consumer. The consumer obtains the available addresses from the service registry, and then selects an appropriate provider from these addresses. The Ribbon belongs to the latter. It is just a class library, integrated into the consumer process, through which the consumer obtains the provider address.


Centralized load balancing



In process load balancing



Ribbon load balancing strategy


Polling policy (default)


Policy corresponding class name: roundrobin rule

Implementation principle: polling strategy means that one provider is taken down in sequence every time, for example, there are five providers in total, the first provider is taken for the first time, the second provider is taken for the second time, the third provider is taken for the third time, and so on.


Weight polling strategy


Policy corresponding class name: WeightedResponseTimeRule

Implementation principle:

  • A weight is assigned according to the response time of each provider. The longer the response time is, the smaller the weight is, and the less likely it is to be selected.
  • Principle: start with polling strategy and start a timer to collect the average response time of each provider every 30 seconds. When the information is enough, attach a weight to each provider, and select the provider randomly according to the weight. The provider with higher weight will be selected with high probability.


Stochastic strategy


Policy corresponding class name: RandomRule

Implementation principle: select one randomly from the provider list.


Minimum concurrent number policy


Policy corresponding class name: BestAvailableRule

Implementation principle: select the provider with the minimum concurrent number in the request, unless the provider is in fusing.


Retry strategy


Policy corresponding class name: RetryRule

Implementation principle: in fact, it is the enhanced version of polling policy. When the polling policy service is unavailable, it will not be processed. When the retry policy service is unavailable, it will try other nodes in the cluster again.


Availability sensitive policy


Policy corresponding class name: availabilityfiltering rule

Implementation principle: provider with poor filtering performance

  • The first is to filter out provider s in Eureka that have failed to connect all the time.
  • Second, filter out the provider s with high concurrency (busy).


Regional sensitivity strategy


Policy corresponding class name: zoneaavoidancerule

Implementation principle:

  • The availability is evaluated in one area. For the whole area that is not available, select the available provider from the remaining areas.
  • If one or more instances in this ip area are not reachable or the response is slow, the right to select other IPS in this ip area will be reduced Heavy.


Introduction to Ribbon


The default load balancing policy for clustered services in Ribbon is polling.


Create project


Use the Eureka demo project when learning Eureka to create the subproject service-provider02 in this project


Add dependency



<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="" xmlns:xsi=""


    <!-- Inherit parent dependency -->

    <!-- Project dependence -->
        <!-- netflix eureka client rely on -->
        <!-- spring boot web rely on -->
        <!-- lombok rely on -->
        <!-- spring boot actuator rely on -->

        <!-- spring boot test rely on -->


configuration file



  port: 7071 # port

    name: service-provider # Application name (same under cluster)

# Configure Eureka Server registry
    prefer-ip-address: true       # Use ip address to register
    instance-id: ${}:${server.port} # ip:port
    service-url:                  # Set service registry address
      defaultZone: http://root:123456@localhost:8761/eureka/,http://root:123456@localhost:8762/eureka/

# Measurement monitoring and health examination
        include: shutdown         # Turn on shutdown endpoint access
      enabled: true               # Turn on shutdown to realize elegant service stop


Service and startup


Copy and paste a copy of all the codes to server-provider02, and modify the name of the startup class.

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class ServiceProvider02Application {

    public static void main(String[] args) {, args);


In order to see the effect of load balancing more intuitively, we print the service address to the console in the service consumer project.


Registry Center


Run the complete Eureka environment, visit: http://localhost:8761/ You can see that there are already two service providers.


Consumer service


Multiple visits: http://localhost:9090/order/1 You can see that the polling policy is used by default.


Ribbon load balancing policy settings


Overall situation


Inject the load balancing policy object into the startup class or configuration class. This policy is used by all service requests.

public RandomRule randomRule() {
    return new RandomRule();

Multiple visits: http://localhost:9090/order/1 The results are as follows:




Modify the load balancing policy of the specified service in the configuration file. Format: service application name.ribbon.NFLoadBalancerRuleClassName

# Load balancing strategy
# Service provider is the name of the called service

Multiple visits: http://localhost:9090/order/1 The results are as follows:


Ribbon point to point direct connection


Point to point direct connection refers to bypassing the registry and directly connecting service providers to obtain services, which is commonly used in the test phase.


Add dependency


When introducing Ribbon into pom files, it should be noted that if there is Eureka dependency in pom, it is necessary to remove Eureka dependency.

<!-- netflix ribbon rely on -->


configuration file


Close Eureka in the configuration file and add the directly connected service address. If load balancing policy is not set, polling policy is used by default.

# Load balancing strategy
# Service provider is the name of the called service
    # Specify specific Provider service list, multiple separated by commas
    listOfServers: http://localhost:7070,http://localhost:7071

# Close Eureka to realize the point-to-point direct connection of Ribbon
    enabled: false # false: off, true: on




Close the Eureka registry. The service provider will report a connection exception because it cannot connect to the registry. But the service can be consumed normally, so the current use of point-to-point method to call.

Multiple visits: http://localhost:9090/order/1 The results are as follows:

At this point, all knowledge points of Ribbon load balancing are finished.


You can go through classification See more about Spring Cloud Article.

This paper adopts Intellectual sharing "signature - non-commercial use - no deduction 4.0 international" License Agreement.


This chapter is over here. If you like it, please like it and forward it.

   A kind of Pay attention to Mr. halloward and learn more IT technology~

Posted by washbucket on Sun, 08 Mar 2020 22:52:43 -0700