What to do if you get an "unknown service" error from your gRPC server

Recently popular gRPC. Data can be serialized by the schema language Protocol Buffers. As it can communicate faster than json communication which was mainstream so far, Increasingly, it is being adopted in microservice development.

During this time, a bug as the title occurred when trying to communicate between the client and server built with gRPC. I've had a lot of trouble, so I'll leave the cause and solution as a memorandum.

Source code

Click here for the source. https://github.com/yzmw1213/PostService

What I tried to do

Implement the following two services on the gRPC server written in go. --Posting service --Tag management service for posts (handled as master data)

Then, the client side code implemented by typescript sends a request to the above service and processes it.

client.ts


import { Tag, CreateTagRequest } from "~/grpc/tag_pb"
import { TagServiceClient } from "~/grpc/TagServiceClientPb"

post() {
  const client = new TagServiceClient(
    "http://localhost:8080", {}, {}
  )
  
  const request = new CreateTagRequest()
  var tag = new Tag()
  tag.setTagId(postTag.tagID)
  tag.setTagName(postTag.tagName)
  tag.setStatus(postTag.status)
  request.setTag(tag)
  //Send a request to the createTag method of TagService
  client.createTag(request, {}, (err, res) => {
    if (err != null) {
      console.log(err)
    }
    console.log(res)
  })
}

When requesting tag creation from the client side to the tag service as described above, the following error occurred.

{ code: 12, message: "unknown service post_grpc.TagService" }

solution

Look at the official git wondering what code: 12 is ... saw.

Then

// HTTP Mapping: 501 Not Implemented UNIMPLEMENTED = 12;

in short, The content meant "the service has not been implemented."

So, check if the calling service is registered on the gRPC server.

Use grpcurl to check the operation of the grpc server.

There were many details in this article. https://qiita.com/yukina-ge/items/a84693f01f3f0edba482

For example, if you're building a gRPC server on port 50051, hit it like this:

#List of services registered on the port
$ grpcurl -plaintext localhost:50051 list
grpc.reflection.v1alpha.ServerReflection
post_grpc.PostService

Server Reflection and Post Service seem to be registered. Well then, I thought TagService was ... and when I read the code on the server side, I noticed an obvious mistake.

server.go


package grpc

import (
	"fmt"
	"log"
	"net"
	"os"
	"os/signal"

	"github.com/yzmw1213/PostService/grpc/post_grpc"
	"github.com/yzmw1213/PostService/usecase/interactor"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
)

type server struct {
	PostUsecase interactor.PostInteractor
	TagUsecase  interactor.TagInteractor
}

//NewPostGrpcServer gRPC server start
func NewPostGrpcServer() {
	lis, err := net.Listen("tcp", "0.0.0.0:50051")
	if err != nil {
		log.Fatalf("Failed to listen: %v", err)
	}

	server := &server{}

	s := makeServer()
     //Register PostService on server
	post_grpc.RegisterPostServiceServer(s, server)

     //TagService is missing!!!


	// Register reflection service on gRPC server.
	reflection.Register(s)
	log.Println("main grpc server has started")

	go func() {
		if err := s.Serve(lis); err != nil {
			log.Fatalf("failed to serve: %v", err)
		}
	}()

	ch := make(chan os.Signal, 1)
	signal.Notify(ch, os.Interrupt)

	// Block until a sgnal is received
	<-ch
	fmt.Println("Stopping the server")
	s.Stop()
	fmt.Println("Closing the client")
	lis.Close()
	fmt.Println("End of Program")
}

func makeServer() *grpc.Server {
	s := grpc.NewServer(
		grpc.UnaryInterceptor(grpc.UnaryServerInterceptor(transmitStatusInterceptor)),
	)

	return s
}

I have registered PostService with post_grpc.RegisterPostServiceServer TagService must be registered with the gRPC server as well.

I added the following code and solved it.

server.go


     //Register PostService on server
	post_grpc.RegisterPostServiceServer(s, server)
	//Add the following
	//Tag service registration
	post_grpc.RegisterTagServiceServer(s, server)

Looking back

This time, I spent a lot of time identifying the cause. I put an envoy Proxy between the client and the server, and I was distracted by the proxy. .. It was a fairly rudimentary mistake for me, even if I was worried about it. grpcurl, let's use it from the beginning.

Supplement

