Centos7 搭建yapi接口管理工具

简介

​ Yapi是去哪网开源的一款api管理应用。当前前后端分离开发大势所趋,为减少前后端工程师的沟通障碍,避免不必要的争吵,建议搭建这么一套api管理工具。后端把开发好的接口信息整理到工具中,前端调用统一参阅api管理文档。

​ 注意:如果后端接口修改了,一定要及时更新。

参考:YApi

安装

安装yapi,需要提前安装mongodb,nodejs(6.X以上),最好把git也安装上

安装nodejs

1
2
3
4
5
6
下载源文件
# curl -sL https://rpm.nodesource.com/setup_8.x | bash -
安装
# sudo yum install -y nodejs
查看版本
# node -v

安装git

1
2
3
# yum install -y git
查看版本
# git --version

安装mongodb

1
2
修改`源` 文件,会自动创建 mongodb-org-4.0.repo
# vi /etc/yum.repos.d/mongodb-org-4.0.repo

内容如下

1
2
3
4
5
6
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7Server/mongodb-org/4.0/x86_64/
gpgcheck=0
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc

修改好后,执行yum install -y mongodb-org

1
2
3
4
5
6
7
8
9
# yum install -y mongodb-org
查看安装目录
# rpm -ql mongodb-org-server
开机启动
#systemctl enable mongod
启动
#systemctl start mongod
查看启动状态
#systemctl status mongdo

安装yapi

安装镜像

1
2
3
# npm install -g yapi-cli --registry https://registry.npm.taobao.org
运行安装
# yapi server

这里会提醒你,通过浏览器进行简单配置。

我这里第一次安装失败了,报错:

1
2
> Error:  (node:1847) 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.
>

需要修改 /my-yapi/vendors/server/utils/db.js文件

大概在22行 增加 useUnifiedTopology: true

1
2
> let options = {useNewUrlParser: true, useCreateIndex: true,useUnifiedTopology:true};
>

修改后,再次运行 yapi server,刷新浏览器访问页面,提示系统已经安装,要删除什么什么,脑瓜子嗡嗡的。

先删除mongob对应的库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
> 登录mongodb
> # mongo
> 查看都有哪些库
> > show dbs;
> admin 0.000GB
> config 0.000GB
> local 0.000GB
> yapi 0.000GB
> 切换到yapi库
> > use yapi
> switched to db yapi
> 删除该库
> > db.dropDatabase()
> { "dropped" : "yapi", "ok" : 1 }
> > show dbs;
> admin 0.000GB
> config 0.000GB
> local 0.000GB
>
>

在运行yapi server 部署 还是不行,后来查资料说用npm run install-server,成功了一次,后来再试又不行了。最后放弃了,选择从做系统从新在来一次。

不出所料,从来一次依然还是报这个错。继续修改db.js文件

执行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
> [root@iZm5eja14jnse4u1p8f1u9Z utils]# npm install --production --registry https://registry.npm.taobao.org
> npm WARN mongoose-auto-increment@5.0.1 requires a peer of mongoose@^4.1.12 but none is installed. You must install peer dependencies yourself.
>
> up to date in 13.318s
>
> 3 packages are looking for funding
> run `npm fund` for details
>
> [root@iZm5eja14jnse4u1p8f1u9Z utils]# npm run install-server
>
> > yapi-vendor@1.8.8 install-server /my-yapi/vendors
> > node server/install.js
>
> log: mongodb load success...
> (node:1679) UnhandledPromiseRejectionWarning: Error: 初始化管理员账号 "admin@admin.com" 失败, E11000 duplicate key error collection: yapi.user index: email_1 dup key: { : "admin@admin.com" }
> at /my-yapi/vendors/server/install.js:146:17
> at <anonymous>
> at process._tickCallback (internal/process/next_tick.js:189:7)
> (node:1679) 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(). (rejection id: 1)
> (node:1679) [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.
>
>

出现了新的错误,提示创建用户失败,删除mongodb中yapi库下user表中的数据

1
2
3
4
5
6
7
8
> > use yapi
> switched to db yapi
> > db.user.find()
> { "_id" : 11, "study" : false, "type" : "site", "username" : "admin", "email" : "admin@admin.com", "password" : "14973458db11325c1d3f0670d60146d35efc936e", "passsalt" : "a4nuifu57kd", "role" : "admin", "add_time" : 1584441851, "up_time" : 1584441851, "__v" : 0 }
> > db.user.drop()
> true
> > db.user.find();
>

再次执行npm run install-server

1
2
3
4
5
6
7
8
9
> [root@iZm5eja14jnse4u1p8f1u9Z utils]# npm run install-server
>
> > yapi-vendor@1.8.8 install-server /my-yapi/vendors
> > node server/install.js
>
> log: mongodb load success...
> 初始化管理员账号成功,账号名:"admin@admin.com",密码:"ymfe.org"
>
>

终于安装成功了。

参考:

Yapi 安装报错

安装pm2

当我们在终端退出之后,yapi的服务也就停了,安装pm2来守护进程

1
2
3
4
5
# npm install -g pm2 --registry=https://registry.npm.taobao.org
pm2启动,目录以自己安装的目录为准
# pm2 start /my-yapi/vendors/server/app.js
pm2停止
# pm2 stop /my-yapi/vendors/server/app.js

参考:

CentOS7 安装 YAPI