简体   繁体   English

使用haproxy和php websocket执行查询

[英]executing queries using haproxy and php websocket

I'm developing load balancing php app using HAproxy and php websocket a link !. 我正在使用HAproxy和php websocket 链接开发负载平衡php应用程序! Users login in to my app (every has own session) and they send form which triggers Execution of mysql queries. 用户登录到我的应用程序(每个会话都有自己的会话),然后他们发送触发mysql查询执行的表单。

I created backend as three instances of websocket server, which run on different IP addresses and ports in background. 我将后端创建为websocket服务器的三个实例,这些实例在后台运行在不同的IP地址和端口上。

Class server 类服务器

class server {
...
}
        $server1=new server('127.0.0.2','9001');
        $server2=new server('127.0.0.3','9002');
        $server3= new server('127.0.0.4','9003');  

        $server1->run(); // calling function run from library
        $server2->run();
        $server3->run();

How can I ensure,that mysql queries will be processed by php websockets ? 我如何确保mysql查询将由php websockets处理?

Problem is that tasks (performing queries) are not distributed among this servers (I don't see the changes in statistics report). 问题是任务(执行查询)未在该服务器之间分配(我看不到统计报告中的更改)。 Of course, I configure properly my haproxy config file.I don't think that haproxy cannot catch my http request an process it. 当然,我正确配置了haproxy配置文件,我认为haproxy无法捕获我的http请求对其进行处理。

HAproxy frontend HAproxy前端

frontend webservers
     bind *:80
     mode http
     default_backend websockets

Haproxy backend Haproxy后端

  backend websockets
      mode http
      balance roundrobin
      server web1 127.0.0.2:9001  check
      server web2 127.0.0.3:9002  check
      server web3 127.0.0.4:9003  check

I check haproxy statistics report and I see that haproxy knows about this servers. 我查看了haproxy统计报告,发现haproxy知道此服务器。

It is possible to use websockets for this ? 可以使用websockets吗? or better solution is node.js ? 还是更好的解决方案是node.js? I didn't see any similiar solution with php websocket. 我没有用php websocket看到任何类似的解决方案。 Can php websocket communicate with DB? php websocket可以与DB通信吗? If you know some articles about it... Let me know. 如果您知道一些有关此的文章,请告诉我。

Thanks for your answer. 感谢您的回答。

In this case, you need 3 different scripts running at the same time. 在这种情况下,您需要同时运行3个不同的脚本。 Preferably on 3 different machines, otherwise there's no point to load balancing. 最好是在3台不同的机器上,否则就没有负载平衡的意义。

server1.php: server1.php:

<?php
require_once('./websockets.php');
class FirstServer extends WebSocketServer { ... }

$server = new FirstServer('127.0.0.2', '9001');
$server->run();

server2.php: server2.php:

<?php
require_once('./websockets.php');
class SecondServer extends WebSocketServer { ... }

$server = new SecondServer('127.0.0.3', '9002');
$server->run();

server3.php: server3.php:

<?php
require_once('./websockets.php');
class ThirdServer extends WebSocketServer { ... }

$server = new ThirdServer('127.0.0.4', '9003');
$server->run();

Then, at your command line, do the following: 然后,在您的命令行中,执行以下操作:

user@web1$ nohup php /path/to/server1.php > /dev/null 2>&1 &
user@web1$ ssh user@web2 "nohup php /path/to/server2.php > /dev/null 2>&1 &"
user@web1$ ssh user@web3 "nohup php /path/to/server3.php > /dev/null 2>&1 &"

A little bit of commentary: 一点评论:

$server = new Server('127.0.0.4', '9003'); should probably be $server = new Server('0.0.0.0', '9003'); 应该可能是$server = new Server('0.0.0.0', '9003'); unless you have a reason (it doesn't even have to be a good reason... just any reason) to not listen on all IP addresses for that machine. 除非您有一个理由(甚至没有必要是一个很好的理由……也没有任何理由)不要监听该计算机的所有IP地址。 That is, every machine has 127.0.0.1, as well as an address for each configured network interface. 也就是说,每台机器都有127.0.0.1,以及每个配置的网络接口的地址。 Most people want to listen on all addresses. 大多数人都想听所有地址。

I suspect that there's absolutely nothing wrong with your connection to the MySQL server. 我怀疑与MySQL服务器的连接绝对没有错。 The issue is that $server1->run() never returns, so never executes the next line, $server2->run() . 问题是$server1->run()从不返回,因此从不执行下一行$server2->run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM