本篇文章給大家帶來的內容是關于laravel框架下的配置管理系統的設計過程(附代碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。
項目背景
硬件架構采用Nginx + SLB,應用程式使用 laravel 的 .env 進行配置管理 ,隨著業務的迭代越來越多的配置被寫入 .env 文件,變得越來越臃腫,管理起來也不方便。
按照集群設計,支持分布式擴展,配置中心不可用要保證不影響業務,客戶端使用Redis + File的方式保存 配置 信息。
使用 supervisor 守護進程,支持秒級獲取配置,后續可擴展為消息訂閱。按照集群設計,支持分布式擴展,配置中心不可用要保證不影響業務,客戶端使用Redis + File的方式保存 配置 信息。
使用 supervisor 守護進程,支持秒級獲取配置,后續可擴展為消息訂閱。
架構圖
基于composer開發擴展,配置中心與客戶端通信基于RESTful,系統拆分為2個composer,server 包 + client 包。
server 負責配置管理,client 負責API封裝
UI界面
配置管理
數組支持用.號,支持鍵值使用json
接口數據
客戶端請求接口,最終轉被換成PHP數組。
表設計
多應用
CREATE TABLE `tms_configure_client` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `is_active` tinyint(1) NOT NULL DEFAULT '1' COMMENT '狀態', `app_id` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'APPID', `title` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '名稱', `intro` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '描述', `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), KEY `configure_client_app_id_index` (`app_id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
給每個應用分配一個APPID是很有必要的。
配置分組
CREATE TABLE `tms_configure_group` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `ip` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'ip地址', `title` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '標題', `intro` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '描述', `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
暫時僅支持定義到 APPID + IP 級別配置
配置節點
CREATE TABLE `tms_configure_node` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `app_id` bigint(20) unsigned NOT NULL COMMENT 'APPID', `is_active` tinyint(3) unsigned NOT NULL DEFAULT '1', `version_id` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL, `group_id` bigint(20) unsigned NOT NULL, `skey` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL, `svalue` varchar(2000) COLLATE utf8mb4_unicode_ci NOT NULL, `remark` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), KEY `idx_acitve` (`is_active`,`group_id`), KEY `idx_skey` (`skey`), KEY `configure_node_app_id_is_active_group_id_index` (`app_id`,`is_active`,`group_id`) ) ENGINE=InnoDB AUTO_INCREMENT=102 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
這里我們支持 mysql.port 這種采用.號key的形式,后面最終轉化為php數組。
Composer包
服務端
{ "name": "xxx/xxx", "type": "library", "keywords": ["laravel","php","configure"], "description": "configure-server module", "homepage": "https://github.com/xxx", "license": "MIT", "authors": [ { "name": "OkamiChen", "email": "x25125x@126.com" } ], "require": { "php": ">=7.1.0" }, "autoload": { "psr-4": { "OkamiChenConfigureServer":"src/" } }, "extra": { "laravel": { "providers": [ "OkamiChenConfigureServerServerServiceProvider" ] } } }
客戶端
{ "name": "xxx/xxx", "type": "library", "keywords": ["laravel","php","configure"], "description": "configure-client module", "homepage": "https://github.com/xxx", "license": "MIT", "authors": [ { "name": "OkamiChen", "email": "x25125x@126.com" } ], "require": { "php": ">=7.1.0" }, "autoload": { "psr-4": { "OkamiChenConfigureClient":"src/" }, "files": [ "src/helper.php" ] }, "extra": { "laravel": { "providers": [ "OkamiChenConfigureClientClientServiceProvider" ] } } }
相關文章推薦:
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END