Spring Cloud (6): Ribbon load balancing

1. Background

In the previous study, we have used RestTemplate to implement the service consumer's call to the service provider. If in a specific business scenario, the number of calls to a service suddenly increases significantly, this time we need to call the service Implement load balancing to meet high concurrent access conditions. In large-scale distributed projects, load balancing is necessary, so Ribbon can be used to achieve it.

2. What is Ribbon

Ribbon is a component of Sping Cloud. Spring Cloud Ribbon is a load balancing solution. Ribbon is a load balancer released by Netflix. Spring integrates it. Spring Cloud Ribbon is implemented based on Netflix Ribbon. A load balancing client that controls HTTP requests. Spring Cloud Ribbon is also used in conjunction with Eureka Server, because it is also registered in the registry. After Ribbon is registered in the registration center, Ribbon can automatically help service consumers call interfaces based on a certain load balancing algorithm, such as round robin, random, weighted round robin, weighted random, etc. Developers can also customize Ribbon according to specific needs load balancing algorithm. In actual development, Spring Cloud Eureka is used. Eureka Server provides a list of all service providers that can be called. Ribbon selects specific instances to call from these service providers based on a specific load balancing algorithm. An example image is as follows:
insert image description here

3. Actual combat, quickly build Ribbon instance

  1. Create Module, add Eureka client dependency in pom.xml, and register it to the registration center. Add the code as follows:
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
	<version>2.0.2.RELEASE</version>
</dependency>
  1. Create a configuration file application.yml, the configuration information is as follows:
server:
  port: 8040
spring:
  application:
    name: ribbon
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    prefer-ip-address: true
属性说明
  • spring.application.name : The name of the current service registered on Eureka Server.

  • eureka.client.service-url.defaultZone : The access address of the registry.

  • eureka.instance.prefer-ip-address : Whether to register the IP of the current service with Eureka Server.

  1. Create a startup class with the following code:
package com.zing;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class RibbonApplication {
    
    
	public static void main(String[] args) throws Exception {
    
    
		SpringApplication.run(RibbonApplication.class, args);
	}
	
	@Bean
	@LoadBalanced
	public RestTemplate restTemplate() {
    
    
		return new RestTemplate();
	}

}
注解说明:
  • @Bean: Inject the instance into the IOC container through the @Bean annotation. In fact, @SpringBootApplication is equivalent to using the default properties of @Configuration, @EnableAutoConfiguration and @ComponentScan, while @Bean is a method-level annotation, which is mainly used in classes annotated by @Configuration. @Configuration is equivalent to the beans tag in the xml file. @Bean is equivalent to a bean tag.

  • @LoadBalanced: Declare a Ribbon-based load balancing. After marking this annotation, RestTemplate has the ability of client load balancing.

4. The controller layer code is implemented as follows:

package com.zing.controller;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.zing.entity.Student;

@RestController
@RequestMapping("/ribbon")
public class RibbonHandler {
    
    
	
	@Autowired
	private RestTemplate restTemplate;
	
	@GetMapping("/findAll")
	public Collection<Student> findAll(){
    
    
		return restTemplate.getForObject("http://provider/student/findAll", Collection.class);
	}
	
	@GetMapping("/index")
	public String index() {
    
    
		return restTemplate.getForObject("http://provider/student/index", String.class);
	}
	
}
  1. Test interface
    (1) Start the registration center, and then start two provider instances respectively. Open the registration center and you can see the following information:
    insert image description here
    (2) Start Ribbon, and you can see an instance of RIBBON in the registration center, as follows:
    insert image description here
    (3) Test the index interface, and you can see that the ports appear alternately, and the results are as follows:
    insert image description here
    insert image description here
    4. Summary
    of this time The code uses the method of Ribbon+RestTemplate to realize the load balancing of service calls. The client calls the service provider by polling by default, and the two provider instances respond alternately. But this development method can be simplified. There is a more convenient way in actual development, which can also realize such a function. Please look forward to my next article Spring Cloud (7): Feign declarative interface call .

One development engineer is also in the continuous learning stage, and the usual small experiences are shared from time to time. I hope that those who read the text I wrote can avoid detours and wish you success in work and study.
Bloggers have limited experience, if there are any shortcomings, welcome to communicate and improve together~ I hope to make progress together with you who are also in CSDN.

Author | Sweet Little Sweet Potato
Produced | Little Sweet Potato

Guess you like

Origin blog.csdn.net/qq_36836370/article/details/130871582
Recommended