Hyperf方案 Apollo配置中心

张开发
2026/4/11 7:57:45 15 分钟阅读

分享文章

Hyperf方案 Apollo配置中心
?php/** * 案例标题Apollo配置中心 * 说明集成Apollo配置中心实现配置热更新不重启服务就能改配置 * 需要安装的包 * composer require hyperf/config-apollo * composer require hyperf/config-center */declare(strict_types1);// config/autoload/config_center.php // Apollo配置中心接入配置return[enabletrue,// 开启配置中心driverapollo,// 用Apollomode\Hyperf\ConfigCenter\Mode::PROCESS,// 独立进程拉取配置不影响业务进程drivers[apollo[driver\Hyperf\ConfigApollo\ApolloDriver::class,serverenv(APOLLO_SERVER,http://apollo-config:8080),// Apollo地址appidenv(APOLLO_APP_ID,hyperf-app),// 在Apollo后台配的应用IDclusterenv(APOLLO_CLUSTER,default),// 集群生产用prodnamespaces[// 要监听的namespace可以多个application,// 默认namespace放通用配置database,// 数据库相关配置redis,// Redis配置],interval5,// 每5秒轮询一次配置变更strict_modefalse,// 非严格模式Apollo不可用时用本地配置],],];// app/Listener/ApolloConfigListener.php // 监听配置变更事件配置更新时执行自定义逻辑namespaceApp\Listener;useHyperf\ConfigCenter\Event\ConfigChanged;useHyperf\Event\Contract\ListenerInterface;usePsr\Log\LoggerInterface;classApolloConfigListenerimplementsListenerInterface{publicfunction__construct(privateLoggerInterface$logger){}publicfunctionlisten():array{return[ConfigChanged::class];// 配置变了就触发这个事件}/** * 配置变更时会跑到这里可以做连接池重置、告警通知等操作 */publicfunctionprocess(object$event):void{/** var ConfigChanged $event */$this-logger-info(Apollo配置已更新,[key$event-key,// 哪个key变了newValue$event-newValue,// 新值是啥oldValue$event-oldValue,// 旧值是啥]);// 根据变更的key做不同的处理if(str_starts_with($event-key,database.)){// 数据库配置变了记一下日志就行连接池会自动重建$this-logger-warning(数据库配置已变更下次获取连接时生效);}}}// app/Service/DynamicConfigService.php namespaceApp\Service;useHyperf\Contract\ConfigInterface;/** * 动态配置服务从Apollo拉下来的配置通过这里读取 * 不要缓存配置值每次都从ConfigInterface读这样Apollo更新后立即生效 */classDynamicConfigService{publicfunction__construct(privateConfigInterface$config){}/** * 获取限流配置运营可以在Apollo后台直接改不用重新部署 */publicfunctiongetRateLimit():int{// Apollo里配 rate_limit.qps 1000这里就能读到最新值return(int)$this-config-get(rate_limit.qps,1000);}/** * 获取功能开关A/B测试或者紧急关停某功能用这个 */publicfunctionisFeatureEnabled(string$feature):bool{// Apollo里配 feature.new_payment true 就开启false就关掉return(bool)$this-config-get(feature.{$feature},false);}/** * 获取白名单用户列表可以在Apollo动态增减 */publicfunctiongetWhitelist():array{$raw$this-config-get(whitelist.user_ids,);// Apollo存成逗号分隔的字符串if(empty($raw))return[];returnarray_map(intval,explode(,,$raw));// 转成int数组}/** * 获取第三方API配置密钥轮换不用重启服务 */publicfunctiongetPaymentConfig():array{return[app_id$this-config-get(payment.alipay.app_id,),private_key$this-config-get(payment.alipay.private_key,),notify_url$this-config-get(payment.alipay.notify_url,),];}}// app/Controller/ConfigDemoController.php namespaceApp\Controller;useApp\Service\DynamicConfigService;useHyperf\HttpServer\Annotation\Controller;useHyperf\HttpServer\Annotation\GetMapping;#[Controller(prefix:/apollo)]classConfigDemoController{publicfunction__construct(privateDynamicConfigService$configService){}/** * GET /apollo/feature/{name} - 检查某个功能开关是否开启 * 在Apollo后台改 feature.{name} 的值这里立即反映 */#[GetMapping(path:/feature/{name})]publicfunctioncheckFeature(string$name):array{$enabled$this-configService-isFeatureEnabled($name);return[feature$name,enabled$enabled,msg$enabled?功能已开启:功能已关闭,];}/** * GET /apollo/rate-limit - 查看当前限流配置 */#[GetMapping(path:/rate-limit)]publicfunctionrateLimit():array{return[qps$this-configService-getRateLimit(),remark在Apollo后台修改 rate_limit.qps 即可动态调整,];}}

更多文章