All Projects → wefront → node-codis

wefront / node-codis

Licence: MIT license
Codis client for Node.js.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to node-codis

Ansible Kubernetes Openshift Pi3
Ansible playbooks for setting up a Kubernetes Raspberry Pi 3 cluster
Stars: ✭ 189 (+950%)
Mutual labels:  cluster
Oq Engine
OpenQuake's Engine for Seismic Hazard and Risk Analysis
Stars: ✭ 207 (+1050%)
Mutual labels:  cluster
Turing Pi Cluster
Turing Pi cluster configuration for Raspberry Pi Compute Modules
Stars: ✭ 233 (+1194.44%)
Mutual labels:  cluster
Deploykit
A toolkit for creating and managing declarative, self-healing infrastructure.
Stars: ✭ 2,237 (+12327.78%)
Mutual labels:  cluster
Dcos
DC/OS - The Datacenter Operating System
Stars: ✭ 2,316 (+12766.67%)
Mutual labels:  cluster
Pyslurm
Python Interface to Slurm
Stars: ✭ 222 (+1133.33%)
Mutual labels:  cluster
Dst Dedicated Server
Don't Starve Together dedicated server guide for all platforms (Linux, Mac, Windows) with Docker. Extensive documentation covering mods installation, server config and performance, world generation and setting up admins.
Stars: ✭ 187 (+938.89%)
Mutual labels:  cluster
Mosquitto Cluster
a built-in, autonomous Mosquitto Cluster implementation. MQTT集群.
Stars: ✭ 238 (+1222.22%)
Mutual labels:  cluster
Tiup
A component manager for TiDB
Stars: ✭ 207 (+1050%)
Mutual labels:  cluster
Redis Cluster
Redis Cluster setup running on Kubernetes
Stars: ✭ 230 (+1177.78%)
Mutual labels:  cluster
Cookim
Distributed web chat application base websocket built on akka.
Stars: ✭ 198 (+1000%)
Mutual labels:  cluster
Cluster Lifecycle Manager
Cluster Lifecycle Manager (CLM) to provision and update multiple Kubernetes clusters
Stars: ✭ 200 (+1011.11%)
Mutual labels:  cluster
Reading And Comprehense Redis Cluster
分布式NOSQL redis源码阅读中文分析注释,带详尽注释以及相关流程调用注释,提出改造点,redis cluster集群功能、节点扩容、槽位迁移、failover故障切换、一致性选举完整分析,对理解redis源码很有帮助,解决了source insight中文注释乱码问题,更新完毕(redis源码学习交流QQ群:568892619)
Stars: ✭ 224 (+1144.44%)
Mutual labels:  cluster
Actionhero
Actionhero is a realtime multi-transport nodejs API Server with integrated cluster capabilities and delayed tasks
Stars: ✭ 2,280 (+12566.67%)
Mutual labels:  cluster
Raspi Cluster
Notes and scripts for setting up (yet another) Raspberry Pi computing cluster
Stars: ✭ 235 (+1205.56%)
Mutual labels:  cluster
Gateway
🚀构建分布式即时聊天、消息推送系统。 Building distributed instant messaging, push notification systems.
Stars: ✭ 188 (+944.44%)
Mutual labels:  cluster
Hkube
🐟 High Performance Computing over Kubernetes - Core Repo 🎣
Stars: ✭ 214 (+1088.89%)
Mutual labels:  cluster
Magento-2-aws-cluster-terraform
Magento 2 AWS autoscaling cluster with Terraform and Packer or ImageBuilder. Adobe Commerce Cloud alternative. The best ecommerce infrastructure. Drive more sales online. Transparent billing. Developer-friendly. No hidden bottlenecks.
Stars: ✭ 107 (+494.44%)
Mutual labels:  cluster
Lokomotive
Lokomotive is a 100% open-source, easy to use and secure Kubernetes distribution from the volks at Kinvolk
Stars: ✭ 233 (+1194.44%)
Mutual labels:  cluster
Coerce Rs
Coerce - an asynchronous (async/await) Actor runtime and cluster framework for Rust
Stars: ✭ 231 (+1183.33%)
Mutual labels:  cluster

