一个轻量级的企业级 Node.js 服务间通信模块,基于 JSON-RPC 2.0 协议。提供简单的 API 让服务之间像调用本地函数一样进行远程调用,无需复杂的 proto 文件定义,开箱即用。
- 🚀 开箱即用 - 无需 proto 文件,API 简洁
- 🔌 多传输协议 - 支持 HTTP、Socket (TCP)、HTTP2
- 📦 零依赖 - 仅使用 Node.js 内置模块(v0.1.0)
- 🎯 JSON-RPC 2.0 - 严格遵循协议标准
- 🔧 TypeScript 支持 - 提供完整的类型定义
- 🏢 企业级 - 分布式能力、服务发现、负载均衡(v0.2.0+)
- 📊 可观测性 - Prometheus 监控、健康检查(v0.3.0+)
npm install jrpc服务端:
const { Server } = require('jrpc');
const server = new Server({
transport: 'http',
port: 3000
});
// 注册方法
server.register('add', (a, b) => a + b);
server.register('getUserById', async (id) => {
const user = await db.users.findById(id);
return user;
});
await server.listen();
console.log('Server started on port 3000');客户端:
const { Client } = require('jrpc');
const client = new Client({
transport: 'http',
host: 'localhost',
port: 3000
});
// 调用远程方法
const result = await client.call('add', [1, 2]);
console.log(result); // => 3
// 批量调用
const results = await client.batch([
{ method: 'add', params: [1, 2] },
{ method: 'add', params: [3, 4] }
]);
console.log(results); // => [3, 7]
await client.close();// HTTP
const client = new Client({ transport: 'http', host: 'localhost', port: 3000 });
// Socket (TCP)
const client = new Client({ transport: 'socket', host: 'localhost', port: 3001 });
// HTTP2
const client = new Client({ transport: 'http2', host: 'localhost', port: 3002 });- ✅ JSON-RPC 2.0 协议实现
- ✅ HTTP/Socket/HTTP2 传输层
- ✅ 客户端/服务端核心功能
- ✅ TypeScript 类型定义
- 🔄 服务发现(Consul、etcd、Nacos)
- 🔄 负载均衡(轮询、随机、最少连接)
- 🔄 连接池(HTTP Keep-Alive、Socket 复用)
- 🔄 健康检查(自动摘除/恢复)
- 📊 Prometheus 监控指标
- 🔄 超时控制与自动重试
- 🛡️ 熔断降级
- 📝 结构化日志
- ✅ 完整文档
- ✅ 生产案例
- ✅ 性能基准测试
# 运行测试
npm test
# 测试覆盖率
npm run test:coverage
# 代码检查
npm run lint欢迎贡献!请查看 CONTRIBUTING.md 了解详情。
本项目受以下项目启发: