AGC远程配置高级用法:条件发布与灰度发布
一、远程配置概述
AppGallery Connect(AGC)远程配置服务允许开发者在不发布新版本的情况下动态修改应用行为和外观。鸿蒙5结合AGC远程配置可以实现:
动态功能开关:控制功能的上线与下线
个性化配置:根据不同用户群体展示不同内容
紧急修复:快速修复线上问题
A/B测试:验证不同方案的效果
二、环境配置与初始化
- 在AGC控制台启用服务
登录AGC控制台
选择目标项目和应用
进入"增长" → "远程配置"
启用服务并创建默认配置 - 项目依赖配置
在entry/build.gradle中添加依赖:
dependencies {
implementation 'com.huawei.agconnect:agconnect-remoteconfig-harmony:1.6.5.300'
}
3. 初始化远程配置
import remoteConfig from '@hw-agconnect/remoteconfig-harmony';
export default class RemoteConfigService {
private static instance: RemoteConfigService;
private config = remoteConfig.instance();
private constructor() {
// 设置默认配置
this.config.setDefaults({
"welcome_message": "欢迎使用我们的应用",
"feature_new_version": false,
"special_offer": null
});
// 开发模式设置
if (process.env.NODE_ENV === 'development') {
this.config.setDeveloperMode(true);
}
}
static getInstance(): RemoteConfigService {
if (!RemoteConfigService.instance) {
RemoteConfigService.instance = new RemoteConfigService();
}
return RemoteConfigService.instance;
}
async fetchConfig(): Promise<boolean> {
try {
// 设置缓存策略(单位:秒)
await this.config.fetch(3600); // 1小时缓存
await this.config.apply();
return true;
} catch (error) {
console.error('远程配置获取失败:', error);
return false;
}
}
getValue(key: string): any {
return this.config.getValue(key);
}
}
三、条件发布实现
-
基于用户属性的条件发布
// 用户属性条件检查器
class UserConditionChecker {
static async checkConditions(conditions: object): Promise{
const user = await UserService.getCurrentUser();// 检查用户等级 if (conditions.minLevel && user.level < conditions.minLevel) { return false; } // 检查注册时间 if (conditions.minRegisterDays) { const registerDays = this.getDaysSince(user.registerDate); if (registerDays < conditions.minRegisterDays) { return false; } } // 检查用户标签 if (conditions.requiredTags) { const hasAllTags = conditions.requiredTags.every(tag => user.tags.includes(tag) ); if (!hasAllTags) return false; } return true;}
private static getDaysSince(dateStr: string): number {
const date = new Date(dateStr);
return Math.floor((Date.now() - date.getTime()) / (1000 * 60 * 60 * 24));
}
}
// 条件配置获取
async function getConditionalConfig(key: string) {
const config = RemoteConfigService.getInstance().getValue(key);
if (config.conditions) {
const meetsConditions = await UserConditionChecker.checkConditions(config.conditions);
return meetsConditions ? config.value : config.defaultValue;
}
return config.value;
}
2. 基于设备的条件发布
// 设备条件检查器
class DeviceConditionChecker {
static checkDeviceConditions(conditions: object): boolean {
const deviceInfo = device.deviceInfo;
// 检查设备型号
if (conditions.allowedModels &&
!conditions.allowedModels.includes(deviceInfo.model)) {
return false;
}
// 检查系统版本
if (conditions.minOsVersion &&
this.compareVersions(deviceInfo.osVersion, conditions.minOsVersion) < 0) {
return false;
}
// 检查地区
if (conditions.allowedRegions &&
!conditions.allowedRegions.includes(deviceInfo.region)) {
return false;
}
return true;
}
private static compareVersions(v1: string, v2: string): number {
const parts1 = v1.split('.').map(Number);
const parts2 = v2.split('.').map(Number);
for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
const num1 = parts1[i] || 0;
const num2 = parts2[i] || 0;
if (num1 !== num2) return num1 - num2;
}
return 0;
}
}
四、灰度发布实现
-
百分比灰度发布
// 灰度发布管理器
class GrayReleaseManager {
private static readonly STORAGE_KEY = 'gray_release_groups';
private static groups: Map<string, boolean> = new Map();static init() {
// 从本地存储加载分组信息
const saved = storage.get(this.STORAGE_KEY);
if (saved) {
this.groups = new Map(JSON.parse(saved));
}
}static isInGroup(featureName: string): boolean {
if (this.groups.has(featureName)) {
return this.groups.get(featureName);
}// 新特性随机分组(50%概率) const isInGroup = Math.random() < 0.5; this.groups.set(featureName, isInGroup); this.saveGroups(); return isInGroup;}
private static saveGroups() {
storage.set(this.STORAGE_KEY,
JSON.stringify([...this.groups.entries()]));
}
}
// 使用示例
async function checkFeatureToggle(featureName: string) {
const config = RemoteConfigService.getInstance();
await config.fetchConfig();
const featureConfig = config.getValue(featureName);
// 检查灰度发布设置
if (featureConfig.grayRelease) {
return GrayReleaseManager.isInGroup(featureName) ?
featureConfig.grayValue : featureConfig.defaultValue;
}
return featureConfig.value;
}
2. 分层灰度发布
// 分层灰度策略
class TieredGrayRelease {
static getReleaseTier(userId: string): number {
// 使用用户ID哈希确定分层(0-99)
const hash = this.hashCode(userId);
return hash % 100;
}
static shouldEnable(featureConfig: any, tier: number): boolean {
if (!featureConfig.tiers) return true;
// 检查当前层是否在启用范围内
const tierConfig = featureConfig.tiers.find(
t => tier >= t.start && tier <= t.end
);
return tierConfig ? tierConfig.enabled : false;
}
private static hashCode(str: string): number {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = (hash << 5) - hash + str.charCodeAt(i);
hash |= 0; // 转换为32位整数
}
return Math.abs(hash);
}
}
// 在页面中使用
@Entry
@Component
struct FeaturePage {
@State featureEnabled: boolean = false;
aboutToAppear() {
this.checkFeatureAvailability();
}
async checkFeatureAvailability() {
const userId = UserService.getUserId();
const tier = TieredGrayRelease.getReleaseTier(userId);
const config = RemoteConfigService.getInstance();
await config.fetchConfig();
const featureConfig = config.getValue('experimental_feature');
this.featureEnabled = TieredGrayRelease.shouldEnable(featureConfig, tier);
}
build() {
Column() {
if (this.featureEnabled) {
ExperimentalFeatureComponent()
} else {
StandardFeatureComponent()
}
}
}
}
五、高级应用场景
-
动态主题切换
// 主题管理器
class ThemeManager {
private static currentTheme: string = 'default';static async applyRemoteTheme() {
const config = RemoteConfigService.getInstance();
await config.fetchConfig();const themeConfig = config.getValue('app_theme'); // 检查条件主题 if (themeConfig.conditions) { const meetsConditions = await this.checkThemeConditions(themeConfig.conditions); this.currentTheme = meetsConditions ? themeConfig.conditionalTheme : themeConfig.defaultTheme; } else { this.currentTheme = themeConfig.theme; } this.saveTheme();}
private static async checkThemeConditions(conditions: any): Promise
{
// 实现条件检查逻辑...
}static getCurrentTheme(): Theme {
return this.loadTheme(this.currentTheme);
}private static loadTheme(themeName: string): Theme {
// 加载主题资源...
}
}
// 在应用启动时调用
ThemeManager.applyRemoteTheme().then(() => {
// 主题应用完成
});
2. 紧急开关控制
// 紧急开关服务
class EmergencySwitch {
static async checkServiceStatus(serviceName: string): Promise
const config = RemoteConfigService.getInstance();
try {
// 强制获取最新配置(忽略缓存)
await config.fetch(0);
await config.apply();
const status = config.getValue(`emergency_${serviceName}`);
return status !== 'disabled';
} catch (error) {
console.error('紧急开关检查失败,默认启用服务:', error);
return true; // 失败时默认启用
}
}
}
// 使用示例
async function callCriticalService() {
const isAvailable = await EmergencySwitch.checkServiceStatus('api_service');
if (!isAvailable) {
showMaintenancePage();
return;
}
// 正常调用服务...
}
六、配置版本控制与回滚
-
版本化配置管理
// 配置版本管理器
class ConfigVersionManager {
static async getConfigWithFallback(key: string): Promise{
const config = RemoteConfigService.getInstance();try { await config.fetchConfig(); const value = config.getValue(key); // 检查版本兼容性 if (value.minAppVersion && !this.checkVersionCompatibility(value.minAppVersion)) { return this.getFallbackValue(key); } return value; } catch (error) { return this.getFallbackValue(key); }}
private static checkVersionCompatibility(minVersion: string): boolean {
const currentVersion = getAppVersion();
// 实现版本比较逻辑...
}private static getFallbackValue(key: string): any {
// 获取本地缓存的旧版本配置...
}
} -
配置变更监听
// 配置变更观察者
class ConfigObserver {
private static listeners: Map<string, Function[]> = new Map();static init() {
// 定时检查配置更新
setInterval(() => this.checkUpdates(), 300000); // 5分钟
}static addListener(key: string, callback: Function) {
if (!this.listeners.has(key)) {
this.listeners.set(key, []);
}
this.listeners.get(key)?.push(callback);
}private static async checkUpdates() {
const config = RemoteConfigService.getInstance();
const lastFetchTime = config.getLastFetchTime();try { await config.fetch(0); const updated = await config.apply(); if (updated) { this.notifyListeners(); } } catch (error) { console.error('配置更新检查失败:', error); }}
private static notifyListeners() {
const config = RemoteConfigService.getInstance();this.listeners.forEach((callbacks, key) => { const newValue = config.getValue(key); callbacks.forEach(cb => cb(newValue)); });}
}
// 使用示例
ConfigObserver.addListener('feature_toggle', (newValue) => {
updateFeatureVisibility(newValue);
});
七、最佳实践与调试技巧
-
开发调试工具
// 远程配置调试面板
@Entry
@Component
struct ConfigDebugPanel {
@State configValues: object = {};
@State lastFetchTime: string = '';aboutToAppear() {
this.loadConfig();
}async loadConfig() {
const config = RemoteConfigService.getInstance();
await config.fetchConfig();this.configValues = { 'welcome_message': config.getValue('welcome_message'), 'feature_flag': config.getValue('experimental_feature'), // 添加其他需要监控的键... }; this.lastFetchTime = new Date(config.getLastFetchTime()).toLocaleString();}
build() {
Column() {
Text('远程配置调试')
.fontSize(20)
.margin(10)Text(`最后更新时间: ${this.lastFetchTime}`) .fontSize(14) .margin(5) ForEach(Object.entries(this.configValues), ([key, value]) => { Column() { Text(`${key}: ${JSON.stringify(value)}`) .fontSize(16) } .margin(5) }) Button('强制刷新') .onClick(() => this.loadConfig()) .margin(10) }}
} -
性能优化建议
合理设置缓存时间:
// 根据配置类型设置不同缓存时间
const cacheTimes = {
feature_flags: 3600, // 1小时
ui_config: 86400, // 24小时
emergency_switch: 0 // 总是最新
};
await remoteConfig.fetch(cacheTimes[configType]);
批量获取配置:
async function getMultipleConfigs(keys: string[]) {
await RemoteConfigService.getInstance().fetchConfig();
return keys.reduce((result, key) => {
result[key] = RemoteConfigService.getInstance().getValue(key);
return result;
}, {});
}
差异化加载策略:
async function loadConfigStrategically() {
// 首屏关键配置立即获取
await fetchUrgentConfigs();
// 非关键配置延迟加载
setTimeout(() => fetchNonCriticalConfigs(), 3000);
}
八、总结
通过AGC远程配置服务,鸿蒙5应用可以实现:
条件发布:基于用户属性、设备特征等条件动态下发配置
graph LR
A[用户请求] --> B{满足条件?}
B -->|是| C[返回特殊配置]
B -->|否| D[返回默认配置]
灰度发布:通过百分比或分层方式逐步放量
// 示例灰度检查逻辑
function shouldEnableFeature(user) {
return user.tier < currentReleasePercentage;
}
紧急控制:快速关闭问题功能或服务
实施建议:
配置分类管理:
├── features/ # 功能开关
├── ui/ # 界面配置
├── experiments/ # A/B测试
└── emergency/ # 紧急控制
变更日志记录:
class ConfigChangeLogger {
static logChange(key: string, oldVal: any, newVal: any) {
analytics.instance().logEvent('config_change', {
key,
old_value: JSON.stringify(oldVal),
new_value: JSON.stringify(newVal),
changed_at: new Date().toISOString()
});
}
}
监控与报警:
配置异常值监控
变更影响范围评估
关键配置变更报警

浙公网安备 33010602011771号