@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.
Files changed (35) hide show
  1. package/dist/controller/controller.d.ts.map +1 -1
  2. package/dist/core/application.d.ts.map +1 -1
  3. package/dist/error/handler.d.ts.map +1 -1
  4. package/dist/index.js +98 -21
  5. package/dist/middleware/builtin/error-handler.d.ts.map +1 -1
  6. package/dist/middleware/builtin/logger.d.ts.map +1 -1
  7. package/dist/router/decorators.d.ts +10 -10
  8. package/dist/router/decorators.d.ts.map +1 -1
  9. package/dist/router/router.d.ts.map +1 -1
  10. package/docs/api.md +194 -81
  11. package/docs/extensions.md +53 -0
  12. package/docs/guide.md +243 -1
  13. package/docs/microservice-config-center.md +73 -74
  14. package/docs/microservice-nacos.md +89 -90
  15. package/docs/microservice-service-registry.md +85 -86
  16. package/docs/microservice.md +142 -137
  17. package/docs/request-lifecycle.md +45 -4
  18. package/docs/symbol-interface-pattern.md +106 -106
  19. package/docs/zh/api.md +458 -18
  20. package/docs/zh/extensions.md +53 -0
  21. package/docs/zh/guide.md +251 -4
  22. package/docs/zh/microservice-config-center.md +258 -0
  23. package/docs/zh/microservice-nacos.md +346 -0
  24. package/docs/zh/microservice-service-registry.md +306 -0
  25. package/docs/zh/microservice.md +680 -0
  26. package/docs/zh/request-lifecycle.md +43 -5
  27. package/package.json +1 -1
  28. package/src/controller/controller.ts +10 -2
  29. package/src/core/application.ts +11 -0
  30. package/src/error/handler.ts +17 -0
  31. package/src/middleware/builtin/error-handler.ts +10 -0
  32. package/src/middleware/builtin/logger.ts +17 -0
  33. package/src/router/decorators.ts +15 -15
  34. package/src/router/router.ts +18 -1
  35. package/tests/controller/path-combination.test.ts +135 -0
@@ -1,30 +1,30 @@
1
- # 服务注册与发现使用指南
1
+ # Service Registration and Discovery Usage Guide
2
2
 
3
- 本文档介绍如何使用 Bun Server Framework 的服务注册与发现功能。
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
- 最简单的方式是使用 `@ServiceRegistry` 装饰器:
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`:服务端口(可选,默认从 Application 获取)
103
- - `ip`:服务 IP(可选,默认从 Application 获取)
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
- 使用 `@ServiceDiscovery` 装饰器自动注入服务实例列表:
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 支持多种负载均衡策略,详见[服务调用文档](./microservice.md#负载均衡)
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. 使用 @ServiceRegistry 装饰器
256
+ ### 2. Use @ServiceRegistry Decorator
257
257
 
258
- 使用 `@ServiceRegistry` 装饰器的服务会自动根据健康检查状态更新:
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
- - 30 秒检查一次健康状态
271
- - 如果健康检查失败,服务会被标记为不健康
272
- - 不健康的服务实例不会被负载均衡器选择(如果设置了 `healthyOnly: true`)
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
- - **使用装饰器**:优先使用 `@ServiceRegistry` 装饰器自动注册
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
- - **健康过滤**:使用 `healthyOnly: true` 只获取健康实例
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
- - **检查频率**:合理设置检查频率(默认 30 秒)
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`.