Node Codis

Node-codis is a codis client running on nodejs, Used to connect to redis cluster services.

Use the node-zookeeper-client library for service discovery.

Use the redis library to connect to the codis proxy service. The ioredis is supported as well.

中文说明

Getting started

Installation

npm i node-codis -S

Usage

const { NodeCodis } = require('node-codis')

const nodeCodis = new NodeCodis({
  zkServers: '127.0.0.1:6701, 127.0.0.1:6702',
  zkCodisProxyDir: '/zk/codis/db_test_node/proxy',
  codisPassword: 'your_codis_password'
})

nodeCodis.on('connected', (err, client) => {
  if (err) {
    console.log(err)
    return
  }

  // Expires after 100 seconds
  client.SETEX('node-codis:test', 100, 'hello world', NodeCodis.print)
  client.GET('node-codis:test', (err, data) => {
    console.log(data) // hello world
  })
})

ioredis

Codis instance with ioredis can be created by NodeIOCodis.

const Codis = require('node-codis')
const nodeCodis = new Codis.NodeIOCodis({
  // ...
})

nodeCodis.on('connected',
/**
 * @param {Codis.CodisIOClient} client
 */
(err, client) => {
  // ...
})

Please NOTE that NodeIOCodis doesn't have a print method like NodeCodis!

Documentation

Constructor Options

zkServers string required

Comma separated host:port pairs, each represents a ZooKeeper server. You can optionally append a chroot path, then the client would be rooted at the given path. e.g.

'localhost:3000,localhost:3001,localhost:3002'
'localhost:2181,localhost:2182/test'

zkCodisProxyDir string required

Node path of codis-proxy on zookeeper. NodeCodis will establish a connection with all codis-proxy in this directory, and then randomly select one as the client. link this:

/zk/codis/db_test_node/proxy

In the codis2.x, it is usually located at /zk/codis/db_${product_name}/proxy

In the codis3.x, If the server codis-proxy is configured as jodis_compatible = false, it is usually located at /jodis/${product_name}/proxy

codisPassword string optional

Login password for codis-proxy.

zkClientOpts object optional

An object to set the zookeeper client options. Currently available options are:

  • sessionTimeout Session timeout in milliseconds, defaults to 30 seconds.

  • spinDelay The delay (in milliseconds) between each connection attempts.

  • retries The number of retry attempts for connection loss exception.

Defaults options:

{
    sessionTimeout: 30000,
    spinDelay: 1000,
    retries: 1
}

For example:

const nodeCodis = new NodeCodis({
  zkServers: '127.0.0.1:6701, 127.0.0.1:6702',
  zkCodisProxyDir: '/zk/codis/db_test_node/proxy',
  codisPassword: 'your_codis_password',
  zkClientOpts: {
    sessionTimeout: 10000
  }
})

redisClientOpts object optional

We use redis to connect to the codis-proxy service, so you can pass in these parameters when redis creates the client.

Reference here https://github.com/NodeRedis/node_redis#rediscreateclient

log boolean | Function

Whether to enable the log, default open, use the debug library. The printed log looks like this:

node-codis Connect to codis at proxy:e203bf77d1c7b3e2c132984f14827c04 @192.168.3.62:19201 +0ms

node-codis Connect to codis at proxy:40297cde8c3453714459ab1c452c6c56 @192.168.3.72:19201 +7ms

You can also set up a custom logger, like this:

const nodeCodis = new NodeCodis({
  zkServers: '127.0.0.1:6701, 127.0.0.1:6702',
  zkCodisProxyDir: '/zk/codis/db_test_node/proxy',
  codisPassword: 'your_codis_password',
  log: console.log
})

You can pass false to close log.

proxyAddrKey string optional

