Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis Sentinel Configuration #424

Open
engmsilva opened this issue Jan 10, 2023 · 1 comment
Open

Redis Sentinel Configuration #424

engmsilva opened this issue Jan 10, 2023 · 1 comment

Comments

@engmsilva
Copy link

engmsilva commented Jan 10, 2023

My application works correctly accessing only one instance of Redis, but I am not able to configure my application with Redis Sentinel.

I tried to follow the example of the project's repositories, but it seems that nothing happens.

My module looks like this:

import { Module } from '@nestjs/common';
import { RedisModule } from '@liaoliaots/nestjs-redis';
import { NestRedisService } from './nestjs-redis.service';
import { RedisConfigService } from './nestjs-redis.config.service';

@Module({
  imports: [
    RedisModule.forRootAsync({
      useClass: RedisConfigService,
    }),
  ],
  providers: [NestRedisService],
  exports: [NestRedisService],
})
export class NestjsRedisModule {}

My connection configuration file looks like this:

import { Injectable } from '@nestjs/common';
import {
  RedisOptionsFactory,
  RedisModuleOptions,
} from '@liaoliaots/nestjs-redis';
import { InjectPinoLogger } from 'nestjs-pino/InjectPinoLogger';
import { PinoLogger } from 'nestjs-pino';

export interface ErrnoException extends Error {
  errno?: number;
  code?: string;
  path?: string;
  syscall?: string;
  stack?: string;
}

@Injectable()
export class RedisConfigService implements RedisOptionsFactory {
  constructor(
    @InjectPinoLogger(RedisConfigService.name)
    private readonly logger: PinoLogger,
  ) {}

  async createRedisOptions(): Promise<RedisModuleOptions> {
    const logger = this.logger;
    return {
      config: {
        role: 'master',
        host: process.env.REDIS_HOST,
        port: parseInt(process.env.REDIS_PORT),
        password: process.env.REDIS_PASSWORD,
        maxRetriesPerRequest: 0,
        onClientCreated(client) {
          client.on('error', (error: ErrnoException) => {
            logger.info(
              { description: '[Create Conection][Error]' },
              JSON.stringify(error),
            );
          });
        },
        retryStrategy(times) {
          const delay = Math.min(times * 50, 2000);
          return delay;
        },
      },
    };
  }
}

Are these my Redis and Sentinel servers?

Master - 10.1.2.3 - 6379 - has password
Slave - 10.1.2.4 - 6379 - has password
Slave - 10.1.2.5 - 6379 - has password
Sentinel - 10.1.2.3 - 26379 - has no password
Sentinel - 10.1.2.4 - 26379 - has no password

What would be the best way to adapt the code above to connect with Redis sentinel?

@engmsilva
Copy link
Author

For anyone experiencing the same problem, here's the change I made to the connection file to be able to use redis with sentinel.

import { Injectable } from '@nestjs/common';
import {
  RedisOptionsFactory,
  RedisModuleOptions,
} from '@liaoliaots/nestjs-redis';
import { InjectPinoLogger } from 'nestjs-pino/InjectPinoLogger';
import { PinoLogger } from 'nestjs-pino';

export interface ErrnoException extends Error {
  errno?: number;
  code?: string;
  path?: string;
  syscall?: string;
  stack?: string;
}

@Injectable()
export class RedisConfigService implements RedisOptionsFactory {
  constructor(
    @InjectPinoLogger(RedisConfigService.name)
    private readonly logger: PinoLogger,
  ) {}

  async createRedisOptions(): Promise<RedisModuleOptions> {
    const logger = this.logger;
    return {
      readyLog: true,
      commonOptions: {
        name: process.env.REDIS_SENTINEL_MASTER,
        sentinels: [
          {
            host: process.env.REDIS_SENTINEL_01,
            port: parseInt(process.env.REDIS_SENTINEL_PORT),
          },
          {
            host: process.env.REDIS_SENTINEL_02,
            port: parseInt(process.env.REDIS_SENTINEL_PORT),
          },
          {
            host: process.env.REDIS_SENTINEL_03,
            port: parseInt(process.env.REDIS_SENTINEL_PORT),
          },
        ],
      },
      config: [
        {
          role: 'master',
          password: process.env.REDIS_PASSWORD,
          maxRetriesPerRequest: 0,
          onClientCreated(client) {
            client.on('error', (error: ErrnoException) => {
              logger.info(
                { description: '[Create Conection][Error]' },
                JSON.stringify(error),
              );
            });
          },
          retryStrategy(times) {
            const delay = Math.min(times * 50, 2000);
            return delay;
          },
        },
      ],
    };
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant