README.md 4.2 KB
Newer Older
Q
Quinton.Xu 已提交
1 2 3
[![NPM version][npm-image]][npm-url]
[![David deps][david-image]][david-url]
[![npm download][download-image]][download-url]
Q
qifeng 已提交
4
[![Build Status](https://travis-ci.org/Quinton/node-apollo.svg?branch=master)](https://travis-ci.org/Quinton/node-apollo)
Q
Quinton.Xu 已提交
5 6 7 8 9 10 11

[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 已提交
12 13 14 15 16 17 18
# node-apollo

  携程Apollo配置中心node SDK.

## Install

```bash
Q
Quinton.Xu 已提交
19
$ npm i node-apollo --save-dev
Q
Quinton.Xu 已提交
20
```
子丰 已提交
21
Node.js >= v7.6.0 required.
Q
Quinton.Xu 已提交
22 23 24

## Features

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

## Usage

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

Q
Quinton.Xu 已提交
33
## Methods
Q
Quinton.Xu 已提交
34

Q
Quinton.Xu 已提交
35
### apollo# ```remoteConfigServiceFromCache(config)```
Q
qifeng 已提交
36
```javascript
Q
Quinton.Xu 已提交
37
  const config = {
K
kalid 已提交
38 39
    configServerUrl: 'http://example.com',   
    // configServerUrl: 'https://example.com', // Support https
Q
Quinton.Xu 已提交
40 41
    appId: '<appId>',
    clusterName: 'default',
Q
Quinton.Xu 已提交
42
    namespaceName: [ 'namespaceName1', 'namespaceName2' ], // n1的配置会被n2配置覆盖
Q
Quinton.Xu 已提交
43 44 45 46 47
    // 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 已提交
48
### apollo# ```remoteConfigServiceSkipCache(config)```
Q
qifeng 已提交
49
```javascript
Q
Quinton.Xu 已提交
50
  const config = {
K
kalid 已提交
51 52
    configServerUrl: 'http://example.com', 
    // configServerUrl: 'https://example.com', // Support https
Q
Quinton.Xu 已提交
53 54
    appId: '<appId>',
    clusterName: 'default',
Q
Quinton.Xu 已提交
55 56 57
    namespaceName: [ 'namespaceName1', 'namespaceName2' ], // n1的配置会被n2配置覆盖
    // clientIp: '', // optional
    // releaseKey: '', // optional
Q
Quinton.Xu 已提交
58
    };
A
architect.bian 已提交
59
  const result = await apollo.remoteConfigServiceSkipCache(config);
Q
Quinton.Xu 已提交
60
```
Q
Quinton.Xu 已提交
61
  >**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 已提交
62
### apollo# ```remoteConfigService(config)```
Q
qifeng 已提交
63
```javascript
Q
Quinton.Xu 已提交
64
  const config = {
K
kalid 已提交
65 66
      configServerUrl: 'http://example.com',  
      // configServerUrl: 'https://example.com',  // Support https
Q
Quinton.Xu 已提交
67 68
      appId: '<appId>',
      clusterName: 'default',
Q
Quinton.Xu 已提交
69
      namespaceName: '',  //no surport multi namespace name, optional
Q
Quinton.Xu 已提交
70
      apolloEnv: 'dev',
Q
Quinton.Xu 已提交
71
      token: '<apollo access token>', // required
Q
Quinton.Xu 已提交
72 73
      // clientIp: '',
    };
Q
fix  
Quinton.Xu 已提交
74
  const result = await apollo.remoteConfigService(config);
Q
Quinton.Xu 已提交
75
```
Q
Quinton.Xu 已提交
76
  >**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 已提交
77
### apollo# ```createEnvFile(envConfig)```
Q
qifeng 已提交
78
```javascript
Q
Quinton.Xu 已提交
79
  const envConfig = {
Q
Quinton.Xu 已提交
80 81 82
      mongoose: 'mongo://127.0.0.1:27017',
      appId: '<appId>',
    };
Q
Quinton.Xu 已提交
83
  apollo.createEnvFile(envConfig);
Q
Quinton.Xu 已提交
84 85
```
>**NOTE:** 生成环境变量文件,常用于docker化项目,详细请参考[example](https://github.com/Quinton/node-apollo/tree/master/example).
Q
Quinton.Xu 已提交
86
### apollo# ```setEnv()```
Q
qifeng 已提交
87
```javascript
Q
Quinton.Xu 已提交
88 89
apollo.setEnv();
```
Q
Quinton.Xu 已提交
90
>**NOTE:** 注入环境变量到process.env, 详情请参考[dotenv](https://github.com/motdotla/dotenv#usage).
Q
Quinton.Xu 已提交
91 92 93

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

Q
Quinton.Xu 已提交
97 98
## License

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