In the last article I talked about three algorithms for load balancing, polling, random, and minimum connection, but there are other algorithms for load balancing that we need you to look at, and you may be asked about in the interview process.
To achieve high performance cluster, it is very important to select a good load balancer, and it is also very important to select an appropriate load balancing algorithm for different business scenarios. I’ve already listed a few of them, but now I’m going to talk about the rest of them,
1. Source ADDRESS hashing algorithm
The only algorithm that doesn’t lose policy, but load balancing has a lot to do with the source data information and the hash algorithm.
The idea of source address hash method is that according to the IP address of the client requested by the service consumer, a hash value is calculated through the hash function, and the hash value and the size of the server list are modular operation, and the result is the serial number of the server address to be accessed. The source address hash method is used for load balancing. If the server list remains unchanged, the same IP client will be mapped to the same background server for access.
Same as before, pseudo code:
private static Map<String, Integer> serviceWeightMap = new HashMap<String, Integer>(); The static {serviceWeightMap. Put (" 192.168.1.100 ", 1); ServiceWeightMap. Put (" 192.168.1.101 ", 1); ServiceWeightMap. Put (" 192.168.1.102 ", 4); ServiceWeightMap. Put (" 192.168.1.103 ", 1); } public static String testConsumerHash(String remoteIp) { Map<String, Integer> serverMap = new HashMap<String, Integer>(); serverMap.putAll(serviceWeightMap); List Set<String> keySet = servermap.keyset (); ArrayList<String> keyList = new ArrayList<String>(); keyList.addAll(keySet); int hashCode = remoteIp.hashCode(); int pos = hashCode % keyList.size(); return keyList.get(pos); }
Copy the code
Wrote this code from Will. Shun, when I saw don’t really understand what do you mean, then look at the, actually very similar and its interpretation, through the hash function to calculate a hash value, the hash value and the size of the server list modulus operation, the result is the serial number to access the server address.
2. Weighted polling algorithm
Let’s take a look at the weight configuration in Nginx:
http {upstream cluster {server a weight=1; server b weight=2; server c weight=3; }
Copy the code
If Nginx receives six requests from clients, it forwards one of them to back-end A, two of them to back-end B, and three of them to back-end C.
The result of the weighted polling algorithm is to generate a sequence of servers. Each time a request comes in, the next server is taken out of the sequence to process the request.
Pseudo code of weighted rotation training algorithm:
private static Map<String, Integer> serviceWeightMap = new HashMap<String, Integer>(); The static {serviceWeightMap. Put (" 192.168.1.100 ", 1); ServiceWeightMap. Put (" 192.168.1.101 ", 1); ServiceWeightMap. Put (" 192.168.1.102 ", 4); ServiceWeightMap. Put (" 192.168.1.103 ", 1); } public static String testWeightRoundRobin() {// create a map, Map<String, Integer> serverMap = new HashMap<String, Integer>(); serverMap.putAll(serviceWeightMap); List Set<String> keySet = servermap.keyset (); Iterator<String> it = keySet.iterator(); List<String> serverList = new ArrayList<String>(); while (it.hasNext()) { String server = it.next(); Integer weight = serverMap.get(server); for (int i=0; i<weight; i++) { serverList.add(server); } } String server = null; synchronized (pos) { if (pos > serverList.size()) { pos = 0; } server = serverList.get(pos); pos++; } return server; }
Copy the code
In fact, there are defects in the weighted round training algorithm. Under some special weights, the weighted round scheduling will generate uneven instance sequences. Such uneven load may cause some instances to have instantaneous high load, leading to the risk of system downtime. In order to solve the scheduling defects, there is a smooth weighted rotation scheduling, interested students must go to see the smooth weighted rotation training.
3. Weighted random algorithm
The weighted random method is similar to the weighted polling method, which configures different weights according to the different configuration and load of the background server. The difference is that it randomly selects servers by weight, not order.
private static Map<String, Integer> serviceWeightMap = new HashMap<String, Integer>(); The static {serviceWeightMap. Put (" 192.168.1.100 ", 1); ServiceWeightMap. Put (" 192.168.1.101 ", 1); ServiceWeightMap. Put (" 192.168.1.102 ", 4); ServiceWeightMap. Put (" 192.168.1.103 ", 1); } public static String testWeightRandom() {// create a map, Map<String, Integer> serverMap = new HashMap<String, Integer>(); serverMap.putAll(serviceWeightMap); List Set<String> keySet = servermap.keyset (); List<String> serverList = new ArrayList<String>(); Iterator<String> it = keySet.iterator(); while (it.hasNext()) { String server = it.next(); Integer weight = serverMap.get(server); for (int i=0; i<weight; i++) { serverList.add(server); } } Random random = new Random(); int randomPos = random.nextInt(serverList.size()); String server = serverList.get(randomPos); return server; }
Copy the code
The difference here is that the server is acquired by a random algorithm.
In fact, we can think of an example: for example, in the following scenario: there is A set S, which has for example A,B,C, and D. So we want to pick one of these at random, but with different probabilities, so let’s say we want A 50% chance of picking A, A 20% chance of picking B and C, and A 10% chance of picking D. In general, we can attach a weight to each term, and the probability of extraction is proportional to that weight.
4. HTTP internationalization
HTTP messages can carry content in any language, just as they can carry images, movies, or any type of media. For HTTP, entity bodies are simply containers for binary information.
To support international content, the server needs to tell the client the alphabet and language of each document so that the client can properly unpack the information in the document into characters and present the content to the user.
The server uses the charset parameter in the CONTent-Type header and the Content-language header to inform the client of the document’s alphabet and Language. These “information boxes” begin with a description of what is inside the physical body, how to translate the content into the appropriate characters for display on the screen and what language the words inside are in.
At the same time, the client needs to tell the server what language the user understands and what alphabet encoding algorithm is installed on the browser. The client sends the accept-charset and accept-language headers to tell the server which character set encoding algorithms and languages it understands and the order in which they are prioritised.
That’s all I have to say about HTTP, but if you’re interested, you can go to the HTTP Diagram, the definitive GUIDE to HTTP.
Well, readers, that’s all for this article. Can see here are the most outstanding programmers, we must extend a proud thumb for you a thumbs-up 👍. If you don’t like it, and want to see more, I’d like to recommend a few more.
Operating a HashMap in a multithreaded environment can cause the program to loop forever
Synchronized keyword for thread safety
When we talk about containers, what are we talking about
Soul Torture: How does Java get the length of arrays and strings? Length or length()?
Here comes the routine! If you think this article is useful, please read it and forward it. We love this feeling of being spoiled by big guys.
One more thing! If you want to become a Java god in the shortest time, you can scan the qr code below and join our knowledge planet. See you in the next article!