New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

node-ribbon

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-ribbon

A Node.js microservice IPC Library, like Java Netflix Ribbon , support HTTP protocol

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

Introduce

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

用法

nacos demo

//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

Keywords

ribbon

FAQs

Package last updated on 16 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts