Skip to content

Commit

Permalink
rabitMq runned
Browse files Browse the repository at this point in the history
  • Loading branch information
morteza-mortezai committed Apr 20, 2023
1 parent 0f7769e commit b16194c
Show file tree
Hide file tree
Showing 20 changed files with 161 additions and 30 deletions.
4 changes: 4 additions & 0 deletions apps/billing/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
APP_PORT=3001

RMQ_URI=amqp://localhost:5672
RMQ_BILLING_QUEUE=billing
15 changes: 9 additions & 6 deletions apps/billing/src/billing/billing.controller.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { Controller, Get } from '@nestjs/common';
import { Controller, Get, } from '@nestjs/common';
import { BillingService } from './billing.service';
import { EventPattern, Payload, RmqContext, Ctx } from '@nestjs/microservices'
import { MessagePattern, Payload, RmqContext, Ctx } from '@nestjs/microservices'
import { RmqService } from '@app/common'
import { RMQ_EVENTS } from '../constants/rmq.constants';
import { Observable, from } from 'rxjs';
@Controller()
export class BillingController {
constructor(
private readonly billingService: BillingService,
private readonly rmqService: RmqService
) { }

@Get()
@Get('u')
getHello(): string {
return this.billingService.getHello();
}
@EventPattern('order_created')
order(@Payload() data: any, @Ctx() context: RmqContext) {
@MessagePattern('GET_USER_BY_ID')
order(@Payload() data: number[], @Ctx() context: RmqContext): Observable<number> {
this.billingService.order(data);
this.rmqService.ack(context)
// this.rmqService.ack(context)
return from(data);
}
}
5 changes: 3 additions & 2 deletions apps/billing/src/billing/billing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { Injectable } from '@nestjs/common';
@Injectable()
export class BillingService {
getHello(): string {
return 'Hello World!';
return 'Hello World!!!!!!';
}

order(data) {
return console.log('bill is cretaed ', data)
console.log('bill is cretaed ', data)
return 'hi'
}
}
7 changes: 7 additions & 0 deletions apps/billing/src/constants/rmq.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const RMQ_SERVICE = {
USERS: 'USERS'
}
export enum RMQ_EVENTS {
NEW_USER_CREATED,
GET_USER_BY_ID
}
1 change: 1 addition & 0 deletions apps/billing/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { RmqService } from '@app/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';
import { RMQ_SERVICE } from './constants/rmq.constants';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
Expand Down
3 changes: 2 additions & 1 deletion apps/gateway/.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ MONGODB_URI=mongodb://localhost:27017/orderApp
APP_PORT=3000

RMQ_URI=amqp://localhost:5672
RMQ_USERS_QUEUE=users
RMQ_BILLING_QUEUE=billing

2 changes: 1 addition & 1 deletion apps/gateway/env/local.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ MONGODB_URI=mongodb://localhost:27017/orderApp
APP_PORT=3000

RMQ_URI=amqp://localhost:5672
RMQ_USERS_QUEUE=users
RMQ_BILLING_QUEUE=billing
6 changes: 5 additions & 1 deletion apps/gateway/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { Module } from '@nestjs/common';
import { ControllersModule } from './infrastructure/controllers/controllers.module';
import { ServicesModule } from './infrastructure/services/services.module';
import { EnvironmentModule } from './infrastructure/environment/environment.module';
import { ConfigModule } from '@nestjs/config';

@Module({
imports: [ControllersModule, ServicesModule, EnvironmentModule],
imports: [ControllersModule, ServicesModule,


],
controllers: [],
providers: [],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Controller, Get } from '@nestjs/common';
import { Controller, Get, Param } from '@nestjs/common';
import { UserService } from '../services/user.service';

@Controller('users')
export class UserController {
constructor(private readonly userService: UserService) { }

@Get(':id')
getUserById(): string {
return this.userService.getHello();
getUserById(@Param('id') id: string) {
return this.userService.getUserById(id);
}
}
54 changes: 50 additions & 4 deletions apps/gateway/src/infrastructure/services/services.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,58 @@ import { Module } from '@nestjs/common';
import { UserService } from './user.service';
import { RmqModule } from '@app/common';
import { RMQ_SERVICE } from '../constants/rmq.constants';
import { EnvironmentModule } from '../environment/environment.module';
import { EnvironmentService } from '../environment/environment.service';

// import { EnvironmentModule } from '../environment/environment.module';
// import { EnvironmentService } from '../environment/environment.service';
//
import { ClientsModule, Transport } from '@nestjs/microservices';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [RmqModule.register({ name: RMQ_SERVICE.USERS }, EnvironmentService), EnvironmentModule],
imports: [
RmqModule.register({ name: 'BILLING' }),
ConfigModule.forRoot({
isGlobal: true,
envFilePath: 'apps/gateway/env/local.env'
})
],
// imports: [
// RmqModule.registerAsync({
// imports: [
// ClientsModule.registerAsync([
// {
// name: RMQ_SERVICE.USERS,
// useFactory: () => ({
// transport: Transport.RMQ,
// options: {
// urls: ['amqp://localhost:5672'],
// queue: 'users',
// },
// }),
// inject: [],
// },
// ]),

// ],

// }),

// ],
controllers: [],
providers: [UserService],
exports: [UserService]
})
export class ServicesModule { }
// imports: [
// ClientsModule.registerAsync([
// {
// name,
// useFactory: (EnvironmentService) => ({
// transport: Transport.RMQ,
// options: {
// urls: [EnvironmentService.getRmqUri],
// queue: EnvironmentService.getRmqUsersQueue,
// },
// }),
// inject: [EnvironmentService],
// },
// ]),
// ],
6 changes: 3 additions & 3 deletions apps/gateway/src/infrastructure/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import { RMQ_EVENTS } from '../constants/rmq.constants'
@Injectable()
export class UserService {
constructor(
@Inject(RMQ_SERVICE.USERS) private usersClient: ClientProxy
@Inject('BILLING') private usersClient: ClientProxy
) { }

getHello(): string {
return 'Hello World!';
}

async getUserById(id: number) {
async getUserById(id: string) {
try {
const v = await lastValueFrom(this.usersClient.emit(RMQ_EVENTS.GET_USER_BY_ID, id))
const v = await lastValueFrom(this.usersClient.send('GET_USER_BY_ID', id))
console.log('v**', v)
} catch (error) {
console.log('error in order service', error)
Expand Down
1 change: 1 addition & 0 deletions libs/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './database/abstract.repository';
export * from './database/database.module';
export * from './rmq/rmq.module';
export * from './rmq/rmq.service';
// export * from './myconfig/myconfig.module';
27 changes: 27 additions & 0 deletions libs/common/src/myconfig/myconfig.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { DynamicModule, Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { MongooseModule, MongooseModuleOptions } from "@nestjs/mongoose";

@Module({})
export class myConfigModule {
static register(envPath: string): DynamicModule {
return {
module: myConfigModule,
imports: [
ConfigModule.forRoot({
envFilePath: envPath
}),

],
providers: [
{
provide: 'MY_CONFIG',
useFactory(mongoose: MongooseModuleOptions) {

},
inject: [MongooseModule]
}
]
}
}
}
6 changes: 6 additions & 0 deletions libs/common/src/rmq/config.module-definition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ConfigurableModuleBuilder } from '@nestjs/common';
import { ConfigModuleOptions } from './configModuleOption.interface';
import { RmqOptions } from '@nestjs/microservices';

export const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } =
new ConfigurableModuleBuilder<RmqOptions>().build();
9 changes: 9 additions & 0 deletions libs/common/src/rmq/configModuleOption.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Transport } from "@nestjs/microservices"

export interface ConfigModuleOptions {
transport: any
options: {
urls: string[]
queue: string
},
}
16 changes: 10 additions & 6 deletions libs/common/src/rmq/rmq.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Module, DynamicModule } from '@nestjs/common';
import { RmqService } from './rmq.service';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { ConfigService } from '@nestjs/config';

import { ConfigurableModuleClass } from './config.module-definition';

interface RmqModuleOptions {
name: string;
Expand All @@ -13,25 +13,29 @@ interface RmqModuleOptions {
exports: [RmqService]
})
export class RmqModule {
static register({ name }: RmqModuleOptions, configS): DynamicModule {
static register({ name }: RmqModuleOptions): DynamicModule {
return {
module: RmqModule,
imports: [
ClientsModule.registerAsync([
{
name,
useFactory: (configS: any) => ({
// اینحا رو میشد بدون فکتوری هم نوشت
useFactory: (configService: ConfigService) => ({
transport: Transport.RMQ,
options: {
urls: [configS.getRmqUri],
queue: configS.getRmqUsersQueue,
urls: [configService.get<string>('RMQ_URI')],
queue: configService.get<string>(`RMQ_${name}_QUEUE`),
},
}),
inject: [configS],
inject: [ConfigService],
},
]),
],
exports: [ClientsModule],
};
}
}


// export class RmqModule extends ConfigurableModuleClass { }
3 changes: 2 additions & 1 deletion libs/common/src/rmq/rmq.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ export class RmqService {
) { }

getOptions(queue: string, noAck = false): RmqOptions {
console.log('rec', this.configService.get<string>(`RMQ_${queue}_QUEUE`))
return {
transport: Transport.RMQ,
options: {
urls: [this.configService.get<string>('RMQ_URI')],
queue: this.configService.get<string>(`RMQ_${queue}_QUEUE`),
noAck,
// noAck,
persistent: true
}
}
Expand Down
9 changes: 9 additions & 0 deletions nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@
"compilerOptions": {
"tsConfigPath": "libs/common/tsconfig.lib.json"
}
},
"config": {
"type": "library",
"root": "libs/config",
"entryFile": "index",
"sourceRoot": "libs/config/src",
"compilerOptions": {
"tsConfigPath": "libs/config/tsconfig.lib.json"
}
}
}
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
"<rootDir>/libs/"
],
"moduleNameMapper": {
"^@app/common(|/.*)$": "<rootDir>/libs/common/src/$1"
"^@app/common(|/.*)$": "<rootDir>/libs/common/src/$1",
"^@app/config(|/.*)$": "<rootDir>/libs/config/src/$1"
}
}
}
}
6 changes: 6 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
],
"@app/common/*": [
"libs/common/src/*"
],
"@app/config": [
"libs/config/src"
],
"@app/config/*": [
"libs/config/src/*"
]
}
}
Expand Down

0 comments on commit b16194c

Please sign in to comment.