一说

前情提示

系统:

前情提示

  1. centos7
  2. MongoDB
  3. node v13.12.0
  4. Git1.8.31

概述

先简单看下几个官方地址:

官方文档:https://hellosean1025.github.io/yapi/devops/index.html

码云Gitee:https://gitee.com/mirrors/YApi

在这里插入图片描述

安装Git

这里使用centOS自带的Git,为了省事,如果有需要自定义安装请移步:

https://janeyork.blog.csdn.net/article/details/103007119

安装node

文本不做介绍,请查看,按步骤来:https://janeyork.blog.csdn.net/article/details/82690129

安装MongoDB

本文不做介绍+1,详情移步:https://xxx.com

安装yapi

方式 一:可视化安装
  1. 安装

npm install -g yapi-cli --registry https://registry.npm.taobao.org

在这里插入图片描述

yapi server如果提示命令没找到,尝试/node目录/bin/server

在这里插入图片描述

  1. 浏览器访问

http://211.149.239.182:9090/

在这里插入图片描述

在这里插入图片描述

稍等片刻…

/home/my-yapi/vendors/server/utils/db.js

1
mongoose.set('useUnifiedTopology', true);

报错:

1
2
Error:  (node:25825) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.

报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@ebs-40617 vendors]# node server/install.js
log: mongodb load success...
(node:25848) UnhandledPromiseRejectionWarning: Error: 初始化管理员账号 "admin@it.com" 失败, E11000 duplicate key error collection: yapi.user index: email_1 dup key: { email: "admin@it.com" }
at /home/my-yapi/vendors/server/install.js:146:17
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:25848) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:25848) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

解决:
mongo --port 27017 -u "root" --authenticationDatabase "admin" -p
Rookie123$
use yapi
switched to db yapi
db.user.remove({'username':'admin'})
WriteResult({ "nRemoved" : 1 })
接着来:
node server/install.js

启动:

1
2
3
4
5
6
7
8
9
[root@ebs-40617 vendors]# node server/install.js
log: mongodb load success...
初始化管理员账号成功,账号名:"admin@it.com",密码:"ymfe.org"
[root@ebs-40617 vendors]# node server/app.js
log: -------------------------------------swaggerSyncUtils constructor-----------------------------------------------
log: 服务已启动,请打开下面链接访问:
http://127.0.0.1:3001/
log: mongodb load success...

  1. 安装pm2
1
2
3
4
5
6
npm i pm2 -g
cd /home/my-yapi
pm2 start "vendors/server/app.js" --name yapi //pm2管理yapi服务
pm2 info yapi //查看服务信息
pm2 stop yapi //停止服务
pm2 restart yapi //重启服务

在这里插入图片描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
解决npm install安装慢的问题
国外镜像会很慢
可用 get命令查看registry

npm congfig get registry
原版结果为

http://registry.npmjs.org
用set命令换成阿里的镜像就可以了

npm config set registry https://registry.npm.taobao.org
再执行命令

npm install
或者直接执行

npm install --registry=https://registry.npm.taobao.org

PM2用法:imweb_io/topic/57c8cbb27f226f687b365636

开机启动PM2:https://pm2.keymetrics.io/docs/usage/startup/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
[root@ebs-40617 my-yapi]# 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/nodejs/bin:/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-failure

ExecStart=/usr/nodejs/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/nodejs/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/nodejs/lib/node_modules/pm2/bin/pm2 kill

[Install]
WantedBy=multi-user.target

Target 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
[root@ebs-40617 my-yapi]#
方式二:命令行部署(个人推荐)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
mkdir yapi && cd yapi
git clone https://gitee.com/mirrors/YApi.git vendors
# git clone https://github.com/YMFE/yapi.git vendors
cp vendors/config_example.json ./config.json
#修改config.json哦,修改数据库名称、端口、账号等

cd vendors
# npm install --production --registry https://registry.npm.taobao.org
npm install --unsafe-perm --production --registry https://registry.npm.taobao.org
npm install --unsafe-perm --registry https://registry.npm.taobao.org

npm run install-server
#启动,在vendors目录下哦
node server/app.js
npm install --unsafe-perm node-sass -g
npm install -g node-gyp
admin@admin.com

成功后如下:

在这里插入图片描述

