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

The function of Java load balancer interceptor and load balancing strategy

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

Share

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

This article mainly introduces "the function of Java load balancer interceptor and load balancing strategy". In daily operation, I believe many people have doubts about the function of Java load balancer interceptor and load balancing strategy. Xiaobian consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the doubts about "Java load balancer interceptor function and load balancing strategy". Next, please follow the editor to study!

Catalogue

Preface

1. Throw out the question

2. Source code analysis

2.1 、 LoadBalancerIntercepor

2.2 、 LoadBalancerClient

2.3.The load balancing strategy IRule

2.4. Summary

3. Load balancing strategy

Preface

The service consumer needs to invoke the service provider of the registry (Eureka) through RestTemplate, but when there are multiple services with the same service name, which service should our service consumer invoke? At this time, we need to learn to understand the implementation principle of Ribbon load balancing.

When we add @ LoadBalanced annotation to the RestTemplate component, we will pull the list of service instances in the registry and implement the load balancer. The bottom layer of SpringCloud actually uses a component called Ribbon to implement the load balancing function.

1. Throw out the question

For example, our service consumer order-service sends out a request: http://userservice/user/1

The request does not match the service list information in the registry, so how do you find http://localhost:8081?

2. Source code parsing 2.1and LoadBalancerIntercepor

The above information has shown that we don't output IP and port number, but we can find the service we want to invoke by using the service name (userservice)!

This is because the LoadBalancerInterceptor (load balancer interceptor) in the Ribbon component will intercept the call request and obtain the ip and port of the service instance according to the service name.

LoadBalancerInterceptor will intercept the RestTemplate request, then obtain the service list based on the service name in the Eureka registry, and then use the load balancing algorithm to get the real service address information to replace the service name.

You can see the intercept method here, intercepting the call request HttpRequest, and then doing the following:

1.request.getURI (): get the request uri, which in this case is http://user-service/user/8

2.originalUri.getHost (): get the hostname of the uri path, which is actually the server name: userservice

3.this.loadBalancer.execute (): handles service names and user requests

The this.loadBalancer here is of type LoadBalancerClient, so let's continue with the execute method!

2.2 、 LoadBalancerClient

The code goes like this:

GetLoadBalancer (serviceId): get the ILoadBalancer interface based on the service name, and ILoadBalancer will go to eureka with the service name to get the service list and save it.

GetServer (loadBalancer): using the built-in load balancing algorithm, select one from the list of services. In this example, you can see that the service at port 8082 has been obtained

After the release, visit and track it again, and find that it becomes a service on port 8081, and the load balance is achieved:

2.3.The load balancing strategy IRule

In the code just now, you can see that getting the service makes it possible to do load balancing through a getServer method:

Continue to follow the getServer method:

Continue to trace the source chooseServer method and find a piece of code like this:

Let's see who this rule is:

The default value of rule here is a RoundRobinRule. Take a look at the introduction:

It translates to the meaning of polling, so we know the whole process of load balancing.

2.4. Summary

The underlying layer of Ribbon uses an interceptor to intercept requests issued by RestTemplate and change the address. Summarize it with a picture:

The basic process is as follows:

Intercept our RestTemplate request http://userservice/user/1

RibbonLoadBalancerClient gets the service name, that is, userservice, from the request url

DynamicServerListLoadBalancer pulls the list of services from userservice to eureka: localhost:8081, localhost:8082

IRule uses built-in load balancing rules to select a service from the list, such as localhost:8081

RibbonLoadBalancerClient modifies the request address, replaces userservice with localhost:8081, gets http://localhost:8081/user/1, and initiates the real request

3. Load balancing strategy

The rules of load balancer are defined in the IRule interface, while IRule has many different implementation classes:

The implications of different rules are as follows:

The default implementation is ZoneAvoidanceRule, which is a polling scheme

So how to customize the load balancing policy?

You can modify load balancing rules by defining an IRule implementation. There are two ways:

1. Code method: in the configuration class or startup class (which can be regarded as the configuration class), define a new IRule:

@ Beanpublic IRule randomRule () {/ / Random Policy return new RandomRule ();}

Configuration file method: in the application.yml file, you can also modify the rules by adding new configurations:

Userservice: # configure load balancing rules for a micro-service. Here is the load balancing rule for userservice service ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #

Note: we generally use the default load balancing rules and do not modify them!

At this point, the study on "the function of Java load balancer interceptor and load balancing strategy" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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