12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- /**
- * Redis客户端封装。
- * Redis客户端封装发布订阅(redis只要使用了发布订阅,这个client不能做其他操作)
- *
- * 注意Redis使用Promises保证调用流程。
- *
- * https://github.com/NodeRedis/node_redis
- *
- * author: linzhuo
- * since: 2016/12/09
- */
- "use strict";
- let redis = require('redis');
- let configFile = require('../../include/commons').CONFIG_FILE;
- let config = require('../../resources/config/' + configFile);
- let log = require("../../util/log.js");
- let redisPubClient = null;
- class RedisPubClient {
- constructor() {
- var redisConfig = config.redisConfig;
- redisConfig.retry_strategy = function(options){
- log.info("pub Redis重新连接次数:"+options.times_connected);
- if (options.error.code === 'ECONNREFUSED') {
- log.error('pub Redis连接被拒绝');
- }
- if (options.times_connected > 10) {
- log.error('pub Redis重试连接超过十次');
- }
- return Math.max(options.attempt * 100, 3000);
- }
- this._connection = redis.createClient(
- config.redisConfig
- );
- this._connection.auth(config.redisConfig.password||"",function(){
- console.log('pub Redis通过认证');
- });
- this._connection.on('connect', function (res) {
- log.info('pub Redis is connected.');
- });
- this._connection.on('error', function (res) {
- log.error("pub Redis connect failed.");
- })
- }
- get connection() {
- return this._connection;
- }
- static redisClient() {
- if (redisPubClient == null) {
- redisPubClient = new RedisPubClient();
- }
- return redisPubClient;
- }
- }
- function uncaughtExceptionHandler(err){
- if(err && err.code == 'ECONNREFUSED'){
- //do someting
- }else{
- log.error(err+"exit in pub Redis");
- }
- }
- process.on('uncaughtException', uncaughtExceptionHandler);
- module.exports = RedisPubClient;
|