1. 简介
YApi 是高效、易用、功能强大的 api 管理平台,旨在为开发、产品、测试人员提供更优雅的接口管理服务。可以帮助开发者轻松创建、发布、维护 API,YApi 还为用户提供了优秀的交互体验,开发人员只需利用平台提供的接口数据写入工具以及简单的点击操作就可以实现接口的管理。
它既可以本地部署,也可以docker部署。本文主要介绍一下docker部署。
2. docker 部署
2.1. 运行mongo db
# 创建存储卷
docker volume create mongo-data
# 启动 MongoDB
docker run -d \
--name mongo-yapi \
-v mongo-data:/data/db \
-e MONGO_INITDB_ROOT_USERNAME=anoyi \
-e MONGO_INITDB_ROOT_PASSWORD=anoyi.com \
mongo:4.4.6
结果
[root@VM-16-10-centos ~]# docker volume create mongo-data
mongo-data
[root@VM-16-10-centos ~]# docker run -d \
> --name mongo-yapi \
> -v mongo-data:/data/db \
> -e MONGO_INITDB_ROOT_USERNAME=anoyi \
> -e MONGO_INITDB_ROOT_PASSWORD=anoyi.com \
> mongo:4.4.6
Unable to find image 'mongo:4.4.6' locally
4.4.6: Pulling from library/mongo
e7ae86ffe2df: Pull complete
cb44957d0c54: Pull complete
1b034681f705: Pull complete
f68a0696c1b2: Pull complete
e7e03afd9141: Pull complete
a4a217eed0e5: Pull complete
af2e1e83b32e: Pull complete
1a263184825b: Pull complete
8cadecd5d9b5: Pull complete
a0b5b7c565dc: Pull complete
Digest: sha256:6efa052039903e731e4a5550c68a13c4869ddc93742c716332883fd9c77eb79b
Status: Downloaded newer image for mongo:4.4.6
285bbd3a0840be7689fc60e12152f4f4ced721cc810d7350d9432b57ddd2707c
2.2. 获取 YAPI 镜像
docker pull registry.cn-hangzhou.aliyuncs.com/anoyi/yapi
结果
[root@VM-16-10-centos ~]# docker pull registry.cn-hangzhou.aliyuncs.com/anoyi/yapi
Using default tag: latest
latest: Pulling from anoyi/yapi
e7c96db7181b: Pull complete
72484f09da35: Pull complete
86bee4bed5f2: Pull complete
f9e983f0fe2c: Pull complete
3187a7a7f2df: Pull complete
49bf540f312c: Pull complete
Digest: sha256:b330f694939f5412ec0c7a060eee7636f8b0c98775aeb67f1c1b02cbd4f50a3a
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/anoyi/yapi:latest
registry.cn-hangzhou.aliyuncs.com/anoyi/yapi:latest
2.3. 自定义配置文件config.json
{
"port": "3000",
"adminAccount": "664141154@qq.com",
"timeout":120000,
"db": {
"servername": "mongo",
"DATABASE": "yapi",
"port": 27017,
"user": "anoyi",
"pass": "anoyi.com",
"authSource": "admin"
}
}
结果
[root@VM-16-10-centos ~]# cat config.json
{
"port": 3000,
"adminAccount": "664141154@qq.com",
"timeout":120000,
"db": {
"servername": "mongo",
"DATABASE": "yapi",
"port": 27017,
"user": "anoyi",
"pass": "anoyi.com",
"authSource": "admin"
}
}
2.4. 初始化 YAPI 数据库索引及管理员账号
docker run -it --rm \
--link mongo-yapi:mongo \
--entrypoint npm \
--workdir /yapi/vendors \
-v $PWD/config.json:/yapi/config.json \
registry.cn-hangzhou.aliyuncs.com/anoyi/yapi \
run install-server
结果
[root@VM-16-10-centos ~]# docker run -it --rm \
> --link mongo-yapi:mongo \
> --entrypoint npm \
> --workdir /yapi/vendors \
> -v $PWD/config.json:/yapi/config.json \
> registry.cn-hangzhou.aliyuncs.com/anoyi/yapi \
> run install-server
> yapi-vendor@1.9.2 install-server /yapi/vendors
> node server/install.js
log: mongodb load success...
初始化管理员账号成功,账号名:"664141154@qq.com",密码:"ymfe.org"
2.5. 启动yapi服务
docker run -d \
--name yapi \
--link mongo-yapi:mongo \
--workdir /yapi/vendors \
-p 3000:3000 \
-v $PWD/config.json:/yapi/config.json \
registry.cn-hangzhou.aliyuncs.com/anoyi/yapi \
server/app.js
结果
[root@VM-16-10-centos ~]# docker run -d \
> --name yapi \
> --link mongo-yapi:mongo \
> --workdir /yapi/vendors \
> -p 3000:3000 \
> -v $PWD/config.json:/yapi/config.json \
> registry.cn-hangzhou.aliyuncs.com/anoyi/yapi \
> server/app.js
806a22f57c1da82da1d40b6d8dbbbcb1ac15619b5e25cf7d6fcfbada51beb0fe
查看日志
[root@VM-16-10-centos ~]# docker logs -f 806a22f57c1da82da1d40b6d8dbbbcb1ac15619b5e25cf7d6fcfbada51beb0fe
log: -------------------------------------swaggerSyncUtils constructor-----------------------------------------------
log: 服务已启动,请打开下面链接访问:
http://127.0.0.1:3000/
log: mongodb load success...
3. 访问
- 访问: http://localhost:3000
- 登录账号:
664141154@qq.com
- 密码:
ymfe.org
4.参考
[1]github YApi 可视化接口管理平台
[2]顶尖 API 文档管理工具 (YAPI)