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

How to use Ribbon to realize client load balancing in Spring Cloud

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I would like to share with you how to use Ribbon to achieve client-side load balancing in Spring Cloud. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

1. Using Ribbon to achieve load balancing

To achieve load balancing, you must first have multiple order service providers. Currently, we only have one microservice-order-provider01 with port number 8001. We can emulate this service and create two sub-modules, which are also order service providers, named microservice-order-provider02, port number 8002 and microservice-order-provider03, and port number 8003.

Now that three order services are available, coupled with Ribbon and Eureka clusters, the architecture of the system is as follows:

Ribbon is a load balancing tool on the client side. On the consumer side of the service, the first three order services 8001, 8002 and 8003 will all register with the Eureka cluster. Ribbon will first query the list of available services from the Eureka cluster (7001, 7002 and 7003), and then request the available order services according to the load balancing algorithm provided by Ribbon.

The default load balancing algorithm for Ribbon is polling, which is called one by one in order. Let's first build the entire service architecture. As mentioned above, we need to copy two order services with port numbers of 8002 and 8003, respectively. Let's compare the configuration files of the three order services.

Order service configuration for port 8001:

# Service port number

Server:

Port: 8001

# Database address

Datasource:

Url: localhost:3306/microservice01

# omit the basic configuration of database

Spring:

Application:

Name: microservice-order # exposed service name

# client registers in the list of eureka services

Eureka:

Client:

Service-url:

DefaultZone: http://eureka01:7001/eureka/,http://eureka02:7002/eureka/,http://eureka03:7003/eureka/,

Instance:

Instance-id: book order Service-8001 # humanized display of service information

Prefer-ip-address: true # access path can display ip address

Order service configuration for port 8002:

# Service port number

Server:

Port: 8002

# Database address

Datasource:

Url: localhost:3306/microservice02

# basic database configuration omitted

Spring:

Application:

Name: microservice-order # exposed service name

# client registers in the list of eureka services

Eureka:

Client:

Service-url:

DefaultZone: http://eureka01:7001/eureka/,http://eureka02:7002/eureka/,http://eureka03:7003/eureka/,

Instance:

Instance-id: book order Service-8002 # humanized display of service information

Prefer-ip-address: true # access path can display ip address

Order service configuration for port 8003:

# Service port number

Server:

Port: 8003

# Database address

Datasource:

Url: localhost:3306/microservice03

# basic database configuration omitted

Spring:

Application:

Name: microservice-order # exposed service name

# client registers in the list of eureka services

Eureka:

Client:

Service-url:

DefaultZone: http://eureka01:7001/eureka/,http://eureka02:7002/eureka/,http://eureka03:7003/eureka/,

Instance:

Instance-id: book order Service-8003 # humanized display of service information

Prefer-ip-address: true # access path can display ip address

After comparison, it is found that there are several areas to pay attention to:

The exposed service names must be the same, because they are all the same service, but there are multiple, because then Ribbon invokes the service through the service name.

Each service is connected to a different database, which is used to distinguish different services, easy to test, and may be easy to maintain in practice.

The personalized name display of each service can be distinguished so that it can be well identified in eureka.

The Eureka cluster still uses the previous 7001, 7002, and 7003. First we start the Eureka cluster, and then start the 8001, 8002, and 8003 order services, respectively. (friendly reminder: six projects have been started here, and if the performance of the computer can't keep up, it may already start to stutter.

After startup, you can visit eureka01:7001 to see if the three order services are properly registered with the eureka cluster. As shown in the figure below, it shows that the cluster and order service are normal.

OK, next comes the key point. We start the service consumer, that is, the order consumption service in the previous section, and then enter http://localhost:9001/consumer/order/get/1 in the browser to query the corresponding order service:

{"id": 1, "name": "study Spring Boot with Brother Wu", "price": 39.99, "dbSource": "microservice03"}

Refresh the page and you can see that the result is as follows:

{"id": 1, "name": "study Spring Boot with Brother Wu", "price": 39.99, "dbSource": "microservice01"}

Then refresh the page, and you can see that the result is as follows:

{"id": 1, "name": "study Spring Boot with Brother Wu", "price": 39.99, "dbSource": "microservice02"}

As you can see, the value of dbsource switches polling between microservice01, microservice02 and microservice03 (this value is the different value I set in the three databases to show the effect here), which shows that Ribbon's load balancer has worked, and the client will switch access from 8001, 8002 and 8003 according to the service name, thus achieving the effect of load balancer.

At the same time, you can also see that the default load balancing algorithm for Ribbon is polling.

two。 How to specify the load balancing policy of Ribbon

From the above results, we can see that the default policy of Ribbon is polling, so what are the load balancing strategies of Ribbon besides polling? How do we set the strategy we want?

The following load balancing strategies are included with Ribbon:

RoundRibbonRule: polling. Everyone has a share, one by one!

RandomRule: random. Fight for character!

AvailabilityFilteringRule: first filter out the services that are in the circuit breaker tripping state due to multiple access failures, and the services whose concurrent connections exceed the threshold, and use the polling strategy for the rest of the services.

WeightedResponseTimeRule: the weight of all services is calculated according to the average response time. The faster the response, the higher the weight of the service, the easier it is to be selected. When starting at the beginning, polling is used when the statistics are insufficient.

RetryRule: poll first, then retry within a specified time if the acquisition fails, and re-poll the available services.

BestAvailableRule: first filter out the service that is in the circuit breaker tripping state due to multiple access failures, and then select a service with the least concurrency.

ZoneAvoidanceRule: compound judge the performance of the area where the server is located and the availability of the server selection server

How to specify the load balancing policy that comes with Ribbon? We need to specify it in the configuration class, as follows:

/ * *

* configure RestTemplate

* @ author shengwu ni

, /

@ Configuration

Public class RestTemplateConfig {

/ * *

The *'@ LoadBalanced' annotation indicates that Ribbon is used to achieve client-side load balancing

* @ return RestTemplate

, /

@ Bean

@ LoadBalanced

Public RestTemplate getRestTemplate () {

Return new RestTemplate ()

}

/ * *

* specify other load balancing policies

* @ return IRule

, /

@ Bean

Public IRule myRule () {

/ / specify retry policy: poll first, then retry within the specified time if the acquisition fails, and re-poll the available services.

Return new RetryRule ()

}

}

We can new the above corresponding policy to achieve the corresponding load balancing. Readers can new RandomRule () test the random policy, and then repeatedly refresh the above test address. You can find that the three services are randomly requested.

These are all the contents of the article "how to use Ribbon to achieve client-side load balancing in Spring Cloud". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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

Development

Wechat

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

12
Report