主页 > 手机  > 

Hyperf使用配置中心-nacos配置中心


安装

composer require hyperf/config-center composer require hyperf/config-nacos

配置

配置 config/autoload/service.php

<?php return [ 'enable' => [ // 开启服务发现 'discovery' => true, // 开启服务注册 'register' => true, ], // 服务消费者相关配置 'consumers' => [], // 服务提供者相关配置 'providers' => [], // 服务驱动相关配置 'drivers' => [ 'consul' => [ 'uri' => 'http://127.0.0.1:8500', 'token' => '', 'check' => [ 'deregister_critical_service_after' => '90m', 'interval' => '1s', ], ], 'nacos' => [ // nacos server url like https://nacos.hyperf.io, Priority is higher than host:port // 'url' => '', // The nacos host info 'host' => '127.0.0.21, 'port' => 8848, // The nacos account info 'username' => null, 'password' => null, 'guzzle' => [ 'config' => null, ], 'group_name' => 'api', 'namespace_id' => '111123', 'heartbeat' => 5, 'ephemeral' => false, // 是否注册临时实例 ], ], ];

config/autoload/nacos.php

同样配置服务器信息。

配置config center

<?php declare(strict_types=1); /** * This file is part of Hyperf. * * @link https://www.hyperf.io * @document https://hyperf.wiki * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ use Hyperf\ConfigCenter\Mode; return [ 'enable' => (bool) env('CONFIG_CENTER_ENABLE', true), 'driver' => env('CONFIG_CENTER_DRIVER', 'nacos'), 'mode' => env('CONFIG_CENTER_MODE', Mode::PROCESS), 'drivers' => [ 'nacos' => [ 'driver' => Hyperf\ConfigNacos\NacosDriver::class, 'merge_mode' => Hyperf\ConfigNacos\Constants::CONFIG_MERGE_OVERWRITE, 'interval' => 3, 'default_key' => 'nacos_config', 'listener_config' => [ // dataId, group, tenant, type, content 'nacos_config' => [ 'tenant' => 'public', // corresponding with service.namespaceId 'data_id' => 'test', 'group' => 'DEFAULT_GROUP', ], 'nacos_config.data' => [ 'data_id' => 'test', 'group' => 'DEFAULT_GROUP', 'type' => 'txt', ], ], ], ], ];

这个配置描述的是nacos 上配置管理中 data_id 叫test, 类型是txt,命名空间是DEFAULT_GROUP。

nacos_config 与 nocos_config.data关联,这里租户 (tenant) 和命名空间(namespace)是一样的,里面自己看创建时group是填的什么。

使用

config('nacos_config') 可以获取nacos_config对应的配置

标签:

Hyperf使用配置中心-nacos配置中心由讯客互联手机栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“Hyperf使用配置中心-nacos配置中心