本文主要是介绍Hyperf 使用配置中心 - nacos配置中心,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
安装
composer require hyperf/config-center
composer require hyperf/config-nacos
配置
配置 config/autoload/service.php
<?phpreturn ['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
<?phpdeclare(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配置中心的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!