2016-07-21 4 views
1

PhantomJS와의 인터페이스에 selenium webdriver를 사용하는 응용 프로그램이 있습니다. 규모를 확장하기 위해 PhantomJS의 여러 인스턴스를 실행하고 haproxy로로드 밸런스를 조정하려고합니다. 이것은 로컬 응용 프로그램을위한 것이므로 프로덕션 환경이나 그와 비슷한 것에 대한 배포와 관련이 없습니다.도커 - 작성 및 haproxy를 사용하여 팬텀을 어떻게로드 밸런스합니까?

여기 내 docker-compose.yml 파일입니다 :

당신이 볼 수 있듯이, 나는 phantomjs 인스턴스 4, 하나 개 haproxy 인스턴스 및 (파이썬으로 작성) 한 응용 프로그램을 가지고
version: '2' 
services: 
    app: 
    build: . 
    volumes: 
     - .:/code 
    links: 
     - mongo 
     - haproxy 
    mongo: 
    image: mongo 
    phantomjs1: 
    image: wernight/phantomjs:latest 
    ports: 
     - 8910 
    entrypoint: 
     - phantomjs 
     - --webdriver=8910 
     - --ignore-ssl-errors=true 
     - --load-images=false 
    phantomjs2: 
    image: wernight/phantomjs:latest 
    ports: 
     - 8910 
    entrypoint: 
     - phantomjs 
     - --webdriver=8910 
     - --ignore-ssl-errors=true 
     - --load-images=false 
    phantomjs3: 
    image: wernight/phantomjs:latest 
    ports: 
     - 8910 
    entrypoint: 
     - phantomjs 
     - --webdriver=8910 
     - --ignore-ssl-errors=true 
     - --load-images=false 
    phantomjs4: 
    image: wernight/phantomjs:latest 
    ports: 
     - 8910 
    entrypoint: 
     - phantomjs 
     - --webdriver=8910 
     - --ignore-ssl-errors=true 
     - --load-images=false 
    haproxy: 
    image: haproxy 
    volumes: 
     - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro 
    ports: 
     - 8910:8910 
    links: 
     - phantomjs1 
     - phantomjs2 
     - phantomjs3 
     - phantomjs4 

.

은 여기 내 haproxy.cfg :

global 
    log 127.0.0.1 local0 
    log 127.0.0.1 local1 notice 
    maxconn 4096 
    daemon 

defaults 
    log  global 
    mode http 
    option httplog 
    option dontlognull 
    retries 3 
    option redispatch 
    maxconn 2000 
    timeout connect 5000 
    timeout client 50000 
    timeout server 50000 

frontend phantomjs_front 
    bind *:8910 
    stats uri /haproxy?stats 
    default_backend phantomjs_back 

backend phantomjs_back 
    balance roundrobin 
    server phantomjs1 phantomjs1:8910 check 
    server phantomjs2 phantomjs2:8910 check 
    server phantomjs3 phantomjs3:8910 check 
    server phantomjs4 phantomjs4:8910 check 

나는이 작업을 얻을 수 haproxy에 끈적 세션이나 뭔가를 사용할 필요가 알고,하지만 난 그렇게하는 방법을 모르겠어요. 그럼, 어떻게