When building a gRPC server that assumes microservice operation, I have the impression that there are many articles written on the premise of 1 service / 1 server, but it is not always There is no need to divide the server into small parts for all services, as implemented above I think that there will be no problem in operation if each Register Service is used.

Relevant services (such as user registration and authentication services) I think it is possible to operate on the same server in this way.

I plan to write an authentication service in the future, so I'll give it a try.

Reference article

I tried to check the operation of the gRPC server with grpcurl

Recommended Posts

What to do if you get an "unknown service" error from your gRPC server
What to do if you get an error when trying to load mnist
What to do if you get an error when installing Dlib (Ubuntu)
What to do if you get an error when installing python with pyenv
What to do if you get "coverage unknown" in Coveralls
What to do if you get an OpenSSL error when installing Python 2 with pyenv
What to do if you get a memory error when converting from PySparkDataFrame to PandasDataFrame
What to do if you get an error when importing matplotlib in Python (Mac)
What to do if you get an Import Error when importing matplotlib with Jupyter
What to do if you get an error when running "certbot renew" in CakePHP environment
What to do if you get an Undefined error when trying to use pip with pyenv
What to do if you get an error saying c compiler cannot create executables in configure
What to do if you get a "No versions found" error in pipenv
What to do if you get an error like'Qstring' has already been set to version 1 using mne python
What to do if you get angry with "Value Error: unknown local: UTF-8" in python manage.py syncdb
What to do if you get an error when trying to send a message in tasks.loop () immediately after startup
What to do if you get "(35,'SSL connect error')" in pycurl (one of them)
What to do if you lose your EC2 key pair
What to do if you get the error ʻERR_FEATURE_UNAVAILABLE_ON_PLATFORM` when using ts-node-dev on Linux
What to do if you run python in IntelliJ and end with an error
What to do when you get an error saying "Name resolution temporarily failed" on linux
What to do if you get Swagger-codegen in python and Import Error: No module named
What to do if you get a Cannot retrieve metalink for repository error in yum
What to do if you get an Undefined error:'Module_six_moves_urllib_parse' object has no attribute'urlencode' on MacOS
What to do if you get a minus zero in Python
What to do if you get a UnicodeDecodeError with pip install
What to do if you can't build your project with Maven
What to do if you get an error when vagrant up when you enable public_network or private_network on Vagrant + Arch Linux → Install netctl
What to do if you get a must override `get_config` error when trying to model.save in Keras
What to do if you couldn't send an email to Yahoo with Python.
What to do if you forget your login password on Manjaro Linux
What to do if you get lost in file reference with FileNotFoundError
What to do if you get angry in TensorFlow v2 without attribute'app'
What to do if you get stuck during Anaconda installation on Linux
What to do if pyenv install does not proceed with an error
What to do if an error occurs when importing numpy with VScode
What to do if you get a TypeError with numpy min, max
What to do if you get Could not fetch URL 443 with pip
EC2 / Amazon Linux2: What to do if you get an "unable to execute'gcc': No such file or directory" error with pip install
What to do if you can't pipenv shell
[Django] What to do if an Integrity Error occurs when registering data from the management site to the database
What to do if you get a Permission denied (public key) error when trying to pull on Github
What to do if you get the error Target WSGI script'/var/www/xxx/xxx.wsgi' cannot be loaded as python module
[Python] What to do if an error occurs in pip (pyinstaller, pyautogui, etc.)
Workaround if you get an error when trying to install PySide with pip
What to do if you get angry with swapon failed: Operation not permitted
What to do if Django can't load an image from a static folder
What to do if you get "Python not configured." Using PyDev in Eclipse
What to do if an SSL connection error (ssl.SSLError: [SSL: DH_KEY_TOO_SMALL]) occurs on Ubuntu 20.04
What to do if you can't pip install mysqlclient
No module named What to do if you get'libs.resources'
ModuleNotFoundError: No module What to do if you get'tensorflow.contrib'
What to do if you get `No kernel for language python found` in Hydrogen
What to do if the print command itself causes an error in Maya python
What to do if you get a Call with too many input arguments error at DoAndReturn in a golang test
What to do if you get the error "Error: opencv3: Does not support building both Python 2 and 3 wrappers" when installing openCV 3
What to do if you are addicted to Windows character code
What to do if you can't sort files with subscripts
What to do if a 0xC0000005 error occurs in tf.train.start_queue_runners ()
What to do when an error occurs with import _ssl
What to do if you can't log in as root