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 customize Ribbon load balancing Policy in Spring Cloud

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Editor to share with you how to customize Ribbon load balancing strategy in Spring Cloud, I believe most people do not know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's learn about it.

1. Main startup class processing

Again, using the client code in the previous section, recall that there are three order service providers called MICROSERVICE-ORDER with ports 8001, 8002, and 8003, respectively. There are three Eureka cluster services with ports 7001, 7002 and 7003, respectively.

The previous section mainly uses Ribbon's polling and random strategies to test load balancing. In this section, we customize a policy. First, add the @ RibbonClient annotation to the startup class, as follows:

@ SpringBootApplication

@ EnableEurekaClient

@ RibbonClient (name = "MICROSERVICE-ORDER", configuration = MyRuleConfig.class)

Public class OrderConsumer {

Public static void main (String [] args) {

SpringApplication.run (OrderConsumer.class, args)

}

}

Name is used to specify the services that need to be balanced, that is, three order services, and configuration is used to specify the policy configuration used. Here we use a custom configuration MyRuleConfig. Next, let's define this configuration.

two。 Custom configuration

One thing to note about the location of this configuration is that the package cannot be under the same package as the main startup class, which is mentioned in the official documentation. So we create a new package at the same level as the main startup class, and then write MyRuleConfig.

/ * *

* Custom rules

* @ author shengwu ni

, /

@ Configuration

Public class MyRuleConfig {

@ Bean

Public IRule myselfRule () {

/ / specify policy: our custom policy

Return new CustomRule ()

}

}

3. Custom policy

OK, the next step is to implement this custom policy: CustomRule. Let's assume that the strategy we define is as follows:

The service is still selected by polling, but for each polled service, the next 4 visits (default is 1), after 4 visits, switch to the next service, visit 4 times, and so on.

After getting this requirement, we need to rewrite the strategy. According to the official github source code, we can see that strategies such as polling and random inherit the AbstractLoadBalancerRule class, and then rewrite the choose method. Therefore, the custom policy is divided into two steps:

Implement the AbstractLoadBalancerRule class

Override the choose method

I'll copy the code first, and then analyze it:

/ * *

* Custom rules

* @ author shengwu ni

, /

Public class CustomRule extends AbstractLoadBalancerRule {

/ * *

* Total number of calls. Currently, each machine is required to be called 4 times.

, /

Private int total = 0

/ * *

* Index of currently provided service list

, /

Private int currentIndex = 0

@ Override public void initWithNiwsConfig (IClientConfig iClientConfig) {

}

/ * *

* in the choose method, customize our own rules, and the returned Server is the selected service.

* own rules: according to the rules of polling, but each polled service is called 5 times.

* @ param o

* @ return

, /

@ Override public Server choose (Object o) {

/ / get the load balancer lb

ILoadBalancer lb = getLoadBalancer ()

If (lb = = null) {

Return null

}

Server server = null

While (server = = null) {

If (Thread.interrupted ()) {

Return null

}

/ / get the list of available services

List upList = lb.getReachableServers ()

/ / get a list of all services

List allList = lb.getAllServers ()

Int serverCount = allList.size ()

If (serverCount = = 0) {

Return null

}

/ / if the number of calls is less than 4 times, the service indexed as currentIndex in the list of available services will be called all the time

If (total < 4)

{

Server = upList.get (currentIndex)

Total++

} else {

/ / after 4 times, the index value + + in the service list indicates the next call to the next service

Total = 0

CurrentIndex++

/ / when the index is larger than the size of the list of available services, start all over again

CurrentIndex = currentIndex% upList.size ()

If (server = = null) {

Thread.yield ()

Continue

}

If (server.isAlive ()) {

Return (server)

}

Server = null

Thread.yield ()

}

}

Return server

}

}

Let me briefly analyze the code: first get the ILoadBalancer object, which can get the current service. We need to get a list of currently available services and a list of all current services.

Total represents the number of times the service has been invoked, and up to 4 times, the service call stops and switches to the next available service; currentIndex represents the index in the list of currently available services. If the number of calls is less than 4 times, the service indexed as currentIndex in the available service list will be called all the time. After 4 times, the index value + + in the service list indicates that the next service is called. You can take a look at the comments in the code.

4. Test it

OK, at this point, the custom load balancing policy is complete. We start three eureka clusters and three order service providers, then start the consumer client (port 9001), and still enter it in the browser:

Http://localhost:9001/consumer/order/get/1, just test it.

Normal: the current order service will be called four times, and then switch to another order service, switching in turn.

These are all the contents of this article entitled "how to customize Ribbon load balancing policies in Spring Cloud". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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

Internet Technology

Wechat

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

12
Report