詳解兩個Node.js進(jìn)程是如何通信
前言
兩個 Node.js 進(jìn)程之間如何進(jìn)行通信呢?這里要分兩種場景:
- 不同電腦上的兩個 Node.js 進(jìn)程間通信
- 同一臺電腦上兩個 Node.js 進(jìn)程間通信
對于第一種場景,通常使用 TCP 或 HTTP 進(jìn)行通信,而對于第二種場景,又分為兩種子場景:
- Node.js 進(jìn)程和自己創(chuàng)建的 Node.js 子進(jìn)程通信
- Node.js 進(jìn)程和另外不相關(guān)的 Node.js 進(jìn)程通信
前者可以使用內(nèi)置的 IPC 通信通道,后者可以使用自定義管道,接下來進(jìn)行詳細(xì)介紹:
不同電腦上的兩個 Node.js 進(jìn)程間通信
要想進(jìn)行通信,首先得搞清楚如何標(biāo)識網(wǎng)絡(luò)中的進(jìn)程?網(wǎng)絡(luò)層的 ip 地址可以唯一標(biāo)識網(wǎng)絡(luò)中的主機,而傳輸層的協(xié)議和端口可以唯一標(biāo)識主機中的應(yīng)用程序(進(jìn)程),這樣利用三元組(ip 地址,協(xié)議,端口)就可以標(biāo)識網(wǎng)絡(luò)的進(jìn)程了。
使用 TCP 套接字
TCP 套接字(socket)是一種基于 TCP/IP 協(xié)議的通信方式,可以讓通過網(wǎng)絡(luò)連接的計算機上的進(jìn)程進(jìn)行通信。一個作為 server 另一個作為 client,server.js 代碼如下:
const net = require('net')
const server = net.createServer(socket => {
console.log('socket connected')
socket.on('close', () => console.log('socket disconnected'))
socket.on('error', err => console.error(err.message))
socket.on('data', data => {
console.log(`receive: ${data}`)
socket.write(data)
console.log(`send: ${data}`)
})
})
server.listen(8888)
client.js 代碼:
const net = require('net')
const client = net.connect(8888, '192.168.10.105')
client.on('connect', () => console.log('connected.'))
client.on('data', data => console.log(`receive: ${data}`))
client.on('end', () => console.log('disconnected.'))
client.on('error', err => console.error(err.message))
setInterval(() => {
const msg = 'hello'
console.log(`send: ${msg}`)
client.write(msg)
}, 3000)
運行效果:
$ node server.js client connected receive: hello send: hello $ node client.js connect to server send: hello receive: hello
使用 HTTP 協(xié)議
因為 HTTP 協(xié)議也是基于 TCP 的,所以從通信角度看,這種方式本質(zhì)上并無區(qū)別,只是封裝了上層協(xié)議。server.js 代碼為:
const http = require('http')
http.createServer((req, res) => res.end(req.url)).listen(8888)
client.js 代碼:
const http = require('http')
const options = {
hostname: '192.168.10.105',
port: 8888,
path: '/hello',
method: 'GET',
}
const req = http.request(options, res => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', d => process.stdout.write(d))
})
req.on('error', error => console.error(error))
req.end()
運行效果:
$ node server.js url /hello $ node client.js statusCode: 200 hello
同一臺電腦上兩個 Node.js 進(jìn)程間通信
雖然網(wǎng)絡(luò) socket 也可用于同一臺主機的進(jìn)程間通訊(通過 loopback 地址 127.0.0.1),但是這種方式需要經(jīng)過網(wǎng)絡(luò)協(xié)議棧、需要打包拆包、計算校驗和、維護(hù)序號和應(yīng)答等,就是為網(wǎng)絡(luò)通訊設(shè)計的,而同一臺電腦上的兩個進(jìn)程可以有更高效的通信方式,即 IPC(Inter-Process Communication),在 unix 上具體的實現(xiàn)方式為 unix domain socket,這是服務(wù)器端和客戶端之間通過本地打開的套接字文件進(jìn)行通信的一種方法,與 TCP 通信不同,通信時指定本地文件,因此不進(jìn)行域解析和外部通信,所以比 TCP 快,在同一臺主機的傳輸速度是 TCP 的兩倍。
使用內(nèi)置 IPC 通道
如果是跟自己創(chuàng)建的子進(jìn)程通信,是非常方便的,child_process 模塊中的 fork 方法自帶通信機制,無需關(guān)注底層細(xì)節(jié),例如父進(jìn)程 parent.js 代碼:
const fork = require("child_process").fork
const path = require("path")
const child = fork(path.resolve("child.js"), [], { stdio: "inherit" });
child.on("message", (message) => {
console.log("message from child:", message)
child.send("hi")
})
子進(jìn)程 child.js 代碼:
process.on("message", (message) => {
console.log("message from parent:", message);
})
if (process.send) {
setInterval(() => process.send("hello"), 3000)
}
運行效果如下:
$ node parent.js message from child: hello message from parent: hi message from child: hello message from parent: hi
使用自定義管道
如果是兩個獨立的 Node.js 進(jìn)程,如何建立通信通道呢?在 Windows 上可以使用命名管道(Named PIPE),在 unix 上可以使用 unix domain socket,也是一個作為 server,另外一個作為 client,其中 server.js 代碼如下:
const net = require('net')
const fs = require('fs')
const pipeFile = process.platform === 'win32' ? '\\\\.\\pipe\\mypip' : '/tmp/unix.sock'
const server = net.createServer(connection => {
console.log('socket connected.')
connection.on('close', () => console.log('disconnected.'))
connection.on('data', data => {
console.log(`receive: ${data}`)
connection.write(data)
console.log(`send: ${data}`)
})
connection.on('error', err => console.error(err.message))
})
try {
fs.unlinkSync(pipeFile)
} catch (error) {}
server.listen(pipeFile)
client.js 代碼如下:
const net = require('net')
const pipeFile = process.platform === 'win32' ? '\\\\.\\pipe\\mypip' : '/tmp/unix.sock'
const client = net.connect(pipeFile)
client.on('connect', () => console.log('connected.'))
client.on('data', data => console.log(`receive: ${data}`))
client.on('end', () => console.log('disconnected.'))
client.on('error', err => console.error(err.message))
setInterval(() => {
const msg = 'hello'
console.log(`send: ${msg}`)
client.write(msg)
}, 3000)
運行效果:
$ node server.js socket connected. receive: hello send: hello $ node client.js connected. send: hello receive: hello
總結(jié)
到此這篇關(guān)于兩個Node.js進(jìn)程是如何通信的文章就介紹到這了,更多相關(guān)兩個Node.js進(jìn)程通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
編譯打包nodejs服務(wù)代碼如何部署到服務(wù)器
這篇文章主要介紹了編譯打包nodejs服務(wù)代碼如何部署到服務(wù)器問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
基于 Docker 開發(fā) NodeJS 應(yīng)用
這是兩篇文章的第一篇。本文涵蓋了有關(guān)在使用 Express 框架開發(fā)一個Node應(yīng)用時,用Docker 替代 Vagrant 的比較詳細(xì)的教程, 應(yīng)用將使用 connect-redis 中間件將會話信息持久化到Redis中. 第二篇文章將介紹到將這個開發(fā)的設(shè)置產(chǎn)品化.2014-07-07
Node.js服務(wù)端實戰(zhàn)之服務(wù)啟動過程詳解
這篇文章主要為大家介紹了Node.js服務(wù)端實戰(zhàn)之服務(wù)啟動過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
詳解如何在Node.js的httpServer中接收前端發(fā)送的arraybuffer數(shù)據(jù)
這篇文章主要介紹了詳解如何在Node.js的httpServer中接收前端發(fā)送的arraybuffer數(shù)據(jù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
node.js中stream流中可讀流和可寫流的實現(xiàn)與使用方法實例分析
這篇文章主要介紹了node.js中stream流中可讀流和可寫流的實現(xiàn)與使用方法,結(jié)合實例形式分析了node.js stream流可讀流和可寫流基本分類、原理、定義、使用方法及相關(guān)注意事項,需要的朋友可以參考下2020-02-02

