php nacos服务注册与发现

13 篇文章 0 订阅

本文为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'

通过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

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'

通过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

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'

通过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

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

通过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

Copy

5 关闭服务器

5.1 Linux/Unix/Mac

sh shutdown.sh

5.2 Windows

shutdown.cmd

或者双击shutdown.cmd运行文件。

本文为joshua317原创文章,转载请注明:转载自joshua317博客 php nacos服务注册与发现 - joshua317的博客

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值