目录[-]
Crawlab 爬虫框架单机直接部署教程
1.前言:
- github 项目地址: https://github.com/crawlab-team/crawlab
- 官方文档地址:https://tikazyq.github.io/crawlab-docs/Installation/Direct.html
- Q&A: https://tikazyq.github.io/crawlab-docs/QA/index.html
- Docker: https://hub.docker.com/r/tikazyq/crawlab
- 单机器部署: https://juejin.im/post/5d65e8aaf265da03970bca13
- 多机部署:https://mp.weixin.qq.com/s/3Q1BQATUIEE_WXcHPqhYbA
2.部署环境:
- Go 1.12+
- Node 8.12+
- Redis
- MongoDB 3.6+
注意事项:采用直接部署的好处是你可以清楚的了解项目的内部构造,相对而言部署起来也比较麻烦。在开始部署的时候一定要先确定好自己的安装环境,部署的时候尽可能的将redis和mongodb单独部署,如果你的服务器是部署在阿里云或者腾讯云的上边,首先要做的就是在任何地方可以访问到redis和mongo,如果你是在公司的局域网部署,记得要开启相应的端口,确保局域网内的任何机器都可以正常访问。为尤其是redis,它在整个项目中项目中相当重要,切记!切记!切记!
3.环境介绍:
- Vmware12
- Centos7.2
- 网络采用桥接的形式,保证虚拟机之间可以互通,都是192.168.开头的地址
4.安装数据库和编译环境
- 安装 sshd :https://blog.csdn.net/weixin_44009445/article/details/84874458 (记得同时开启6379,8000,27017,8080,8001端口,因为后期的程序要用)
- 安装 Go1.12+ :https://www.cnblogs.com/nickchou/p/10934025.html
- 安装 Redis :https://www.cnblogs.com/wujf-myblog/p/10043197.html
- 安装 Node 8.12+ :https://www.cnblogs.com/miaocbin/p/11428350.html
- 安装 MongoDB 3.6+:https://blog.csdn.net/lss0217/article/details/90144274 注意事项:安装好数据库以后请记住你的redis数据库的地址和mongodb的地址,一会需要在后端的打包配置文件中用到
5.项目介绍:
我们只需要用到两个文件夹,一个是backend后端项目,前端项目frontend。
6.编译项目:
- 克隆项目:git clone https://github.com/crawlab-team/crawlab
- 编译前端项目
~ 编译前端项目之前,请先修改配置文件 .env.production
~ 修改前: NODE_ENV='production' VUE_APP_BASE_URL='http://localhost:8000' ~修改后: NODE_ENV='production' VUE_APP_BASE_URL='http://192.168.66.171:8000'~ 192.168.66.171:8000 是我后端的项目的API接口 ~ 开始编译,构建前端项目
npm install -g yarn cd frontend yarn install 如果出现错误 yarn: command not found 请看此链接: https://blog.csdn.net/yjaspire/article/details/89668838~ 构建
npm run build:prod ~ 构建完成后,会在./frontend目录下创建一个dist文件夹,里面是打包好后的静态文件。 ~ 如果你不能确定你的项目是否编译成功,可以在frontend 文件下运行此命令 npm run serve 运行成功后,浏览器打开项目所在的ip地址,你就可以看到运行成功后的界面了

