【发布时间】:2023-01-10 10:53:42
【问题描述】:
我正在使用 Nest JS 开发服务,
我想创建 redis 客户端模块,它具有发布服务和将被其他模块使用的缓存服务
所以这是我的代码:
- Redis 客户端模块
import { CacheModule, Module } from "@nestjs/common"; import { ConfigService } from "@nestjs/config"; import * as redisStore from "cache-manager-redis-store"; import { ClientsModule, Transport } from "@nestjs/microservices"; import { RedisCacheService } from "./redis-cache.service"; import { RedisPublishService } from "./redis-publish.service"; @Module({ imports: [ CacheModule.register({ inject: [ConfigService], useFactory: async (configService: ConfigService) => ({ store: redisStore, host: configService.get<string>("REDIS_HOST"), port: configService.get<string>("REDIS_PORT"), password: configService.get<string>("REDIS_PASSWORD"), }), }), ClientsModule.registerAsync([ { name:"PUBLISH_SERVICE", useFactory: async (configService: ConfigService) => { const url = `redis://${configService.get<string>( "REDIS_HOST" )}:${configService.get<string>("REDIS_PORT")}`; return { transport: Transport.REDIS, options: { url: url, password: configService.get<string>("REDIS_PASSWORD"), }, } }, inject: [ConfigService], }, ]), ], providers: [RedisCacheService, RedisPublishService], exports: [RedisCacheService, RedisPublishService], }) export class RedisClientModule {}2.Redis缓存服务
import { CACHE_MANAGER, Inject, Injectable } from "@nestjs/common"; import { Cache } from "cache-manager"; @Injectable() export class RedisCacheService { constructor(@Inject(CACHE_MANAGER) private readonly cache: Cache) {} async get<T>(key: string): Promise<T | undefined>{ console.log("getting data from redis with key: " + key) return this.cache.get(key); } async set<T>(key:string, value:T):Promise<T> { return this.cache.set(key, value); } del(key: string): Promise<any>{ return this.cache.del(key); } async setTtl<T>(key: string, value: T, ttl: number): Promise<T>{ return this.cache.set(key,value,ttl); } }- Redis 发布服务
import { Inject, Injectable } from "@nestjs/common"; import { ClientProxy } from "@nestjs/microservices"; import { Observable } from "rxjs"; @Injectable() export class RedisPublishService { constructor(@Inject('PUBLISH_SERVICE') private client: ClientProxy) {} emit<TResult = any, TInput = any>(pattern: any, data: TInput): Observable<TResult>{ return this.client.emit(pattern,data); } send<TResult = any, TInput = any>(pattern: any, data: TInput): Observable<TResult>{ return this.client.send(pattern,data); } }和使用它的模块:
@Module({ imports:[RedisClientModule], providers: [ExampleService], controllers: [ExampleController] }) export class ExampleModule {}服务等级:
@Injectable() export class ExampleService { constructor( private readonly cacheService:RedisCacheService, private readonly publishService:RedisPublishService, ) { } async getExample() { return this.cacheService.get("example"); } async setExample(value:string){ await this.publishService.emit("example:set",value); return this.cacheService.set("example",value); } }我的问题是每次将事件发布到 redis 时都会出现此错误
[admin-service] error 2022-09-03 17:36:15 connect ECONNREFUSED 127.0.0.1:6379 - {"stack":["ClientProxy"],"errno":-111,"code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":6379} +18s 2022-09-03T10:36:15.377101300Z [admin-service] error 2022-09-03 17:36:15 connect ECONNREFUSED 127.0.0.1:6379 - {"stack":["ClientProxy"],"errno":-111,"code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":6379} +9ms 2022-09-03T10:36:15.378503300Z [admin-service] error 2022-09-03 17:36:15 Retry time exhausted - {"stack":["ClientProxy"]} +2ms 2022-09-03T10:36:15.381020100Z [admin-service] error 2022-09-03 17:36:15 Retry time exhausted - {"stack":["ClientProxy"]} +2ms我不明白 127.0.0.1 是从哪里来的,因为我在我的 .env 中将 REDIS_HOST 设置到我在 docker 容器中的 redis
REDIS_HOST=redis-service-1 REDIS_PORT=6379 REDIS_PASSWORD=abc123yes任何建议都会很有帮助,我坚持了 3 天
【问题讨论】:
-
您可以分享运行 Redis docker 必须执行的命令吗?
标签: javascript typescript redis nestjs nestjs-config