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 = const nodeCodis = zkServers: '127.0.0.1:6701, 127.0.0.1:6702' zkCodisProxyDir: '/zk/codis/db_test_node/proxy' codisPassword: 'your_codis_password' nodeCodis
ioredis
Codis instance with ioredis can be created by NodeIOCodis
.
const Codis = const nodeCodis = // ... nodeCodis
Please NOTE that
NodeIOCodis
doesn't have aNodeCodis
!
Documentation
Constructor Options
string
required
zkServers 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'
string
required
zkCodisProxyDir 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 asjodis_compatible = false
, it is usually located at/jodis/${product_name}/proxy
string
optional
codisPassword Login password for codis-proxy.
object
optional
zkClientOpts 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 = 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
object
optional
redisClientOpts 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
boolean | Function
log 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 = zkServers: '127.0.0.1:6701, 127.0.0.1:6702' zkCodisProxyDir: '/zk/codis/db_test_node/proxy' codisPassword: 'your_codis_password' log: consolelog
You can pass false
to close log.
string
optional
proxyAddrKey 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 = 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 = zkServers: '127.0.0.1:6701, 127.0.0.1:6702' zkCodisProxyDir: '/zk/codis/db_test_node/proxy' codisPassword: 'your_codis_password' console
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 = zkServers: '127.0.0.1:6701, 127.0.0.1:6702' zkCodisProxyDir: '/zk/codis/db_test_node/proxy' codisPassword: 'your_codis_password' nodeCodis
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 = zkServers: '127.0.0.1:6701, 127.0.0.1:6702' zkCodisProxyDir: '/zk/codis/db_test_node/proxy' codisPassword: 'your_codis_password' nodeCodis
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.jsvar createError = ;var express = ;var path = ;var cookieParser = ;var logger = ;var NodeCodis = var indexRouter = ; var app = ; app;app;app;app;app; const nodeCodis = zkServers: '127.0.0.1:6701, 127.0.0.1:6702' zkCodisProxyDir: '/zk/codis/db_test_node/proxy' codisPassword: 'your_codis_password' nodeCodis app app; app; app; moduleexports = app;
// routes/index.jsvar express = ;var router = express; router moduleexports = router;
When you always request localhost:3000/test
, printed as follows:
192.168.3.72:19201GET /test - - ms - -192.168.3.72:19201GET /test - - ms - -192.168.3.62:19201GET /test - - ms - -192.168.3.62:19201GET /test - - ms - -192.168.3.72:19201GET /test - - ms - -192.168.3.62:19201GET /test - - ms - -192.168.3.62:19201GET /test - - ms - -192.168.3.62:19201GET /test - - ms - -192.168.3.72:19201GET /test - - ms - -192.168.3.62:19201
Licence
NodeCodis is licensed under the MIT License - see the LICENSE file for details.