First of all, we need to understand, what data do we need? The key and the value. So what exactly is that? What data is key? Which server node is value?

key

When called, it’s actually a method. And then, the method has parameters. The main parameter, of course, is data. But there might be other parameters besides the data itself that we’re dealing with. What parameters? These are the parameters for load balancing, these are the auxiliary parameters.

// The interface is defined as follows:

Method (first parameter // for load balancing, second parameter // business data);Copy the code

Notice is the first parameter. Not all of the parameters, together. In other words, there must be some place where the parameter is configured as key. Or is the first parameter key by default? Because I don’t see where the configuration is. The answer is that the default is the first parameter, as key.

value

Server node

How to configure it?

1. Load balancing algorithm You are using a consistent hash load balancing algorithm because dubbo is not a consistent hash by default. 2. In other words, the parameter to be configured as the key is the first parameter by default, so no configuration is required. 3. Method Which method uses consistent hash? That is, the method name is configured.


Configuration code

// Service provider

< Expose service class > < which method load balancing algorithm = consistencyhash/>
</>
Copy the code

Application Scenario – Payment in the payment system

Method (String orderId // consistencyhash,Order Order //Copy the code

Realize the principle of

1. Which load balancing algorithm is found? 2. Which server node is found?

To find these two pieces of data, you’re essentially finding value. To find value, you need to find key.

The first key is the method name. The method name/load balancing algorithm has been specified in the configuration file. When the system starts up and loads the configuration file, all the keys/values have been loaded into the memory map. Even if it’s not loaded, if it’s loaded the first time it’s used, it puts the key/value in the map so that the next time it’s used, it gets it directly from the map.

The second key is the first argument to the method (the default is the first argument as the key).

In other words, the data structures used here are all maps.

reference

Blog.csdn.net/Revivedsun/… // There is a source code analysis, write very clear


The official document // principle explains very detailed, the principle looks at this is enough dubbo.apache.org/zh-cn/blog/… Dubbo.apache.org/zh-cn/docs/…


Other blog.csdn.net/lz710117239… Zhuanlan.zhihu.com/p/50620367 bumind.com/distributed… Weicheng97. Cn / 2019/090642…

Why is the payment service using a consistent hash here?

There are only two places where consistent hashes are used. Everything else is random by default.

1. Payment producer — calls consumer interface: write data to queue.

2.Http notification processing data for message-oriented middleware


Why is that?

Why do I have to use a consistent hash for payments when everything else is random?

First, let’s get back to basics and take a look at what consistent hash solves and understand why it’s used here.

Consistent hash is used to better solve the problem of load balancing. What’s a better name? It’s more even, more balanced and more fair. Anyway, that’s the idea.

In other words, the fairness of different load balancing algorithms is different.

Fairness is one of the most important indicators of load balancing.

Another indicator is fault tolerance. What if one of the nodes fails? After hanging, the result is a request that affects the node. To minimize the impact of every node failure, use a consistent hash. Because of consistent hash, only two adjacent nodes are affected, rather than all nodes having their data scrambled.