Ribbon of Spring Cloud: load balancing

The previous one Service registration and discovery , friends who haven't read it first read the previous one, which is based on the previous one.

Ribbon introduction

Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.

<!-- more -->

Ribbon is a load balancing client, which can well control the behavior of HTTP and TCP clients. Feign has integrated the ribbon.

Spring Cloud Netflix provides the following beans for ribbon (BeanType beanName: ClassName) by default:

  • IClientConfig ribbonClientConfig: DefaultClientConfigImpl

  • IRule ribbonRule: ZoneAvoidanceRule

  • IPing ribbonPing: NoOpPing

  • ServerList<Server> ribbonServerList: ConfigurationBasedServerList

  • ServerListFilter<Server> ribbonServerListFilter: ZonePreferenceServerListFilter

  • ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer


Based on the previous article, this article starts the Eureka server project, starts the Eureka client project, with the port of 8040, changes the profile port of Eureka client to 8041, and starts it. In the same project, modify the port number, and start multiple instances. Just check Allow parallel run in the IDEA:

At this point, you will find that two Eureka client instances are registered in the registration service, which is equivalent to a cluster of two nodes. Visit http://localhost:9090:

Create a new service consumer

Use Spring Initializr to create a new project, named Ribbon service. Check Eureka Discovery Client in Spring Cloud Discovery, Ribbon in Spring Cloud Routing, and Spring Web in the Web:

After creation, the project pom.xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <relativePath/> <!-- lookup parent from repository -->
    <description>Ribbon Service</description>







The ribbon service configures the service center address, application name and port. The content of the configuration file is as follows:

  port: 8050

    name: ribbon-service

      defaultZone: http://localhost:9090/eureka/

Annotate @ EnableDiscoveryClient on the project startup class, enable registration with the service center, define a RestTemplate Bean, and annotate @ LoadBalanced to enable the load balancing function:

public class RibbonServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(RibbonServiceApplication.class, args);

    public RestTemplate restTemplate() {
        return new RestTemplate();


Write a controller to consume the / hello interface of Eureka client service through the RestTemplate defined previously. The application name is used in the url. ribbon will select a specific service instance according to the application name, and replace the service name with a specific url when requesting according to the service instance:

public class HelloController {

    private final RestTemplate restTemplate;

    public HelloController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;

    public String hello(@RequestParam("name") String name) {
        return restTemplate.getForObject("http://HELLO-ERUEKA-CLIENT/hello?name=" + name, String.class);

Multiple access on Browser http://localhost:8050/hello?name=Mars :

Hello, My name is Mars, I'm from port: 8040
Hello, My name is Mars, I'm from port: 8041

This means that the load balancer is working.

Pay attention to the public address: JAVA half past nine class, technical articles daily service!

Personal blog: http://blog.hxpgxt.cn

Click to enter the source warehouse

Keywords: Programming Spring Maven Apache Java

Added by sirfartalot on Tue, 29 Oct 2019 08:52:53 +0200