Spring Cloud-Ribbon (load balancing) related notes

Hello, I’m Chenxi. I’m glad you can read it. This article is a note about ribbon-related knowledge of springcloud. Then there is a case of producer and consumer. I share it with beginners. I hope it will be helpful to you. Share Get new knowledge and make progress together!


1. Introduction to Ribbon

Spring Cloud Ribbon is a client-side load balancing tool based on HTTP and TCP, which is implemented based on Netflix Ribbon.

Advantages of Spring Cloud Ribbon:
Load balancing and
fault-tolerant
multi-protocols (HTTP, TCP, UDP) support asynchronous and reactive model
caching and batch processing

Insert picture description here


The Spring Cloud encapsulation allows us to easily convert service-oriented REST template requests into client-side load balancing service calls.

Although Spring Cloud Ribbon is only a tool framework, it does not require independent deployment like the service registry, configuration center, and API gateway, but it exists in almost every microservice and infrastructure built by Spring Cloud.

Because the call between the microservices, the request forwarding of the API gateway, etc., are actually implemented through Ribbon, including Feign to learn, which is also a tool based on Ribbon.

Therefore, the understanding and use of Spring Cloud Ribbon is very important for us to use Spring Cloud to build microservices.


Client load balancing

Load balancing is one of the important means for the high availability of the system, the alleviation of network pressure and the expansion of processing capacity.

What we usually call load balancing refers to server-side load balancing, which is divided into hardware load balancing and software load balancing.

Hardware load balancing is mainly based on equipment dedicated to load balancing between server nodes, such as F5;

The software load balancing is to complete the request distribution work by installing some software for load balancing functions or modules on the server, such as Nginx.

How to construct the server-side load balancing graph architecture

Insert picture description here


Two, Ribbon case

1. Create a service provider project

Create SpringBoot project "spring-cloud-provider1" in the project folder and add the following dependency:

Spring web
Thymeleaf
Eureka Server

Maven adds the "spring-cloud-provider1" project pom file and synchronizes the dependent libraries
Insert picture description here
2. Register the service provider to the registry

Configure application.yml as follows (defalultZone is consistent with the registry)

server:
  port: 8762
spring:
  application:
    name: springcloud-provider1
eureka:
  client:
    service-url:
      defalultZone: http://localhost:8761/eureka/

Add @EnableEurekaClient to the startup class to declare that the current class is started as an Eureka service provider

/**
 * 在启动类添加@EnableEurekaClient声明当前类作为Eureka服务提供者启动
 */
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudProvider1Application {
    
    

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

}

3. Create a service (a service is actually an access interface) to
create a controller class, declared by @RestController annotation (default @ResponseBody responds)

@RestController
public class Provider1Controller {
    
    

    @RequestMapping("provider1/get")
    public String provideData(){
    
    
        return "Hello,SpringCloud!--provider1";
    }

}

Start the service provider project "spring-cloud-provider1"

After the startup is successful, refresh the Eureka registration center access page, the following figure indicates that the service provider has been registered
Insert picture description here


1. Create a service consumer project

Create the SpringBoot project "spring-cloud-customer1" in the project folder and add the following dependencies:

Spring web
Thymeleaf
Eureka Server
Ribbon

Insert picture description here

Maven adds the "spring-cloud-customer1" project pom file and synchronizes the dependent libraries

Insert picture description here


2. Configure service consumers

Configure application.yml as follows (defalultZone is consistent with the registry)

server:
  port: 8763
spring:
  application:
    name: springcloud-customer1
eureka:
  client:
    service-url:
      defalultZone: http://localhost:8761/eureka/

Add @EnableDiscoveryClient to the startup class to declare this application as a service discovery client (service consumer)

/**
 * 在启动类添加@EnableDiscoveryClient声明此应用为服务发现客户端(服务消费者)
 */
@EnableDiscoveryClient
@SpringBootApplication
public class SpringCloudCustomer1Application {
    
    

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

}

3. Consume services (request data from service providers)

Through the Java configuration class, instantiate the service consumer to access the tool object RestTemplate

The service provider only needs to start multiple service instances and register to one registry or multiple associated service registries.

The service consumer directly implements the service-oriented interface call by calling the RestTemplate modified by the @LoadBalanced annotation.

In this way, we can realize the high availability of service providers and the load balancing calls of service consumers together.

@Configuration
public class AppConfig {
    
    

    @LoadBalanced //负载均衡配置(启动Ribon框架)
    @Bean
    public RestTemplate getRestTemplate(){
    
    
        return new RestTemplate();
    }
    
}

Create the Service class, inject the RestTemplate object, and access the service

@Service
public class RibbonService {
    
    

    //注入消费者访问对象
    @Autowired
    private RestTemplate restTemplate;

    public String getMsg(){
    
    
        String str = restTemplate.getForObject("http://springcloud-provider1/provider1/get",String.class);
        return str;
    }
}

Create a controller to inject the service object

@Controller
public class CustomerController {
    
    

    @Autowired
    private RibbonService ribbonService;

    @RequestMapping("customer1/get")
    @ResponseBody
    public String custome(){
    
    
        String str = ribbonService.getMsg();
        return str;
    }

}

Start the consumer application and access the service consumer to obtain the data provided by the service provider

Insert picture description here
Here we find that the information of the producer can be read, which means that the consumption is successful!

See you in the next issue of springcloud-zuul


The best investment is to invest in yourself.

Insert picture description here

2020.10.20 23:25 PM, I hope you all go to be in your love!

Guess you like

Origin blog.csdn.net/weixin_45393094/article/details/109191982
Recommended