What is ZooKeeper?

Foreword

Some time ago, I was debugging zookeeper in the company during my free time, and I was seen by my colleagues (also classmates, temporarily called Brother Xiao Ma). He said you still play with this thing? In fact, zookeeper is not used in 85% of the projects of small companies like ours, and everyone is less exposed to it. Some have never even heard of it.

Later, Brother Xiao Ma asked me in private what exactly is zookeeper (all the following uses zk to refer to zookeeper), then quickly explained: distributed coordination, configuration management, master election, distributed lock , You can also implement queues.

Yes, few people seem to understand this explanation. Baidu has a lot of words like this. In this article, we will not introduce the api and use of zk, just let everyone understand why there is zk. For specific use, we will explain in detail in the next article.

There are two reasons that prompted me to write this article:

1. I hope to bring some help to people who read this article. After writing, first push it to Xiao Ma Take a look, increase the amount of reading…

2, deepen your understanding.

(An article with pure text description, try to write the story so that everyone is interested to read it)

From centralized to distributed

1 The younger brother entered the industry late. When he entered the programming industry in mid-2016, there were already many martial arts masters and great systems. However, I believe that more than 10 years ago, in China, most of the projects should be centralized. The so-called centralized, that is, the centralized deployment of one application. Take the online shopping mall as an example< /span>, the functions of this application include user registration, merchants launching new products, customers searching for products and placing orders, orders, warehouses, logistics updates, and a series of functions. However, there are very few people shopping online in China, and maybe only a few hundred to several thousand people use this system every day. There is no problem.

2. Later, people found that online shopping was very cool and cheap. They verbally recommended each other and shop online. It sounded fashionable. What to do with the goods, haha) Later, more people started shopping online. I found that this server couldn’t stand it anymore, so I started to expand the server capacity and added N servers to do load balancing. OK, things seemed to ease. The user experience is smoother.

3. After another period of time, the product manager of the mall made a request. The merchant was very dissatisfied with the function of adding new goods to the backend of our system. We need to reconstruct the function here. After 1 month, the back-end reconstruction was completed, the website issued an announcement, and the system was shut down for maintenance tonight for 1 hour to upgrade the system. Well, the upgrade is completed in 1 hour. In the past hour, many shopping fans prepared to buy something, but they maintained it and went to another platform to buy it. Our mall has lost 10,000 users in this hour, each of whom is expected to consume 100 yuan, and lost 1 million transactions. The boss is angry and scolds the CTO!

4. In order to solve this dilemma, the CTO thought of a way. Let’s re-develope a system for the module of adding new goods to the back-end merchants, so that when we maintain in the future, we will only maintain it on the merchant side and will not affect it. The user places an order.

OK, the first version of the transition from centralized to distributed scenarios came out, separating the merchant and the user into two projects, I found several very big benefits:

1, 2 sides have established 2 development teams, everyone does not interfere with each other, and the merchant side has fewer functions. We only need 3 people to maintain this project.

2. There are fewer users on the merchant side. We only need to cluster 2 machines. On the user side, there are too many online shopping users in China. Let’s deploy 900 machines.

3. Moreover, the company is headquartered in Zhejiang, and the team has 3 brothers from Beijing. In the future, these 3 brothers can be assigned to the office area in Beijing to be responsible for the business side.

Later, because of this benefit, the CTO deployed the user module, search module, order module, and warehousing logistics module separately.

Problems after distribution

Question 1, user modules, orders, search and other modules are now different The project has been completed, and now the problem has arisen. After Xiao Ma logged in to the front desk and browsed a piece of clothing, he placed the order and jumped to the order module. (From the developer’s point of view, these are two systems, and the session is not shared) Xiao Ma Log in to the order system again, and want to check the logistics information (skip to the logistics module), it prompts that Xiao Ma is not logged in, Xiao Ma is angry, what kind of stupid thing, I don’t buy it, and I keep prompting me to log in. The problems caused by distribution should be paid by our developers instead of making users feel uncomfortable.

The solution to this problem is very simple. Let’s share the session. Don’t put the session in the HashMap of the JVM (from the perspective of a java programmer). Let’s cache the session in redis. But, this way Cookies are not shared. If the first-level domain names are not the same, the cookie returned by accessing system A will not be brought to system B (cookie domain). This problem can be solved better. Just share the first-level domain name, we call a.hkmall.com, b.hkmall.com. In fact, data shows that early distributed systems solved the session sharing problem in this way.

But in this way, there are still many problems. For example, the system of the brothers in the logistics module is not written in java. Python is used, and people curse. What is the jessionid in your cookie? know. Yes, this way limits the language.

Later, there was finally an enterprise-level solution (Single Sign-On SSO) (for a detailed description of sso, I suggest you check the relevant information yourself, and then you may write a CAS-based SSO, which is also an individual Little rookie, I just used these in the personal learning project hkmall)

BTW, one point: I personally think that SSO is necessary in the above situation, and it is also meaningful, but I have seen some The system’s single sign-on is ignorant, and I have never realized what the purpose of SSO is, such as our company’s. A bunch of unrelated systems are integrated to log in together. In fact, many of these systems are not connected, and it is impossible for users of system A to log in to system B at all. This only increases the difficulty of user login and the difficulty of debugging and docking for developers. In other words, if one day a user of system A wants to be authorized to use system B, system B must accept the refactoring workload of system A users (such as Some systems do user localization and other operations…). Of course, the individual is just standing on the technical point of view, I did not think of high-level ideas!

Question 2,

Leave a Comment

Your email address will not be published.