From e5d8c9986d369826fba0944a1f08b10b578b453f Mon Sep 17 00:00:00 2001 From: blueo Date: Sat, 7 May 2022 11:50:22 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8DBool=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/alibaba/nacos/util/HttpUtil.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/alibaba/nacos/util/HttpUtil.php b/src/alibaba/nacos/util/HttpUtil.php index c5beb60..ca2ab28 100644 --- a/src/alibaba/nacos/util/HttpUtil.php +++ b/src/alibaba/nacos/util/HttpUtil.php @@ -16,6 +16,13 @@ class HttpUtil { public static function request($verb, $uri, $body = [], $headers = [], $options = []) { + //这个主要是为了解决 http_build_query 会转布尔型转换成0和1的问题 + foreach ($body as &$value){ + if (is_bool($value)) { + $value = $value ? "true" : "false"; + } + } + $httpClient = self::getGuzzle(); $parameterList = [ 'headers' => $headers, From 3181ea29a96d297b62e31a70dbd236d9686123fa Mon Sep 17 00:00:00 2001 From: blueo Date: Sat, 7 May 2022 11:50:34 +0800 Subject: [PATCH 2/3] enabled --- src/alibaba/nacos/Naming.php | 6 +++--- src/alibaba/nacos/NamingClient.php | 4 ++-- .../nacos/request/naming/RegisterInstanceNaming.php | 12 ++++++------ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/alibaba/nacos/Naming.php b/src/alibaba/nacos/Naming.php index d6dd959..05b08bd 100644 --- a/src/alibaba/nacos/Naming.php +++ b/src/alibaba/nacos/Naming.php @@ -38,7 +38,7 @@ public static function init($serviceName, $ip, $port, $namespaceId = "", $weight } /** - * @param bool $enable + * @param bool $enabled * @param bool $healthy * @param string $clusterName * @param string $metadata @@ -48,7 +48,7 @@ public static function init($serviceName, $ip, $port, $namespaceId = "", $weight * @throws exception\RequestVerbRequiredException * @throws exception\ResponseCodeErrorException */ - public function register($enable = true, $healthy = true, $clusterName = "", $metadata = "{}") + public function register($enabled = true, $healthy = true, $clusterName = "", $metadata = "{}") { return NamingClient::register( NamingConfig::getServiceName(), @@ -56,7 +56,7 @@ public function register($enable = true, $healthy = true, $clusterName = "", $me NamingConfig::getPort(), NamingConfig::getWeight(), NamingConfig::getNamespaceId(), - $enable, + $enabled, $healthy, $clusterName, $metadata diff --git a/src/alibaba/nacos/NamingClient.php b/src/alibaba/nacos/NamingClient.php index c5af6ac..846d883 100644 --- a/src/alibaba/nacos/NamingClient.php +++ b/src/alibaba/nacos/NamingClient.php @@ -45,7 +45,7 @@ class NamingClient * @throws RequestVerbRequiredException * @throws ResponseCodeErrorException */ - public static function register($serviceName, $ip, $port, $weight = "", $namespaceId = "", $enable = true, $healthy = true, $clusterName = "", $metadata = "{}") + public static function register($serviceName, $ip, $port, $weight = "", $namespaceId = "", $enabled = true, $healthy = true, $clusterName = "", $metadata = "{}") { $registerInstanceDiscovery = new RegisterInstanceNaming(); $registerInstanceDiscovery->setServiceName($serviceName); @@ -53,7 +53,7 @@ public static function register($serviceName, $ip, $port, $weight = "", $namespa $registerInstanceDiscovery->setPort($port); $registerInstanceDiscovery->setNamespaceId($namespaceId); $registerInstanceDiscovery->setWeight($weight); - $registerInstanceDiscovery->setEnable($enable); + $registerInstanceDiscovery->setEnabled($enabled); $registerInstanceDiscovery->setHealthy($healthy); $registerInstanceDiscovery->setMetadata($metadata); $registerInstanceDiscovery->setClusterName($clusterName); diff --git a/src/alibaba/nacos/request/naming/RegisterInstanceNaming.php b/src/alibaba/nacos/request/naming/RegisterInstanceNaming.php index 0a6514c..a49a800 100644 --- a/src/alibaba/nacos/request/naming/RegisterInstanceNaming.php +++ b/src/alibaba/nacos/request/naming/RegisterInstanceNaming.php @@ -40,7 +40,7 @@ class RegisterInstanceNaming extends NamingRequest * * @var */ - private $enable; + private $enabled; /** * 是否健康 @@ -137,17 +137,17 @@ public function setWeight($weight) /** * @return mixed */ - public function getEnable() + public function getEnabled() { - return $this->enable; + return $this->enabled; } /** - * @param mixed $enable + * @param mixed $enabled */ - public function setEnable($enable) + public function setEnabled($enabled) { - $this->enable = $enable; + $this->enabled = $enabled; } /** From 68c8d9c0f2c67ff4f63a7db60173e716ba765ca4 Mon Sep 17 00:00:00 2001 From: blueo Date: Mon, 9 May 2022 11:51:11 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=BF=83=E8=B7=B3=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/alibaba/nacos/Naming.php | 19 +++- src/alibaba/nacos/NamingClient.php | 6 +- .../request/naming/BeatInstanceNaming.php | 91 +++++++++++++++++++ 3 files changed, 112 insertions(+), 4 deletions(-) diff --git a/src/alibaba/nacos/Naming.php b/src/alibaba/nacos/Naming.php index 05b08bd..53d8cc3 100644 --- a/src/alibaba/nacos/Naming.php +++ b/src/alibaba/nacos/Naming.php @@ -132,21 +132,34 @@ public function listInstances($healthyOnly = false, $namespaceId = "", $clusters /** * @param Instance $instance + * @param bool $lightBeatEnabled * @return model\Beat * @throws ReflectionException * @throws exception\RequestUriRequiredException * @throws exception\RequestVerbRequiredException * @throws exception\ResponseCodeErrorException */ - public function beat(Instance $instance = null) + public function beat(Instance $instance = null, $lightBeatEnabled = true) { if ($instance == null) { $instance = $this->get(); } - return NamingClient::beat( + + $result = NamingClient::beat( NamingConfig::getServiceName(), - $instance->encode() + $instance->getIp(), + $instance->getPort(), + $lightBeatEnabled ? "" : $instance->encode(), //如果是轻量级心跳,不能传beat信息,需要设置为空,否则心跳会失败 + "", + $instance->getClusterName() ); + + //如果轻量级失败,走一次重量级心跳 + if ($result->getCode() == 20404) { + $this->beat($instance, false); + } + + return $result; } /** diff --git a/src/alibaba/nacos/NamingClient.php b/src/alibaba/nacos/NamingClient.php index 846d883..43509ab 100644 --- a/src/alibaba/nacos/NamingClient.php +++ b/src/alibaba/nacos/NamingClient.php @@ -199,10 +199,14 @@ public static function get($serviceName, $ip, $port, $healthyOnly = false, $weig * @throws RequestVerbRequiredException * @throws ResponseCodeErrorException */ - public static function beat($serviceName, $beat) + public static function beat($serviceName, $ip, $port, $beat, $namespaceId = "", $cluster = "") { $beatInstanceDiscovery = new BeatInstanceNaming(); $beatInstanceDiscovery->setServiceName($serviceName); + $beatInstanceDiscovery->setIp($ip); + $beatInstanceDiscovery->setPort($port); + $beatInstanceDiscovery->setNamespaceId($namespaceId); + $beatInstanceDiscovery->setCluster($cluster); $beatInstanceDiscovery->setBeat($beat); $response = $beatInstanceDiscovery->doRequest(); diff --git a/src/alibaba/nacos/request/naming/BeatInstanceNaming.php b/src/alibaba/nacos/request/naming/BeatInstanceNaming.php index aecaf76..1516fe0 100644 --- a/src/alibaba/nacos/request/naming/BeatInstanceNaming.php +++ b/src/alibaba/nacos/request/naming/BeatInstanceNaming.php @@ -14,6 +14,33 @@ class BeatInstanceNaming extends NamingRequest */ private $serviceName; + /** + * IP + * + * @var + */ + private $ip; + + /** + * PORT + * + * @var + */ + private $port; + + /** + * 命名空间ID + * + * @var + */ + private $namespaceId; + + /** + * 集群名称 + * @var + */ + private $cluster; + /** * 实例心跳内容 * @@ -37,6 +64,70 @@ public function setServiceName($serviceName) $this->serviceName = $serviceName; } + /** + * @return mixed + */ + public function getIp() + { + return $this->ip; + } + + /** + * @param mixed $ip + */ + public function setIp($ip) + { + $this->ip = $ip; + } + + /** + * @return mixed + */ + public function getPort() + { + return $this->port; + } + + /** + * @param mixed $port + */ + public function setPort($port) + { + $this->port = $port; + } + + /** + * @return mixed + */ + public function getNamespaceId() + { + return $this->namespaceId; + } + + /** + * @param mixed $namespaceId + */ + public function setNamespaceId($namespaceId) + { + $this->namespaceId = $namespaceId; + } + + /** + * @return mixed + */ + public function getCluster() + { + return $this->cluster; + } + + /** + * @param mixed $cluster + */ + public function setCluster($cluster) + { + $this->cluster = $cluster; + } + /** * @return mixed */