Proxy address field. Usually when the codis-proxy is registered to zk, the field of the proxy address is called addr. If not, you can pass in a custom field. link this:

const nodeCodis = new NodeCodis({
  zkServers: '127.0.0.1:6701, 127.0.0.1:6702',
  zkCodisProxyDir: '/zk/codis/db_test_node/proxy',
  codisPassword: 'your_codis_password',
  proxyAddrKey: 'proxy_addr'
})

Property

codisClientPool

Return all connected codis client. link this:

const nodeCodis = new NodeCodis({
  zkServers: '127.0.0.1:6701, 127.0.0.1:6702',
  zkCodisProxyDir: '/zk/codis/db_test_node/proxy',
  codisPassword: 'your_codis_password'
})

console.log(nodeCodis.codisClientPool)

Event

connected

When all codis-proxy are connected for the first time, the connect event is fired and a connected codis client is randomly selected as the argument to the callback function. link this:

const nodeCodis = new NodeCodis({
  zkServers: '127.0.0.1:6701, 127.0.0.1:6702',
  zkCodisProxyDir: '/zk/codis/db_test_node/proxy',
  codisPassword: 'your_codis_password'
})

nodeCodis.on('connected', (err, client) => {
  if (err) {
    console.log(err)
    return
  }

  // Expires after 100 seconds
  client.SETEX('node-codis:test', 100, 'hello world', NodeCodis.print)
  client.GET('node-codis:test', (err, data) => {
    console.log(data) // hello world
  })
})

reconnected

When the zookeeper node of codis changes, the codis-proxy is reconnected internally, and the reconnected event is triggered, and a random connected codis client is passed to the callback function. link this:

const nodeCodis = new NodeCodis({
  zkServers: '127.0.0.1:6701, 127.0.0.1:6702',
  zkCodisProxyDir: '/zk/codis/db_test_node/proxy',
  codisPassword: 'your_codis_password'
})

nodeCodis.on('reconnected', (err, client) => {
  if (err) {
    console.log(err)
    return
  }

  console.log(client)
})

Static methods

getRandomClient(clientsMap: CodisClientPool)

Randomly get a connected codis client, if the client pool is empty, return null. You can use this method in some framework middleware to achieve load balancing. link this:

// app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var { NodeCodis } = require('node-codis')

var indexRouter = require('./routes/index');

var app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

const nodeCodis = new NodeCodis({
  zkServers: '127.0.0.1:6701, 127.0.0.1:6702',
  zkCodisProxyDir: '/zk/codis/db_test_node/proxy',
  codisPassword: 'your_codis_password'
})

nodeCodis.on('connected', (err, client) => {
  if (err) {
    console.log(err)
    return
  }
  app.set('codisClientPool', nodeCodis.codisClientPool)
})

app.use(function(req, res, next) {
  var codisClientPool = req.app.get('codisClientPool')
  req.codisClientPool = NodeCodis.getRandomClient(codisClientPool)
  next()
})

app.use('/', indexRouter);

app.use(function (req, res, next) {
  next(createError(404));
});

app.use(function (err, req, res, next) {
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
// routes/index.js
var express = require('express');
var router = express.Router();

router.get('/test', function (req, res) {
  const codisClient = req.codisClient
  if (codisClient) {
    console.log(codisClient.address)
  }
})

module.exports = router;

When you always request localhost:3000/test, printed as follows:

192.168.3.72:19201
GET /test - - ms - -
192.168.3.72:19201
GET /test - - ms - -
192.168.3.62:19201
GET /test - - ms - -
192.168.3.62:19201
GET /test - - ms - -
192.168.3.72:19201
GET /test - - ms - -
192.168.3.62:19201
GET /test - - ms - -
192.168.3.62:19201
GET /test - - ms - -
192.168.3.62:19201
GET /test - - ms - -
192.168.3.72:19201
GET /test - - ms - -
192.168.3.62:19201

Licence

NodeCodis is licensed under the MIT License - see the LICENSE file for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].