In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the applicable scenarios in node.js, which can be used for reference. I hope you can learn a lot after reading this article. Let's take a look at it.
Node.js is a platform based on Chrome JavaScript runtime, and its applicable scenarios include localized online music applications, localized online search applications, localized online APP and so on.
This article operating environment: windows7 system, nodejs10.16.2 version, Dell G3 computer.
To put it simply, Node.js is a JavaScript running on the server side.
Node.js is a platform based on the Chrome JavaScript runtime.
Node.js is an event-driven JavaScript environment on the server side of Google. The V8 engine based on Google executes Javascript very fast and the performance is very good.
Understand the applicable scenarios of NodeJS in principle
NodeJS is a popular server-side JS platform in recent years, on the one hand, it benefits from its excellent performance in dealing with high concurrency in the back-end, on the other hand, the rise of powerful code and project management applications such as npm, grunt, express on the nodeJS platform almost redefines the front-end work mode and process.
The success of NodeJS marks its strength, but not all cases are suitable for using NodeJS as a server-side platform?
The answer is, of course, no, and opinions vary on the Internet. Then let's learn about the application of NodeJS from the principle.
Before we talk about NodeJS, let's take a look at the way the traditional server-side processing platform (represented by Apache) handles concurrency.
1. Multithreading and high concurrency mode of Apache
Apache is currently the number one Web server software in the world. It is welcomed by the majority of server technology selectors because it supports multi-thread concurrency. But later, Apache has gradually exposed its disadvantage in some large-scale applications of WEB: blocking.
Some students will wonder, Apache is not multithreaded processing concurrency, why there is still blocking?
To understand this, we first need to understand the concept of threads.
1.1 what is a thread?
We quote the official explanation: the smallest CPU unit that a thread can run independently, can run concurrently in the same process, sharing the memory address space under that process (note this feature).
We can see that threads under the same process share the same files and memory (memory address space), so you can imagine that when different threads need to occupy the same variable, according to the first-come-first-served principle, when the first-come thread is running, the later thread can only wait around, that is, adding to the blocking queue. So this is the cause of thread blocking.
Therefore, although processes can support multiple threads, they seem to be executing at the same time, but they are not synchronized with each other. Multiple threads in a process share the same memory address space, which means they can access the same variables and objects, and they allocate objects from the same heap. Although this makes it easier to share information between threads, programmers must be careful to ensure that they do not interfere with other threads in the same process.
After understanding the shortcomings of multithreaded parallelism, we can better understand the power of NodeJS. Because NodeJS is asynchronous single-threaded!
2. NodeJS's asynchronous IBO principle.
Let's first look at a piece of code that Apache requests the database:
When the code reaches the first line, the thread blocks, waits for the query to return the result, and then continues processing. Due to database queries, disk reads and writes, network traffic, and so on (the so-called Icano) blocking time can be very large (relative to the CPU always frequency). For highly concurrent access, on the one hand, threads block waiting for a long time, on the other hand, adding new threads to cope with new requirements will waste a lot of system resources. At the same time, the increase of threads will also take up a lot of CPU time to deal with memory context switching. Let's see what node.js does.
See, just four words: asynchronous callback. The second argument to query is a callback function, and instead of waiting for the result to return when the process executes to db.query, it continues to execute the following statement until it enters the event loop. When the database execution result returns, the event is sent to the event queue, and the previous callback function is not called until the thread enters the event loop. To put it more professionally, it is asynchronous Ibank O. As long as a single thread is fine.
So why can NodeJS be single-threaded but asynchronous? Here, let's start with a picture and directly stamp the Event queue in the picture.
See, how NodeJS works is actually an event loop. It can be said that the logic of each NodeJS is written in the callback function, and the callback function is executed asynchronously after it is returned!
Seeing this, you can't help but wonder, NodeJS, if all the processing are asynchronous, don't you know how to fly? Wrong, wrong, wrong! Of course not, don't forget that NodeJS implements this on a single-threaded basis. That's right, single thread! One thread carries all the operations!
You can imagine NodeJS facing 100000 concurrent troops in the cold wind, OK, no problem, the enemy threw one into the city, the other into the city. All the people in the city are soldiers, and these enemies can be well digested. But if there is a figure like Zhang Fei and Zhao Yun, Old Node will be miserable in his heart and fight with Zhang Fei for 300 rounds, maiming him and throwing him into the city. Then the 100000 troops in the back will have to wait for these 300th rounds.
So what does that mean? It indicates that NodeJS is not without blocking, but that blocking does not occur in the process of subsequent callbacks, but in the calculation and processing of logic by NodeJS itself. We already know that NodeJS's distribution capability is so powerful that it can loop events for asynchronous callbacks. But if you encounter complex logic operations when looping events, how can a thin single thread support millions of logic + concurrency? All the time-consuming operations such as NodeJS O and network communication can be handed over to worker threads for callback, so it is very fast. But the normal operation of CPU, it can only resist on its own.
Speaking of which, you probably have a spectrum for the characteristics of NodeJS. So the applicable scene is basically coming out ~!
3. Application scenarios of NodeJS
Since NodeJS has a strong ability to handle concurrency, but its ability to deal with computing and logic is very weak, if we move all complex logic operations to the front end (client side) to complete, and NodeJS only needs to provide asynchronous Imax O, we can achieve high-performance processing for high concurrency. There are many cases, such as: RESTFUL API, real-time chat, client logic powerful single-page APP, specific examples: localized online music application, localized online search application, localized online APP and so on.
By the way, Apache, suppressed so much, give a sweet jujube. Apache due to its multi-threaded high concurrency shared memory address space characteristics, that means that if the server is powerful enough, processor high enough core, Apache operation will be very good, so suitable for (concurrent) asynchronous processing is relatively less, background computation is large, background business logic complex applications.
Thank you for reading this article carefully. I hope the article "what are the applicable scenarios in node.js" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.