@dangao/bun-server 1.8.0 → 1.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/api.md +194 -81
- package/docs/extensions.md +53 -0
- package/docs/guide.md +243 -1
- package/docs/microservice-config-center.md +73 -74
- package/docs/microservice-nacos.md +89 -90
- package/docs/microservice-service-registry.md +85 -86
- package/docs/microservice.md +142 -137
- package/docs/request-lifecycle.md +45 -4
- package/docs/symbol-interface-pattern.md +106 -106
- package/docs/zh/api.md +458 -18
- package/docs/zh/extensions.md +53 -0
- package/docs/zh/guide.md +251 -4
- package/docs/zh/microservice-config-center.md +258 -0
- package/docs/zh/microservice-nacos.md +346 -0
- package/docs/zh/microservice-service-registry.md +306 -0
- package/docs/zh/microservice.md +680 -0
- package/docs/zh/request-lifecycle.md +43 -5
- package/package.json +1 -1
- package/tests/auth/auth-decorators.test.ts +241 -0
- package/tests/auth/oauth2-service.test.ts +318 -0
- package/tests/cache/cache-decorators-extended.test.ts +272 -0
- package/tests/cache/cache-interceptors.test.ts +534 -0
- package/tests/cache/cache-service-proxy.test.ts +246 -0
- package/tests/cache/memory-cache-store.test.ts +155 -0
- package/tests/cache/redis-cache-store.test.ts +199 -0
- package/tests/config/config-center-integration.test.ts +334 -0
- package/tests/config/config-module-extended.test.ts +165 -0
- package/tests/controller/param-binder.test.ts +333 -0
- package/tests/error/error-handler.test.ts +166 -57
- package/tests/error/i18n-extended.test.ts +105 -0
- package/tests/events/event-listener-scanner.test.ts +114 -0
- package/tests/events/event-module.test.ts +133 -302
- package/tests/extensions/logger-module.test.ts +158 -0
- package/tests/files/file-storage.test.ts +136 -0
- package/tests/interceptor/base-interceptor.test.ts +605 -0
- package/tests/interceptor/builtin/cache-interceptor.test.ts +233 -86
- package/tests/interceptor/builtin/log-interceptor.test.ts +469 -0
- package/tests/interceptor/builtin/permission-interceptor.test.ts +219 -120
- package/tests/interceptor/interceptor-chain.test.ts +241 -189
- package/tests/interceptor/interceptor-metadata.test.ts +221 -0
- package/tests/microservice/circuit-breaker.test.ts +221 -0
- package/tests/microservice/service-client-decorators.test.ts +86 -0
- package/tests/microservice/service-client-interceptors.test.ts +274 -0
- package/tests/microservice/service-registry-decorators.test.ts +147 -0
- package/tests/microservice/tracer.test.ts +213 -0
- package/tests/microservice/tracing-collectors.test.ts +168 -0
- package/tests/middleware/builtin/middleware-builtin-extended.test.ts +237 -0
- package/tests/middleware/builtin/rate-limit.test.ts +257 -0
- package/tests/middleware/middleware-decorators.test.ts +222 -0
- package/tests/middleware/middleware-pipeline.test.ts +160 -0
- package/tests/queue/queue-decorators.test.ts +139 -0
- package/tests/queue/queue-service.test.ts +191 -0
- package/tests/request/body-parser-extended.test.ts +291 -0
- package/tests/request/request-wrapper.test.ts +319 -0
- package/tests/router/router-decorators.test.ts +260 -0
- package/tests/router/router-extended.test.ts +298 -0
- package/tests/security/guards/reflector.test.ts +188 -0
- package/tests/security/security-filter.test.ts +182 -0
- package/tests/security/security-module-extended.test.ts +133 -0
- package/tests/session/memory-session-store.test.ts +172 -0
- package/tests/session/session-decorators.test.ts +163 -0
- package/tests/swagger/ui.test.ts +212 -0
package/docs/microservice.md
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Microservice Architecture Support
|
|
2
2
|
|
|
3
|
-
Bun Server Framework
|
|
3
|
+
Bun Server Framework provides comprehensive microservice architecture support, including configuration center, service registration and discovery, service invocation, service governance, and observability features.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Table of Contents
|
|
6
6
|
|
|
7
|
-
- [
|
|
8
|
-
- [
|
|
9
|
-
- [
|
|
10
|
-
- [
|
|
11
|
-
- [
|
|
12
|
-
- [
|
|
13
|
-
- [
|
|
7
|
+
- [Quick Start](#quick-start)
|
|
8
|
+
- [Configuration Center](#configuration-center)
|
|
9
|
+
- [Service Registration and Discovery](#service-registration-and-discovery)
|
|
10
|
+
- [Service Invocation](#service-invocation)
|
|
11
|
+
- [Service Governance](#service-governance)
|
|
12
|
+
- [Monitoring and Tracing](#monitoring-and-tracing)
|
|
13
|
+
- [Best Practices](#best-practices)
|
|
14
14
|
|
|
15
|
-
##
|
|
15
|
+
## Quick Start
|
|
16
16
|
|
|
17
|
-
###
|
|
17
|
+
### Install Dependencies
|
|
18
18
|
|
|
19
19
|
```bash
|
|
20
20
|
bun add @dangao/bun-server
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
###
|
|
23
|
+
### Basic Example
|
|
24
24
|
|
|
25
25
|
```typescript
|
|
26
26
|
import { Application } from '@dangao/bun-server';
|
|
@@ -30,10 +30,10 @@ import {
|
|
|
30
30
|
ServiceClient,
|
|
31
31
|
} from '@dangao/bun-server';
|
|
32
32
|
|
|
33
|
-
//
|
|
33
|
+
// Create application
|
|
34
34
|
const app = new Application();
|
|
35
35
|
|
|
36
|
-
//
|
|
36
|
+
// Register configuration center module
|
|
37
37
|
app.registerModule(
|
|
38
38
|
ConfigCenterModule.forRoot({
|
|
39
39
|
provider: 'nacos',
|
|
@@ -48,7 +48,7 @@ app.registerModule(
|
|
|
48
48
|
}),
|
|
49
49
|
);
|
|
50
50
|
|
|
51
|
-
//
|
|
51
|
+
// Register service registry module
|
|
52
52
|
app.registerModule(
|
|
53
53
|
ServiceRegistryModule.forRoot({
|
|
54
54
|
provider: 'nacos',
|
|
@@ -63,13 +63,13 @@ app.registerModule(
|
|
|
63
63
|
}),
|
|
64
64
|
);
|
|
65
65
|
|
|
66
|
-
//
|
|
66
|
+
// Start application
|
|
67
67
|
await app.listen(3000);
|
|
68
68
|
```
|
|
69
69
|
|
|
70
|
-
##
|
|
70
|
+
## Configuration Center
|
|
71
71
|
|
|
72
|
-
###
|
|
72
|
+
### Basic Usage
|
|
73
73
|
|
|
74
74
|
```typescript
|
|
75
75
|
import {
|
|
@@ -95,7 +95,7 @@ class MyService {
|
|
|
95
95
|
}
|
|
96
96
|
```
|
|
97
97
|
|
|
98
|
-
###
|
|
98
|
+
### Using Decorators
|
|
99
99
|
|
|
100
100
|
```typescript
|
|
101
101
|
import { ConfigCenterValue, Injectable } from '@dangao/bun-server';
|
|
@@ -104,32 +104,32 @@ import { ConfigCenterValue, Injectable } from '@dangao/bun-server';
|
|
|
104
104
|
class MyService {
|
|
105
105
|
@ConfigCenterValue('my-config', 'DEFAULT_GROUP', {
|
|
106
106
|
defaultValue: 'default-value',
|
|
107
|
-
watch: true, //
|
|
107
|
+
watch: true, // Watch for configuration changes
|
|
108
108
|
})
|
|
109
109
|
public configValue: string = '';
|
|
110
110
|
|
|
111
111
|
public getConfig() {
|
|
112
|
-
return this.configValue; //
|
|
112
|
+
return this.configValue; // Automatically fetched from config center
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
115
|
```
|
|
116
116
|
|
|
117
|
-
###
|
|
117
|
+
### Hot Configuration Update
|
|
118
118
|
|
|
119
|
-
|
|
119
|
+
The configuration center supports hot configuration updates, automatically notifying the application when configurations change:
|
|
120
120
|
|
|
121
121
|
```typescript
|
|
122
122
|
const configCenter = container.resolve<ConfigCenter>(CONFIG_CENTER_TOKEN);
|
|
123
123
|
|
|
124
124
|
configCenter.watchConfig('my-config', 'DEFAULT_GROUP', (newConfig) => {
|
|
125
125
|
console.log('Config updated:', newConfig.content);
|
|
126
|
-
//
|
|
126
|
+
// Update application configuration
|
|
127
127
|
});
|
|
128
128
|
```
|
|
129
129
|
|
|
130
|
-
### ConfigModule
|
|
130
|
+
### ConfigModule Integration
|
|
131
131
|
|
|
132
|
-
ConfigModule
|
|
132
|
+
ConfigModule supports deep integration with the configuration center, automatically refreshing when configurations change:
|
|
133
133
|
|
|
134
134
|
```typescript
|
|
135
135
|
import { ConfigModule } from '@dangao/bun-server';
|
|
@@ -142,16 +142,16 @@ ConfigModule.forRoot({
|
|
|
142
142
|
['app.name', { dataId: 'app-name', groupName: 'DEFAULT_GROUP' }],
|
|
143
143
|
['app.port', { dataId: 'app-port', groupName: 'DEFAULT_GROUP' }],
|
|
144
144
|
]),
|
|
145
|
-
configCenterPriority: true, //
|
|
145
|
+
configCenterPriority: true, // Config center has highest priority
|
|
146
146
|
},
|
|
147
147
|
});
|
|
148
148
|
```
|
|
149
149
|
|
|
150
|
-
##
|
|
150
|
+
## Service Registration and Discovery
|
|
151
151
|
|
|
152
|
-
###
|
|
152
|
+
### Service Registration
|
|
153
153
|
|
|
154
|
-
####
|
|
154
|
+
#### Automatic Registration with Decorator
|
|
155
155
|
|
|
156
156
|
```typescript
|
|
157
157
|
import { ServiceRegistry, Controller, GET } from '@dangao/bun-server';
|
|
@@ -169,13 +169,13 @@ class UserController {
|
|
|
169
169
|
}
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
-
//
|
|
172
|
+
// Service will be automatically registered when application starts
|
|
173
173
|
const app = new Application();
|
|
174
174
|
app.registerController(UserController);
|
|
175
175
|
await app.listen(3000);
|
|
176
176
|
```
|
|
177
177
|
|
|
178
|
-
####
|
|
178
|
+
#### Manual Registration
|
|
179
179
|
|
|
180
180
|
```typescript
|
|
181
181
|
import {
|
|
@@ -203,9 +203,9 @@ class MyService {
|
|
|
203
203
|
}
|
|
204
204
|
```
|
|
205
205
|
|
|
206
|
-
###
|
|
206
|
+
### Service Discovery
|
|
207
207
|
|
|
208
|
-
####
|
|
208
|
+
#### Automatic Discovery with Decorator
|
|
209
209
|
|
|
210
210
|
```typescript
|
|
211
211
|
import { ServiceDiscovery, Injectable } from '@dangao/bun-server';
|
|
@@ -214,18 +214,18 @@ import type { ServiceInstance } from '@dangao/bun-server';
|
|
|
214
214
|
@Injectable()
|
|
215
215
|
class MyService {
|
|
216
216
|
@ServiceDiscovery('user-service', {
|
|
217
|
-
healthyOnly: true, //
|
|
217
|
+
healthyOnly: true, // Only get healthy instances
|
|
218
218
|
})
|
|
219
219
|
public instances: ServiceInstance[] = [];
|
|
220
220
|
|
|
221
221
|
public async getAvailableInstances() {
|
|
222
|
-
// instances
|
|
222
|
+
// instances will be automatically updated
|
|
223
223
|
return this.instances;
|
|
224
224
|
}
|
|
225
225
|
}
|
|
226
226
|
```
|
|
227
227
|
|
|
228
|
-
####
|
|
228
|
+
#### Manual Discovery
|
|
229
229
|
|
|
230
230
|
```typescript
|
|
231
231
|
const instances = await serviceRegistry.getInstances('user-service', {
|
|
@@ -233,38 +233,38 @@ const instances = await serviceRegistry.getInstances('user-service', {
|
|
|
233
233
|
namespaceId: 'public',
|
|
234
234
|
});
|
|
235
235
|
|
|
236
|
-
//
|
|
236
|
+
// Watch for service instance changes
|
|
237
237
|
serviceRegistry.watchInstances('user-service', (newInstances) => {
|
|
238
238
|
console.log('Instances updated:', newInstances);
|
|
239
239
|
});
|
|
240
240
|
```
|
|
241
241
|
|
|
242
|
-
###
|
|
242
|
+
### Health Check Integration
|
|
243
243
|
|
|
244
|
-
|
|
244
|
+
Service registration automatically integrates with the health check module, updating service health status based on health check results:
|
|
245
245
|
|
|
246
246
|
```typescript
|
|
247
247
|
import { HealthModule } from '@dangao/bun-server';
|
|
248
248
|
|
|
249
|
-
//
|
|
249
|
+
// Register health check module
|
|
250
250
|
HealthModule.forRoot({
|
|
251
251
|
indicators: [
|
|
252
252
|
{
|
|
253
253
|
name: 'db',
|
|
254
254
|
async check() {
|
|
255
|
-
//
|
|
255
|
+
// Check database connection
|
|
256
256
|
return { status: 'up' };
|
|
257
257
|
},
|
|
258
258
|
},
|
|
259
259
|
],
|
|
260
260
|
});
|
|
261
261
|
|
|
262
|
-
//
|
|
262
|
+
// Services using @ServiceRegistry decorator will automatically update based on health check status
|
|
263
263
|
```
|
|
264
264
|
|
|
265
|
-
##
|
|
265
|
+
## Service Invocation
|
|
266
266
|
|
|
267
|
-
###
|
|
267
|
+
### Basic Usage
|
|
268
268
|
|
|
269
269
|
```typescript
|
|
270
270
|
import {
|
|
@@ -296,7 +296,7 @@ class MyService {
|
|
|
296
296
|
}
|
|
297
297
|
```
|
|
298
298
|
|
|
299
|
-
###
|
|
299
|
+
### Using Decorator Injection
|
|
300
300
|
|
|
301
301
|
```typescript
|
|
302
302
|
import { ServiceClient, Injectable } from '@dangao/bun-server';
|
|
@@ -317,12 +317,12 @@ class MyService {
|
|
|
317
317
|
}
|
|
318
318
|
```
|
|
319
319
|
|
|
320
|
-
###
|
|
320
|
+
### Load Balancing
|
|
321
321
|
|
|
322
|
-
ServiceClient
|
|
322
|
+
ServiceClient supports multiple load balancing strategies:
|
|
323
323
|
|
|
324
324
|
```typescript
|
|
325
|
-
//
|
|
325
|
+
// Random load balancing
|
|
326
326
|
await serviceClient.call({
|
|
327
327
|
serviceName: 'user-service',
|
|
328
328
|
method: 'GET',
|
|
@@ -330,7 +330,7 @@ await serviceClient.call({
|
|
|
330
330
|
loadBalanceStrategy: 'random',
|
|
331
331
|
});
|
|
332
332
|
|
|
333
|
-
//
|
|
333
|
+
// Round-robin load balancing
|
|
334
334
|
await serviceClient.call({
|
|
335
335
|
serviceName: 'user-service',
|
|
336
336
|
method: 'GET',
|
|
@@ -338,7 +338,7 @@ await serviceClient.call({
|
|
|
338
338
|
loadBalanceStrategy: 'roundRobin',
|
|
339
339
|
});
|
|
340
340
|
|
|
341
|
-
//
|
|
341
|
+
// Weighted round-robin
|
|
342
342
|
await serviceClient.call({
|
|
343
343
|
serviceName: 'user-service',
|
|
344
344
|
method: 'GET',
|
|
@@ -346,7 +346,7 @@ await serviceClient.call({
|
|
|
346
346
|
loadBalanceStrategy: 'weightedRoundRobin',
|
|
347
347
|
});
|
|
348
348
|
|
|
349
|
-
//
|
|
349
|
+
// Consistent hashing (for scenarios requiring session affinity)
|
|
350
350
|
await serviceClient.call({
|
|
351
351
|
serviceName: 'user-service',
|
|
352
352
|
method: 'GET',
|
|
@@ -355,7 +355,7 @@ await serviceClient.call({
|
|
|
355
355
|
consistentHashKey: 'user-id-123',
|
|
356
356
|
});
|
|
357
357
|
|
|
358
|
-
//
|
|
358
|
+
// Least active
|
|
359
359
|
await serviceClient.call({
|
|
360
360
|
serviceName: 'user-service',
|
|
361
361
|
method: 'GET',
|
|
@@ -364,9 +364,9 @@ await serviceClient.call({
|
|
|
364
364
|
});
|
|
365
365
|
```
|
|
366
366
|
|
|
367
|
-
###
|
|
367
|
+
### Streaming Calls
|
|
368
368
|
|
|
369
|
-
|
|
369
|
+
Supports streaming responses like Server-Sent Events:
|
|
370
370
|
|
|
371
371
|
```typescript
|
|
372
372
|
const stream = await serviceClient.callStream({
|
|
@@ -375,7 +375,7 @@ const stream = await serviceClient.callStream({
|
|
|
375
375
|
path: '/api/events',
|
|
376
376
|
});
|
|
377
377
|
|
|
378
|
-
//
|
|
378
|
+
// Read stream data
|
|
379
379
|
const reader = stream.getReader();
|
|
380
380
|
while (true) {
|
|
381
381
|
const { done, value } = await reader.read();
|
|
@@ -384,9 +384,9 @@ while (true) {
|
|
|
384
384
|
}
|
|
385
385
|
```
|
|
386
386
|
|
|
387
|
-
###
|
|
387
|
+
### Interceptors
|
|
388
388
|
|
|
389
|
-
####
|
|
389
|
+
#### Request Interceptors
|
|
390
390
|
|
|
391
391
|
```typescript
|
|
392
392
|
import {
|
|
@@ -397,7 +397,7 @@ import {
|
|
|
397
397
|
serviceClient.addRequestInterceptor(new TraceIdRequestInterceptor());
|
|
398
398
|
serviceClient.addRequestInterceptor(new RequestLogInterceptor());
|
|
399
399
|
|
|
400
|
-
//
|
|
400
|
+
// Custom request interceptor
|
|
401
401
|
serviceClient.addRequestInterceptor({
|
|
402
402
|
async intercept(options) {
|
|
403
403
|
options.headers = {
|
|
@@ -409,7 +409,7 @@ serviceClient.addRequestInterceptor({
|
|
|
409
409
|
});
|
|
410
410
|
```
|
|
411
411
|
|
|
412
|
-
####
|
|
412
|
+
#### Response Interceptors
|
|
413
413
|
|
|
414
414
|
```typescript
|
|
415
415
|
import {
|
|
@@ -420,10 +420,10 @@ import {
|
|
|
420
420
|
serviceClient.addResponseInterceptor(new ResponseLogInterceptor());
|
|
421
421
|
serviceClient.addResponseInterceptor(new ErrorHandlerInterceptor());
|
|
422
422
|
|
|
423
|
-
//
|
|
423
|
+
// Custom response interceptor
|
|
424
424
|
serviceClient.addResponseInterceptor({
|
|
425
425
|
async intercept(response) {
|
|
426
|
-
//
|
|
426
|
+
// Transform response data
|
|
427
427
|
return {
|
|
428
428
|
...response,
|
|
429
429
|
data: transformData(response.data),
|
|
@@ -432,24 +432,27 @@ serviceClient.addResponseInterceptor({
|
|
|
432
432
|
});
|
|
433
433
|
```
|
|
434
434
|
|
|
435
|
-
##
|
|
435
|
+
## Service Governance
|
|
436
436
|
|
|
437
|
-
###
|
|
437
|
+
### Circuit Breaker
|
|
438
438
|
|
|
439
|
-
####
|
|
439
|
+
#### Using Decorator
|
|
440
440
|
|
|
441
441
|
```typescript
|
|
442
442
|
import { CircuitBreaker, Injectable } from '@dangao/bun-server';
|
|
443
443
|
|
|
444
444
|
@Injectable()
|
|
445
445
|
class MyService {
|
|
446
|
-
@CircuitBreaker(
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
446
|
+
@CircuitBreaker(
|
|
447
|
+
{
|
|
448
|
+
failureThreshold: 0.5,
|
|
449
|
+
timeWindow: 60000,
|
|
450
|
+
minimumRequests: 10,
|
|
451
|
+
},
|
|
452
|
+
'fallbackMethod',
|
|
453
|
+
)
|
|
451
454
|
public async callExternalService() {
|
|
452
|
-
//
|
|
455
|
+
// Automatically applies circuit breaker protection
|
|
453
456
|
return await externalService.call();
|
|
454
457
|
}
|
|
455
458
|
|
|
@@ -459,7 +462,7 @@ class MyService {
|
|
|
459
462
|
}
|
|
460
463
|
```
|
|
461
464
|
|
|
462
|
-
####
|
|
465
|
+
#### Manual Usage
|
|
463
466
|
|
|
464
467
|
```typescript
|
|
465
468
|
import { CircuitBreaker } from '@dangao/bun-server';
|
|
@@ -474,15 +477,15 @@ const result = await circuitBreaker.execute(
|
|
|
474
477
|
return await serviceClient.call(options);
|
|
475
478
|
},
|
|
476
479
|
async () => {
|
|
477
|
-
//
|
|
480
|
+
// Fallback handling
|
|
478
481
|
return { fallback: true };
|
|
479
482
|
},
|
|
480
483
|
);
|
|
481
484
|
```
|
|
482
485
|
|
|
483
|
-
###
|
|
486
|
+
### Rate Limiting
|
|
484
487
|
|
|
485
|
-
####
|
|
488
|
+
#### In-Memory Rate Limiting
|
|
486
489
|
|
|
487
490
|
```typescript
|
|
488
491
|
import { RateLimiter } from '@dangao/bun-server';
|
|
@@ -498,12 +501,12 @@ if (!allowed) {
|
|
|
498
501
|
}
|
|
499
502
|
```
|
|
500
503
|
|
|
501
|
-
####
|
|
504
|
+
#### Distributed Rate Limiting (Redis)
|
|
502
505
|
|
|
503
506
|
```typescript
|
|
504
507
|
import { RedisRateLimiter } from '@dangao/bun-server';
|
|
505
508
|
|
|
506
|
-
//
|
|
509
|
+
// Redis client needs to be provided
|
|
507
510
|
const redisClient = {
|
|
508
511
|
get: async (key: string) => await redis.get(key),
|
|
509
512
|
set: async (key: string, value: string, options?: any) => {
|
|
@@ -528,7 +531,7 @@ const rateLimiter = new RedisRateLimiter(
|
|
|
528
531
|
const allowed = await rateLimiter.allow('service-key');
|
|
529
532
|
```
|
|
530
533
|
|
|
531
|
-
###
|
|
534
|
+
### Retry Strategy
|
|
532
535
|
|
|
533
536
|
```typescript
|
|
534
537
|
import {
|
|
@@ -536,34 +539,37 @@ import {
|
|
|
536
539
|
ExponentialBackoffRetryStrategy,
|
|
537
540
|
} from '@dangao/bun-server';
|
|
538
541
|
|
|
539
|
-
//
|
|
542
|
+
// Fixed interval retry
|
|
540
543
|
const fixedRetry = new FixedIntervalRetryStrategy({
|
|
541
544
|
maxRetries: 3,
|
|
542
545
|
retryDelay: 1000,
|
|
543
546
|
});
|
|
544
547
|
|
|
545
|
-
//
|
|
548
|
+
// Exponential backoff retry
|
|
546
549
|
const exponentialRetry = new ExponentialBackoffRetryStrategy({
|
|
547
550
|
maxRetries: 3,
|
|
548
551
|
baseDelay: 1000,
|
|
549
552
|
maxDelay: 30000,
|
|
550
553
|
});
|
|
551
554
|
|
|
552
|
-
//
|
|
555
|
+
// Use in ServiceClient
|
|
553
556
|
serviceClient.setDefaultRetryStrategy({
|
|
554
557
|
maxRetries: 3,
|
|
555
558
|
retryDelay: 1000,
|
|
556
559
|
exponentialBackoff: true,
|
|
557
560
|
shouldRetry: (error) => {
|
|
558
|
-
//
|
|
559
|
-
return
|
|
561
|
+
// Only retry on network errors
|
|
562
|
+
return (
|
|
563
|
+
error.message.includes('timeout') ||
|
|
564
|
+
error.message.includes('network')
|
|
565
|
+
);
|
|
560
566
|
},
|
|
561
567
|
});
|
|
562
568
|
```
|
|
563
569
|
|
|
564
|
-
##
|
|
570
|
+
## Monitoring and Tracing
|
|
565
571
|
|
|
566
|
-
###
|
|
572
|
+
### Distributed Tracing
|
|
567
573
|
|
|
568
574
|
```typescript
|
|
569
575
|
import {
|
|
@@ -579,40 +585,40 @@ const tracer = new Tracer({
|
|
|
579
585
|
|
|
580
586
|
tracer.addCollector(new ConsoleTraceCollector());
|
|
581
587
|
|
|
582
|
-
//
|
|
588
|
+
// Use in ServiceClient
|
|
583
589
|
serviceClient.setTracer(tracer);
|
|
584
590
|
|
|
585
|
-
//
|
|
591
|
+
// Manually create span
|
|
586
592
|
const span = tracer.startSpan('my-operation', SpanKind.INTERNAL);
|
|
587
593
|
tracer.setSpanTags(span.context.spanId, {
|
|
588
594
|
'operation.name': 'process-data',
|
|
589
595
|
'user.id': '123',
|
|
590
596
|
});
|
|
591
|
-
// ...
|
|
597
|
+
// ... execute operation
|
|
592
598
|
tracer.endSpan(span.context.spanId, SpanStatus.OK);
|
|
593
599
|
```
|
|
594
600
|
|
|
595
|
-
###
|
|
601
|
+
### Service Monitoring
|
|
596
602
|
|
|
597
603
|
```typescript
|
|
598
604
|
import { ServiceMetricsCollector } from '@dangao/bun-server';
|
|
599
605
|
|
|
600
606
|
const metricsCollector = new ServiceMetricsCollector({
|
|
601
607
|
enabled: true,
|
|
602
|
-
autoReportToMetrics: true, //
|
|
608
|
+
autoReportToMetrics: true, // Automatically report to MetricsModule
|
|
603
609
|
});
|
|
604
610
|
|
|
605
|
-
//
|
|
611
|
+
// Use in ServiceClient
|
|
606
612
|
serviceClient.setMetricsCollector(metricsCollector);
|
|
607
613
|
|
|
608
|
-
//
|
|
614
|
+
// Query metrics
|
|
609
615
|
const allMetrics = metricsCollector.getAllMetrics();
|
|
610
616
|
const healthStatus = metricsCollector.getAllHealthStatus();
|
|
611
617
|
```
|
|
612
618
|
|
|
613
|
-
### Prometheus
|
|
619
|
+
### Prometheus Integration
|
|
614
620
|
|
|
615
|
-
|
|
621
|
+
Service monitoring metrics are automatically reported to MetricsModule and exported in Prometheus format via the `/metrics` endpoint:
|
|
616
622
|
|
|
617
623
|
```typescript
|
|
618
624
|
import { MetricsModule } from '@dangao/bun-server';
|
|
@@ -621,60 +627,59 @@ MetricsModule.forRoot({
|
|
|
621
627
|
enableHttpMetrics: true,
|
|
622
628
|
});
|
|
623
629
|
|
|
624
|
-
//
|
|
630
|
+
// Access http://localhost:3000/metrics to get Prometheus format metrics
|
|
625
631
|
```
|
|
626
632
|
|
|
627
|
-
|
|
628
|
-
- `service_calls_total` -
|
|
629
|
-
- `service_calls_success_total` -
|
|
630
|
-
- `service_calls_failure_total` -
|
|
631
|
-
- `service_call_latency_avg_ms` -
|
|
632
|
-
- `service_call_latency_min_ms` -
|
|
633
|
-
- `service_call_latency_max_ms` -
|
|
634
|
-
- `service_call_error_rate` -
|
|
635
|
-
- `service_instance_healthy` -
|
|
636
|
-
- `service_instance_consecutive_failures` -
|
|
637
|
-
|
|
638
|
-
## 最佳实践
|
|
633
|
+
Exported metrics include:
|
|
634
|
+
- `service_calls_total` - Total service calls
|
|
635
|
+
- `service_calls_success_total` - Successful calls
|
|
636
|
+
- `service_calls_failure_total` - Failed calls
|
|
637
|
+
- `service_call_latency_avg_ms` - Average latency
|
|
638
|
+
- `service_call_latency_min_ms` - Minimum latency
|
|
639
|
+
- `service_call_latency_max_ms` - Maximum latency
|
|
640
|
+
- `service_call_error_rate` - Error rate
|
|
641
|
+
- `service_instance_healthy` - Instance health status
|
|
642
|
+
- `service_instance_consecutive_failures` - Consecutive failures
|
|
639
643
|
|
|
640
|
-
|
|
644
|
+
## Best Practices
|
|
641
645
|
|
|
642
|
-
|
|
643
|
-
- 为不同环境使用不同的命名空间
|
|
644
|
-
- 启用配置监听以实现热更新
|
|
645
|
-
- 设置合理的配置优先级
|
|
646
|
+
### 1. Configuration Management
|
|
646
647
|
|
|
647
|
-
|
|
648
|
+
- Use configuration center to manage dynamic configurations
|
|
649
|
+
- Use different namespaces for different environments
|
|
650
|
+
- Enable configuration watching for hot updates
|
|
651
|
+
- Set reasonable configuration priorities
|
|
648
652
|
|
|
649
|
-
|
|
650
|
-
- 配置合理的服务元数据(版本、权重等)
|
|
651
|
-
- 启用健康检查集成
|
|
652
|
-
- 确保应用关闭时正确注销服务
|
|
653
|
+
### 2. Service Registration
|
|
653
654
|
|
|
654
|
-
|
|
655
|
+
- Use `@ServiceRegistry` decorator for automatic registration
|
|
656
|
+
- Configure reasonable service metadata (version, weight, etc.)
|
|
657
|
+
- Enable health check integration
|
|
658
|
+
- Ensure services are properly deregistered when application shuts down
|
|
655
659
|
|
|
656
|
-
|
|
657
|
-
- 配置合理的超时时间
|
|
658
|
-
- 使用拦截器统一处理请求/响应
|
|
659
|
-
- 启用追踪和监控
|
|
660
|
+
### 3. Service Invocation
|
|
660
661
|
|
|
661
|
-
|
|
662
|
+
- Choose appropriate load balancing strategy
|
|
663
|
+
- Configure reasonable timeout values
|
|
664
|
+
- Use interceptors for unified request/response handling
|
|
665
|
+
- Enable tracing and monitoring
|
|
662
666
|
|
|
663
|
-
|
|
664
|
-
- 配置合理的限流策略
|
|
665
|
-
- 实现降级处理逻辑
|
|
666
|
-
- 使用重试策略处理临时故障
|
|
667
|
+
### 4. Service Governance
|
|
667
668
|
|
|
668
|
-
|
|
669
|
+
- Enable circuit breaker for critical services
|
|
670
|
+
- Configure reasonable rate limiting strategies
|
|
671
|
+
- Implement fallback handling logic
|
|
672
|
+
- Use retry strategies for temporary failures
|
|
669
673
|
|
|
670
|
-
|
|
671
|
-
- 配置合理的采样率
|
|
672
|
-
- 集成 Prometheus 监控
|
|
673
|
-
- 设置告警规则
|
|
674
|
+
### 5. Observability
|
|
674
675
|
|
|
675
|
-
|
|
676
|
+
- Enable distributed tracing
|
|
677
|
+
- Configure reasonable sampling rates
|
|
678
|
+
- Integrate Prometheus monitoring
|
|
679
|
+
- Set up alerting rules
|
|
676
680
|
|
|
677
|
-
|
|
678
|
-
- [服务注册与发现使用指南](./microservice-service-registry.md)
|
|
679
|
-
- [Nacos 集成文档](./microservice-nacos.md)
|
|
681
|
+
## More Resources
|
|
680
682
|
|
|
683
|
+
- [Configuration Center Guide](./zh/microservice-config-center.md)
|
|
684
|
+
- [Service Registration and Discovery Guide](./zh/microservice-service-registry.md)
|
|
685
|
+
- [Nacos Integration Documentation](./zh/microservice-nacos.md)
|