The original intention of this blog is that readers who read this article can easily understand what I want to express. But the program to share often inadvertently pasted code, it is likely to let people see very confused. And I think sharing something is meaningful only when the other party really understands the logic. So next, I will try to talk about “micro service” in a language that everyone can understand.
[write in front]
So, what is micro service? You may not know about microservice, but you must know about Mai Mou Lao, who has a dessert station. You may ask, what is the relationship between dessert station and micro service?
Let’s first assume that we don’t stand up for desserts, but an ordinary McDonald’s shop. After a period of operation, you will find that although this place has a large flow of people and customers, 80-90% of the customers’ demand is concentrated on desserts, which leads to the shortage of desserts, while few people buy the rest of the dishes. But did you close this shop? That’s not good. There’s always traffic.
Therefore, in a comprehensive consideration, the plan is to separate the “module” of desserts from the whole store and provide external services separately. This can not only keep the flow, but also avoid waste.
The same is true for micro services, such as e-commerce services, assuming that the services of goods, inventory, orders and users are coupled. However, the order module may be accessed very frequently, while the frequency of users and goods is not so high. If you deploy a service containing goods, inventory, orders and users in order to provide services to more users at the same time, There are two modules whose resources are not fully utilized.
The idea is the same as that of the dessert station. If the commodity, inventory, order and user are divided into four service modules, each service is only responsible for handling its own affairs, just like the dessert station only sells desserts. If the order has a large number of visits, it only needs to deploy another order module, which will not cause the situation of insufficient resource utilization and strong coupling.
At the beginning, the corresponding concept of the “whole Mai Mou Lao” store is the single application, and the “dessert station” is the “micro service” extracted from the single application. It doesn’t matter what a single application is and what a micro service is, or why and how a single application can be transformed into a micro service. Let’s understand this concept through a story.
Suppose you, who are reading this article, own a hotel.
0. [where dreams begin]
You set up a shop to sell fried chicken and desserts, so you simply renovate the shop, purchase relevant equipment, hire cooks, waiters and other relevant staff, and then start to hang up signs for business,
It’s all kinds of C-end products that we usually use. If you don’t have a concept, you can directly understand it as nailing, wechat, an app that we use everyday. They are C-end products.
Shop signboards (such as Ken Da Ji and Mai Mou Lao) can be understood as the things we can see and use in our ordinary app. We call them clients.
The whole physical store can be understood as the “single application” mentioned at the beginning to provide customers with practical services.
0.2 decoration and purchase of equipment
The corresponding is our development team, which starts from the product manager receiving the customer’s requirements, arranges the prototype map according to the requirements, constantly communicates with the customer to exchange details, repeatedly modifies the prototype map, and finally determines the requirements.
Then our UI students intervened, worked out the UI diagram according to the prototype diagram, and finally determined it. Then there is the front-end and back-end development. According to the logic in the prototype diagram and the style details of the UI diagram, the students started the iterative development, and finally put the product online.
0.3 fried chicken and desserts
With the example of Mr. Mak, this should be very easy to understand. Fried chicken and desserts are actually different kinds of services provided to users as a C-end product.
For example, our daily nails can be used to chat, hold video conference and reserve conference room.
1. [getting better]
As you continue to introduce new dishes, as well as enough weight, cost-effective, word of mouth between friends – “that fly restaurant is OK”, but also to retain a large number of repeat customers. Slowly, more and more customers came to the store to eat, and the kitchen became too busy. Many of the guests who came here after waiting for a long time still didn’t serve the dishes, and what’s more, they didn’t even have seats. Guests’ desire for food turns into disappointment and anger, so backhand is a super bad review.
1.1 continuously launch new dishes
On behalf of our product iteration, constantly launch new functions, in order to provide users with more diversified services, attract more users to use our products. For example, during the epidemic, nail launched many new functions in online learning.
1.2 word of mouth among friends
It means that some users begin to replace advertising as a way of communication, and start the self communication of users in the community. This is the goal that many products want to achieve, to tap the potential of the product to the extreme, let the product speak for themselves.
Compared with the flying advertisements, this kind of recommendation from friends is more trusted by users.
1.3 about bad comments
“Kitchen is too busy” means that with the increase of users, the number of daily requests will increase. However, the number of requests that a single service instance can handle in a certain time unit is limited (that is, the number of kitchen is limited, so there are only a few chefs, and the number of dishes that can be cooked is also limited), which directly leads to the slow requests of some users, I can’t even visit it directly (the kitchen is too busy, some guests have to wait for a long time). The patience of users for products is very limited, which will lead to the loss of some or even a large number of users.
With more and more bad comments, you can’t sit still. You think that if it goes on like this, you may be cold, so you think that if the chef is not enough, you should recruit more people. So you put up the recruitment notice and successfully recruited people.
The problem of people has been solved. There is also the problem of seats. You didn’t expect so many people to come to the store before, so the tables and chairs in the store are very loose, but in fact, if you re plan, you can still put several more tables, so that more people can eat in the store at the same time.
The resources we can use for each service are limited. For example, you can set the maximum heap memory for your JVM to 1g or 2G. Ah? What is a JVM? It doesn’t matter. Let’s understand the previous sentence again. Two chefs can cook five dishes a minute. If you recruit another two chefs, you can cook 10 dishes a minute. The corresponding concept is capacity expansion, that is, to increase the operating resources that the server can control, so that the server can handle more requests and serve more users.
The expansion here is for CPU and memory
2.2 rearrange the arrangement of tables and chairs
At the beginning of development, there was not such a large number of users, so most of the API didn’t do any optimization, and the framework of the whole server-side application was also built at random, because the online time is very tight, which may cause the disadvantages of the application in maintenance, scalability and performance.
Generally speaking, it was too wide between the desks and chairs for the sake of good-looking and fashionable display. But later, there were a lot of people outside and there were a lot of people queuing up, but in fact, you were already “full”.
This situation is very unfavorable for subsequent iterations. So the concept corresponding to the development is to refactor the code and framework, optimize the algorithm, make the API occupy less system resources, reduce the response delay, so as to improve the service ability of the whole service. In this way, the service instance can handle more requests.
Sometimes, however, you can’t have both reducing latency and occupying less system resources. If the response speed is very high, it may take up more system resources and trade space for time.
Just like if you rearrange the store, the store will accommodate more people (more resources), and the response time of the service may slow down (the serving time will slow down). So what is the trade of space for time? That is, you will prepare a lot of food materials, even semi-finished food materials, in advance. In this way, you only need to spend half of the usual time to prepare the dishes (such as cooked oil, various accessories, etc.) in advance.
Although more chefs have been recruited and the arrangement of tables and chairs in the shop has been redesigned, with the passage of time, more and more people come to the shop to eat every day. Chefs can no longer recruit and there are not so many cooktops. The previous “bad review boom” began to be staged in the store. You think, although the store is hot is a good thing, but every day there are so many people come to the store to see there is no place to go, this is not money can not make it?
And many target users see these bad comments on the third-party website, which will directly affect whether customers are willing to come to the store, which will make you lose a lot of hidden customers. So you started a vigorous branch business, and opened a store nearby. It’s still the same formula. That’s the taste
3.1 opening a branch
The corresponding concept of opening a branch is multi entity deployment, which is to deploy one same service to another, so that the traffic from users will be transferred from one service to two services.
The branch stores can take over the flow that would have jammed the head office, relieving the pressure.
With the passage of time, more and more branches are opened, and the business is booming. But as a designer who can not only look at the surface, you find that the actual situation is not as good as it appears.
In some areas, there is a large flow of people, while in some areas, although there is a large flow of people, there are not many people who are interested in the store, and even there are few people around the branch store. And some local branches are extremely popular. Although the hot shop can make up for the loss of the cold shop, but the pursuit of perfection you think this situation must be improved, because it is always a waste of resources.
So after some research, you decided to open a customer center in the central area. All the people who want to come to the restaurant come to the customer center, and the people in the customer center will send the customers in batches according to the hot situation of each branch. In this way, we can solve the problem that some stores are hot while others are cold.
4.1 uneven distribution of people flow
This situation mainly occurs when the server runs multiple instances, that is to say, for multiple IP addresses, which one to call is determined by the client. If the design is not good enough, some instances will become hot instances, or even “bad comments” instances sometimes, while the other instances do not bear much traffic from the user.
In other words, if I’m a customer, which store I go to depends on my mood? I can go to any store I want. Of course, the actual situation is not so exaggerated, the client will have its own strategy.
It is also very troublesome to maintain so many server addresses at the same time. If the server adds an instance at this time, the client needs to update synchronously. However, if the user volume is large, because of this problem, frequent contract letting users update will cause bad experience for users.
4.2 customer center
This analogy is somewhat exaggerated. In reality, some people may have gone bankrupt early“ “Customer center” is what we call “gateway”. With the gateway, the client does not need to care about the number of instances of the service, and does not need to maintain all the host. All the requests go directly from the gateway, and the gateway decides which instance to distribute the current request to.
“Customer center sends customers in batches according to the situation” actually corresponds to the concept of load balancing. What does it mean? It is to let the traffic generated by the client to rain and dew on all instances. There are many ways to implement it, including random, polling, consistent hash, weighted and so on.
Besides load balancing, gateway has many other features. For example, dynamic routing, current limiting, authentication, logging, fusing, programmable plug-in configuration and so on.
5. [focus of micro service]
After reading this story, you may feel nothing, but in fact you have already understood the architecture life cycle and process of an application from single application architecture to micro service. This also includes why we need to switch to a microservice architecture and what are the benefits of switching to a microservice architecture. If you don’t realize you understand the concept, you can read the above story again.
In fact, the actual micro service is much more complex than the above story, and the micro service needs to pay more attention than the traditional single application. In microservice, we need to pay attention to service discovery, load balancing, unified configuration management, self-healing and elastic scaling of microservice cluster, service scheduling and publishing, call chain monitoring in microservice, including metrics monitoring, log monitoring, service security considerations, API unified management, etc.
First of all, it is very complex to split the code that is strongly coupled together in an orderly way, and the granularity of microservices is also a great challenge. For example, in a very complex system, the code of a user service may have tens of thousands of lines, but it is still a micro service, and user service a and user service B are no longer split. However, some services may have only a few hundred lines of code, which requires the granularity to be selected according to the actual business situation.
In addition to the granularity of splitting services, there are many components in the microservice itself. You only know gateway in the story. There are many other components.
Service publishing involves Jenkins. We package our application as docker image, and then publish it to the corresponding environment through automation tools. Our application runs in docker, so we need a container choreography tool to manage so many containers. For example, we are using docker swarm. Like kubernetes, which is popular in the industry, many people are using it.
With the container choreography tool, there is the visual interface of the tool, the container corresponding to docker swarm, and the rancher corresponding to kubernetes. In addition, we need to use gitlab as our code version management tool, MySQL and mongodb as data storage solutions. Redis is the solution of cache. At the same time, we need a place to manage the configuration of all services. We call it the configuration center of micro services.
In addition, if services want to call each other, they must know each other’s addresses. They need a registry to keep the addresses of all services and update the status of services in time.
There are also many details, such as how to build a Jenkins build pipeline, how to implement a set of CI / CD process, how to use elk to do unified log collection, how to implement SSO in the cluster, and so on. Due to the length of the article, I will not repeat them here.
If you are interested in microservices, you can communicate with each other in detail. You can also learn more about spring cloud, Dubbo or kubernetes, especially kubernetes. I personally think this must be the mainstay of the future microservice framework.
If you think this article is helpful to you, it’s troublesomeLike it，Close a note，Sharing，Leave a message
WeChat can also search official account.Notes of SH】Of course, you can also scan the QR code directly
- Simple understanding of the underlying principle of InnoDB
- Talking about JVM and garbage collection
- Learn more about concurrent HashMap
- [simple understanding series] dig deep into HashMap from the use of foundation
- Understanding TCP’s three handshakes and four waves from another perspective