xuezier / egg-apollo-client

16 stars 6 forks source link

覆盖配置 #12

Closed ugvgylkm closed 3 years ago

ugvgylkm commented 3 years ago

可以设置时间间隔不断更新配置文件(config.default.ts)里的对象字段么,如何配置

config.apollo = { config_server_url: 'http://106.54.227.205:8080', // required, 配置中心服务地址 app_id: 'appid', // required, 需要加载的配置 init_on_start: true, // optional, 在 app 启动时同时加载配置,加载的配置会在插件加载前被加载 cluster_name: 'default', // optional, 加载配置的集群名称, default: 'default' namespace_name: 'application', // optional, 加载配置的命名空间, default: 'application' release_key: '', // optional, 加载配置的版本 key, default: '' ip: '', // optional, set_env_file: true, // optional, 是否写入到 env 文件, default: false env_file_path: '', // optional, 写入的 env 文件路径, default: ${app.baseDir}/.env.apollo watch: true, // optional, 长轮询查看配置是否更新, default: false timeout: 61000, // optional, 长轮询 timeout 设置,默认 50000 };

yuu2lee4 commented 3 years ago

watch:true ,配置更新的时候会发射事件,这个时候可以在app.js里监听自行合并

export default async app => {
    app.apollo.on('config.updated', () => {
       // 此时从app.apollo上获取的配置就是最新的,可以自行合并到app.config上
    });
};
ugvgylkm commented 3 years ago

@yuu2lee4 这个通知接口,只支持application命名空间更新么,这个方法的参数config没有传值?

async remoteConfigFromServiceLongPolling(config: IApolloRequestConfig = {}) { const { cluster_name = this.cluster_name, notifications = [] } = config; if (!notifications.length) { notifications[0] = { namespaceName: 'application', notificationId: 0, }; }

    for (const notification of notifications) {
        const { namespaceName } = notification;
        if (this.notifications[namespaceName]) {
            notification.notificationId = this.notifications[namespaceName];
        }
    }

    const url = `${this.config_server_url}/notifications/v2?appId=${this.app_id}&cluster=${cluster_name}&notifications=${encodeURI(JSON.stringify(notifications))}`;

    const response = await request(url, {
        timeout: this.timeout,
    });

    if (response.statusCode !== 304 && !response.isJSON()) {
        throw new RequestError(response.data);
    } else {
        return response.data;
    }
}