目錄
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