phantomjs2_1 | [ERROR - 2016-07-12T23:35:25.454Z] RouterReqHand - _handle.error - {"name":"Variable Resource Not Found","message":"{\"headers\":{\"Accept\":\"application/json\",\"Accept-Encoding\":\"identity\",\"Connection\":\"close\",\"Content-Length\":\"96\",\"Content-Type\":\"application/json;charset=UTF-8\",\"Host\":\"172.19.0.7:8910\",\"User-Agent\":\"Python-urllib/3.5\"},\"httpVersion\":\"1.1\",\"method\":\"POST\",\"post\":\"{\\\"url\\\": \\\"\\\\\\\"http://www.REDACTED.com\\\\\\\"\\\", \\\"sessionId\\\": \\\"4eff6a60-4889-11e6-b4ad-095b9e1284ce\\\"}\",\"url\":\"/session/4eff6a60-4889-11e6-b4ad-095b9e1284ce/url\",\"urlParsed\":{\"anchor\":\"\",\"query\":\"\",\"file\":\"url\",\"directory\":\"/session/4eff6a60-4889-11e6-b4ad-095b9e1284ce/\",\"path\":\"/session/4eff6a60-4889-11e6-b4ad-095b9e1284ce/url\",\"relative\":\"/session/4eff6a60-4889-11e6-b4ad-095b9e1284ce/url\",\"port\":\"\",\"host\":\"\",\"password\":\"\",\"user\":\"\",\"userInfo\":\"\",\"authority\":\"\",\"protocol\":\"\",\"source\":\"/session/4eff6a60-4889-11e6-b4ad-095b9e1284ce/url\",\"queryKey\":{},\"chunks\":[\"session\",\"4eff6a60-4889-11e6-b4ad-095b9e1284ce\",\"url\"]}}","line":80,"sourceURL":"phantomjs://code/router_request_handler.js","stack":"[email protected]://code/router_request_handler.js:80:82"} 
phantomjs2_1 | 
phantomjs2_1 | phantomjs://platform/console++.js:263 in error 
app_1   | Traceback (most recent call last): 
app_1   | File "selenium_process.py", line 69, in <module> 
app_1   |  main() 
app_1   | File "selenium_process.py", line 61, in main 
app_1   |  source = get_page(args.url) 
app_1   | File "selenium_process.py", line 52, in get_page 
app_1   |  driver.get(url) 
app_1   | File "/usr/local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 248, in get 
app_1   |  self.execute(Command.GET, {'url': url}) 
app_1   | File "/usr/local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute 
app_1   |  self.error_handler.check_response(response) 
app_1   | File "/usr/local/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 163, in check_response 
app_1   |  raise exception_class(value) 
app_1   | selenium.common.exceptions.WebDriverException: Message: Variable Resource Not Found - {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"96","Content-Type":"application/json;charset=UTF-8","Host":"172.19.0.7:8910","User-Agent":"Python-urllib/3.5"},"httpVersion":"1.1","method":"POST","post":"{\"url\": \"\\\"http://www.REDACTED.com\\\"\", \"sessionId\": \"4eff6a60-4889-11e6-b4ad-095b9e1284ce\"}","url":"/session/4eff6a60-4889-11e6-b4ad-095b9e1284ce/url","urlParsed":{"anchor":"","query":"","file":"url","directory":"/session/4eff6a60-4889-11e6-b4ad-095b9e1284ce/","path":"/session/4eff6a60-4889-11e6-b4ad-095b9e1284ce/url","relative":"/session/4eff6a60-4889-11e6-b4ad-095b9e1284ce/url","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/session/4eff6a60-4889-11e6-b4ad-095b9e1284ce/url","queryKey":{},"chunks":["session","4eff6a60-4889-11e6-b4ad-095b9e1284ce","url"]}} 
app_1   | 

:

def get_page(url): 
    driver = webdriver.Remote(
     command_executor='http://haproxy:8910', 
     desired_capabilities=DesiredCapabilities.PHANTOMJS 
    ) 

    driver.get(url) 
    source = driver.page_source 
    driver.close() 

    return source 

이 코드를 실행하려고하면 내가 오류는 이것이다 : 여기

이 서비스에 연결 내 파이썬 응용 프로그램 코드의 관련 조각의 로드 균형 조정이 작동합니까? 내가 뭘 놓치고 있니?

UPDATE

은 내가 haproxy에서 세션 관리의 일종이 필요하다는 것을 알아 냈다. 셀레늄 webdriver와 phantomjs는 세션을 통해 통신합니다. 클라이언트는 POST /session을 전송하고 본문의 세션 ID와 함께 회신을 수신합니다. 세션이 진행

{"sessionId":"5a27f2b0-48a5-11e6-97d7-7f5820fc7aa6","status":0,"value":{"browserName":"phantomjs","version":"2.1.1","driverName":"ghostdriver","driverVersion":"1.2.0","platform":"linux-unknown-64bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"}}} 

그런 다음, 세션 ID가 후속 요청의 URI의 일부로 서버에 전송되고, 같은 GET /session/5a27f2b0-48a5-11e6-97d7-7f5820fc7aa6/source 등 : 그 대답은 다음과 같이 보입니다. haproxy의 끈적 세션을 위해이 자료를 사용하려면 어떻게해야합니까?

+0

FWIW, 내 phantomjs 서버에 여러 개의 연결을 만드는 응용 프로그램 인스턴스가 하나만 있기 때문에 세션을 요청한 IP 주소에 연결하는 것은 유용하지 않습니다. – DynamiteReed

+0

해결책을 찾았습니까? –

+0

불행히도, 나는하지 않았다. – DynamiteReed

답변

0

당신은 haproxy 설정 자체 내에서 쿠키를 추가 할 수 있어야 ..

cookie SERVERID insert indirect nocache 
server httpd1 10.0.0.19:9443 cookie httpd1 check 
server httpd2 10.0.0.18:9443 cookie httpd2 check 

그런 다음 세션이 haproxy 자체를 통해 부착됩니다.

+0

불행히도이 경우 클라이언트는 쿠키를 존중하지 않습니다. 표준 브라우저는 아니며 Python의 셀레늄 웹 드라이브 라이브러리입니다. RPC API는 쿠키를 사용하지 않기 때문에 실제로 라이브러리가 쿠키를 존중할 이유가 없습니다. – DynamiteReed

+0

방금 ​​RPC 코드에서 쿠키를 지원하기 위해 셀렌 코드베이스에 대한 티켓을 제출했습니다. 그러면이 문제가 매우 쉽게 해결됩니다. https://github.com/SeleniumHQ/selenium/issues/2505 – DynamiteReed

관련 문제