~ 安装nginx 并且进行配置
yum install nginx~ 添加/etc/nginx/conf.d/crawlab.conf文件,输入以下内容。
server { listen 8080; server_name dev.crawlab.com; root /root/crawlab/frontend/dist; index index.html; }
现在,只需要启动nginx服务就完成了启动前端服务。
nginx reload
至此前端项目已经运行成功
注意:解决Nginx出现403 forbidden (13: Permission denied)报错的四种方法:
https://blog.csdn.net/onlysunnyboy/article/details/75270533
- 编译后端项目
Tip: 我个人选择的是前端项目部署在数据存储的节点上,后端项目部署在工作节点和主节点上,现在开始进行部署后端。
~ 找到backend文件夹,先安装后端所需要的库:
cd ../backend go install ./...Tip: 你会发现无论怎么执行go install ./...都是失败的,因为在安装golang的第三方库的时候,你会发现你无法访问,被墙了!这就很难受,这个时候不要急.
命令行输入:go --help 看一下go 的帮助文档,会发现有一个go env 的命令,我们输入一下看看 [root@localhost backend]# go env GOARCH="amd64" GOBIN="" GOCACHE="/root/.cache/go-build" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/home/gopath" GOPROXY="" GORACE="" GOROOT="/usr/local/go" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GCCGO="gccgo" CC="gcc" CXX="g++" 我们发现有一个变量GOPROXY 说明我们可以通过加代理的方式进行安装第三方包。
~ 开始添加代理镜像:
export GOPROXY=https://goproxy.io 再次执行 go env 这个变量已经赋值成功,这只是临时的,重启以后还需要重新执行。这个时候执行
go install~ 配置config.yaml 文件
vim ../backend/conf/config.yml api: address: "192.168.66.171:8001" # 这个地址是后端API的地址 mongo: host: 192.168.66.170 # mongo 的地址 port: 27017 db: crawlab_test username: "" password: "" authSource: "admin" redis: address: 192.168.66.170 # redis 的地址 password: "" database: 1 port: 6379 log: level: info path: "/var/logs/crawlab" isDeletePeriodically: "N" #这个开启的话,会定时删除日志,个人感觉贼难受 deleteFrequency: "@hourly" server: host: 0.0.0.0 #这个地址是后端API的地址 port: 8001 master: "Y" #是否为主节点 secret: "crawlab" register: # mac地址 或者 ip地址,如果是ip,则需要手动指定IP type: "mac" ip: "" spider: path: "/app/spiders" task: workers: 4 other: tmppath: "/tmp"
~ 构建后端
cd ../backend go buildgo build命令会将Golang代码打包为一个执行文件,默认在$GOPATH/bin里
~ 进行后端测试,执行命令:
[root@localhost backend]# crawlab [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.ok,运行成功,访问http://192.168.66.128:8001/users , 这个时候就会有返回信息,这个时候说明你后端已经运行成功了。[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production. - using env: export GIN_MODE=release - using code: gin.SetMode(gin.ReleaseMode)
2019/10/15 13:35:11 info 初始化配置成功
2019/10/15 13:35:11 info 初始化日志设置成功
2019/10/15 13:35:11 info 初始化定期清理日志配置成功
2019/10/15 13:35:11 info 初始化Mongodb数据库成功
2019/10/15 13:35:11 info 初始化Redis数据库成功
2019/10/15 13:35:11 info 初始化定时任务成功
2019/10/15 13:35:11 info 初始化任务执行器成功
2019/10/15 13:35:11 info register type is :*register.MacRegister {subscribe nodes:master 1} {subscribe nodes:public 1} 2019/10/15 13:35:11 info 初始化节点配置成功
2019/10/15 13:35:11 info 初始化爬虫服务成功
2019/10/15 13:35:11 info 初始化用户服务成功
[GIN-debug] POST /login --> crawlab/routes.Login (4 handlers) [GIN-debug] PUT /users --> crawlab/routes.PutUser (4 handlers) [GIN-debug] GET /nodes --> crawlab/routes.GetNodeList (5 handlers) [GIN-debug] GET /nodes/:id --> crawlab/routes.GetNode (5 handlers) [GIN-debug] POST /nodes/:id --> crawlab/routes.PostNode (5 handlers) [GIN-debug] GET /nodes/:id/tasks --> crawlab/routes.GetNodeTaskList (5 handlers) [GIN-debug] GET /nodes/:id/system --> crawlab/routes.GetSystemInfo (5 handlers) [GIN-debug] DELETE /nodes/:id --> crawlab/routes.DeleteNode (5 handlers) [GIN-debug] GET /spiders --> crawlab/routes.GetSpiderList (5 handlers) [GIN-debug] GET /spiders/:id --> crawlab/routes.GetSpider (5 handlers) [GIN-debug] POST /spiders --> crawlab/routes.PutSpider (5 handlers) [GIN-debug] POST /spiders/:id --> crawlab/routes.PostSpider (5 handlers) [GIN-debug] POST /spiders/:id/publish --> crawlab/routes.PublishSpider (5 handlers) [GIN-debug] DELETE /spiders/:id --> crawlab/routes.DeleteSpider (5 handlers) [GIN-debug] GET /spiders/:id/tasks --> crawlab/routes.GetSpiderTasks (5 handlers) [GIN-debug] GET /spiders/:id/file --> crawlab/routes.GetSpiderFile (5 handlers) [GIN-debug] POST /spiders/:id/file --> crawlab/routes.PostSpiderFile (5 handlers) [GIN-debug] GET /spiders/:id/dir --> crawlab/routes.GetSpiderDir (5 handlers) [GIN-debug] GET /spiders/:id/stats --> crawlab/routes.GetSpiderStats (5 handlers) [GIN-debug] GET /spider/types --> crawlab/routes.GetSpiderTypes (5 handlers) [GIN-debug] GET /tasks --> crawlab/routes.GetTaskList (5 handlers) [GIN-debug] GET /tasks/:id --> crawlab/routes.GetTask (5 handlers) [GIN-debug] PUT /tasks --> crawlab/routes.PutTask (5 handlers) [GIN-debug] DELETE /tasks/:id --> crawlab/routes.DeleteTask (5 handlers) [GIN-debug] POST /tasks/:id/cancel --> crawlab/routes.CancelTask (5 handlers) [GIN-debug] GET /tasks/:id/log --> crawlab/routes.GetTaskLog (5 handlers) [GIN-debug] GET /tasks/:id/results --> crawlab/routes.GetTaskResults (5 handlers) [GIN-debug] GET /tasks/:id/results/download --> crawlab/routes.DownloadTaskResultsCsv (5 handlers) [GIN-debug] GET /schedules --> crawlab/routes.GetScheduleList (5 handlers) [GIN-debug] GET /schedules/:id --> crawlab/routes.GetSchedule (5 handlers) [GIN-debug] PUT /schedules --> crawlab/routes.PutSchedule (5 handlers) [GIN-debug] POST /schedules/:id --> crawlab/routes.PostSchedule (5 handlers) [GIN-debug] DELETE /schedules/:id --> crawlab/routes.DeleteSchedule (5 handlers) [GIN-debug] GET /stats/home --> crawlab/routes.GetHomeStats (5 handlers) [GIN-debug] GET /users --> crawlab/routes.GetUserList (5 handlers) [GIN-debug] GET /users/:id --> crawlab/routes.GetUser (5 handlers) [GIN-debug] POST /users/:id --> crawlab/routes.PostUser (5 handlers) [GIN-debug] DELETE /users/:id --> crawlab/routes.DeleteUser (5 handlers) [GIN-debug] GET /me --> crawlab/routes.GetMe (5 handlers) [GIN-debug] GET /ping --> crawlab/routes.Ping (4 handlers) [GIN-debug] Listening and serving HTTP on 0.0.0.0:8001
结束进程,我们让他后台运行,这样就会很方便。
nohup crawlab &
配置 nginx 反向代理8001端口
vim /etc/nginx/conf.d/go.confserver { listen 8000; #映射的端口号 server_name 127.0.0.1; location / { proxy_pass http://127.0.0.1:8001/; # 被反向代理的端口或者是其他地址 } }
至此后端已经搭建完成了。
Tip:你可能会遇到的问题:
-
登陆的时候登陆错误:
这个时候需要f12 打开控制台的network 选项卡,查看一下是否出现了报红的请求,看一下请求的连接是什么。如果是localhost:8000 ,说明在编译前端的时候没有修改文件.env.production -
启动后端的时候运行错误:
这个时候需要仔细检查你的数据是否正常运行。