From 0a4ecc50809797a0d9e531a2f5b13121d33d719f Mon Sep 17 00:00:00 2001 From: windhoney <524415250@qq.com> Date: Mon, 31 Aug 2020 14:33:58 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=9B=91=E5=90=AC=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=8F=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Aliyun/ACM/Client.php | 65 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/Aliyun/ACM/Client.php b/Aliyun/ACM/Client.php index 0cbc3a8..c52cf6b 100644 --- a/Aliyun/ACM/Client.php +++ b/Aliyun/ACM/Client.php @@ -25,7 +25,9 @@ class Aliyun_ACM_Client { protected $port; protected $appName; - + + public $errors; + public $serverList = array(); public function __construct($endpoint, $port){ @@ -236,5 +238,66 @@ private function getCommonHeaders($group){ $headers['Spas-Signature'] = base64_encode(hash_hmac('sha1', $signStr, $this->secretKey,true)); return $headers; } + + /** + * 监听配置变化 + * + * @param string $dataId + * @param string $group + * @param string $tenant + * @param string $content + * + * @return false|mixed|string + * @throws \Aliyun_ACM_Exception + * @throws \RequestCore_Exception + * @link https://help.aliyun.com/document_detail/64132.html?spm=a2c4g.11186623.6.577.67395d3eu7PD6q + */ + public function listenConfig($dataId, $group, $tenant, $content) + { + if ( !is_string($this->secretKey) || + !is_string($this->accessKey)) { + throw new Aliyun_ACM_Exception ('Invalid auth string', "invalid auth info for dataId:"); + } + Aliyun_ACM_Util::checkDataId($dataId); + $group = Aliyun_ACM_Util::checkGroup($group); + $servers = $this->serverList; + $singleServer = $servers[array_rand($servers)]; + $acm_host = str_replace(array('host', 'port'), array($singleServer->url, $singleServer->port), + 'http://host:port/diamond-server/config.co'); + $content_md5 = md5($content); + $acm_host .= "?Probe-Modify-Request={$dataId}%02{$group}%02{$content_md5}%02{$tenant}%01"; + $request = new RequestCore(); + $request->method = $request::HTTP_POST; + $request->set_request_url($acm_host); + $headers = $this->getHttpApiHeaders($group); + foreach ($headers as $header_key => $header_val) { + $request->add_header($header_key, $header_val); + } + $request->send_request(true); + if ($request->get_response_code() != '200') { + $this->errors = $request->get_response_body(); + + return false; + } + $response_header = $request->get_response_header(); + + return isset($response_header['probe-modify-response-new']) ? $response_header['probe-modify-response-new'] : ''; + } + + private function getHttpApiHeaders($group) + { + $headers = array(); + $headers['Spas-AccessKey'] = $this->accessKey; + $ts = round(microtime(true) * 1000); + $headers['timeStamp'] = $ts; + $signStr = $this->nameSpace . '+'; + if (is_string($group)) { + $signStr .= $group . "+"; + } + $signStr = $signStr . $ts; + $headers['Spas-Signature'] = base64_encode(hash_hmac('sha1', $signStr, $this->secretKey, true)); + + return $headers; + } } \ No newline at end of file From b16bb7b1f63bf6186c5ff229f7f0641c710502f2 Mon Sep 17 00:00:00 2001 From: windhoney <524415250@qq.com> Date: Mon, 31 Aug 2020 17:06:38 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=9B=91=E5=90=AC=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=8F=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Aliyun/ACM/Client.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Aliyun/ACM/Client.php b/Aliyun/ACM/Client.php index c52cf6b..6503852 100644 --- a/Aliyun/ACM/Client.php +++ b/Aliyun/ACM/Client.php @@ -276,12 +276,11 @@ public function listenConfig($dataId, $group, $tenant, $content) $request->send_request(true); if ($request->get_response_code() != '200') { $this->errors = $request->get_response_body(); - + return false; } - $response_header = $request->get_response_header(); - - return isset($response_header['probe-modify-response-new']) ? $response_header['probe-modify-response-new'] : ''; + + return $request->get_response_body(); } private function getHttpApiHeaders($group) @@ -290,6 +289,7 @@ private function getHttpApiHeaders($group) $headers['Spas-AccessKey'] = $this->accessKey; $ts = round(microtime(true) * 1000); $headers['timeStamp'] = $ts; + $headers['longPullingTimeout'] = 30000; $signStr = $this->nameSpace . '+'; if (is_string($group)) { $signStr .= $group . "+";