e.g connecting go microservice made with rpcx with nestJs(popular node framework)

25 Views Asked by At

server.go

 package main
    
        import (
            "context"
        
            mathservices "service/mathservices"
        
            "github.com/smallnest/rpcx/server"
        )
            func main() {
                
                s := server.NewServer()
                s.RegisterName("MathService", new(mathService.MathService), "")
                s.Serve("tcp", ":8972")
                
            }

math_service.go

package mathservices
    
    import (
        "context"
        "fmt"
    )
    
   
    
    type Args struct {
        A int
        B int
    }
    
    type Reply struct {
        C int
    }
    
    type MathService int
    
    func (t *MathService) Mul(ctx context.Context, args *Args, reply *Reply) error {
        reply.C = args.A * args.B
        return nil
    }
    

given this go microservice made with rpcx framework i want it to communicate with nest js microservice, here is the code of the nest js microservice:

nestmicroservice/src/appmodule.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ClientsModule, Transport } from '@nestjs/microservices';

@Module({
  imports: [
    ClientsModule.register([
      {
        name: 'MathService',
        transport: Transport.TCP,
        options: {
          host: 'localhost',
          port: 8972,
        },
      },
    ]),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

nestmicroservice/src/appcontroller.ts

import { Controller, Inject } from '@nestjs/common';
import {
  ClientProxy,
  EventPattern,
  MessagePattern,
} from '@nestjs/microservices';
import { Observable } from 'rxjs';

@Controller()
export class AppController {
  constructor(
    @Inject('MathService') private client: ClientProxy,
  ) {}

  

   multiply(): Observable<any> {
    {
      console.log('first line run');
    
      const pattern = { cmd: 'Mul' };
      
      return this.client.send<any>(pattern, {a:10, b:5});
    }
  }
}

first line run gets printed in the console but the code return this.client.send(pattern, {a:10, b:5}); produces error please guys help me!

0

There are 0 best solutions below