0

I have 2 gRPC microservices. I want to use one of these 2 microservices as a client and the other as a server and exchange data between each other. However, while the server microservice is reaching from my client microservice,

code: 12,
details: 'The server does not implement the method /user.CommentService/findComments',
metadata: Metadata { internalRepr: Map(0) {}, options: {} }

i am getting error. My codes are like this:

userService.js

// I will use this microservice as a client
const SERVICE_PROTO = path.resolve(__dirname, '__proto/user.proto')
const server = new Mali();
const { commentClient } = require('./commentClient');
server.addService(SERVICE_PROTO, null, {
        keepCase: true,
        enums: String,
        oneofs: true
})
server.use({
  getUserWithComment: (call, callback) => {
    const user = this.model.find(call.req.id).then((data) => {
      commentClient.findComments({
            where: { userId: call.req.id }
        }, (findCommentResult, err) => {
            callback(null, {...data, ...findCommentResult});
        }) 
 });
}
});

await server.start('0.0.0.0:6150');

in commentClient.js

// I'm creating a client to connect to the comment grpc microservice for use in userServer.js
const path = require('path')
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');

const COMMENT_HOST_PORT = `0.0.0.0:6160`;
const COMMENT_PROTO = path.resolve(__dirname, '__proto/user.proto')

const packageDefinition = protoLoader.loadSync(
    COMMENT_PROTO,
    {
        keepCase: true,
        enums: String,
        oneofs: true
    });


const {CommentService} = grpc.loadPackageDefinition(packageDefinition).user;

module.exports = {
    commentService: new CommentService(COMMENT_HOST_PORT, grpc.credentials.createInsecure())
}

in commentServer.js

const SERVICE_PROTO = path.resolve(__dirname, '__proto/comment.proto')
const server = new Mali();
server.addService(SERVICE_PROTO, null, {
        keepCase: true,
        enums: String,
        oneofs: true
})
server.use({
  findComments: (call, callback) => {
    const comment = this.model.findByUserId(call.req.where.userId).then((data) => {
       callback(null, data);
 });
}
});

await server.start('0.0.0.0:6160');

in user.proto

// I added 2 services into this proto file.
syntax = "proto3";

package user;

message User {
  int32 id = 1;
  string name = 2;
  string email = 3;
  string password = 4;
  int32 age = 5;
}

message UserId {
  int32 id = 1;
}

message GetUserWithCommentResponse {
  int32 id = 1;
  string name = 2;
  string email = 3;
  repeated UserComment comments = 4;
}

message UserComment {
  int32 id = 1;
  string comment = 2;
  int32 userId = 3;
  int32 postId = 4;
  string createdAt = 5;
}
service UsersService {
   rpc getUserWithComment(UserId)  returns (GetUserWithCommentResponse) {}
   
}
service CommentService {
  rpc findComments (FindCommentByUserRequest) returns (FindCommentsPayload) {}
}

in comment.proto


syntax = "proto3";

package comment;

message Comment {
  int32 id = 1;
  string comment = 2;
  int32 userId = 3;
  int32 postId = 4;
  string createdAt = 5;
}

message WhereCondition {
  int32 userId = 1;
}
message FindAllCommentRequest {
  WhereCondition where = 1;
}
message FindCommentsPayload {
  repeated Comment data = 1;
}
service CommentsService {
  rpc findComments (FindAllCommentRequest) returns (FindCommentsPayload) {}
}

thank you for your help.

Onur Özkır
  • 559
  • 3
  • 12

0 Answers0