README.md 3.8 KB
Newer Older
Q
Quinton.Xu 已提交
1 2 3 4 5 6 7 8 9 10
[![NPM version][npm-image]][npm-url]
[![David deps][david-image]][david-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/node-apollo.svg?style=flat-square
[npm-url]: https://npmjs.org/package/node-apollo
[david-image]: https://img.shields.io/david/Quinton/node-apollo.svg?style=flat-square
[david-url]: https://david-dm.org/Quinton/node-apollo/repo.svg
[download-image]: https://img.shields.io/npm/dm/node-apollo.svg?style=flat-square
[download-url]: https://npmjs.org/package/node-apollo
Q
Quinton.Xu 已提交
11 12 13 14 15 16 17
# node-apollo

  携程Apollo配置中心node SDK.

## Install

```bash
Q
Quinton.Xu 已提交
18
$ npm i node-apollo --save-dev
Q
Quinton.Xu 已提交
19
```
Q
Quinton.Xu 已提交
20 21 22 23
Node.js >= 6.0.0 required.

## Features

Q
quinton 已提交
24
- ✔︎ 适用eggjs([egg-apollojs](https://github.com/yhj2009/egg-apollojs)), thinkjs等docker部署项目
Q
Quinton.Xu 已提交
25
- ✔︎ 适用自己手动搭建的服务,可直接读取JSON格式的配置
Q
Quinton.Xu 已提交
26 27 28

## Usage

Q
Quinton.Xu 已提交
29
* See [examples](https://github.com/Quinton/node-apollo/tree/master/example)
Q
Quinton.Xu 已提交
30

Q
Quinton.Xu 已提交
31
## Methods
Q
Quinton.Xu 已提交
32

Q
Quinton.Xu 已提交
33 34 35 36 37 38
### apollo# ```remoteConfigServiceFromCache(config)```
```
  const config = {
    configServerUrl: 'http://example.com',
    appId: '<appId>',
    clusterName: 'default',
Q
Quinton.Xu 已提交
39
    namespaceName: [ 'namespaceName1', 'namespaceName2' ], // n1的配置会被n2配置覆盖
Q
Quinton.Xu 已提交
40 41 42 43 44
    // clientIp: '',
    };
  const result = await apollo.remoteConfigServiceFromCache(config);
```
  >**NOTE:** 通过带缓存的Http接口从Apollo读取配置,详情请参考[Apollo开放平台](https://github.com/ctripcorp/apollo/wiki/%E5%85%B6%E5%AE%83%E8%AF%AD%E8%A8%80%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97#12-%E9%80%9A%E8%BF%87%E5%B8%A6%E7%BC%93%E5%AD%98%E7%9A%84http%E6%8E%A5%E5%8F%A3%E4%BB%8Eapollo%E8%AF%BB%E5%8F%96%E9%85%8D%E7%BD%AE).
A
architect.bian 已提交
45
### apollo# ```remoteConfigServiceSkipCache(config)```
Q
Quinton.Xu 已提交
46 47 48 49 50
```
  const config = {
    configServerUrl: 'http://example.com',
    appId: '<appId>',
    clusterName: 'default',
Q
Quinton.Xu 已提交
51 52 53
    namespaceName: [ 'namespaceName1', 'namespaceName2' ], // n1的配置会被n2配置覆盖
    // clientIp: '', // optional
    // releaseKey: '', // optional
Q
Quinton.Xu 已提交
54
    };
A
architect.bian 已提交
55
  const result = await apollo.remoteConfigServiceSkipCache(config);
Q
Quinton.Xu 已提交
56
```
Q
Quinton.Xu 已提交
57
  >**NOTE:** 通过不带缓存的Http接口从Apollo读取配置,详情请参考[Apollo开放平台](https://github.com/ctripcorp/apollo/wiki/%E5%85%B6%E5%AE%83%E8%AF%AD%E8%A8%80%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97#13-%E9%80%9A%E8%BF%87%E4%B8%8D%E5%B8%A6%E7%BC%93%E5%AD%98%E7%9A%84http%E6%8E%A5%E5%8F%A3%E4%BB%8Eapollo%E8%AF%BB%E5%8F%96%E9%85%8D%E7%BD%AE).
Q
Quinton.Xu 已提交
58
### apollo# ```remoteConfigService(config)```
Q
Quinton.Xu 已提交
59 60 61 62 63
```
  const config = {
      configServerUrl: 'http://example.com',
      appId: '<appId>',
      clusterName: 'default',
Q
Quinton.Xu 已提交
64
      namespaceName: '',  //no surport multi namespace name, optional
Q
Quinton.Xu 已提交
65
      apolloEnv: 'dev',
Q
Quinton.Xu 已提交
66
      token: '<apollo access token>', // required
Q
Quinton.Xu 已提交
67 68
      // clientIp: '',
    };
Q
fix  
Quinton.Xu 已提交
69
  const result = await apollo.remoteConfigService(config);
Q
Quinton.Xu 已提交
70
```
Q
Quinton.Xu 已提交
71
  >**NOTE:** 读取携程Apollo配置,调用第三方应用接入Apollo开放平台, 详情请参考[Apollo开放平台](https://github.com/ctripcorp/apollo/wiki/Apollo%E5%BC%80%E6%94%BE%E5%B9%B3%E5%8F%B0).
Q
Quinton.Xu 已提交
72
### apollo# ```createEnvFile(envConfig)```
Q
Quinton.Xu 已提交
73
```
Q
Quinton.Xu 已提交
74
  const envConfig = {
Q
Quinton.Xu 已提交
75 76 77
      mongoose: 'mongo://127.0.0.1:27017',
      appId: '<appId>',
    };
Q
Quinton.Xu 已提交
78
  apollo.createEnvFile(envConfig);
Q
Quinton.Xu 已提交
79 80
```
>**NOTE:** 生成环境变量文件,常用于docker化项目,详细请参考[example](https://github.com/Quinton/node-apollo/tree/master/example).
Q
Quinton.Xu 已提交
81
### apollo# ```setEnv()```
Q
Quinton.Xu 已提交
82 83 84
```
apollo.setEnv();
```
Q
Quinton.Xu 已提交
85
>**NOTE:** 注入环境变量到process.env, 详情请参考[dotenv](https://github.com/motdotla/dotenv#usage).
Q
Quinton.Xu 已提交
86 87 88

## TODO
  
Q
qifeng 已提交
89 90
 - ~~[x] 支持局部配置热更新~~
>**NOTE:** 不支持热更新,具体原因和方案请参考:[关于eggjs热部署问题 ](https://github.com/eggjs/egg/issues/947)
Q
Quinton.Xu 已提交
91

Q
Quinton.Xu 已提交
92 93
## License

Q
Quinton.Xu 已提交
94
[MIT](LICENSE)