A Node.js client side IPC library for microservice, like Java Netflix Ribbon , support
- HTTP protocol call
- load balance: Random, RoundRibbon
- service discovery support nacos
这是一个客户端负载均衡 IPC 库,为微服务场景设计, 参照 Java Netflix Ribbon。 目前支持:
- HTTP 协议调用; 可扩展
- 负载均衡支持两种算法: 随机, 轮训; 可扩展
- 服务发现支持 nacos
- 支持纯服务地址列表
- 支持 fallback
//sleep util function
async function sleep(time) {
return new Promise((resolve: any) => {
setTimeout(() => {
resolve();
}, time);
});
}
//create service discovery
let nacosIns = await ServiceDiscoveryFactory.createDiscovery({
type: ServiceDiscoveryTypeEnum.Nacos,
option: {
logger,
serverList: '127.0.0.1:8849',
namespace: 'public',
},
});
//create loadbalancer
const lbIns = LoadBalancerFactory.getLoadBalancer({ type: LoadBalancerEnum.Random });
//create ribbon instance
const ribbonIns = new RibbonBuilder({
service: {
type: ServiceNameTypeEnum.Name,
serviceName: 'provider',
},
})
.withLoadBalancer(lbIns)
.withServiceDiscovery(nacosIns)
.build();
//wait for setup
await ribbonIns.setup();
//get http client
const httpClient = await ribbonIns.getHttpClient({
fallback: (err) => {
console.log('req err:', err);
},
});
await sleep(3000);
try {
//send request
for (let i = 0, ilen = 10; i < ilen; i++) {
const ret = await httpClient.get('/hello');
console.log('random ret:', ret.data);
}
} catch (e) {
console.error(e);
}
- node ./test/server.js
- npm run test