Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

SpringCloud uses Ribbon for load balancing

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "SpringCloud uses Ribbon for load balancing". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "SpringCloud uses Ribbon for load balancing".

Ribbon load balancing 1. Introduction 1: what is load balancing

Load balancing is based on the existing network structure, which provides a cheap, effective and transparent way to expand the bandwidth of network devices and servers, increase throughput, enhance network data processing capacity, and improve network flexibility and availability.

Load balancing (Load Balance) means to distribute the execution among multiple operation units, such as Web server, FTP server, enterprise critical application server and other critical task servers, so as to complete the work task together. The above content comes from Baidu encyclopedia.

As far as personal understanding is concerned, load balancing makes up for the overall performance bottleneck caused by the lack of load capacity of a single architecture or a single service. Therefore, it is possible to deploy multiple servers for one service, and then distribute the original pressure of one machine to multiple execution units, thus improving the performance bottleneck of the original architecture. It can be understood as "load balancing among multiple services". There are two common strategies for load balancing:

Nginx independent process does load balancing, and forwards requests to different execution units through load balancing strategy.

Client load balancing strategy, by saving the service list information on the client, and then invoking the load balancing strategy to apportion and call different execution units.

2: what is Ribbon

Ribbon is an open source load balancing component of Netflix, which belongs to the second of the above load balancing methods. The logic of load balancing is encapsulated in the client and run on the client. After a very rigorous test, Ribbon can better control the load balancing behavior of Http and Tcp clients.

There are two ways to load balance Ribbon.

Combine with RestTemplate

Combine with Feign

Core sub-module of Ribbon

Ribbon-loadbalancer: load balancing API that can be used independently or with other modules

Ribbon-eureka: API combined with Eureka as the client

Core API of ribbon-core:Ribbon

3: what is RestTemplate

RestTemplate is a network communication framework in SpringResource that accesses the third-party RESTful API interface. Its main methods are closely related to the methods of REST's HTTP protocol. RestTemplate supports common HTTP request methods, such as GET, POST, PUT, DELETE, etc., so it is easy for RestTemplate to build RESTful API calls, such as

RestTemplate.getForObject ("http://common-service/hello", String.class)

The core API is as follows

Https://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html

Second, start using Ribbon Ⅰ. Code writing

Ribbon-test parent project

Pom.xml

Com.calvin.ribbon ribbon-test pom 1.0-SNAPSHOT common-service eureka-server ribbon-service org.springframework.boot spring-boot-starter-parent 1.5.3.RELEASE UTF-8 UTF-8 1.8 org.springframework.cloud spring-cloud-dependencies Dalston.SR4 pom import

Eureka-server

Pom.xml

Com.calvin.ribbon ribbon-test 1.0-SNAPSHOT4.0.0eureka-server 1.8 org.springframework.cloud spring-cloud-starter-eureka-server org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin

Application.yml

Server: port: 8080eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka

ServerApplication.java

@ EnableEurekaServer@SpringBootApplicationpublic class ServerApplication {public static void main (String [] args) {SpringApplication.run (ServerApplication.class);}}

Commons-service

Pom.xml

Ribbon-test com.calvin.ribbon 1.0-SNAPSHOT4.0.0common-service org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-maven-plugin

Application.yml

Spring: application: name: common-serviceeureka: client: service-url: defaultZone: http://localhost:8080/eureka/

Server.port is not specified here because when we start this service, we need to start it locally with different ports, that is, configure two different Name startup classes and specify different Environment variables for server.port=8083. The specific server.port=8084 tutorial is to do the following in EditConfiguration under Idea:

CommonServiceApplication.java

@ SpringBootApplication@EnableEurekaClientpublic class CommonServiceApplication {public static void main (String [] args) {SpringApplication.run (CommonServiceApplication.class);}}

HelloController.java

@ RestControllerpublic class HelloController {@ Value ("${server.port}") private String port; / * * Interface Test * @ return * / @ GetMapping ("/ hello") public String sayHello () {return "port:" + port;}}

Ribbon-service

Pom.xml

Ribbon-test com.calvin.ribbon 1.0-SNAPSHOT4.0.0ribbon-service org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-ribbon org.springframework.boot spring-boot-starter-test test Org.springframework.boot spring-boot-maven-plugin

Application.yml

Server: port: 8082spring: application: name: ribbon-serviceeureka: client: service-url: defaultZone: http://localhost:8080/eureka/

RibbonClientApplication.java

@ EnableEurekaClient@SpringBootApplicationpublic class RibbonServerApplication {public static void main (String [] args) {SpringApplication.run (RibbonServerApplication.class);} / * configure LoadBalance and RestTemplate * @ return RestTemplate * / @ Bean @ LoadBalanced public RestTemplate restTemplate () {return new RestTemplate ();}}

Then comes the key code for the call.

RemoteCommonService.java

/ *

* CommonService service remote invocation class *

* @ author Calvin * @ date 2019-10-09 * @ since 1.0 * / @ Servicepublic class RemoteCommonService {/ * inject RestTemplate * / @ Autowired private RestTemplate restTemplate; / * remote call common-service/hello * @ return * / public String sayHi () {return restTemplate.getForObject ("http://common-service/hello", String.class);}}

SayHiController.java

RestControllerpublic class SayHiController {@ Autowired private RemoteCommonService remoteCommonService; @ GetMapping ("hi") public String sayHi () {return remoteCommonService.sayHi () + ", this is ribbon service";}} Ⅱ. Configuration startup

Finally configure four startup classes, as shown in the figure:

Boot sequence:

Step1. EurekaSeverApplicaton

Step2. CommonServiceApplication

Step3. CommonServiceApplication2

Step4. RibbonServerApplication

Ⅲ. Result verification

Eureka Management Interface http://localhost:8080/

Call http://localhost:8082/hi

Refresh the page

Third, core analysis-- LoadBalancerClient

The core class of load balancer is LoadBalanceClient, which can obtain the instance information of load balancer service provider. Of course, these instance information is obtained through EurekaClient, and a copy of service instance information is cached.

@ Autowired private LoadBalancerClient loanBalanceClient; / * * remote call common-service/hello * @ return * / public String sayHi () {ServiceInstance serviceInstance = loanBalanceClient.choose ("common-service"); logger.info ("current service info is {}: {}", serviceInstance.getHost (), serviceInstance.getPort ()); return restTemplate.getForObject ("http://common-service/hello", String.class);}

When you constantly swipe the interface at this time, you can also see that LoadBalancerClient is constantly changing the instance information of the request.

IV. Summary

Understand the concepts of Ribbon and common load balancing

Hands-on practice of Ribbon for load balancing call

Know a core class of Ribbon

V. Summary

This paper is the first time to practice Ribbon for load balancing, lack of principle analysis.

The code in this article is relatively simple, but it can basically express what Ribbon does.

In the future, the source code of the entire Ribbon needs to be parsed (it can be regarded as an insert).

There are still many uses of RestTemplate, and later articles can be published for in-depth understanding

Thank you for your reading. the above is the content of "SpringCloud uses Ribbon for load balancer". After the study of this article, I believe you have a deeper understanding of the problem of SpringCloud using Ribbon for load balancer, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report