代码拉取完成,页面将自动刷新
import { ApolloClient } from './client';
interface Config {
key1: string;
key2: number;
key3: boolean,
// 此处的mongodb和redis分别对应两个namespace下的数据,部门名称会被自动去除
mongodb: {
url: string,
},
redis: {
port: number,
host: string,
password: string,
}
}
const client = new ApolloClient<Config>({
appId: 'lock-server',
cluster: 'nuc',
namespace: [
'nuc.mongodb',
],
host: 'http://192.168.8.8:8080',
secret: 'b27a01fea3bd4f23a83e3261be146036',
configConstructor: {
key1: String,
key2: Number,
key3: Boolean,
mongodb: {
url: String,
},
redis: {
port: Number,
host: String,
password: String,
},
},
// 以同步的方式获取配置
sync: true,
});
console.log(client.config);
// 监听key值变动
client.changeEvent.on('key1', (notify) => {
console.log(notify);
});
// 监听某个namespace下的key值变动
client.changeEvent.on('mongodb.url', (notify) => {
console.log(notify);
});
// 监听namespace,其下任何值发生变化都会触发事件
client.namespaceChangeEvent.on('redis', (notify) => {
console.log(notify);
});
以下版本有致命bug,不推荐使用
interface Config {
key1: string;
key2: number;
}
const client = new ApolloClient<Config>({
appId: 'mind-server',
cluster: 'nuc',
namespace: [
'nuc.mongodb',
'tx.redis',
],
});
client.namespaceChangeEvent.on('redis', (notify) => {
console.log(notify);
});
const client = new ApolloClient<Config>({
secret: '18b079a14e9c43ca83374f614da793b4',
// ...
});
const client = new ApolloClient<Config>({
sync: true,
// ...
});
console.log(client.config);
支持实时推送
配置类型泛形
interface Config {
key1: string;
key2: number;
}
const client = new ApolloClient<Config>({
appId: 'mind-server',
cluster: 'nuc',
namespace: [
'nuc.mongodb',
'tx.redis',
],
});
// client.config的类型为Config
由于Apollo获取到的参数值只能是string类型,使用时还需要二次解析,新版本内置了类型转换:
interface Config {
key1: string;
key2: number;
key3: boolean,
mongoDb: {
url: string,
},
}
const client = new ApolloClient<Config>({
appId: 'mind-server',
cluster: 'nuc',
namespace: [
'nuc.mongodb',
'tx.redis',
],
configConstructor: {
key1: String,
key2: Number,
key3: Boolean,
mongodb: {
url: String,
},
},
});
console.log(client.config);
// {
// key1: '字符串',
// key2: 12345,
// key3: false,
// mongodb: {
// url: 'mongodb://root:xx@127.0.0.1:27017/auth?authSource=admin'
// },
// redis: {
// port: 6379,
// host: '127.0.0.1',
// password: 'xxxxxxx'
// }
// }
const client = new ApolloClient<Config>({
appId: 'mind-server',
cluster: 'nuc',
namespace: [
'nuc.mongodb',
'tx.redis',
],
secret: '18b079a14e9c43ca83374f614da793b4',
configConstructor: {
key1: String,
key2: Number,
},
}, (config: Config) => {
console.log(config);
});
当设置sync不为true的时候必须传入callback回调
const client = new ApolloClient<Config>({
appId: 'mind-server',
cluster: 'nuc',
namespace: [
'nuc.mongodb',
'tx.redis',
],
configConstructor: {
key1: String,
key2: Number,
key3: Boolean,
mongodb: {
url: String,
},
redis: {
port: Number,
host: String,
password: String,
},
},
sync: true,
});
client.changeEvent.on('key1', (notify) => {
console.log(notify);
});
client.changeEvent.on('redis.port', (notify) => {
console.log(notify);
});
client.changeEvent.on('mongodb.url', (notify) => {
// 可以在此回调中进行重连
console.log(notify);
});
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。