Skip to content

Commit a33c17d

Browse files
authored
Merge pull request #3 from ddzyan/feat/bill
docs(完善单元测试): 📃
2 parents b41891e + 438b2fc commit a33c17d

File tree

9 files changed

+108
-46
lines changed

9 files changed

+108
-46
lines changed

.github/workflows/npm-publish.yml

-24
This file was deleted.

README.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
1-
midway的apollo组件
1+
# 前言
2+
3+
midway 携程异步配置中心 apollo 组件
4+
5+
Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。
6+
7+
底层依赖 @lvgithub/ctrip-apollo-client 模块包
8+
9+
# 使用
10+
11+
## 初始化配置介绍
12+
13+
- metaServerUrl string meta server 地址,负载均衡获取 config server 地址
14+
- configServerUrl string config server 地址,配置了 metaServerUrl 本项可以不配置
15+
- appId string required 应用的 appId
16+
- accessKey string Apollo AccessKey
17+
- clusterName string 集群名,默认值:default
18+
- namespaceList array Namespace 的名字,默认值:[application]
19+
- configPath string 本地配置文件路径 默认值./config/apolloConfig.json
20+
- logger object 日志类 必须实现 logger.info(),logger.error() 两个方法
21+
22+
## 使用
23+
24+
@HotValue() 获取具体的配置字段,封装 getter(热更新)
25+
26+
@GetValue() 直接获取具体的配置字段

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "midway-apollo",
3-
"version": "1.0.16",
3+
"version": "1.1.1",
44
"description": "",
55
"main": "dist/index.js",
66
"typings": "index.d.ts",

src/configuration.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Configuration, Inject, Init } from '@midwayjs/decorator';
22
import { MidwayDecoratorService } from '@midwayjs/core';
33

44
import { ApolloServiceFactory, ApolloService } from './manager';
5-
import { APOLLO } from './decorator';
5+
import { HOT_VALUE, GET_VALUE } from './decorator';
66

77
@Configuration({
88
namespace: 'apollo',
@@ -25,11 +25,18 @@ export class ApolloConfiguration {
2525
async init() {
2626
// 实现装饰器
2727
this.decoratorService.registerPropertyHandler(
28-
APOLLO,
28+
HOT_VALUE,
2929
(propertyName, meta) => {
3030
return this.apolloService.hotValue(meta.key).value;
3131
}
3232
);
33+
34+
this.decoratorService.registerPropertyHandler(
35+
GET_VALUE,
36+
(propertyName, meta) => {
37+
return this.apolloService.getValue(meta.key);
38+
}
39+
);
3340
}
3441

3542
async onReady(container) {

src/decorator.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
// src/decorator/memoryCache.decorator.ts
21
import { createCustomPropertyDecorator } from '@midwayjs/decorator';
32

43
// 装饰器内部的唯一 id
5-
export const APOLLO = 'decorator:apollo';
4+
export const HOT_VALUE = 'apollo:hotValue';
5+
export const GET_VALUE = 'apollo:getValue';
66

7-
export function Apollo(key?: string): PropertyDecorator {
8-
return createCustomPropertyDecorator(APOLLO, {
7+
export function HotValue(key?: string): PropertyDecorator {
8+
return createCustomPropertyDecorator(HOT_VALUE, {
9+
key,
10+
});
11+
}
12+
13+
export function GetValue(key?: string): PropertyDecorator {
14+
return createCustomPropertyDecorator(GET_VALUE, {
915
key,
1016
});
1117
}

src/manager.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class ApolloServiceFactory extends ServiceFactory<CtripApolloClient> {
3636
return client;
3737
} catch (error) {
3838
this.logger.error('[midway:apollo] client error: %s', error);
39-
return;
39+
throw error;
4040
}
4141
}
4242

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
export const apollo = {
22
client: {
3-
metaServerUrl: 'http://106.54.227.205:8080',
4-
appId: 'apolloclient',
5-
accessKey: '35be8a4868c748ec96faef3e326adad5',
3+
appId: 'midway-apollo',
4+
accessKey: '36104c4b15784a7b97708435ef97ee27',
65
configPath: './config/apolloConfig.json',
6+
namespaceList: ['application'],
7+
configServerUrl: 'http://106.54.227.205:8080',
78
},
89
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Provide } from '@midwayjs/decorator';
2+
import { HotValue, GetValue } from '../../../../src/decorator';
3+
4+
@Provide()
5+
export class UserService {
6+
@HotValue('name')
7+
private name: string;
8+
9+
@HotValue('className:三年二班')
10+
private className: string;
11+
12+
@GetValue('age')
13+
private age: string;
14+
15+
getName() {
16+
return this.name;
17+
}
18+
19+
getClassName() {
20+
return this.className;
21+
}
22+
23+
getAge() {
24+
return this.age;
25+
}
26+
}

test/index.test.ts

+31-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,46 @@
11
import { join } from 'path';
2-
32
import { close, createLightApp } from '@midwayjs/mock';
43

5-
import { ApolloService } from '../src';
4+
import { UserService } from './base-app-single-client/src/service/user';
65

76
describe('/test/index.test.ts', () => {
87
let app;
98

9+
beforeAll(async () => {
10+
app = await createLightApp(join(__dirname, './base-app-single-client'));
11+
});
12+
1013
afterAll(async () => {
1114
await close(app);
1215
});
1316

14-
it('test single client', async () => {
15-
app = await createLightApp(join(__dirname, './base-app-single-client'));
16-
const apolloService = await app
17+
it('test hot value', async () => {
18+
const userService: UserService = await app
19+
.getApplicationContext()
20+
.getAsync(UserService);
21+
22+
const name = userService.getName();
23+
24+
expect(name).toEqual('张飞');
25+
});
26+
27+
it('test hot value default Value', async () => {
28+
const userService: UserService = await app
29+
.getApplicationContext()
30+
.getAsync(UserService);
31+
32+
const className = userService.getClassName();
33+
34+
expect(className).toEqual('三年二班');
35+
});
36+
37+
it('test get value', async () => {
38+
const userService: UserService = await app
1739
.getApplicationContext()
18-
.getAsync(ApolloService);
40+
.getAsync(UserService);
41+
42+
const age = userService.getAge();
1943

20-
expect(apolloService.hotValue('mysql.port:3306').value).toBe('3306');
21-
expect(apolloService.hotValue('mysql.host:127.0.0.1').value).toBe(
22-
'127.0.0.1'
23-
);
44+
expect(age).toEqual('13');
2445
});
2546
});

0 commit comments

Comments
 (0)