...
|
...
|
@@ -11,7 +11,9 @@ |
|
|
|
|
|
namespace Api;
|
|
|
|
|
|
use Plugin\Cache;
|
|
|
use WebPlugin\Cache;
|
|
|
use WebPlugin\UdpLog;
|
|
|
|
|
|
class Yohobuy
|
|
|
{
|
|
|
|
...
|
...
|
@@ -153,13 +155,20 @@ class Yohobuy |
|
|
$urlBack = $url;
|
|
|
|
|
|
if (USE_INTER_FACE_SHUNT) {
|
|
|
if (strpos($url, 'api.')) {
|
|
|
if (strpos($url, 'api')) {
|
|
|
$isApi = true;
|
|
|
$url = self::_getUrl($url, $data);
|
|
|
} else if (strpos($url, 'service.')) {
|
|
|
} else if (strpos($url, 'service')) {
|
|
|
$isService = true;
|
|
|
$url = self::_getUrl($url, $data);
|
|
|
}
|
|
|
|
|
|
//$url 返回为空,走原先的url
|
|
|
if (empty($url)) {
|
|
|
$isApi = false;
|
|
|
$isService = false;
|
|
|
$url = $urlBack;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 销毁私钥参数
|
...
|
...
|
@@ -171,6 +180,8 @@ class Yohobuy |
|
|
$url = self::httpBuildQuery($url, $data);
|
|
|
} //echo $url;
|
|
|
|
|
|
UdpLog::debug('get shut api', $url);
|
|
|
|
|
|
/* 开启缓存的情况 */
|
|
|
if ($cache && USE_CACHE) {
|
|
|
// 先尝试获取一级缓存(master), 有数据则直接返回.
|
...
|
...
|
@@ -265,6 +276,8 @@ class Yohobuy |
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
|
|
|
}
|
|
|
|
|
|
UdpLog::debug('call api: ', $url);
|
|
|
|
|
|
$result = curl_exec($ch);
|
|
|
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
|
|
|
if (!$returnJson && !empty($result)) {
|
...
|
...
|
@@ -306,13 +319,20 @@ class Yohobuy |
|
|
$urlBack = $url;
|
|
|
|
|
|
if (USE_INTER_FACE_SHUNT) {
|
|
|
if (strpos($url, 'api.')) {
|
|
|
if (strpos($url, 'api')) {
|
|
|
$isApi = true;
|
|
|
$url = self::_getUrl($url, $data);
|
|
|
} else if (strpos($url, 'service.')) {
|
|
|
} else if (strpos($url, 'service')) {
|
|
|
$isService = true;
|
|
|
$url = self::_getUrl($url, $data);
|
|
|
}
|
|
|
|
|
|
//$url 返回为空,走原先的url
|
|
|
if (empty($url)) {
|
|
|
$isApi = false;
|
|
|
$isService = false;
|
|
|
$url = $urlBack;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
$ch = curl_init($url);
|
...
|
...
|
@@ -353,6 +373,9 @@ class Yohobuy |
|
|
$result = curl_exec($ch);
|
|
|
|
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
|
|
UdpLog::debug('post api', array('url' => $url, 'httpCode' => $httpCode));
|
|
|
|
|
|
//如果分流执行失败,则走原来的逻辑
|
|
|
if (USE_INTER_FACE_SHUNT && $httpCode !== 200) {
|
|
|
curl_close($ch);
|
...
|
...
|
@@ -399,6 +422,9 @@ class Yohobuy |
|
|
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
|
|
|
}
|
|
|
|
|
|
UdpLog::debug('post fail api', $url);
|
|
|
|
|
|
$result = curl_exec($ch);
|
|
|
if (!$returnJson && !empty($result)) {
|
|
|
$result = json_decode($result, true);
|
...
|
...
|
@@ -678,34 +704,90 @@ class Yohobuy |
|
|
*/
|
|
|
private static function _getUrl($url, $param)
|
|
|
{
|
|
|
if (USE_INTER_FACE_SHUNT === false) {
|
|
|
return $url;
|
|
|
if (USE_CACHE === false || USE_INTER_FACE_SHUNT === false) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
$uid = 0;
|
|
|
$mod = 0;
|
|
|
|
|
|
if (isset($param['uid'])) {
|
|
|
$uid = intval($param['uid']);
|
|
|
$mod = $uid % 1024;
|
|
|
UdpLog::debug('read memcache');
|
|
|
$interfaceShunt = Cache::getBy('INTERFACE_SHUT_KEY', 'master');
|
|
|
|
|
|
UdpLog::debug('read memcahe: ', $interfaceShunt);
|
|
|
if (empty($interfaceShunt)) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if ($mod > 128 || $uid === 0) {
|
|
|
$paseUrl = parse_url($url);
|
|
|
if (strpos($url, 'api.')) {
|
|
|
$url = 'http://' . self::$interfaceShunt['awsServers']['api'] . rtrim($paseUrl['path'], '/') . '/';
|
|
|
} else if (strpos($url, 'service.')) {
|
|
|
$url = 'http://' . self::$interfaceShunt['awsServers']['service'] . rtrim($paseUrl['path'], '/') . '/';
|
|
|
$interfaceShunt = json_decode($interfaceShunt, true);
|
|
|
|
|
|
if (empty($interfaceShunt['cloud'])) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
$uid = isset($param['uid']) ? intval($param['uid']) : 0;
|
|
|
$mod = 0;
|
|
|
$cloud = intval($interfaceShunt['cloud']);
|
|
|
$ips = '';
|
|
|
|
|
|
switch ($cloud) {
|
|
|
case 1:
|
|
|
$ips = $interfaceShunt['aws'];
|
|
|
break;
|
|
|
case 2:
|
|
|
$ips = $interfaceShunt['tencent'];
|
|
|
break;
|
|
|
case 3:
|
|
|
if (empty($uid) || empty($interfaceShunt['strategy']) ||
|
|
|
empty($interfaceShunt['strategy']['model']) ||
|
|
|
empty($interfaceShunt['strategy']['tencentMax'])) {
|
|
|
break;
|
|
|
} else {
|
|
|
$ips = $uid % $interfaceShunt['strategy']['model'] > $interfaceShunt['strategy']['tencentMax'] ?
|
|
|
$interfaceShunt['aws'] : $interfaceShunt['tencent'];
|
|
|
}
|
|
|
break;
|
|
|
default:
|
|
|
$ips = '';
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
//未登录情况下
|
|
|
if (empty($uid) && empty($ips) && !empty($interfaceShunt['strategy']) &&
|
|
|
!empty($interfaceShunt['strategy']['notLogin'])) {
|
|
|
$ips = $interfaceShunt[$interfaceShunt['strategy']['notLogin']];
|
|
|
}
|
|
|
|
|
|
if (empty($ips)) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
$paseUrl = parse_url($url);
|
|
|
if (strpos($url, 'api.')) {
|
|
|
$num = array_rand(self::$interfaceShunt['tencentServers']['api']);
|
|
|
$url = 'http://' . self::$interfaceShunt['tencentServers']['api'][$num] . rtrim($paseUrl['path'], '/') . '/';
|
|
|
} else if (strpos($url, 'service.')) {
|
|
|
$num = array_rand(self::$interfaceShunt['tencentServers']['service']);
|
|
|
$url = 'http://' . self::$interfaceShunt['tencentServers']['service'][$num] . rtrim($paseUrl['path'], '/') . '/';
|
|
|
if (strpos($url, 'api')) {
|
|
|
|
|
|
if (empty($ips['api.yoho.cn'])) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
$num = array_rand($ips['api.yoho.cn']);
|
|
|
|
|
|
if (empty($ips['api.yoho.cn'][$num]['ip'])) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
$url = 'http://' . $ips['api.yoho.cn'][$num]['ip'] . rtrim($paseUrl['path'], '/') . '/';
|
|
|
|
|
|
} else if (strpos($url, 'service')) {
|
|
|
|
|
|
if (empty($ips['service.yoho.cn'])) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
$num = array_rand($ips['service.yoho.cn']);
|
|
|
|
|
|
if (empty($ips['service.yoho.cn'][$num]['ip'])) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
$url = 'http://' . $ips['service.yoho.cn'][$num]['ip'] . rtrim($paseUrl['path'], '/') . '/';
|
|
|
}
|
|
|
|
|
|
return $url;
|
...
|
...
|
|