@dangao/bun-server 1.8.1 → 1.8.3
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/dist/controller/controller.d.ts.map +1 -1
- package/dist/core/application.d.ts.map +1 -1
- package/dist/error/handler.d.ts.map +1 -1
- package/dist/index.js +98 -21
- package/dist/middleware/builtin/error-handler.d.ts.map +1 -1
- package/dist/middleware/builtin/logger.d.ts.map +1 -1
- package/dist/router/decorators.d.ts +10 -10
- package/dist/router/decorators.d.ts.map +1 -1
- package/dist/router/router.d.ts.map +1 -1
- 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/src/controller/controller.ts +10 -2
- package/src/core/application.ts +11 -0
- package/src/error/handler.ts +17 -0
- package/src/middleware/builtin/error-handler.ts +10 -0
- package/src/middleware/builtin/logger.ts +17 -0
- package/src/router/decorators.ts +15 -15
- package/src/router/router.ts +18 -1
- package/tests/controller/path-combination.test.ts +135 -0
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Service Registration and Discovery Usage Guide
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This document introduces how to use the service registration and discovery features in Bun Server Framework.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Table of Contents
|
|
6
6
|
|
|
7
|
-
- [
|
|
8
|
-
- [
|
|
9
|
-
- [
|
|
10
|
-
- [
|
|
11
|
-
- [
|
|
12
|
-
- [
|
|
13
|
-
- [
|
|
7
|
+
- [Overview](#overview)
|
|
8
|
+
- [Quick Start](#quick-start)
|
|
9
|
+
- [Service Registration](#service-registration)
|
|
10
|
+
- [Service Discovery](#service-discovery)
|
|
11
|
+
- [Load Balancing](#load-balancing)
|
|
12
|
+
- [Health Check Integration](#health-check-integration)
|
|
13
|
+
- [Best Practices](#best-practices)
|
|
14
14
|
|
|
15
|
-
##
|
|
15
|
+
## Overview
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
Service registration and discovery provides core capabilities in microservice architecture:
|
|
18
18
|
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
19
|
+
- **Service Registration**: Register service instances to the registry
|
|
20
|
+
- **Service Discovery**: Discover service instances from the registry
|
|
21
|
+
- **Load Balancing**: Supports multiple load balancing strategies
|
|
22
|
+
- **Health Check**: Automatically update service health status based on health check results
|
|
23
|
+
- **Instance Watching**: Watch for service instance changes
|
|
24
24
|
|
|
25
|
-
##
|
|
25
|
+
## Quick Start
|
|
26
26
|
|
|
27
|
-
### 1.
|
|
27
|
+
### 1. Register Service Registry Module
|
|
28
28
|
|
|
29
29
|
```typescript
|
|
30
30
|
import { Application } from '@dangao/bun-server';
|
|
@@ -42,13 +42,13 @@ app.registerModule(
|
|
|
42
42
|
username: 'nacos',
|
|
43
43
|
password: 'nacos',
|
|
44
44
|
},
|
|
45
|
-
heartbeatInterval: 5000, //
|
|
45
|
+
heartbeatInterval: 5000, // Heartbeat interval (milliseconds)
|
|
46
46
|
},
|
|
47
47
|
}),
|
|
48
48
|
);
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
-
### 2.
|
|
51
|
+
### 2. Register Service
|
|
52
52
|
|
|
53
53
|
```typescript
|
|
54
54
|
import { ServiceRegistry, Controller, GET } from '@dangao/bun-server';
|
|
@@ -68,14 +68,14 @@ class UserController {
|
|
|
68
68
|
|
|
69
69
|
app.registerController(UserController);
|
|
70
70
|
await app.listen(3000);
|
|
71
|
-
//
|
|
71
|
+
// Service will be automatically registered to the registry
|
|
72
72
|
```
|
|
73
73
|
|
|
74
|
-
##
|
|
74
|
+
## Service Registration
|
|
75
75
|
|
|
76
|
-
###
|
|
76
|
+
### Automatic Registration with Decorator
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
The simplest way is to use the `@ServiceRegistry` decorator:
|
|
79
79
|
|
|
80
80
|
```typescript
|
|
81
81
|
@ServiceRegistry('user-service', {
|
|
@@ -97,19 +97,19 @@ class UserController {
|
|
|
97
97
|
}
|
|
98
98
|
```
|
|
99
99
|
|
|
100
|
-
|
|
101
|
-
- `serviceName
|
|
102
|
-
- `port
|
|
103
|
-
- `ip
|
|
104
|
-
- `weight
|
|
105
|
-
- `enabled
|
|
106
|
-
- `healthy
|
|
107
|
-
- `metadata
|
|
108
|
-
- `clusterName
|
|
109
|
-
- `namespaceId
|
|
110
|
-
- `groupName
|
|
100
|
+
**Configuration Description**:
|
|
101
|
+
- `serviceName`: Service name (required)
|
|
102
|
+
- `port`: Service port (optional, default from Application)
|
|
103
|
+
- `ip`: Service IP (optional, default from Application)
|
|
104
|
+
- `weight`: Service weight (for weighted load balancing)
|
|
105
|
+
- `enabled`: Whether enabled
|
|
106
|
+
- `healthy`: Initial health status
|
|
107
|
+
- `metadata`: Service metadata (version, region, etc.)
|
|
108
|
+
- `clusterName`: Cluster name
|
|
109
|
+
- `namespaceId`: Namespace
|
|
110
|
+
- `groupName`: Group name
|
|
111
111
|
|
|
112
|
-
###
|
|
112
|
+
### Manual Registration
|
|
113
113
|
|
|
114
114
|
```typescript
|
|
115
115
|
import {
|
|
@@ -142,7 +142,7 @@ class MyService {
|
|
|
142
142
|
}
|
|
143
143
|
|
|
144
144
|
public async renewService() {
|
|
145
|
-
//
|
|
145
|
+
// Renew service (heartbeat)
|
|
146
146
|
await this.registry.renew({
|
|
147
147
|
serviceName: 'my-service',
|
|
148
148
|
ip: '127.0.0.1',
|
|
@@ -151,7 +151,7 @@ class MyService {
|
|
|
151
151
|
}
|
|
152
152
|
|
|
153
153
|
public async deregisterService() {
|
|
154
|
-
//
|
|
154
|
+
// Deregister service
|
|
155
155
|
await this.registry.deregister({
|
|
156
156
|
serviceName: 'my-service',
|
|
157
157
|
ip: '127.0.0.1',
|
|
@@ -161,11 +161,11 @@ class MyService {
|
|
|
161
161
|
}
|
|
162
162
|
```
|
|
163
163
|
|
|
164
|
-
##
|
|
164
|
+
## Service Discovery
|
|
165
165
|
|
|
166
|
-
###
|
|
166
|
+
### Automatic Discovery with Decorator
|
|
167
167
|
|
|
168
|
-
|
|
168
|
+
Use the `@ServiceDiscovery` decorator to automatically inject service instance lists:
|
|
169
169
|
|
|
170
170
|
```typescript
|
|
171
171
|
import { ServiceDiscovery, Injectable } from '@dangao/bun-server';
|
|
@@ -174,23 +174,23 @@ import type { ServiceInstance } from '@dangao/bun-server';
|
|
|
174
174
|
@Injectable()
|
|
175
175
|
class MyService {
|
|
176
176
|
@ServiceDiscovery('user-service', {
|
|
177
|
-
healthyOnly: true, //
|
|
177
|
+
healthyOnly: true, // Only get healthy instances
|
|
178
178
|
namespaceId: 'public',
|
|
179
179
|
groupName: 'DEFAULT_GROUP',
|
|
180
180
|
})
|
|
181
181
|
public instances: ServiceInstance[] = [];
|
|
182
182
|
|
|
183
183
|
public async getAvailableInstances() {
|
|
184
|
-
// instances
|
|
184
|
+
// instances will be automatically updated
|
|
185
185
|
return this.instances;
|
|
186
186
|
}
|
|
187
187
|
}
|
|
188
188
|
```
|
|
189
189
|
|
|
190
|
-
###
|
|
190
|
+
### Manual Discovery
|
|
191
191
|
|
|
192
192
|
```typescript
|
|
193
|
-
//
|
|
193
|
+
// Get service instance list
|
|
194
194
|
const instances = await serviceRegistry.getInstances('user-service', {
|
|
195
195
|
healthyOnly: true,
|
|
196
196
|
namespaceId: 'public',
|
|
@@ -198,12 +198,12 @@ const instances = await serviceRegistry.getInstances('user-service', {
|
|
|
198
198
|
clusterName: 'default',
|
|
199
199
|
});
|
|
200
200
|
|
|
201
|
-
//
|
|
201
|
+
// Watch for service instance changes
|
|
202
202
|
const unsubscribe = serviceRegistry.watchInstances(
|
|
203
203
|
'user-service',
|
|
204
204
|
(newInstances) => {
|
|
205
205
|
console.log('Instances updated:', newInstances);
|
|
206
|
-
//
|
|
206
|
+
// Update local cache
|
|
207
207
|
updateLocalCache(newInstances);
|
|
208
208
|
},
|
|
209
209
|
{
|
|
@@ -212,19 +212,19 @@ const unsubscribe = serviceRegistry.watchInstances(
|
|
|
212
212
|
},
|
|
213
213
|
);
|
|
214
214
|
|
|
215
|
-
//
|
|
215
|
+
// Unsubscribe
|
|
216
216
|
unsubscribe();
|
|
217
217
|
```
|
|
218
218
|
|
|
219
|
-
##
|
|
219
|
+
## Load Balancing
|
|
220
220
|
|
|
221
|
-
ServiceClient
|
|
221
|
+
ServiceClient supports multiple load balancing strategies. For details, see [Service Invocation Documentation](./microservice.md#load-balancing).
|
|
222
222
|
|
|
223
|
-
##
|
|
223
|
+
## Health Check Integration
|
|
224
224
|
|
|
225
|
-
|
|
225
|
+
Service registration automatically integrates with the health check module, updating service health status based on health check results:
|
|
226
226
|
|
|
227
|
-
### 1.
|
|
227
|
+
### 1. Register Health Check Module
|
|
228
228
|
|
|
229
229
|
```typescript
|
|
230
230
|
import { HealthModule } from '@dangao/bun-server';
|
|
@@ -234,7 +234,7 @@ HealthModule.forRoot({
|
|
|
234
234
|
{
|
|
235
235
|
name: 'db',
|
|
236
236
|
async check() {
|
|
237
|
-
//
|
|
237
|
+
// Check database connection
|
|
238
238
|
const isHealthy = await checkDatabase();
|
|
239
239
|
return {
|
|
240
240
|
status: isHealthy ? 'up' : 'down',
|
|
@@ -245,7 +245,7 @@ HealthModule.forRoot({
|
|
|
245
245
|
{
|
|
246
246
|
name: 'cache',
|
|
247
247
|
async check() {
|
|
248
|
-
//
|
|
248
|
+
// Check cache connection
|
|
249
249
|
return { status: 'up' };
|
|
250
250
|
},
|
|
251
251
|
},
|
|
@@ -253,54 +253,53 @@ HealthModule.forRoot({
|
|
|
253
253
|
});
|
|
254
254
|
```
|
|
255
255
|
|
|
256
|
-
### 2.
|
|
256
|
+
### 2. Use @ServiceRegistry Decorator
|
|
257
257
|
|
|
258
|
-
|
|
258
|
+
Services using the `@ServiceRegistry` decorator will automatically update based on health check status:
|
|
259
259
|
|
|
260
260
|
```typescript
|
|
261
261
|
@ServiceRegistry('user-service')
|
|
262
262
|
@Controller('/api/users')
|
|
263
263
|
class UserController {
|
|
264
|
-
//
|
|
264
|
+
// Service will automatically update health status based on health check
|
|
265
265
|
}
|
|
266
266
|
```
|
|
267
267
|
|
|
268
|
-
### 3.
|
|
268
|
+
### 3. Health Check Update Mechanism
|
|
269
269
|
|
|
270
|
-
-
|
|
271
|
-
-
|
|
272
|
-
-
|
|
270
|
+
- Check health status every 30 seconds
|
|
271
|
+
- If health check fails, service will be marked as unhealthy
|
|
272
|
+
- Unhealthy service instances will not be selected by load balancer (if `healthyOnly: true` is set)
|
|
273
273
|
|
|
274
|
-
##
|
|
274
|
+
## Best Practices
|
|
275
275
|
|
|
276
|
-
### 1.
|
|
276
|
+
### 1. Service Registration
|
|
277
277
|
|
|
278
|
-
-
|
|
279
|
-
-
|
|
280
|
-
-
|
|
281
|
-
-
|
|
278
|
+
- **Use Decorator**: Prefer using `@ServiceRegistry` decorator for automatic registration
|
|
279
|
+
- **Configure Metadata**: Set metadata like version, region for easier management
|
|
280
|
+
- **Reasonable Weight**: Set weight based on service instance performance
|
|
281
|
+
- **Automatic Deregistration**: Ensure services are properly deregistered when application shuts down
|
|
282
282
|
|
|
283
|
-
### 2.
|
|
283
|
+
### 2. Service Discovery
|
|
284
284
|
|
|
285
|
-
-
|
|
286
|
-
-
|
|
287
|
-
-
|
|
285
|
+
- **Health Filtering**: Use `healthyOnly: true` to only get healthy instances
|
|
286
|
+
- **Instance Watching**: Watch for instance changes and update local cache promptly
|
|
287
|
+
- **Namespace**: Use namespaces to distinguish different environments
|
|
288
288
|
|
|
289
|
-
### 3.
|
|
289
|
+
### 3. Load Balancing
|
|
290
290
|
|
|
291
|
-
-
|
|
292
|
-
-
|
|
293
|
-
-
|
|
294
|
-
-
|
|
295
|
-
-
|
|
291
|
+
- **Random**: Suitable for stateless services
|
|
292
|
+
- **Round-robin**: Suitable for instances with similar performance
|
|
293
|
+
- **Weighted Round-robin**: Suitable for instances with significant performance differences
|
|
294
|
+
- **Consistent Hashing**: Suitable for scenarios requiring session affinity
|
|
295
|
+
- **Least Active**: Suitable for long connection scenarios
|
|
296
296
|
|
|
297
|
-
### 4.
|
|
297
|
+
### 4. Health Check
|
|
298
298
|
|
|
299
|
-
-
|
|
300
|
-
-
|
|
301
|
-
-
|
|
299
|
+
- **Critical Dependencies**: Add health checks for critical dependencies (database, cache, etc.)
|
|
300
|
+
- **Check Frequency**: Set check frequency reasonably (default 30 seconds)
|
|
301
|
+
- **Fast Failure**: Health checks should fail fast to avoid blocking
|
|
302
302
|
|
|
303
|
-
##
|
|
304
|
-
|
|
305
|
-
完整示例请参考 `examples/microservice-app.ts`。
|
|
303
|
+
## Examples
|
|
306
304
|
|
|
305
|
+
For complete examples, please refer to `examples/microservice-app.ts`.
|