Use the concept of 12 factor to quickly create high maintainability applications - Ye Feng (1213 developer practice day)

original
2014/12/17 09:19
Number of readings 505
[Ye Feng]: The title of today's sharing is twelve To quickly create highly maintainable applications. If you can use it well twelve We can fight against each other more   software  erosion this twelve The bar ratio is Adam  Winggins Proposed twelve Concept.

 

 

But now many teams have found this in their own groping twelve A part of the article. Then I also talked about this topic with many friends. If it came from a large company, many of them would think that twelve The article should be a daily thing to do. If you come from a startup or small team, this twelve Structured thinking will slow down development. But actually this twelve In fact, the concept can be very good against software  erosion

 

 

It is meant to be worn away or corroded with the passage of time. The code written a few weeks ago is hard to understand now, or the company has a system, and you have a new demand now. To meet this demand, you need to meet this demand twelve This requirement can be implemented as quickly as possible. If a product is running well now, the operating system may be updated a year or two later, and the underlying dependency has changed. At this time, you restart the system and find that it can no longer move. I don't know what is going on. This version must be cleaned up because the system resources are insufficient. Let's talk about it Heroku This company has very powerful iOS The author who unlocked. This company is not very successful because it ten When it was acquired in, I saw that there were only about 10000 online applications, and their cash flow should be relatively limited. But they have very strong engineering ability, and they are still ten It was acquired by US $200 million in. Everyone may reuse git To manage code, it is troublesome to deploy, but you only need to use git  push You can complete the code version.

 

 

I just want fork Just a moment. If there is any change in my living environment, I can directly fork One copy. Why don't we just use Heroku To develop? because Heroku Than all EPS They all need to be a little more expensive. There is also a big problem. In fact, it is a very strong problem AT It was dropped by the wall, so there is no way. actually Heroku There are many open sources available, but we find that these cloned versions have many limitations. We don't have to build one Heroku Come out and do our development. I just need to Heroku The founders are creating Heroku In this process, we can learn this set of ideas and apply it to our development. This is specific twelve According to our practice, this is it twelve Article.

 

 

We don't need to apply them all. But some of them are actually valuable. Article 1 In fact, its original text says that each code should be managed by an independent code warehouse, which is now microservices Schema for. We know Twitter Every public API All calls generate more than one hundred Secondary internal API Call.

 

 

It helps the development team to collaborate, reduce deployment costs, and roll back quickly. These are some internal private warehouses that we use. If we follow this rule, we will have some benefits. For example, I only need to modify and customize a piece of code. If a system cannot store one code, it must be divided into multiple code warehouses. Here we use three basic things. This model layer is actually private Ruby Code base for.

 

 

It encapsulates some operations on the database. It is an independent code warehouse. We can inject read-only databases through environment variables to ensure that it is clean. There is also a write only service APT Interface. It is divided into different subsystems here, and we can use X-Rate-Limit The advantage of this is that we can minimize the attack whether it is a problem of the client or other internal subsystems, or a malicious attack from outside. The second point is just mentioned at the beginning. If you need to migrate or upgrade the system, how can you ensure that the system can be easily migrated. The dependency needs to be declared. This way Gemfile This file will specifically state which database data is used. You can make sure that you use the same version. It's still a little abstract. Here's an example, Sidekg It is inconsistent with the default version of the system. If I use Bundle  exec In the future, if they are incompatible or the code is migrated to other systems, the default library of this system is incompatible.

 

Of course, it's very popular now docker It is easy to meet this demand. I will not talk about it today. If there is a third point, we advocate putting the configuration Unix Environment variable. At first, it was put in the code constant, but the code constant has a bad place. If you want to modify it, you need to change the code back and forth. This will affect your situation in the code warehouse. If you have different configurations between the version before going online and the version after going online, for example, you need to maintain two branches in the code warehouse, which must be unreasonable. For example, if it is ruby He has a more respected .ENB

 

 

The fourth and third points have a lot to do with our dependence on external systems. For example, we need to visit Qiniu's API We suggest to put it in stable variables, including other subsystems. Just now we mentioned our model layer. We will have two interfaces exposed externally, and these two interfaces will be injected into it. Here is a simple test. You should never submit a code repository that should not be submitted. Here is a simple example. This script is very simple to execute environment commands. If we use dot  enb No matter what voice is used, it is easy to achieve. But it is very helpful to the stability of the whole system.

 

 

