Getting Started with Ribbon Load Balancing in SpringCloud

Keywords: Java Spring Dubbo Nginx Load Balance

Load balancing using Ribbon

Before using Ribbon, let's think about a previous question. We registered our service provider with the eureka registry, but on the consumer side, when we still used the restTemplate call, did it write something inappropriate like http://localhost:8001?Does the application use the service name to make calls like dubbo?Otherwise, what is the use of the registry?

Okay, let's keep this thinking going.To enter Ribbon's studies

What is Ribbon?

Ribbon [rbn], is a load balancing plug-in for clients in SpringCloud Netflix.

The main explanations are as follows:

  • This client mainly refers to the service consumer, that is, this plug-in is used on the consumer side, it will identify the same service providers (that is, the application.name of these service providers is the same) according to some algorithms, and decide which one I want to visit.
  • Nginx is a load-balanced server. When browsers and other devices request access, it will choose the path of the service according to its configuration.

Ribbon integration (client, consumer)

As mentioned above, it's load balancing on the consumer side, so I'm going to modify the cousumer side, but for ease of learning, I've created a new project, demo3-ribbon-consumer, with code that hasn't changed much.Also, do you need multiple service providers with the same name to load balance and select before multiple service providers?It's like you're a queen, but there's only one queen in the palace. Is there any other choice?Ha-ha, this car is a little fast!

Similarly, instead of calling with localhost, we'll use the service name.Is it also necessary to integrate the eureka registry?Only then can I find it in the registry?

  • As mentioned earlier, the first of the two steps to using SpringCloud components is to introduce dependencies (note that this is for Consumer)
<!--ribbon-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>

<!--The bottom two are ribbon Dependent-->
<!--eureka Client-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

<!--SpringCloud Configuration-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
  • Add a corresponding comment on the startup class
@EnableEurekaClient //Client to eureka
  • Modify application.yml
server:
  port: 80

#  The bottom plus eureka is configured to discover service providers in the eureka registry
eureka:
  client:
    register-with-eureka: false
    service-url:
#    Using the eureka cluster, previously configured
      defaultZone: http://eureka1:7001/eureka/,http://eureka2:7002/eureka/,http://eureka3:7003/eureka/
  • Turn on load balancing on the configuration class for bean s that configure RestTemplate
    @Bean
    @LoadBalanced //Turn on load balancing
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
  • Contoller class, temporarily modified as follows:
    @GetMapping("demo3/consumer/hello/{id}")
    public String hello(@PathVariable("id") String id){
        //Remotely invoke the interface in the provider, this time using the provider's name
        return restTemplate.getForObject("http://demo3-ribbon-provider/demo3/provider/hello/"+id,String.class);
    }

Create multiple service providers with the same name

As mentioned above, this must be there.You can only make a choice when you have a choice.This step is too simple. Just write one of the codes and copy the rest yourself (module name is demo3-ribbon-provider-800x).

  • pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
  • application.yml
server:
#Each service provider must have a different port number
  port: 8001

spring:
  application:
#  The name of the application used to register the name in the registry
    name: demo3-ribbon-provider

eureka:
  client: #Clients are registered in the eureka service list
    service-url:
#       Registered address of eureka cluster
      defaultZone: http://eureka1:7001/eureka,http://eureka2:7002/eureka,http://eureka3:7003/eureka
  instance:
    instance-id: demo3-ribbon-provider-8001
    prefer-ip-address: true     #Access paths can display IP addresses
  • Controller class
@RestController
public class HelloController {

    @GetMapping("demo3/provider/hello/{id}")
    public String hello(@PathVariable("id") Integer id) {
        return "This is a message from the provider,Your message" + id+"\n\t Service Provider 8001 for this visit";
    }
}
  • Main startup class with corresponding comment
@SpringBootApplication
@EnableEurekaClient //This service will be automatically registered with the eureka service after it is started
@EnableDiscoveryClient//This indicates that the service can be discovered, that is, consumers can call it
public class Demo3RibbonProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(Demo3RibbonProvider_8001.class, args);
    }
}

Finally, live together.Testing

Start three eureka clusters, three service providers with the same name, and one consumer.There are seven services, seven processes. No, my computer is already demonstrating to me to see the memory of my computer.My memory is 8G and Idea already accounts for more than 3G.

  • Picture of Eureka Cluster

  • Result of three consecutive visits to http://localhost/demo3/consumer/hello/99

From the above results, it can be seen that the results of three consecutive visits are different, which is the load balancing algorithm started by ribbon.Why is this so?

Ribbon's built-in load balancing algorithm

Ribbon uses the polling algorithm by default, which polls the same service provider one by one so that each service provider is accessed once.Of course, Ribbon's load balancing algorithm also supports customization, so let's talk about it next time!

For more information on SpringCloud and project code, please follow the WeChat Public Number "Fish and Java" for

Posted by xterra on Wed, 26 Feb 2020 10:46:58 -0800