diff --git a/src/alibaba/nacos/Naming.php b/src/alibaba/nacos/Naming.php index d6dd959..53d8cc3 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 @@ -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 c5af6ac..43509ab 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); @@ -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 */ 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; } /** 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,