node 版本升级_Node-RED: 自动化事件触发工具的安装与介绍
Node-RED 介紹
- Node-RED 是一種基于流程的編程工具
- 由 IBM 的新興技術服務團隊原創開發
- Node-RED 是一種事件觸發工具,和 StackStorm 類似, 可以歸類為上層的自動化工具,可以用來觸發與之相對應的下層自動化工具,比如 ansible,來更加優化的完成自動化任務。
Node-RED 環境
我們首先需要明白 Node-RED 是基于Node.js來運行的。我們可以使用 Web 瀏覽器來訪問到流程編輯器。我們可以使用拖拽和連線的方式來創建應用,也就是將所需的節點從面板拖拽到工作區中,然后用連接這些節點到一起。通過一鍵部署方式可以自動將已經創建好的應用部署和運行。
Node-RED 安裝 之前 - 在 CentOS 中安裝Node.js
添加 node.js yum repository
首先我們需要將 node.js 的 yum repository 添加到我們的系統中,它來源于nodejs的官方網站。
當前 LTS 版本是版本 14。
Index of /download/release/latest-v14.x/?nodejs.org如果您想安裝版本 8,只需在下面的命令中更改setup_14 到 setup_8 x。
[alick@devnet ~]# curl -sL https://rpm.nodesource.com/setup_14.x | sudo -E bash -其他組件如下:
## You may also need development tools to build native addons: [alick@devnet ~]# sudo yum install -y gcc-c++ make## To install the Yarn package manager, run: [alick@devnet ~]# curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo [alick@devnet ~]# sudo yum install yarn安裝 node.js 和 NPM
查看將要安裝的版本
[alick@devnet ~]# sudo yum list available nodejs Loaded plugins: fastestmirror, langpacks Repository epel is listed more than once in the configuration Repository epel-debuginfo is listed more than once in the configuration Repository epel-source is listed more than once in the configuration Loading mirror speeds from cached hostfile Available Packages nodejs.x86_64 2:14.5.0-1nodesource然后安裝 node.js 包和 NPM 包,運行以下命令執行此操作。
## Run `sudo yum install -y nodejs` to install Node.js 14.x and npm. [alick@devnet ~]# sudo yum install -y nodejs如果你裝錯了版本或者你要從舊版本升級,你可以使用如下的命令
[alick@devnet ~]# rm -f /etc/yum.repos.d/nodesource-el* [alick@devnet ~]# curl -sL https://rpm.nodesource.com/setup_14.x | bash - [alick@devnet ~]# sudo yum install -y nodejs查看 node.js 和 npm 版本
使用如下命令來查看 node 和 npm 的版本
[alick@devnet ~]# node -v v14.5.0[alick@devnet ~]# npm -v 6.14.5使用簡單的 javascript 代碼來測試安裝環境
我們創建一個文件 devnet.js
其中 192.168.100.100 你可以改為你 centos 的 ip 地址,12345 可以改為你需要訪問的端口。
[alick@devnet ~]# cat devnet.js var http = require('http'); http.createServer(function (req, res) {res.writeHead(200, {'Content-Type': 'text/plain'});res.end('Welcome to DevNet SG'); }).listen(12345, "192.168.100.100"); console.log('Server running at http://192.168.100.100:12345/');然后使用下面的代碼來運行
[alick@devnet ~]# node --inspect devnet.js Debugger listening on ws://127.0.0.1:9229/b7c74321-a226-4bcb-9e1d-bc0e1c90379d For help, see: https://nodejs.org/en/docs/inspector Server running at http://192.168.100.100:12345/可以在瀏覽器中訪問,運行成功!
Node-RED 正式安裝
這個時候 node.js 的環境已經安裝完成,下面將開始安裝 Node-RED
直接執行下面命令即可
[alick@devnet ~]# sudo npm install -g node-red然后我們運行 node-red, 可以看到一些輸出信息
[alick@devnet ~]# node-red 9 Jul 18:34:12 - [info] Welcome to Node-RED ===================9 Jul 18:34:12 - [info] Node-RED version: v1.1.1 9 Jul 18:34:12 - [info] Node.js version: v14.5.0 9 Jul 18:34:12 - [info] Linux 3.10.0-1062.el7.x86_64 x64 LE 9 Jul 18:34:12 - [info] Loading palette nodes 9 Jul 18:34:13 - [info] Settings file : /root/.node-red/settings.js 9 Jul 18:34:13 - [info] Context store : 'default' [module=memory] 9 Jul 18:34:13 - [info] User directory : /root/.node-red 9 Jul 18:34:13 - [warn] Projects disabled : editorTheme.projects.enabled=false 9 Jul 18:34:13 - [info] Flows file : /root/.node-red/flows_Al-TERAFORM-CENTOS-1.json 9 Jul 18:34:13 - [info] Creating new flow file 9 Jul 18:34:13 - [warn] --------------------------------------------------------------------- Your flow credentials file is encrypted using a system-generated key.If the system-generated key is lost for any reason, your credentials file will not be recoverable, you will have to delete it and re-enter your credentials.You should set your own key using the 'credentialSecret' option in your settings file. Node-RED will then re-encrypt your credentials file using your chosen key the next time you deploy a change. ---------------------------------------------------------------------9 Jul 18:34:13 - [info] Server now running at http://127.0.0.1:1880/ 9 Jul 18:34:13 - [info] Starting flows 9 Jul 18:34:13 - [info] Started flowsPM2是Node.js的進程管理工具,利用它可以非常容易地實現開機應用自動啟動以及必要時自動啟動的功能。
然后我們可以使用 pm2 來守護進程
[alick@devnet ~]# sudo npm install -g pm2然后我們啟動 node-red
[alick@devnet ~]# pm2 start node-red-------------__/____/____________/____/______//_/________/______/_______/_///____///_///______//___//__//_/___________//____//____/_____/________///______/_____________/____///_____/_____///_________/_____________/_____________/___//____________/_____________/_____________/__/__///______________///______________///__///__Runtime EditionPM2 is a Production Process Manager for Node.js applicationswith a built-in Load Balancer.Start and Daemonize any application:$ pm2 start app.jsLoad Balance 4 instances of api.js:$ pm2 start api.js -i 4Monitor in production:$ pm2 monitorMake pm2 auto-boot at server restart:$ pm2 startupTo go further checkout:http://pm2.io/-------------[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2 [PM2] PM2 Successfully daemonized [PM2] Starting /usr/bin/node-red in fork_mode (1 instance) [PM2] Done. ┌─────┬─────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐ │ id │ name │ namespace │ version │ mode │ pid │ uptime │ ? │ status │ cpu │ mem │ user │ watching │ ├─────┼─────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤ │ 0 │ node-red │ default │ N/A │ fork │ 24229 │ 0s │ 0 │ online │ 0% │ 13.5mb │ root │ disabled │ └─────┴─────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘然后我們重啟所有 ProcessId
[alick@devnet ~]# pm2 restart all Use --update-env to update environment variables [PM2] Applying action restartProcessId on app [all](ids: [ 0 ]) [PM2] [node-red](0) ? ┌─────┬─────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐ │ id │ name │ namespace │ version │ mode │ pid │ uptime │ ? │ status │ cpu │ mem │ user │ watching │ ├─────┼─────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤ │ 0 │ node-red │ default │ N/A │ fork │ 24257 │ 0s │ 1 │ online │ 0% │ 13.2mb │ root │ disabled │ └─────┴─────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘我們可以查看 node-red 相關系統信息如下:
[alick@devnet ~]# pm2 info node-redDescribing process with id 0 - name node-red ┌───────────────────┬────────────────────────────────────┐ │ status │ online │ │ name │ node-red │ │ namespace │ default │ │ version │ N/A │ │ restarts │ 1 │ │ uptime │ 64s │ │ script path │ /usr/bin/node-red │ │ script args │ N/A │ │ error log path │ /root/.pm2/logs/node-red-error.log │ │ out log path │ /root/.pm2/logs/node-red-out.log │ │ pid path │ /root/.pm2/pids/node-red-0.pid │ │ interpreter │ node │ │ interpreter args │ N/A │ │ script id │ 0 │ │ exec cwd │ /root │ │ exec mode │ fork_mode │ │ node.js version │ 14.5.0 │ │ node env │ N/A │ │ watch & reload │ ? │ │ unstable restarts │ 0 │ │ created at │ 2020-07-09T10:38:40.795Z │ └───────────────────┴────────────────────────────────────┘Actions available ┌────────────────────────┐ │ km:heapdump │ │ km:cpu:profiling:start │ │ km:cpu:profiling:stop │ │ km:heap:sampling:start │ │ km:heap:sampling:stop │ └────────────────────────┘Trigger via: pm2 trigger node-red <action_name>Code metrics value ┌────────────────────────┬───────────┐ │ Heap Size │ 26.41 MiB │ │ Heap Usage │ 93.64 % │ │ Used Heap Size │ 24.73 MiB │ │ Active requests │ 0 │ │ Active handles │ 4 │ │ Event Loop Latency │ 0.45 ms │ │ Event Loop Latency p95 │ 1.55 ms │ └────────────────────────┴───────────┘Divergent env variables from local env Add your own code metrics: http://bit.ly/code-metricsUse `pm2 logs node-red [--lines 1000]` to display logsUse `pm2 env 0` to display environment variablesUse `pm2 monit` to monitor CPU and Memory usage node-red我們可以查看 node-red 相關日志信息如下:
[alick@devnet ~]# pm2 logs node-red [TAILING] Tailing last 15 lines for [node-red] process (change the value with --lines option) /root/.pm2/logs/node-red-error.log last 15 lines: /root/.pm2/logs/node-red-out.log last 15 lines: 0|node-red | --------------------------------------------------------------------- 0|node-red | Your flow credentials file is encrypted using a system-generated key. 0|node-red | 0|node-red | If the system-generated key is lost for any reason, your credentials 0|node-red | file will not be recoverable, you will have to delete it and re-enter 0|node-red | your credentials. 0|node-red | 0|node-red | You should set your own key using the 'credentialSecret' option in 0|node-red | your settings file. Node-RED will then re-encrypt your credentials 0|node-red | file using your chosen key the next time you deploy a change. 0|node-red | --------------------------------------------------------------------- 0|node-red | 0|node-red | 9 Jul 18:38:45 - [info] Starting flows 0|node-red | 9 Jul 18:38:45 - [info] Started flows 0|node-red | 9 Jul 18:38:45 - [info] Server now running at http://127.0.0.1:1880/我們保存現在的 process 信息然后設置為開機啟動
[alick@devnet ~]# pm2 save [PM2] Saving current process list... [PM2] Successfully saved in /root/.pm2/dump.pm2[alick@devnet ~]# pm2 startup [PM2] Init System found: systemd Platform systemd Template [Unit] Description=PM2 process manager Documentation=https://pm2.keymetrics.io/ After=network.target[Service] Type=forking User=root LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin Environment=PM2_HOME=/root/.pm2 PIDFile=/root/.pm2/pm2.pid Restart=on-failureExecStart=/usr/lib/node_modules/pm2/bin/pm2 resurrect ExecReload=/usr/lib/node_modules/pm2/bin/pm2 reload all ExecStop=/usr/lib/node_modules/pm2/bin/pm2 kill[Install] WantedBy=multi-user.targetTarget path /etc/systemd/system/pm2-root.service Command list [ 'systemctl enable pm2-root' ] [PM2] Writing init configuration in /etc/systemd/system/pm2-root.service [PM2] Making script booting at startup... [PM2] [-] Executing: systemctl enable pm2-root... Created symlink from /etc/systemd/system/multi-user.target.wants/pm2-root.service to /etc/systemd/system/pm2-root.service. [PM2] [v] Command successfully executed. +---------------------------------------+ [PM2] Freeze a process list on reboot via: $ pm2 save[PM2] Remove init script via: $ pm2 unstartup systemd訪問瀏覽器界面,我們可以通過 1880 端口來正常訪問 Node-RED 了。
訪問的鏈接為 http://{Node-RED-machine-ip-address}:1880
到這里,通過這篇詳細的教程,相信大家已經了解如何安裝 Node-RED 了,歡迎關注,我們將繼續分享和深入學習。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的node 版本升级_Node-RED: 自动化事件触发工具的安装与介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3分钟就能完成的Redis主从复制搭建
- 下一篇: maven项目和普通项目转换