1.scrapyd
scrapyd 是由scrapy 官方提供的爬蟲管理工具,使用它我們可以非常方便地上傳、控制爬蟲并且查看運行日志。
參考官方文檔 http://scrapyd.readthedocs.org/en/latest/api.html
Installing
pip install scrapyd
Usage
scrapyd
2.spiderkeeper
主要實現(xiàn) scrapy 工程的部署,抓取任務(wù)狀態(tài)監(jiān)控,定時啟動爬蟲等功能。支持多個 scrapyd 服務(wù) ,方便爬蟲集群的管理
Github
Installing
pip install spiderkeeper
Deployment
spiderkeeper [options]
Options:
-h, --help show this help message and exit
--host=HOST host, default:0.0.0.0
--port=PORT port, default:5000
--username=USERNAME basic auth username ,default: admin
--password=PASSWORD basic auth password ,default: admin
--type=SERVER_TYPE access spider server type, default: scrapyd
--server=SERVERS servers, default: ['http://localhost:6800']
--database-url=DATABASE_URL
SpiderKeeper metadata database default: sqlite:////home/souche/SpiderKeeper.db
--no-auth disable basic auth
-v, --verbose log level
example:
spiderkeeper --server=http://localhost:6800
Usage
Visit:
- web ui : http://localhost:5000
1. Create Project
2. Use [scrapyd-client](https://github.com/scrapy/scrapyd-client) to generate egg file
scrapyd-deploy --build-egg output.egg
2. upload egg file (make sure you started scrapyd server)
3. Done & Enjoy it
- api swagger: http://localhost:5000/api.html
Screenshot
3.supervisor
Supervisor (http://supervisord.org) 是一個用 Python 寫的進程管理工具,可以很方便的用來啟動、重啟、關(guān)閉進程(不僅僅是 Python 進程)。除了對單個進程的控制,還可以同時啟動、關(guān)閉多個進程,比如很不幸的服務(wù)器出問題導(dǎo)致所有應(yīng)用程序都被殺死,此時可以用 supervisor 同時啟動所有應(yīng)用程序而不是一個一個地敲命令啟動。
Installing
pip install supervisor
Setting
Supervisor 相當(dāng)強大,提供了很豐富的功能,不過我們可能只需要用到其中一小部分。安裝完成之后,可以編寫配置文件,來滿足自己的需求。為了方便,我們把配置分成兩部分:supervisord(supervisor 是一個 C/S 模型的程序,這是 server 端,對應(yīng)的有 client 端:supervisorctl)和應(yīng)用程序(即我們要管理的程序)。
首先來看 supervisord 的配置文件。安裝完 supervisor 之后,可以運行echo_supervisord_conf 命令輸出默認(rèn)的配置項,也可以重定向到一個配置文件里:
echo_supervisord_conf > /etc/supervisord.conf
去除里面大部分注釋和“不相關(guān)”的部分,我們可以先看這些配置:
[unix_http_server]
file=/tmp/supervisor.sock ; UNIX socket 文件,supervisorctl 會使用
;chmod=0700 ; socket 文件的 mode,默認(rèn)是 0700
;chown=nobody:nogroup ; socket 文件的 owner,格式: uid:gid
;[inet_http_server] ; HTTP 服務(wù)器,提供 web 管理界面
;port=127.0.0.1:9001 ; Web 管理后臺運行的 IP 和端口,如果開放到公網(wǎng),需要注意安全性
;username=user ; 登錄管理后臺的用戶名
;password=123 ; 登錄管理后臺的密碼
[supervisord]
logfile=/tmp/supervisord.log ; 日志文件,默認(rèn)是 $CWD/supervisord.log
logfile_maxbytes=50MB ; 日志文件大小,超出會 rotate,默認(rèn) 50MB
logfile_backups=10 ; 日志文件保留備份數(shù)量默認(rèn) 10
loglevel=info ; 日志級別,默認(rèn) info,其它: debug,warn,trace
pidfile=/tmp/supervisord.pid ; pid 文件
nodaemon=false ; 是否在前臺啟動,默認(rèn)是 false,即以 daemon 的方式啟動
minfds=1024 ; 可以打開的文件描述符的最小值,默認(rèn) 1024
minprocs=200 ; 可以打開的進程數(shù)的最小值,默認(rèn) 200
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; 通過 UNIX socket 連接 supervisord,路徑與 unix_http_server 部分的 file 一致
;serverurl=http://127.0.0.1:9001 ; 通過 HTTP 的方式連接 supervisord
; 包含其他的配置文件
[include]
files = relative/directory/*.ini ; 可以是 *.conf 或 *.ini
我們把上面這部分配置保存到 /etc/supervisord.conf(或其他任意有權(quán)限訪問的文件),然后啟動 supervisord(通過 -c 選項指定配置文件路徑:
supervisord -c /etc/supervisord.conf
查看 supervisord 是否在運行:
ps aux | grep supervisord
program setting
上面我們已經(jīng)把 supervisrod 運行起來了,現(xiàn)在可以添加我們要管理的進程的配置文件。可以把所有配置項都寫到 supervisord.conf 文件里,但并不推薦這樣做,而是通過 include 的方式把不同的程序(組)寫到不同的配置文件里。
為了舉例,我們新建一個目錄 /etc/supervisor/ 用于存放這些配置文件,相應(yīng)的,把 /etc/supervisord.conf 里 include 部分的的配置修改一下:
[include]
files = /etc/supervisor/*.conf
接下來將scrapyd和spiderkeeep的部署命令填寫到配置文件里
[program:spiderkeeper]
command=spiderkeeper --server=http://localhost:6800 --username=sam --password=*****
directory=/srv/www/python/
autostart=true
autorestart=true
startretries=3
[program:scrapyd]
command=source /srv/www/python/pyenv/bin/activate
directory=/srv/www/python/captain
command=scrapyd
autostart=true
autorestart=true
redirect_stderr=true
一份配置文件至少需要一個 [program:x] 部分的配置,來告訴 supervisord 需要管理那個進程。[program:x] 語法中的 x 表示 program name,會在客戶端(supervisorctl 或 web 界面)顯示,在 supervisorctl 中通過這個值來對程序進行 start、restart、stop 等操作。
supervisorctl
Supervisorctl 是 supervisord 的一個命令行客戶端工具,啟動時需要指定與 supervisord 使用同一份配置文件,否則與 supervisord 一樣按照順序查找配置文件。
(pyenv)[root@iZ2597c5i5hZ supervisor.d]# supervisorctl
scrapyd RUNNING pid 25070, uptime 1:09:28
spiderkeeper RUNNING pid 25068, uptime 1:09:28
upload RUNNING pid 25069, uptime 1:09:28
supervisor>
上面這個命令會進入 supervisorctl 的 shell 界面,然后可以執(zhí)行不同的命令了:
> status # 查看程序狀態(tài)
> stop usercenter # 關(guān)閉 usercenter 程序
> start usercenter # 啟動 usercenter 程序
> restart usercenter # 重啟 usercenter 程序
> reread # 讀取有更新(增加)的配置文件,不會啟動新添加的程序
> update # 重啟配置文件修改過的程序
上面這些命令都有相應(yīng)的輸出,除了進入 supervisorctl 的 shell 界面,也可以直接在 bash 終端運行:
$ supervisorctl status
$ supervisorctl stop usercenter
$ supervisorctl start usercenter
$ supervisorctl restart usercenter
$ supervisorctl reread
$ supervisorctl update
4. nginx配置
設(shè)置80端口跳轉(zhuǎn)到5000上
server {
listen 80;
server_name 你的域名;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}