yapi开放接口平台部署文档

介绍

YApi 是高效、易用、功能强大的 api 管理平台,旨在为开发、产品、测试人员提供更优雅的接口管理服务。可以帮助开发者轻松创建、发布、维护 API,YApi 还为用户提供了优秀的交互体验,开发人员只需利用平台提供的接口数据写入工具以及简单的点击操作就可以实现接口的管理。

特性:

  • 基于 Json5 和 Mockjs 定义接口返回数据的结构和文档,效率提升多倍
  • 扁平化权限设计,即保证了大型企业级项目的管理,又保证了易用性
  • 类似 postman 的接口调试
  • 自动化测试, 支持对 Response 断言
  • MockServer 除支持普通的随机 mock 外,还增加了 Mock 期望功能,根据设置的请求过滤规则,返回期望数据
  • 支持 postman, har, swagger 数据导入
  • 免费开源,内网部署,信息再也不怕泄露了

yapi镜像生成

由node基础镜像封装官方部署应用

1
2
3
4
5
6
7
8
9
10
11
12
# Dockerfile 文件
FROM node:12-alpine
COPY repositories /etc/apk/repositories

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

EXPOSE 3000 9090

# repositories 文件
https://mirrors.aliyun.com/alpine/v3.6/main/
https://mirrors.aliyun.com/alpine/v3.6/community/

kubernetes部署

部署mongodb数据库

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Deployment
kind: Deployment
apiVersion: apps/v1
metadata:
name: yapi-mongodb
namespace: tool
labels:
app: yapi-mongodb
spec:
replicas: 1
selector:
matchLabels:
app: yapi-mongodb
template:
metadata:
labels:
app: yapi-mongodb
spec:
volumes:
- name: volume-xa2bc9
configMap:
name: yapi-mongodb-configmap
items:
- key: init
path: init-mongo.js
defaultMode: 420
- name: volume-n2e67r
persistentVolumeClaim:
claimName: yapi-mongodb-pvc
- name: volume-klexta
persistentVolumeClaim:
claimName: yapi-mongodb-data-pvc
containers:
- name: container-81jufd
image: '10.221.1.41/library/mongo:4'
ports:
- name: tcp-27017
containerPort: 27017
protocol: TCP
env:
- name: MONGO_INITDB_ROOT_USERNAME
value: root
- name: MONGO_INITDB_ROOT_PASSWORD
value: kwl123456
- name: MONGO_INITDB_DATABASE
value: yapi
resources:
limits:
cpu: 500m
memory: 500Mi
requests:
cpu: 10m
memory: 10Mi
volumeMounts:
- name: volume-xa2bc9
mountPath: /docker-entrypoint-initdb.d/init-mongo.js
subPath: init-mongo.js
- name: volume-n2e67r
mountPath: /etc/mongo
subPath: mongo
- name: volume-klexta
mountPath: /data/db
subPath: db
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
restartPolicy: Always
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst
serviceAccountName: default
serviceAccount: default
securityContext: {}
imagePullSecrets:
- name: local-harbor
affinity: {}
schedulerName: default-scheduler
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
revisionHistoryLimit: 10
progressDeadlineSeconds: 600

# svc
kind: Service
apiVersion: v1
metadata:
name: yapi-mongodb-svc
namespace: tool
labels:
app: yapi-mongodb-svc
annotations: {}
spec:
ports:
- name: tcp-27017
protocol: TCP
port: 27017
targetPort: 27017
nodePort: 31691
selector:
app: yapi-mongodb
clusterIP: 10.233.36.158
type: NodePort
sessionAffinity: ClientIP
externalTrafficPolicy: Cluster

# configmap
kind: ConfigMap
apiVersion: v1
metadata:
name: yapi-mongodb-configmap
namespace: tool
labels:
app: yapi-mongodb-configmap
annotations: {}
data:
init: >-
db.createUser({ user: 'admin', pwd: 'admin123456', roles: [ { role: "root",
db: "admin" } ] });


db.auth("admin", "admin123456");
db.createUser({
user: 'yapi',
pwd: 'yapi123456',
roles: [
{ role: "dbAdmin", db: "yapi" },
{ role: "readWrite", db: "yapi" }
]

});

