在PhP5中是否有办法获取我所有的服务器IP地址?

时间:2022-10-06 09:15:45

Basically I want to load balance downloads over 6+ interfaces. My server is running Apache2 and PHP5, currently I have to manually enter all of the servers ip addresses into the php files array. Is there a way to do it automatically?

基本上我想在6+接口上加载平衡下载量。我的服务器运行的是Apache2和PHP5,目前我必须手动将所有服务器的ip地址输入到php文件数组中。有没有办法自动完成?

For each download link the code currently looks something like this:

对于每个下载链接,代码目前看起来像这样:

<?php
$a = array("192.168.2.x", "192.168.2.x", "192.168.2.x", "192.168.2.x", "192.168.2.x", "192.168.2.x"); 
$b = rand(0,count($a)-1);
echo "http://". $a[$b] . "/yourDownload.zip";
?> 

The server machine has multiple IP's, one for each NIC. The situation here is for a large lan party where we want to distribute files from a main server to all the gamers. Each gamer needs around 20GB of files so serious load balancing is not super important but the ability for the server to be plug and play is. Currently I have to manually enter the IP addresses into the php file itself every time we use a different router or DHCP which takes time and is easily forgotten until it becomes an issue.

服务器机器有多个IP,每个NIC一个。这里的情况是针对大型局域网,我们希望将文件从主服务器分发给所有游戏玩家。每个玩家需要大约20GB的文件,因此严重的负载平衡并不是非常重要,但服务器即插即用的能力是。目前,每次我们使用不同的路由器或DHCP时,我都必须手动将IP地址输入到php文件中,这需要花费时间并且很容易被遗忘,直到它成为一个问题。

In the above code, I would like the array in line 2 to automatically get the IPs of the NICs rather than them being hardcoded.

在上面的代码中,我希望第2行中的数组自动获取NIC的IP而不是硬编码。

1 个解决方案

#1


0  

1st of all how you will maintain balance ? using random function it will not work

首先,你将如何保持平衡?使用随机函数它将无法正常工作

Here you have 6 servers so you need to do something work on priority base

这里有6台服务器,因此您需要在优先级基础上做一些工作

code :

<?php

$pages = array('192.168.2.1'=>16.66, '192.168.2.2'=>16.66,'192.168.2.3'=>16.66,'192.168.2.4'=>16.66,'192.168.2.5'=>16.66,'192.168.2.5'=>16.66,'192.168.2.6'=>16.66);


function getServerByWeight(array $weightedValues) {
    $rand = mt_rand(1, (int) array_sum($weightedValues));

    foreach ($weightedValues as $key => $value) {
      $rand -= $value;
      if ($rand <= 0) {
        return $key;
      }
    }
 }


$page = getServerByWeight($pages);


echo "http://".$page. "/yourDownload.zip";

#1


0  

1st of all how you will maintain balance ? using random function it will not work

首先,你将如何保持平衡?使用随机函数它将无法正常工作

Here you have 6 servers so you need to do something work on priority base

这里有6台服务器,因此您需要在优先级基础上做一些工作

code :

<?php

$pages = array('192.168.2.1'=>16.66, '192.168.2.2'=>16.66,'192.168.2.3'=>16.66,'192.168.2.4'=>16.66,'192.168.2.5'=>16.66,'192.168.2.5'=>16.66,'192.168.2.6'=>16.66);


function getServerByWeight(array $weightedValues) {
    $rand = mt_rand(1, (int) array_sum($weightedValues));

    foreach ($weightedValues as $key => $value) {
      $rand -= $value;
      if ($rand <= 0) {
        return $key;
      }
    }
 }


$page = getServerByWeight($pages);


echo "http://".$page. "/yourDownload.zip";