启动预览,手动安装内置了swagger相关??我辛辛苦苦在方式一中装的插件-。-

在这里插入图片描述

配置邮箱

1
2
3
4
5
6
7
8
9
10
"mail": {
"enable": true,
"host": "smtp.qq.com",
"port": 465,
"from": "14@qq.com",
"auth": {
"user": "14@qq.com",
"pass": "QQ授权码."
}
}

成功:

在这里插入图片描述

安装导出doc插件(不建议)

先安装工具:npm install ykit -g

1
2
3
4
5
6
7
8
9
# 例:export-docx-data
npm install --unsafe-perm ykit -g # 需要安装ykit
ykit # 出现版本信息即可
yapi plugin --name yapi-plugin-export-docx-data # 在yapi目下执行,安装插件,自动执行
npm install --unsafe-perm yapi-plugin-export-docx-data
# 在 my-yapi/vendors 文件下添加模版文件
https://github.com/inceptiongt/Yapi-plugin-export-docx-data/blob/master/input.docx
ykit pack -m
node server/app.js

在这里插入图片描述

报错:

1
2
3
[error] ./client/components/CaseEnv/index.scss
Module build failed: Error: Missing binding /home/my-yapi/vendors/node_modules/node-sass/vendor/linux-x64-79/binding.node
Node Sass could not find a binding for your current environment: Linux 64-bit with Node.js 13.x

vendor目录下添加docx模板:

报错很纠结,但是启动显示安装了??

在这里插入图片描述

安装钉钉推送插件(不建议)

1
2
3
[root@ebs-40617 nodejs]# sudo chown -R root:root /usr/nodejs/lib/module
[root@ebs-40617 nodejs]# sudo chmod -R 755 /usr/nodejs/
npm install --unsafe-perm yapi-plugin-dingding

未安装成功报错:

1
2
3
4
5
6
7
8
9
10
11
Warning: React version not specified in eslint-plugin-react settings. See https://github.com/yannickcr/eslint-plugin-react#configuration .
[error] ./client/components/Loading/Loading.js

/home/my-yapi2/vendors/client/components/Loading/Loading.js
16:3 error componentWillReceiveProps is deprecated since React 16.9.0, use UNSAFE_componentWillReceiveProps instead, see https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components react/no-deprecated

✖ 1 problem (1 error, 0 warnings)


[error] ./client/components/AuthenticatedComponent.js

在这里插入图片描述

config配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
"port": "3001",
"adminAccount": "admin@it.com",
"db": {
"servername": "127.0.0.1",
"DATABASE": "yapi2",
"port": "27017",
"user": "test1",
"pass": "test1"
},
"mail": {
"enable": false,
"host": "smtp.163.com",
"port": 465,
"from": "***@163.com",
"auth": {
"user": "***@163.com",
"pass": "*****"
}
},
"plugins": [
{
"name": "dingding",
"options": {
"host": "http://211.149.239.182:3001"
}
}
]
}

报错了,但是安装上了???shit??

在这里插入图片描述

打开PC版钉钉客户端 - 点击个人头像- 选择机器人管理 - 自定义 - 记得填好IP

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

敲黑板:钉钉消息可以点击,跳转,和你config配置文件钉钉Host一致:

在这里插入图片描述

为什么不建议

好多更新太快,插件可能不兼容,语言语法写法变化大,比如react新版本废弃了好多写法。还有安装完有的插件把其他插件弄丢了??

安装swagger插件

同样报错:(配置会自动添加),此时配置文件如下:

1
npm install --unsafe-perm yapi-plugin-import-swagger-customize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
"port": "3001",
"adminAccount": "admin@it.com",
"db": {
"servername": "127.0.0.1",
"DATABASE": "yapi2",
"port": "27017",
"user": "test1",
"pass": "test1"
},
"mail": {
"enable": false,
"host": "smtp.163.com",
"port": 465,
"from": "***@163.com",
"auth": {
"user": "***@163.com",
"pass": "*****"
}
},
"plugins": [
{
"name": "dingding",
"options": {
"host": "http://211.149.239.182:3001"
}
},
{
"name": "export-docx-data"
},
{
"name": "import-swagger-customize"
}
]
}

在这里插入图片描述

安装成功预览

在这里插入图片描述

在这里插入图片描述