目录
9.2 按前面的步骤重新安装yapi并修改config.json、nginx配置
序言:为什么需要API管理工具
1 并行开发
前后端并行:前端开发人员可直接对接mock接口进行调用;后端开发人员可通过mock接口模拟客户端请求。
研发测试并行:测试人员无需等待研发开发完毕后再进行测试脚本和用例的编写,可以直接对接mock接口。
2 Demo快速搭建:
涉及多接口或多系统集成的项目,在初期直接用mock接口代替后端服务将整个设计串联起来评估效果,
无需投入人力在后端的开发。等方案和接口确认后再启动研发。
3 自动化测试
可以通过配置环境变量直接将测试集合从mock环境指向测试环境,一键式进行自动化测试.
4 API文档库
做为文档工具供团队成员查看,Review接口文档比Review代码更高效. 团队间有可复用的接口可以通过成员管理将接口开放给部分用户,省去了沟通和文档编写等工作.
1 安装yarn
sudo apt-get update
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
source .bashrc
nvm install v10.15.3
npm install -g yarn
2 安装mongodb
sudo apt-get install mongodb
如果需要修改mongodb的配置:/etc/mongodb.conf
登陆mongodb:mongo
创建超级用户:
use admin
db.createUser({user: "root", pwd: "******", roles: [{ role: "root", db: "admin" }]})
3 安装yapi
npm install -g yapi-cli --registry https://registry.npm.taobao.org
yapi server
按照提示在浏览器请求:http://ip:9090
按照实际情况配置内容,请注意这里数据库可以暂时不需要鉴权认证
4 数据库配置鉴权认证
创建yapi数据库用户:
use yapi
db.createUser({user: "mgtvyapi", pwd: "******", roles: [{ role: "dbOwner", db: "yapi" }]})
重启mongodb
先kill老进程
sudo mongod --config /etc/mongodb.conf --auth --fork
再次登陆mongodb的yapi库:
mongo 127.0.0.1/yapi -umgtvyapi -p******
修改yapi连接数据库的配置
vi /home/ubuntu/yapi/config.json
新属性不会写可以参考这里:https://github.com/YMFE/yapi/blob/master/config_example.json
5 启动yapi服务
cd ~/yapi
node vendors/server/app.js
6 nginx代理
6.1安装nginx
sudo apt-get install nginx
6.2 创建nginx运行用户
6.3修改nginx主配置
vi /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
}
6.4修改nginx服务配置
vi default.conf
#
# The default server
#
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
vi yapi.mgtv.com.conf
#
# The default server
#
server {
listen 80;
server_name 10.50.2.6;
#charset koi8-r;
#access_log logs/host.access.log main;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
if ( $args ~ consumerUri= ){
return 403;
}
proxy_pass http://127.0.0.1:3000;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
6.5 重启nginx
sudo service nginx restart
7 对接LDAP
修改config.json配置,添加以下配置:
"ldapLogin": {
"enable": true,
"server": "ldap://ip",
"baseDn": "CN=******,OU=对接系统账号,DC=xx,DC=com",
"bindPassword": "******",
"searchDn": "OU=xxx,OU=Users,OU=xx,DC=x,DC=com",
"searchStandard": "mail"
}
8 解决mongo.db容灾问题
8.1 mongo绑定地址修改为对外服务
sudo vi /etc/mongodb.conf
8.2 修改yapi config.json
8.3 重启mongodb和yapi
先kill老进程
sudo mongod --config /etc/mongodb.conf --auth --fork
nohup node vendors/server/app.js &
8.4 在备机上导出mongodb中yapi数据库
mongodump -h 10.50.2.6 -u mgtvyapi -p ****** -d yapi -o /data/yapi_db/.
8.5 在备机设置定时任务
crontab -e
0 */1 * * * nohup sudo /usr/bin/mongodump -h 10.50.2.6 -u mgtvyapi -p ****** -d yapi -o /data/yapi_db/ &
编译完成后 Ctrl+O 写入
出现“FIile name to Write...”,输入Enter
Ctrl+x 保存输出
提示“crontab:installing new crontab”表示成功。
crontab -l看一下是否已经生效,如果没成效就重启下:service crond restart
9 如何在备机中恢复
9.1 恢复数据
在备机或者把备份文件copy到其它装有mongodb的机器中
mongorestore -h 127.0.0.1 -d yapi yapi
9.2 按前面的步骤重新安装yapi并修改config.json、nginx配置
9.3 联系公共事务部打通LDAP网络
10解决yapi服务长期有效 (pm2)
10.1 pm2安装
npm install -g pm2
10.2 pm2启动服务
先将现在的进程杀死后
cd ~/yapi
pm2 start vendors/server/app.js
pm2 show app 可以看到详细的信息
11 自定义改造
编辑~/yapi/vendors/static/prd/中[email protected]文件,请注意是压缩文件
gunzip [email protected]
vi [email protected]
修改里面想变动的部分
gzip [email protected]
需要pm2重启才生效
pm2 restart all