[selenium-grid]多机分布式环境下的selenium集群

栏目: 服务器 · 发布时间: 8年前

内容简介:[selenium-grid]多机分布式环境下的selenium集群

源起

还是爬虫里面的某个场景需要重度使用selenium场景 ,所以需要起这样一个集群

[selenium-grid]多机分布式环境下的selenium集群

对比

  • 单机
  • docker
  • docker-compose
  • 分布式集群

我们在使用selenium 的时候,我们一般就使用以上的环境和模式,单机怎么操作呢,下载相应的webdriver

单机

对于小型的使用环境,比如单线程操作,我们直接下载好相应版本的driver ,直接安装 selenium,然后使用就可以了

from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
 
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36")
 
# 这里是你的webdriver的实际地址
driver = webdriver.PhantomJS(executable_path='/tools/phantomjs', desired_capabilities=dcap)         
 
# 然后你就可以愉快的写逻辑代码了
......
 

Docker

关于 docker 的安装,以前已经介绍过了,这里就不加以重复了,安装好docker之后,我们选择实际selenium官方提供的镜像

保证安装好docker-compose ,这里就直接使用docker-compose.yml 文件起一个实例

docker-compose.yml

chrome:
    image: selenium/standalone-chrome:3.3.1
    ports:
      - 5555:4444
    environment:
      - GRID_TIMEOUT=40
 

然后使用在docker-compose.yml所在的目录 ,使用命令 sudo docker-compose up -d

这个时候就起了一个实例,打开浏览器 http://127.0.0.1:5555/ 就可以看到

脚本如何使用呢,也满简单

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
 
remote = 'http://127.0.0.1:5555/wd/hub'
 
driver = webdriver.Remote(command_executor=remote,desired_capabilities=DesiredCapabilities.CHROME)
 
#正常的逻辑代码
 

如果你要开很多个也可以,前台挂个nginx然后启用多个之后集群

docker-compose 单机集群

docker-compose.yml

hub:
    image: selenium/hub:3.3.1
    ports:
      - 4444:4444
    environment:
      - GRID_TIMEOUT=40
firefox:
    image: selenium/node-firefox:3.3.1
    links:
      - hub
chrome:
    image: selenium/node-chrome:3.3.1
    links:
      - hub
 

使用命令 sudo docker-compose up -d
浏览器打开 http://127.0.0.1:4444/ 就可以看到我们的hub界面了,脚本就如上,只需要端口改成4444

问题

不论是docker模式,还是以上的docker-compose 单机集群模式,问题都很明显的,就是当这台机器出现问题的时候,整个渲染环境就挂掉了,没办法做到很合理的横向拓展,比如单机资源不够用了(渲染是很消耗内存的),就只能停机加内存,加完然后再起,那有没有方法直接将hub节点和node节点拆分开呢,当然有,虽然官方没有提供,不过在仔细的查看源代码之后,得到了解决方法

多机集群

hub节点假设在A机上,ip地址为:10.10.2.38

hub的主文件配置

hub:
    image: selenium/hub:3.3.1
    ports:
      - 4444:4444
    environment:
      - GRID_TIMEOUT=40
 

node节点假设B机上,ip地址为:

node的docker-compose 配置10.10.2.52

node:
    image: selenium/node-chrome:3.3.1
    ports:
          - "5556:5556"
    environment:
          - HUB_PORT_4444_TCP_ADDR=10.10.2.38
          - HUB_PORT_4444_TCP_PORT=4444
          - SE_OPTS=-host 10.10.2.52 -port 5556
 

这里有一个坑,就是你的 -port 必须和外部暴露的端口一致,源代码的处理是直接将这个port 处理成起机器的端口, ==>源码所在<==

总结

这次我们主要是对selenium环境的各种分析和处理,并且处理分布式和多机的实践,基本上已经够你很多实用环境了,这些都是昨天在和今天查阅了大量的资料总结得来的


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Creative Selection

Creative Selection

Ken Kocienda / St. Martin's Press / 2018-9-4 / USD 28.99

Hundreds of millions of people use Apple products every day; several thousand work on Apple's campus in Cupertino, California; but only a handful sit at the drawing board. Creative Selection recounts ......一起来看看 《Creative Selection》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具