mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-04 12:47:55 +08:00
Format code.
This commit is contained in:
parent
a9c41928c7
commit
bdbfa6723e
@ -1,10 +1,14 @@
|
||||
<?php
|
||||
/**
|
||||
* Nacos doc
|
||||
*
|
||||
* @link https://nacos.io/zh-cn/docs/open-api.html
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
return [
|
||||
'host' => '127.0.0.1',
|
||||
'port' => '8848',
|
||||
@ -45,5 +49,5 @@ return [
|
||||
],
|
||||
],
|
||||
// 负载策略 random, RoundRobin, WeightedRandom, WeightedRoundRobin
|
||||
'loadBalancer' => 'random'
|
||||
'loadBalancer' => 'random',
|
||||
];
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos\Config;
|
||||
|
||||
use Hyperf\Nacos\Util\RemoteConfig;
|
||||
@ -40,6 +49,6 @@ class FetchConfigProcess extends AbstractProcess
|
||||
|
||||
public function isEnable(): bool
|
||||
{
|
||||
return (bool)config('nacos.configReloadInterval', false);
|
||||
return (bool) config('nacos.configReloadInterval', false);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos\Config;
|
||||
|
||||
use Hyperf\Contract\ConfigInterface;
|
||||
|
@ -1,7 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos\Config;
|
||||
|
||||
class PipeMessage
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos;
|
||||
|
||||
use Hyperf\Nacos\Config\FetchConfigProcess;
|
||||
|
@ -1,19 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
use Hyperf\Utils\ApplicationContext;
|
||||
|
||||
if (!function_exists('container')) {
|
||||
if (! function_exists('container')) {
|
||||
function container(string $id)
|
||||
{
|
||||
return ApplicationContext::getContainer()->get($id);
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('is_json_str')) {
|
||||
if (! function_exists('is_json_str')) {
|
||||
function is_json_str($string)
|
||||
{
|
||||
json_decode($string);
|
||||
return (json_last_error() == JSON_ERROR_NONE);
|
||||
return json_last_error() == JSON_ERROR_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
@ -34,6 +43,5 @@ function xml2array($xml_string)
|
||||
return [];
|
||||
}
|
||||
|
||||
return (array)@simplexml_load_string($xml_string, 'SimpleXMLElement', LIBXML_NOCDATA);
|
||||
return (array) @simplexml_load_string($xml_string, 'SimpleXMLElement', LIBXML_NOCDATA);
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos\Lib;
|
||||
|
||||
use Hyperf\Nacos\Util\Guzzle;
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos\Lib;
|
||||
|
||||
use Hyperf\Nacos\Model\ConfigModel;
|
||||
@ -14,7 +23,7 @@ class NacosConfig extends AbstractNacos
|
||||
public function set(ConfigModel $configModel)
|
||||
{
|
||||
$headers = [
|
||||
'Content-Type' => 'application/x-www-form-urlencoded'
|
||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||
];
|
||||
|
||||
return $this->request('POST', '/nacos/v1/cs/configs', $configModel->toArray(), $headers);
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos\Lib;
|
||||
|
||||
use Hyperf\LoadBalancer\Node;
|
||||
@ -9,7 +18,6 @@ use Hyperf\LoadBalancer\WeightedRandom;
|
||||
use Hyperf\LoadBalancer\WeightedRoundRobin;
|
||||
use Hyperf\Nacos\Model\InstanceModel;
|
||||
use Hyperf\Nacos\Model\ServiceModel;
|
||||
use Hyperf\Utils\Arr;
|
||||
|
||||
class NacosInstance extends AbstractNacos
|
||||
{
|
||||
@ -50,7 +58,7 @@ class NacosInstance extends AbstractNacos
|
||||
{
|
||||
$list = $this->list($serviceModel, $clusters, true);
|
||||
$instance = $list['hosts'] ?? [];
|
||||
if (!$instance) {
|
||||
if (! $instance) {
|
||||
return false;
|
||||
}
|
||||
$enabled = array_filter($instance, function ($item) {
|
||||
@ -62,6 +70,34 @@ class NacosInstance extends AbstractNacos
|
||||
return $this->loadBalancer($enabled, $tactics);
|
||||
}
|
||||
|
||||
public function detail(InstanceModel $instanceModel)
|
||||
{
|
||||
return $this->request('GET', "/nacos/v1/ns/instance?{$instanceModel}");
|
||||
}
|
||||
|
||||
public function beat(ServiceModel $serviceModel, InstanceModel $instanceModel)
|
||||
{
|
||||
$serviceName = $serviceModel->serviceName;
|
||||
$groupName = $serviceModel->groupName;
|
||||
$ephemeral = $instanceModel->ephemeral;
|
||||
$params = array_filter(compact('serviceName', 'beat', 'groupName', 'ephemeral'), function ($item) {
|
||||
return $item !== null;
|
||||
});
|
||||
$params['beat'] = $instanceModel->toJson();
|
||||
$params_str = http_build_query($params);
|
||||
|
||||
return $this->request('PUT', "/nacos/v1/ns/instance/beat?{$params_str}");
|
||||
}
|
||||
|
||||
public function upHealth(InstanceModel $instanceModel)
|
||||
{
|
||||
if ($instanceModel->healthy === null) {
|
||||
$instanceModel->healthy = true;
|
||||
}
|
||||
|
||||
return $this->request('PUT', "/nacos/v1/ns/health/instance?{$instanceModel}") == 'ok';
|
||||
}
|
||||
|
||||
protected function loadBalancer($nodes, $tactics = 'random')
|
||||
{
|
||||
$load_nodes = [];
|
||||
@ -92,32 +128,4 @@ class NacosInstance extends AbstractNacos
|
||||
|
||||
return $nacos_nodes["{$select->host}:{$select->port}"];
|
||||
}
|
||||
|
||||
public function detail(InstanceModel $instanceModel)
|
||||
{
|
||||
return $this->request('GET', "/nacos/v1/ns/instance?{$instanceModel}");
|
||||
}
|
||||
|
||||
public function beat(ServiceModel $serviceModel, InstanceModel $instanceModel)
|
||||
{
|
||||
$serviceName = $serviceModel->serviceName;
|
||||
$groupName = $serviceModel->groupName;
|
||||
$ephemeral = $instanceModel->ephemeral;
|
||||
$params = array_filter(compact('serviceName', 'beat', 'groupName', 'ephemeral'), function ($item) {
|
||||
return $item !== null;
|
||||
});
|
||||
$params['beat'] = $instanceModel->toJson();
|
||||
$params_str = http_build_query($params);
|
||||
|
||||
return $this->request('PUT', "/nacos/v1/ns/instance/beat?{$params_str}");
|
||||
}
|
||||
|
||||
public function upHealth(InstanceModel $instanceModel)
|
||||
{
|
||||
if ($instanceModel->healthy === null) {
|
||||
$instanceModel->healthy = true;
|
||||
}
|
||||
|
||||
return $this->request('PUT', "/nacos/v1/ns/health/instance?{$instanceModel}") == 'ok';
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos\Lib;
|
||||
|
||||
class NacosOperator extends AbstractNacos
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos\Lib;
|
||||
|
||||
use Hyperf\Nacos\Model\ServiceModel;
|
||||
|
@ -1,4 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos\Listener;
|
||||
|
||||
use Hyperf\Contract\ConfigInterface;
|
||||
@ -22,7 +32,7 @@ class BootAppConfListener implements ListenerInterface
|
||||
|
||||
public function process(object $event)
|
||||
{
|
||||
if (!config('nacos')) {
|
||||
if (! config('nacos')) {
|
||||
return;
|
||||
}
|
||||
$logger = container(LoggerFactory::class)->get('nacos');
|
||||
@ -32,11 +42,10 @@ class BootAppConfListener implements ListenerInterface
|
||||
$instance = make(ThisInstance::class);
|
||||
/** @var NacosInstance $nacos_instance */
|
||||
$nacos_instance = make(NacosInstance::class);
|
||||
if (!$nacos_instance->register($instance)) {
|
||||
if (! $nacos_instance->register($instance)) {
|
||||
throw new \Exception("nacos register instance fail: {$instance}");
|
||||
} else {
|
||||
$logger->info('nacos register instance success!', compact('instance'));
|
||||
}
|
||||
$logger->info('nacos register instance success!', compact('instance'));
|
||||
|
||||
// 注册服务
|
||||
/** @var NacosService $nacos_service */
|
||||
@ -44,11 +53,10 @@ class BootAppConfListener implements ListenerInterface
|
||||
/** @var ServiceModel $service */
|
||||
$service = make(ServiceModel::class, ['config' => config('nacos.service')]);
|
||||
$exist = $nacos_service->detail($service);
|
||||
if (!$exist && !$nacos_service->create($service)) {
|
||||
if (! $exist && ! $nacos_service->create($service)) {
|
||||
throw new \Exception("nacos register service fail: {$service}");
|
||||
} else {
|
||||
$logger->info('nacos register service success!', compact('instance'));
|
||||
}
|
||||
$logger->info('nacos register service success!', compact('instance'));
|
||||
|
||||
$remote_config = RemoteConfig::get();
|
||||
/** @var \Hyperf\Config\Config $config */
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos\Listener;
|
||||
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
@ -21,11 +30,11 @@ class OnShutdownListener implements ListenerInterface
|
||||
|
||||
public function process(object $event)
|
||||
{
|
||||
if (!config('nacos')) {
|
||||
if (! config('nacos')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!config('nacos.deleteServiceWhenShutdown', false)) {
|
||||
if (! config('nacos.deleteServiceWhenShutdown', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos\Model;
|
||||
|
||||
abstract class AbstractModel
|
||||
@ -15,6 +24,11 @@ abstract class AbstractModel
|
||||
}
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return http_build_query($this->getParams());
|
||||
}
|
||||
|
||||
public function getParams()
|
||||
{
|
||||
$params = array_filter(get_object_vars($this), function ($item) {
|
||||
@ -25,17 +39,12 @@ abstract class AbstractModel
|
||||
sort($this->required_field);
|
||||
sort($intersect);
|
||||
if ($intersect !== $this->required_field) {
|
||||
throw new \Exception("缺少关键信息" . implode(',', $this->required_field));
|
||||
throw new \Exception('缺少关键信息' . implode(',', $this->required_field));
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return http_build_query($this->getParams());
|
||||
}
|
||||
|
||||
public function toJson()
|
||||
{
|
||||
return json_encode($this->getParams());
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos\Model;
|
||||
|
||||
use Hyperf\Nacos\Lib\NacosConfig;
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos\Model;
|
||||
|
||||
class InstanceModel extends AbstractModel
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos\Model;
|
||||
|
||||
class ServiceModel extends AbstractModel
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos\Process;
|
||||
|
||||
use Hyperf\Logger\LoggerFactory;
|
||||
@ -27,10 +36,9 @@ class InstanceBeatProcess extends AbstractProcess
|
||||
if ($send) {
|
||||
$logger->info('nacos send beat success!', compact('instance'));
|
||||
} else {
|
||||
$logger->error("nacos send beat fail}", compact('instance'));
|
||||
$logger->error('nacos send beat fail}', compact('instance'));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function isEnable(): bool
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos;
|
||||
|
||||
use Hyperf\Nacos\Model\InstanceModel;
|
||||
@ -11,7 +20,7 @@ class ThisInstance extends InstanceModel
|
||||
$this->ip = current(swoole_get_local_ip());
|
||||
$this->port = config('server.servers.0.port');
|
||||
$client = config('nacos.client', []);
|
||||
if (!isset($client['serviceName'])) {
|
||||
if (! isset($client['serviceName'])) {
|
||||
throw new \Exception('nacos.client.serviceName is required');
|
||||
}
|
||||
unset($client['ip'], $client['port']);
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos;
|
||||
|
||||
use Hyperf\Nacos\Model\ServiceModel;
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos\Util;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
@ -9,8 +18,6 @@ use Hyperf\Logger\LoggerFactory;
|
||||
class Guzzle
|
||||
{
|
||||
/**
|
||||
* @param array $config
|
||||
*
|
||||
* @return Client
|
||||
*/
|
||||
public static function create(array $config = [])
|
||||
@ -45,7 +52,7 @@ class Guzzle
|
||||
if ($method == 'POST') {
|
||||
$options['headers']['Content-Type'] = $headers['Content-Type'] ?? 'application/json';
|
||||
if ($options['headers']['Content-Type'] == 'application/json' && $params) {
|
||||
$options['body'] = \GuzzleHttp\json_encode($params ? $params : (object)[]);
|
||||
$options['body'] = \GuzzleHttp\json_encode($params ? $params : (object) []);
|
||||
}
|
||||
if ($options['headers']['Content-Type'] == 'application/x-www-form-urlencoded' && $params) {
|
||||
$options['form_params'] = $params;
|
||||
@ -71,7 +78,7 @@ class Guzzle
|
||||
$logger->get('api_request')->error($api, [
|
||||
'method' => $method,
|
||||
'options' => $options,
|
||||
'exception' => (string)$e,
|
||||
'exception' => (string) $e,
|
||||
]);
|
||||
|
||||
return false;
|
||||
|
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nacos\Util;
|
||||
|
||||
use Hyperf\Nacos\Lib\NacosConfig;
|
||||
|
Loading…
Reference in New Issue
Block a user