# smart-load-balancer
**Repository Path**: d86e/smart-load-balancer
## Basic Information
- **Project Name**: smart-load-balancer
- **Description**: 本项目的目标是构建一个运行在浏览器环境中的负载均衡解决方案,能够在客户端智能选择最优服务端点,提升应用响应速度,增强容错能力,同时提供丰富的监控指标。
- **Primary Language**: JavaScript
- **License**: MIT
- **Default Branch**: main
- **Homepage**: https://github.com/d86e/smart-load-balancer
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2025-03-28
- **Last Updated**: 2025-03-31
## Categories & Tags
**Categories**: Uncategorized
**Tags**: 浏览器, 负载均衡器, JS负载均衡, fetch, Fetch负载均衡
## README
# 浏览器端负载均衡器
[](https://www.npmjs.com/package/smart-load-balancer)



[English](README.md)
一个高性能、可配置的浏览器端 JavaScript 网络请求负载均衡器,支持智能路由、健康检查和实时监控。
## 项目背景
现代 Web 应用常常需要与多个后端服务端点通信。随着应用规模的扩大和用户分布全球化,如何高效地选择最优服务端点成为提升应用性能的关键因素。传统的客户端负载均衡方案通常部署在服务端,而浏览器端缺乏类似的智能路由机制。
本项目的目标是构建一个运行在浏览器环境中的负载均衡解决方案,能够在客户端智能选择最优服务端点,提升应用响应速度,增强容错能力,同时提供丰富的监控指标。
## 设计目标
1. **性能优先**:通过实时性能评估选择最优服务端点
2. **高可靠性**:内置熔断机制和自动故障转移
3. **智能路由**:支持权重分配和区域感知路由
4. **可观测性**:提供详细的性能监控指标
5. **无侵入性**:与现有 fetch API 兼容,易于集成
6. **轻量级**:零生产依赖,精简代码体积
7. **轻量高效**:核心代码仅 15KB,无外部依赖
8. **易于集成**:简单 API 设计,快速接入现有项目
## 核心特性
### 智能路由
- 基于实时性能数据自动选择最优端点
- 支持服务器权重配置
- 区域感知路由(根据用户地理位置优化)
### 健康管理
- 定期健康检查
- 熔断机制(自动隔离故障节点)
- 失败重试与自动切换
### 性能优化
- 指数退避重试策略
- 健康检查请求优化(HEAD 方法)
- 请求去重与缓存控制
### 监控指标
- 实时服务器状态监控
- 请求成功率统计
- 延迟百分位计算
- 端点级性能分析
### 可扩展性
- 可插拔的拦截器系统
- 自定义评分算法
- 灵活的配置系统
## 核心算法
负载均衡器采用多维度评分算法选择最优服务端点:
```
服务器得分 =
(延迟得分 × 延迟权重) +
(成功率得分 × 成功率权重) +
(服务器权重 × 权重因子) +
(区域匹配得分 × 区域权重)
```
其中:
- **延迟得分**:基于历史请求延迟的倒数计算(延迟越低得分越高)
- **成功率得分**:基于历史请求成功率计算
- **区域匹配得分**:根据用户地理位置与服务器区域的匹配程度计算
## 安装方法
使用 npm 安装:
```bash
npm install smart-load-balancer
```
或通过 CDN 直接使用:
```html
```
## 快速开始
### 基本用法
```javascript
import LoadBalancer from "smart-load-balancer";
// 配置服务器列表
const servers = [
"https://api1.example.com",
"https://api2.example.com",
"https://api3.example.com",
];
// 创建负载均衡器实例
const lb = LoadBalancer.getInstance(servers);
// 发起请求
async function fetchData() {
try {
const response = await lb.get("/api/data");
const data = await response.json();
console.log(data);
} catch (error) {
console.error("Request failed:", error);
}
}
```
### 带配置的初始化
```javascript
const servers = [
{
url: "https://us-west.api.example.com",
region: "us-west",
weight: 1.2,
},
{
url: "https://eu.api.example.com",
region: "europe",
weight: 1.0,
},
];
const config = {
healthCheckInterval: 30000,
enableRegionalRouting: true,
};
const lb = LoadBalancer.getInstance(servers, config);
```
## 高级用法
### 自定义拦截器
```javascript
// 请求拦截器 - 添加认证头
lb.addRequestInterceptor((options) => {
const token = localStorage.getItem("authToken");
if (token) {
return {
...options,
headers: {
...options.headers,
Authorization: `Bearer ${token}`,
},
};
}
return options;
});
// 响应拦截器 - 统一处理JSON
lb.addResponseInterceptor(async (response) => {
const contentType = response.headers.get("content-type");
if (contentType?.includes("application/json")) {
const data = await response.json();
return { ...response, data };
}
return response;
});
// 错误拦截器 - 统一错误格式
lb.addErrorInterceptor((error) => {
console.error("Request failed:", error);
throw new Error(`API request failed: ${error.message}`);
});
```
### 自定义评分算法
```javascript
const config = {
scoringWeights: {
latency: 0.7, // 提高延迟权重
successRate: 0.2,
serverWeight: 0.1,
region: 0.3, // 启用区域路由
},
};
```
### 动态配置更新
```javascript
// 运行时更新配置
lb.updateConfig({
healthCheckInterval: 60000,
circuitBreakerThreshold: 3,
});
```
## 监控状态
### 获取服务器状态
```javascript
const stats = lb.getServerStats();
console.table(stats);
```
输出示例:
| URL | Region | Weight | Health | Success Rate | Avg Latency |
| ------------------------------- | ------- | ------ | -------- | ------------ | ----------- |
| https://us-west.api.example.com | us-west | 1.2 | healthy | 0.98 | 124ms |
| https://eu.api.example.com | europe | 1.0 | degraded | 0.82 | 236ms |
### 获取性能指标
```javascript
const metrics = lb.getPerformanceMetrics();
console.log("Total requests:", metrics.requests);
console.log("Success rate:", metrics.successRate);
console.log("Average latency:", metrics.avgLatency);
```
### 获取用户位置
```javascript
const location = lb.getUserLocation();
console.log("User location:", location.country, location.region);
```
## 核心 API
### `LoadBalancer.getInstance(servers, config)`
获取负载均衡器单例实例
**参数**:
- `servers`: (Array) 服务器列表,可以是字符串数组或对象数组
- `config`: (Object) 可选配置对象
**示例**:
```javascript
// 简单配置
const lb1 = LoadBalancer.getInstance([
"https://api1.example.com",
"https://api2.example.com",
]);
// 高级配置
const lb2 = LoadBalancer.getInstance(
[
{
url: "https://us-api.example.com",
region: "us-west",
weight: 1.2,
},
],
{
healthCheckInterval: 30000,
}
);
```
### `request(path, options, attempt)`
发起请求的核心方法
**参数**:
- `path`: (String) 请求路径
- `options`: (Object) fetch 请求选项
- `attempt`: (Number) 内部重试计数
**返回值**: Promise
**示例**:
```javascript
lb.request("/api/data", {
method: "POST",
body: JSON.stringify({ key: "value" }),
})
.then((response) => response.json())
.then((data) => console.log(data));
```
## 便捷方法
### `get(path, options)`
发起 GET 请求
**示例**:
```javascript
lb.get("/api/users")
.then((res) => res.json())
.then((users) => console.log(users));
```
### `post(path, body, options)`
发起 POST 请求
**示例**:
```javascript
lb.post("/api/users", { name: "John" })
.then((res) => res.json())
.then((result) => console.log(result));
```
## 配置管理
### `updateConfig(newConfig)`
更新负载均衡器配置
**参数**:
- `newConfig`: (Object) 新的配置对象
**示例**:
```javascript
lb.updateConfig({
healthCheckInterval: 60000,
maxRetryAttempts: 5,
});
```
## 拦截器 API
### `addRequestInterceptor(interceptor)`
添加请求拦截器
**参数**:
- `interceptor`: (Function) 拦截器函数
**示例**:
```javascript
lb.addRequestInterceptor((options) => {
console.log("Request options:", options);
return {
...options,
headers: {
...options.headers,
"X-Trace-Id": generateTraceId(),
},
};
});
```
### `addResponseInterceptor(interceptor)`
添加响应拦截器
**示例**:
```javascript
lb.addResponseInterceptor(async (response) => {
if (!response.ok) {
const error = await response.text();
throw new Error(error);
}
return response;
});
```
### `addErrorInterceptor(interceptor)`
添加错误拦截器
**示例**:
```javascript
lb.addErrorInterceptor((error) => {
Sentry.captureException(error);
throw error;
});
```
## 监控 API
### `getServerStats()`
获取服务器状态统计
**返回值**: Array