CN109669817A - A kind of Node.js Server Restart method based on PM2 - Google Patents
A kind of Node.js Server Restart method based on PM2 Download PDFInfo
- Publication number
- CN109669817A CN109669817A CN201811553604.3A CN201811553604A CN109669817A CN 109669817 A CN109669817 A CN 109669817A CN 201811553604 A CN201811553604 A CN 201811553604A CN 109669817 A CN109669817 A CN 109669817A
- Authority
- CN
- China
- Prior art keywords
- node
- new
- network request
- database
- old
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
- 238000000034 method Methods 0.000 title claims abstract description 100
- 230000008569 process Effects 0.000 claims abstract description 77
- 230000004044 response Effects 0.000 abstract description 6
- 230000006870 function Effects 0.000 description 4
- 238000007726 management method Methods 0.000 description 2
- 230000002159 abnormal effect Effects 0.000 description 1
- 230000005856 abnormality Effects 0.000 description 1
- 230000008859 change Effects 0.000 description 1
- 238000013461 design Methods 0.000 description 1
- 230000004048 modification Effects 0.000 description 1
- 238000012986 modification Methods 0.000 description 1
- 238000012544 monitoring process Methods 0.000 description 1
- 238000012545 processing Methods 0.000 description 1
- 238000003672 processing method Methods 0.000 description 1
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/07—Responding to the occurrence of a fault, e.g. fault tolerance
- G06F11/14—Error detection or correction of the data by redundancy in operation
- G06F11/1402—Saving, restoring, recovering or retrying
- G06F11/1415—Saving, restoring, recovering or retrying at system level
- G06F11/1438—Restarting or rejuvenating
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/44—Arrangements for executing specific programs
- G06F9/4401—Bootstrapping
- G06F9/4416—Network booting; Remote initial program loading [RIPL]
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Software Systems (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Computer Security & Cryptography (AREA)
- Quality & Reliability (AREA)
- Computer And Data Communications (AREA)
Abstract
The present invention discloses a kind of Node.js Server Restart method based on PM2.The described method includes: PM2 sends out code to Node.js server;PM2 establishes new Node.js process;New Node.js process notice PM2 is ready;PM2 controls new Node.js process and receives and respond new network request;PM2, which controls old Node.js process, to be stopped receiving new network request;Old Node.js process completes ongoing network request, disconnects the connection of old Node.js process;PM2 deletes the connection of old Node.js process.The present invention can guarantee the network request that normal response is new when Node.js Server Restart, be not in exception response code, moreover it is possible to which normal return restarts preceding established network request and normal switching-off.It solves the problems, such as Caton phenomenon present in existing method for restarting and responds slow.
Description
Technical field
The invention belongs to computer software technical fields, and in particular to a kind of Server Restart side Node.js based on PM2
Method.
Background technique
Currently, more and more mobile Internet products need in time and are in real time under " internet+" concept influence
Users provide various services, and the stability that program is run in the stability and server of server is more and more important.
Node.js server is higher with its processing high concurrent scene performance, and it is asynchronous to support that functional expression programming is very suitable to write
Chain is adjusted back, is widely used.But the processing method of Node.js Server Restart is perfect not enough.It is a kind of common
Node.js Server Restart method is: first find the process of Node.js server, be then shut off the process, and open newly into
Journey;Still an alternative is that: it is instructed by PM2 and sends instruction notification Node.js process, server closing;Then PM2 is restarted
Node.js server.Node.js server is restarted using the above method, Node.js process is closed, ongoing net
Network request is interrupted, and cannot respond to stylish network request.This state that cannot respond to can be continued until new Node.js into
Journey starts completely to be finished.Since subnetwork requests response abnormality, user terminal program exception, collapse and various boundaries may cause
Under the conditions of unpredictable state.Meanwhile the server closing instruction of Node.js is called directly, it cannot close and build at once
Vertical connection (such as with the connection of database), server closing instruction can just come into force after only reaching time default timeout.It is this
Caton phenomenon will influence the usage experience of client product with slow situation is responded.
Summary of the invention
In order to solve the above-mentioned problems in the prior art, the present invention proposes a kind of Node.js server based on PM2
Method for restarting.
To achieve the above object, the present invention adopts the following technical scheme:
A kind of Node.js Server Restart method based on PM2, comprising:
PM2 sends out code to Node.js server;
PM2 establishes new Node.js process;
New Node.js process notifies PM2 ready by ready for sending signal;
After PM2 receives the ready signal, controls new Node.js process and receive and respond new network request;
PM2, which controls old Node.js process (existing process before restarting), to be stopped receiving new network request;
Old Node.js process completes ongoing network request, after listening to out code, disconnects old
The connection of Node.js process;
PM2 deletes the connection of old Node.js process.
Further, it is established in the PM2 after new Node.js process further include:
The connection of new Node.js process and database is established, the database includes the MongoDB for managing session
One or more of database, the MySQL database for managing application data and Redis database for caching.
Further, after the PM2 controls the new network request of old Node.js process stopping reception further include:
The connection of old Node.js process and database is disconnected, the database includes the MongoDB for managing session
One or more of database, the MySQL database for managing application data and Redis database for caching.
Compared with prior art, the invention has the following advantages:
Before establishing new Node.js process ready for sending signal, network request has still been assigned to by PM2 old
In Node.js process.After new Node.js process is ready, one aspect of the present invention is divided new network request by PM2
It is fitted in new Node.js process and is responded;On the other hand the net for distributing old Node.js process normal response by PM2
Connection is closed after network request, or is connected after reaching closing time-out time by PM2 closing, ensure that Node.js service is thought highly of
When opening can not only the new network request of normal response, be not in exception response code, moreover it is possible to it is normal return restart before built
Vertical network request, and normal switching-off.The stability when present invention can effectively promote Node.js Server Restart and reliable
Property, it solves the problems, such as Caton phenomenon present in existing method for restarting and responds slow.
Detailed description of the invention
Fig. 1 is a kind of flow chart of the Node.js Server Restart method based on PM2 of the embodiment of the present invention.
Specific embodiment
The present invention is described in further detail below in conjunction with the accompanying drawings.
A kind of flow chart of the Node.js Server Restart method based on PM2 of the embodiment of the present invention is as shown in Figure 1, described
Method includes:
S101, PM2 send out code to Node.js server;
Node.js is an event-driven I/O server-side JavaScript environment, the V8 engine based on Google;PM2 is
In the Node management of process tool of server-side operation, it can use to simplify the cumbersome task of many Node application managements, such as
Performance monitoring is restarted automatically, load balancing etc..
It is " SIGINT " signal that this step, which is instructed by PM2 and sends out code, and notice Node.js server stops work
Make.Specific instruction may be expressed as:
pm2reload app
Wherein, in app generation, refers to that Node.js is served by title.Pm2 can be executed when executing the instruction
God.killProcess method, this method can send one when calling process.kill (parseInt (pid), ' SIGINT')
A " SIGINT " signal.
S102, PM2 establish new Node.js process;
This step is the executeApp method using PM2reload instruction internal, establishes new Node.js process.Tool
Body instruction may be expressed as:
God.executeApp(new_env,function(err,new_worker){…});
Wherein, the Node.js process of new_worker Dai Zhixin.
S103, new Node.js process notify PM2 ready by ready for sending signal;
In this step, new Node.js process is " ready " signal by ready for sending signal, and notice PM2 prepares just
Thread.After PM2 receives " ready " signal, so that it may receive and respond new network request (see S104).Specific instruction can table
It is shown as:
Wherein, port refers to the port numbers that Node.js process is listened to, and process.send (' ready ') is exactly to send
The instruction of " ready " signal.
After S104, PM2 receive the ready signal, controls new Node.js process and receive and respond new network and ask
It asks;
In this step, PM2 is controlled new after receiving " ready " signal that new Node.js process issues
Node.js process starts to receive and respond new network request.Specific instruction may be expressed as:
new_worker.once(‘listening',onListen);
Wherein, new_worker is the new Node.js process established in step S102.
S105, PM2, which control old Node.js process, to be stopped receiving new network request;
This step is used for the Node.js process i.e. old to established process before restarting and handles, and is controlled by PM2 old
Node.js process stops receiving new network request.Specific instruction may be expressed as:
old_worker.once(‘disconnect',cleanUp);
Wherein, old_worker is old Node.js process.
S106, old Node.js process complete ongoing network request, after listening to out code, disconnect old
The connection of Node.js process;
This step is listening to " SIGINT " letter for completing the old ongoing network request of Node.js process
After number (step S101 is issued), the connection of old Node.js process is disconnected.Specific instruction may be expressed as:
process.on(‘SIGINT’,function(){
server.stop(function(){});
});
Wherein, server.stop is a method for encapsulating server.close, and specific structure is as follows:
Firstly, one timer of setting, closes all connection sockets by the way that socket.end () is asynchronous;Then, lead to
It crosses socket.destory () and destroys these sockets;Finally, disconnecting current old Node.js by server.close ()
The connection of process.In this manner it is possible to all established connections are disconnected, it is normal to destroy.User terminal can discharge connection socket
Word establishes new network request.The phenomenon that cannot respond to for a long time would not occur in the network request having built up in this way.
S107, PM2 delete old Node.js process.
This step is for deleting old Node.js process.If not deleting old Node.js process, may cause interior
Deposit leakage.After memory overflow, it may cause the program of whole service on the server and abnormal conditions occur.When serious, occur different
Normal is not only Node.js program, it is also possible to java applet, PHP program and Python program etc..In this step, lead to
Crossing PM2 calls God.deleteProcessId () method to delete old Node.js process.
After completing above step, is received by new Node.js process and respond all-network request, Node.js server
Restart and finishes.
As a kind of alternative embodiment, after S102 further include: establish the company of new Node.js process and database
Connect, the database include for manage the MongoDB database of session, for managing application data MySQL database and
One or more of Redis database for caching.
This gives one kind when a kind of connection for needing to establish new Node.js process and database, restarted
Embodiment.The database can be MongoDB database, be also possible to MySQL database, can also be Redis data
Library.
Establishing the instruction that new Node.js process is connect with MongoDB database may be expressed as:
Const Mongoclient=require (' mongodb ') .Mongoclient;
MongoClient.connect(opts,function(err,db){});
Wherein, MongoClient is the driver for connecting MongoDB database, and opts refers to connection configuration.
As a kind of alternative embodiment, after S105 further include: disconnect the company of old Node.js process and database
Connect, the database include for manage the MongoDB database of session, for managing application data MySQL database and
One or more of Redis database for caching.
This gives when old Node.js process is connect with Database, a kind of embodiment restarted,
I.e. after PM2 controls the new network request (step S105) of old Node.js process stopping reception, old Node.js is disconnected
The connection of process and database.If not turning off the connection of old Node.js process and database, these connections can occupy data
Library connection pool, so as to cause the connectivity problem of database and other processes.
Disconnecting the instruction that old Node.js process is connect with MongoDB database may be expressed as:
Wherein, MongoClient is the connection driver of MongoDB, and opts is database connection configuration, resolve
(db) established database is connected and is returned.Call db.close () that the connection with MongoDB database can be disconnected.
It is above-mentioned that only several specific embodiments in the present invention are illustrated, but can not be as protection model of the invention
Enclose, it is all according to the present invention in the equivalent change or modification made of design spirit or equal proportion zoom in or out, should all
Think to fall into protection scope of the present invention.
Claims (3)
1. a kind of Node.js Server Restart method based on PM2 characterized by comprising
PM2 sends out code to Node.js server;
PM2 establishes new Node.js process;
New Node.js process notifies PM2 ready by ready for sending signal;
After PM2 receives the ready signal, controls new Node.js process and receive and respond new network request;
PM2, which controls old Node.js process, to be stopped receiving new network request;
Old Node.js process completes ongoing network request, after listening to out code, disconnect old Node.js into
The connection of journey;
PM2 deletes the connection of old Node.js process.
2. the Node.js Server Restart method according to claim 1 based on PM2, which is characterized in that in the PM2
It establishes after new Node.js process further include:
The connection of new Node.js process and database is established, the database includes the MongoDB data for managing session
One or more of library, the MySQL database for managing application data and Redis database for caching.
3. the Node.js Server Restart method according to claim 1 based on PM2, which is characterized in that in the PM2
It controls after old Node.js process stops receiving new network request further include:
The connection of old Node.js process and database is disconnected, the database includes the MongoDB data for managing session
One or more of library, the MySQL database for managing application data and Redis database for caching.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201811553604.3A CN109669817A (en) | 2018-12-19 | 2018-12-19 | A kind of Node.js Server Restart method based on PM2 |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201811553604.3A CN109669817A (en) | 2018-12-19 | 2018-12-19 | A kind of Node.js Server Restart method based on PM2 |
Publications (1)
Publication Number | Publication Date |
---|---|
CN109669817A true CN109669817A (en) | 2019-04-23 |
Family
ID=66145195
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201811553604.3A Pending CN109669817A (en) | 2018-12-19 | 2018-12-19 | A kind of Node.js Server Restart method based on PM2 |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN109669817A (en) |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN111813571A (en) * | 2020-06-13 | 2020-10-23 | 立乐教育科技(上海)有限公司 | Process management method based on NodeJS |
Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20040210898A1 (en) * | 2003-04-18 | 2004-10-21 | Bergen Axel Von | Restarting processes in distributed applications on blade servers |
JP2015026151A (en) * | 2013-07-25 | 2015-02-05 | 日本電信電話株式会社 | Information processing device, information processing method, data structure, and information processing program |
CN105450782A (en) * | 2016-01-15 | 2016-03-30 | 网宿科技股份有限公司 | A method and system for restart network service without package losses and machine halt |
CN106469154A (en) * | 2015-08-17 | 2017-03-01 | 阿里巴巴集团控股有限公司 | A kind of method and apparatus for publishing web page application |
-
2018
- 2018-12-19 CN CN201811553604.3A patent/CN109669817A/en active Pending
Patent Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20040210898A1 (en) * | 2003-04-18 | 2004-10-21 | Bergen Axel Von | Restarting processes in distributed applications on blade servers |
JP2015026151A (en) * | 2013-07-25 | 2015-02-05 | 日本電信電話株式会社 | Information processing device, information processing method, data structure, and information processing program |
CN106469154A (en) * | 2015-08-17 | 2017-03-01 | 阿里巴巴集团控股有限公司 | A kind of method and apparatus for publishing web page application |
CN105450782A (en) * | 2016-01-15 | 2016-03-30 | 网宿科技股份有限公司 | A method and system for restart network service without package losses and machine halt |
Non-Patent Citations (2)
Title |
---|
MAINDEK: "nodejs中server.close()的使用", vol. 1, pages 99 - 100, Retrieved from the Internet <URL:https://blog.csdn.net/maindek/article/details/74999039> * |
黑马程序员著: "《NODE.JS核心技术教程》", vol. 1, 中国铁道出版社, pages: 99 - 100 * |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN111813571A (en) * | 2020-06-13 | 2020-10-23 | 立乐教育科技(上海)有限公司 | Process management method based on NodeJS |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
KR20200035143A (en) | How to set the type of PDD, how to set the policy, and related entities | |
CN110633442A (en) | Pushing method and device and electronic equipment | |
RU2533638C2 (en) | Data configuration method and device | |
EP3331247A1 (en) | Multi-screen control method and device | |
CN108924043A (en) | System monitoring method, gateway communication, gateway apparatus, service processing equipment | |
CN111405059B (en) | Cloud device data transmission method, electronic device and Internet of things system | |
CN109669817A (en) | A kind of Node.js Server Restart method based on PM2 | |
EP3197207A1 (en) | Method for managing application resources and registered node in m2m | |
US20140032892A1 (en) | Method and apparatus for bootstrapping gateway in device management system | |
CN104410511A (en) | Server management method and system | |
CN110417876A (en) | Node server and main control device in session method, distributed system | |
CN106412062A (en) | Method and system for remotely loading engineering to device and monitoring operation state of engineering | |
CN104104701A (en) | Online service configuration updating method and system | |
CN105938490A (en) | Intelligent switching method and system for connecting web application system with data source | |
CN107291747A (en) | A kind of management method of database connection pool, apparatus and system | |
CN108984191A (en) | A kind of method, apparatus and electronic equipment of application update | |
CN104378449A (en) | Virtual IP realization method | |
EP3280091A1 (en) | Method, apparatus and system for establishing interface between vnfms | |
CN111506502B (en) | Mobile terminal UI automatic distribution network flow test system and method | |
CN111163171B (en) | Remote control method and device for terminal equipment | |
CN107547174B (en) | Method for guiding Internet of things equipment to access network based on android client | |
CN109104321B (en) | Method for improving Web _ HDFS availability under Hadoop dual-computer cluster | |
US11010823B2 (en) | Connector leasing for long-running software operations | |
CN110099208A (en) | A kind of method and device across the cross-domain upgrading video camera of grade in video monitoring system | |
CN105187244A (en) | Access management system of digital communication equipment supporting multiple management modes |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
RJ01 | Rejection of invention patent application after publication |
Application publication date: 20190423 |