php nacos服務注冊與發現


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'

通過App\Console\Commands\NacosRegisterInstance.php文件進行注冊

<?php namespace App\Console\Commands; use alibaba\nacos\NacosConfig; use alibaba\nacos\Naming; use Illuminate\Console\Command; 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

通過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'

通過App\Console\Commands\NacosGetInstance.php文件進行實例發現

<?phpnamespace App\Console\Commands;use alibaba\nacos\NacosConfig;use alibaba\nacos\Naming;use alibaba\nacos\NamingClient;use Illuminate\Console\Command;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

 

 

 

 

 

 

通過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'

通過App\Console\Commands\NacosDeleteInstance.php文件進行實例發現

<?phpnamespace App\Console\Commands;use alibaba\nacos\NacosConfig;use alibaba\nacos\Naming;use alibaba\nacos\NamingClient;use alibaba\nacos\NamingConfig;use Illuminate\Console\Command;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

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={}'

通過App\Console\Commands\NacosUpdateInstance.php文件進行實例發現

<?phpnamespace App\Console\Commands;use alibaba\nacos\NacosConfig;use alibaba\nacos\Naming;use alibaba\nacos\NamingClient;use alibaba\nacos\NamingConfig;use Illuminate\Console\Command;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

5 關閉服務器

5.1 Linux/Unix/Mac

sh shutdown.sh

5.2 Windows

shutdown.cmd

或者雙擊shutdown.cmd運行文件。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM