概述
本文为joshua317原创文章,转载请注明:转载自joshua317博客 php nacos服务注册与发现 - joshua317的博客
1 扩展安装
安装grpc、protobuf
2 Laravel项目安装
2.1 指定仓库地址
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
2.2 创建测试项目
composer create-project --prefer-dist laravel/laravel test-service "6.*"
2.3 引入依赖包
composer require alibaba/nacos
2.4 启动服务
php artisan serve
也可以指定host和端口号
php artisan serve --host 127.0.0.2 --port 8001
3 nacos服务安装
3.1 选择版本,进行安装
本示例使用nacos-server-2.0.3版本
3.1.1 windows安装
下载地址
https://github.com/alibaba/nacos/releases/download/2.0.3/nacos-server-2.0.3.zip
3.1.2 类Unix平台安装
wget https://github.com/alibaba/nacos/releases/download/2.0.3/nacos-server-2.0.3.tar.gz
tar -xvf nacos-server-$version.tar.gz
cd nacos/bin
3.2 启动服务
3.2.1 类Unix平台启动
启动命令(standalone代表着单机模式运行,非集群模式):
sh startup.sh -m standalone
如果使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:
bash startup.sh -m standalone
3.2.2 Windows平台启动
启动命令(standalone代表着单机模式运行,非集群模式):
startup.cmd -m standalone
推荐使用下面方式 更改startup.cmd文件,指定单机模式,可以直接双击运。
set MODE="standalone"
3.3 nacos服务访问
http://10.8.0.27:8848/nacos/index.html
初始账号与密码:nacos nacos
4 服务注册、发现
4.1 实例注册
curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=test-service&ip=127.0.0.1&port=8081'
通过AppConsoleCommandsNacosRegisterInstance.php文件进行注册
<?php
namespace AppConsoleCommands;
use alibabanacosNacosConfig;
use alibabanacosNaming;
use IlluminateConsoleCommand;
class NacosRegisterInstance extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'nacos:register:instance';
/**
* The console command description.
*
* @var string
*/
protected $description = 'nacos:register:instance';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
try {
NacosConfig::setHost("http://127.0.0.1:8848/"); // 配置中心地址
$naming = Naming::init(
"test-service",
"127.0.0.1",
"8081",
"",
"",
true
);
$naming->register();
} catch (Exception $exception) {
}
}
}
PHP
Copy
通过php artisan命令执行
php artisan nacos:register:instance
4.2 实例发现
curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instance/list?serviceName=test-service'
通过AppConsoleCommandsNacosGetInstance.php文件进行实例发现
<?phpnamespace AppConsoleCommands;use alibabanacosNacosConfig;use alibabanacosNaming;use alibabanacosNamingClient;use IlluminateConsoleCommand;class NacosGetInstance extends Command{ /** * The name and signature of the console command. * * @var string */ protected $signature = 'nacos:get:instance'; /** * The console command description. * * @var string */ protected $description = 'nacos:get:instance'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { try { NacosConfig::setHost("http://127.0.0.1:8848/"); // 配置中心地址 $naming = Naming::init( "test-service", "", "", "", "", true ); $instances = $naming->listInstances(true); if ($instances->getHosts()) { $hosts = []; foreach ($instances->getHosts() as $v) { $hosts[] = $v->getIp() . ":" . $v->getPort(); } var_dump($hosts); } else { throw new Exception("未发现实例"); } } catch (Exception $exception) { } }}
PHP
Copy
通过php artisan命令执行
php artisan nacos:get:instance
4.3 注销实例
curl -X DELETE 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=test-service&ip=127.0.0.1&port=8081'
通过AppConsoleCommandsNacosDeleteInstance.php文件进行实例发现
<?phpnamespace AppConsoleCommands;use alibabanacosNacosConfig;use alibabanacosNaming;use alibabanacosNamingClient;use alibabanacosNamingConfig;use IlluminateConsoleCommand;class NacosDeleteInstance extends Command{ /** * The name and signature of the console command. * * @var string */ protected $signature = 'nacos:delete:instance'; /** * The console command description. * * @var string */ protected $description = 'nacos:delete:instance'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { try { NacosConfig::setHost("http://127.0.0.1:8848/"); // 配置中心地址 $naming = Naming::init( "test-service", "127.0.0.1", "8081", "", "", true ); $response = $naming->delete(); } catch (Exception $exception) { } }}
PHP
Copy
4.3 修改实例
curl -X PUT '127.0.0.1:8848/nacos/v1/ns/instance?serviceName=test-service&ip=127.0.0.1&port=8081&clusterName=TEST1&weight=8&metadata={}'
通过AppConsoleCommandsNacosUpdateInstance.php文件进行实例发现
<?phpnamespace AppConsoleCommands;use alibabanacosNacosConfig;use alibabanacosNaming;use alibabanacosNamingClient;use alibabanacosNamingConfig;use IlluminateConsoleCommand;class NacosUpdateInstance extends Command{ /** * The name and signature of the console command. * * @var string */ protected $signature = 'nacos:update:instance'; /** * The console command description. * * @var string */ protected $description = 'nacos:update:instance'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { try { NacosConfig::setHost("http://127.0.0.1:8848/"); // 配置中心地址 $naming = Naming::init( "test-service", "127.0.0.1", "8081", "", "0", true ); $naming->update(); } catch (Exception $exception) { } }}
PHP
Copy
5 关闭服务器
5.1 Linux/Unix/Mac
sh shutdown.sh
5.2 Windows
shutdown.cmd
或者双击shutdown.cmd运行文件。
本文为joshua317原创文章,转载请注明:转载自joshua317博客 php nacos服务注册与发现 - joshua317的博客
最后
以上就是安详热狗为你收集整理的php nacos服务注册与发现的全部内容,希望文章能够帮你解决php nacos服务注册与发现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复