Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复 Bool类型参数转换问题,enabled参数名错误问题, 主动心跳问题 #25

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/alibaba/nacos/Naming.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,15 +48,15 @@ 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(),
NamingConfig::getIp(),
NamingConfig::getPort(),
NamingConfig::getWeight(),
NamingConfig::getNamespaceId(),
$enable,
$enabled,
$healthy,
$clusterName,
$metadata
Expand Down Expand Up @@ -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;
}

/**
Expand Down
10 changes: 7 additions & 3 deletions src/alibaba/nacos/NamingClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ 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);
$registerInstanceDiscovery->setIp($ip);
$registerInstanceDiscovery->setPort($port);
$registerInstanceDiscovery->setNamespaceId($namespaceId);
$registerInstanceDiscovery->setWeight($weight);
$registerInstanceDiscovery->setEnable($enable);
$registerInstanceDiscovery->setEnabled($enabled);
$registerInstanceDiscovery->setHealthy($healthy);
$registerInstanceDiscovery->setMetadata($metadata);
$registerInstanceDiscovery->setClusterName($clusterName);
Expand Down Expand Up @@ -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();
Expand Down
91 changes: 91 additions & 0 deletions src/alibaba/nacos/request/naming/BeatInstanceNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* 实例心跳内容
*
Expand All @@ -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
*/
Expand Down
12 changes: 6 additions & 6 deletions src/alibaba/nacos/request/naming/RegisterInstanceNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class RegisterInstanceNaming extends NamingRequest
*
* @var
*/
private $enable;
private $enabled;

/**
* 是否健康
Expand Down Expand Up @@ -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;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/alibaba/nacos/util/HttpUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down