In fact, the fifth point is not particularly important for us to practice. Just take a look.

 

 

The sixth is that applications should run as stateless processes. Except for services related to persistence, other services should be stateless. What's wrong with this? You can't ensure that the same process of the same customer is saved in it, and the next time the customer's request will be sent to other machines.

 

 

We just mentioned that dependency can be injected through environment variables. We advocate binding each independent application, that is, embedding HTTP Library for. They bind ports directly to the outside world and provide services to the outside world, rather than relying on external service containers to run. The bound port may not HTTP Protocol.

 

 

And we just mentioned that these services can be relied on by other services if they are declared in this way. High-level API Can be based on low-level API Executed. In such a way, there will be very good applications in the future, which means we can use it at the front end layer  awore  tcp  proxy conduct. If it get For a certain path, we can convert to the corresponding m  point above. There are similar practices, such as TCP We can simply copy a copy of the traffic. If I want to test the performance of the system, I can't test it in a normal environment. But if I deploy two identical traffic, I can do performance stress tests. In addition, if there is anything online bug If the new system is going online, I can also test it in this way. What kind of reaction will it have under the same flow rate. In this way GitHub 's application, which cannot allow DNS In this way, you are randomly assigned a machine to respond to your request. Because every push, every push up push The codes of are all related to individuals. But if we do it in this way, we can capture Git A section of the protocol. Then we can grab your user name and direct it to the corresponding machine. This allows easy capacity expansion.

 

Eighth, we advocate the multi process model for capacity expansion.

 

 

Although the capacity is expanded in a multi process mode, it does not mean that each process is a single process. As we just mentioned, we advocate that services are stateless. Each service is stateless, so it is easy to expand. You can deploy the same process to different servers. The last two points actually describe the multi process model. The services we provide should not turn themselves into systems daemon It is a common way to bind a program in the background, but these methods are actually not good. The second is to run the code in Apache containers, so that the two pieces of code will conflict with each other. You can use Apache containers. Other words like ruby Of unicon This is not good enough. You will encounter many strange problems. What if the process fails? The process exits abnormally and will drift to other PID above. If we do not properly isolate the environment, there will be an impure environment, and additional environment variables will have an abnormal impact on the system. This side is still based on Ruby As an example, we take procfile Example, provided externally FTTP The two scripts will exit at any time for various reasons. If you export them to upstyle

 

The ninth point is to start quickly and close gracefully.

 

 

Tenth, we still hope that the environment for development, testing and deployment should be as close as possible.

 

 

Many companies have a clear division of labor. The code is written by development, and the production environment and operation are matters of operation and maintenance. As responsible for development, we should be responsible for the correctness of the whole code from the correctness of requirements to the future. Second, it is rare for the machine to be too slow, so we try to ensure that the development environment is the same as the running environment. Continuous integration is very important. In fact, many people, especially small teams, may feel that continuous integration and testing are very boring. Actually, it is not. It is very important for you to easily build a server that runs tests continuously. It will send a HTTP A file will be sent out regardless of success or failure. The whole environment is very easy to carry. It can be set up in about an hour or two. I always write Ruby Yes, this may be too much for start-ups, because many times you don't know what it will be like. In this case, regression testing is very important bug , writing a test can reproduce it. The next test will tell you how to do it directly. This is also a return to the previous topic. In the future, this code will not be maintainable. If any change destroys the original thing, the test will fail. The tenth point, I think, is particularly important. If each system writes to files, each log will be allocated to a different place. Once there is a problem, you don't know where to look at the logs.

 

According to my personal experience, the server is full, and the disk is full of logs. In an ideal situation, all logs should be output to the server directly as standard, and then the impression should be made on the server. This is an ideal situation.

 

 

All logs are analyzed and processed uniformly. A friend of mine told me this twelve In fact, it is a normal practice for their company, but it is difficult for a small team to set up such an environment. At present, we have not set up our own log service, but now we use a foreign log service paper  trail Services for.

 

 

The other advantage of this is that I can make a log alarm. If such a situation occurs, I must receive an email. No matter how many subsystems there are, if any subsystem has problems, I can receive an email. OK, thank you. That's all for today's sharing.

 

PS: Developer Best Practices Day · The eight Period - Internet products from design to online Beijing Station

Registration address: http://qiniu-8.eventdove.com

Expand to read the full text
Loading
Click to lead the topic 📣 Post and join the discussion 🔥
Reward
zero comment
eighteen Collection
one fabulous
 Back to top
Top