# pvc
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: yapi-mongodb-pvc
namespace: tool
labels:
app: yapi-mongodb-pvc
annotations:
pv.kubernetes.io/bind-completed: 'yes'
pv.kubernetes.io/bound-by-controller: 'yes'
volume.beta.kubernetes.io/storage-provisioner: openebs.io/local
volume.kubernetes.io/selected-node: node3
finalizers:
- kubernetes.io/pvc-protection
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
volumeName: pvc-fe1e3653-9852-4863-8568-66608b97c722
storageClassName: local
volumeMode: Filesystem

# pvc
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: yapi-mongodb-data-pvc
namespace: tool
labels:
app: yapi-mongodb-data-pvc
annotations:
pv.kubernetes.io/bind-completed: 'yes'
pv.kubernetes.io/bound-by-controller: 'yes'
volume.beta.kubernetes.io/storage-provisioner: openebs.io/local
volume.kubernetes.io/selected-node: node3
finalizers:
- kubernetes.io/pvc-protection
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
volumeName: pvc-3cf54100-76c1-4d33-becc-15651cbf101a
storageClassName: local
volumeMode: Filesystem

部署yapi应用
第一次启动采用yapi server命令 访问9090端口 初始化yapi应用
初始完成后使用node /my-yapi/vendors/server/app.js命令运行 使用3000端口正常使用应用

注意:
在访问9090端口初始化应用时 需要开启数据库认证配置
默认部署路径为/my-yapi
修改数据库地址为 集群内部svc访问地址
输入数据库用户名 yapi
输入密码: yapi123456

部署成功后再切换yapi应用的启动参数

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# deployment
kind: Deployment
apiVersion: apps/v1
metadata:
name: yapi
namespace: tool
labels:
app: yapi
spec:
replicas: 1
selector:
matchLabels:
app: yapi
template:
metadata:
creationTimestamp: null
labels:
app: yapi
spec:
volumes:
- name: volume-ojs37j
persistentVolumeClaim:
claimName: yapi-pvc
containers:
- name: container-9860ng
image: '10.221.1.41/library/yapi:v1'
# 初始化命令
# command:
# - yapi
# args:
# - server
# 初始化完成后再以下面的命令运行
command:
- node
args:
- /my-yapi/vendors/server/app.js
ports:
- name: tcp-3000
containerPort: 3000
protocol: TCP
- name: tcp-9090
containerPort: 9090
protocol: TCP
resources:
limits:
cpu: 990m
memory: 1000Mi
requests:
cpu: 10m
memory: 10Mi
volumeMounts:
- name: volume-ojs37j
mountPath: /my-yapi
subPath: my-yapi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
restartPolicy: Always
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst
serviceAccountName: default
serviceAccount: default
securityContext: {}
imagePullSecrets:
- name: local-harbor
affinity: {}
schedulerName: default-scheduler
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
revisionHistoryLimit: 10
progressDeadlineSeconds: 600

# svc
kind: Service
apiVersion: v1
metadata:
name: yapi-svc
namespace: tool
labels:
app: yapi-svc
spec:
ports:
- name: tcp-9090
protocol: TCP
port: 9090
targetPort: 9090
nodePort: 32190
- name: tcp-3000
protocol: TCP
port: 3000
targetPort: 3000
nodePort: 30288
selector:
app: yapi
clusterIP: 10.233.26.46
type: NodePort
sessionAffinity: ClientIP
externalTrafficPolicy: Cluster

# pvc
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: yapi-pvc
namespace: tool
labels:
app: yapi-pvc
annotations:
pv.kubernetes.io/bind-completed: 'yes'
pv.kubernetes.io/bound-by-controller: 'yes'
volume.beta.kubernetes.io/storage-provisioner: openebs.io/local
volume.kubernetes.io/selected-node: node3
finalizers:
- kubernetes.io/pvc-protection
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
volumeName: pvc-40f75a28-9be1-4477-b4e2-8e83e38f35e6
storageClassName: local
volumeMode: Filesystem

使用

访问localhost:3000
用户名为自己输入的管理员邮箱
密码 ymfe.org (默认的初始化密码, 之后可以修改)

自己新建一个项目导入swagger.json文档看能否正常解析和展示

资料

YApi
官方使用文档
docker-compose部署