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运行文件。

 

posted @ 2021-11-04 18:34  joshua317  阅读(1526)  评论(0编辑  收藏  举报