node-apollo-client
node apollo client for Ctrip Apollo
Features:
- implement all APIs described in official documentation
- high availability by caching configs in local files which simulate JAVA SDK does.
- written in TypeScript and typing support
- naturally async await function calling, no event or callback mixed-in.
client logic and availability
local configs object \-+= local cached config files \-+= fetch from Apollo DB at once \-+= fetch from Apollo cache periodically \-+= subscribe notification and fetch from Apollo DB
scene | influence | configs source |
---|---|---|
failed to connect apollo at begining | x | use local configs directly |
failed to connect apollo | x | use cached apollo configs |
failed to connect apollo and wish to update configs | x | update cached configs temporarily |
failed to receive notifications | x | fetch configs periodically fetch from apollo |
failed to fetch from apollo cache | x | subscribe notification and fetch from DB |
Usage
install package from npm
npm install node-apollo-client
const Apollo = // Instantiate Apolloconst apollo = configServerUrl: 'your-config-server-url' appId: 'your-app-id' cluster: 'default' // [optional] default to `default` namespaces: 'application' // default to `['application']`, this is the namespaces that you want to use or maintain. initialConfigs: application: // this is default namespace name foo: 'Mars' bar: 'Jupiter' // [optional] listenOnNotification: true // [optional] default to true fetchCacheInterval: 5 * 60e3 // [optional] default to 5 minutes. can be customize but 30s or shorter time are not acceptable. cachedConfigFilePath: '/tmp' // [optional] cached configs path, default to system's tmp directory, for linux it's basically '/tmp'.; // fetch single configawait apollo;// return 'Mars' // fetch multiple configsawait apollo;// return { foo: 'Mars', bar: 'Jupiter' } // refresh local configs (merely used when Apollo is unavailable)apollo;// check out key `foo`await apollo;// return { foo: 'Mercury' }
License
MIT