1

我有一个调用 REST 服务器的服务。我正在使用 CURL 发出请求。我们有三个端点用于分配负载。我可以创建一些“随机”选择端点的基本逻辑,但这似乎不是一个“好的”解决方案。我想知道是否有更好的解决方案?

define ("REST_SERVER", "http://myService.myCompany.com:8280");
...
$url = REST_SERVER.URL_SIGN;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Content-Type: application/json',
  'Content-Length: ' . strlen($data_string))
);
$curl_result = curl_exec($ch);
4

1 回答 1

1

如果您正在寻找一个穷人的负载均衡器,您可以将您的端点枚举到一个数组中,调用 shuffle() 和 array_pop() 幸运的赢家。

<?php
$endpoints = array(
   'http://api1.myco.com',
   'http://api2.myco.com'
);

shuffle($endpoints);

define('REST_SERVER', array_pop($endpoints));

// ...
?>

我还建议您在发出 API 调用之前审查每位候选人,以确保其“启动”/“可用”,这超出了本问题的范围。

于 2013-06-03T17:05:34.510 回答