Authored by 姜枫

准备灰度测试

... ... @@ -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;
... ...
... ... @@ -94,6 +94,29 @@ class Cache
}
/**
* 直接查询缓存,不对key做任何处理
*/
public static function getBy($key, $node = 'master')
{
$result = array();
try {
// WINDOWS
if (DIRECTORY_SEPARATOR === '\\') {
$result = HoodCache::Memcache($node)->getBy($key);
}
// LINUX
else {
$result = HoodCache::Memcached($node)->getBy($key);
}
} catch (Exception $e) {
$result = array();
}
return $result;
}
/**
* 清除缓存
*
* @param string $key 键名
... ...
... ... @@ -83,6 +83,29 @@ class Cache
}
/**
* 直接查询缓存,不对key做任何处理
*/
public static function getBy($key, $node = 'master')
{
$result = array();
try {
// WINDOWS
if (DIRECTORY_SEPARATOR === '\\') {
$result = HoodCache::Memcache($node)->getBy($key);
}
// LINUX
else {
$result = HoodCache::Memcached($node)->getBy($key);
}
} catch (Exception $e) {
$result = array();
}
return $result;
}
/**
* 清除缓存
*
* @param string $key 键名
... ...
... ... @@ -122,6 +122,11 @@ class Memcache extends Root implements CacheInterface
return $this->init()->get($this->_makeTag() . $key, $flags);
}
public function getBy($key, $cacheCb = null, &$casToken = null)
{
return $this->init()->get($key, $cacheCb, $casToken);
}
/**
* 向一个新的key下面增加一个元素
* @param $key
... ...
... ... @@ -167,6 +167,11 @@ class Memcached extends Root implements CacheInterface
return $this->init()->get($this->_makeTag() . $key, $cacheCb, $casToken);
}
public function getBy($key, $cacheCb = null, &$casToken = null)
{
return $this->init()->get($key, $cacheCb, $casToken);
}
/**
* 向一个新的key下面增加一个元素
* @param $key
... ...
[memcached]
master.hosts=127.0.0.1:11212,127.0.0.1:11213
slave.hosts=127.0.0.1:11212,127.0.0.1:11213
session.hosts=127.0.0.1:11212,127.0.0.1:11213
master.hosts=127.0.0.1:12111
slave.hosts=127.0.0.1:12111
session.hosts=127.0.0.1:12111
[redis]
servers.hosts = 127.0.0.1:6379
\ No newline at end of file
... ...
... ... @@ -12,7 +12,7 @@ defined('APPLICATION_ENV') || define('APPLICATION_ENV', 'preview');
define('EXHIBITION_TICKET', 51335912);//展览票skn
define('USE_INTER_FACE_SHUNT', false);//分流开关
define('USE_INTER_FACE_SHUNT', true);//分流开关
define('API_URL', 'http://api.yoho.yohoops.org/');
define('SERVICE_URL', 'http://service.yoho.yohoops.org/');
... ...
... ... @@ -12,7 +12,7 @@ defined('APPLICATION_ENV') || define('APPLICATION_ENV', 'production');
define('EXHIBITION_TICKET', 51335912);//展览票skn
define('USE_INTER_FACE_SHUNT', false);//分流开关
define('USE_INTER_FACE_SHUNT', true);//分流开关
define('API_URL', 'http://api.yoho.yohoops.org/');
define('SERVICE_URL', 'http://service.yoho.yohoops.org/');
... ...