{"id":597,"date":"2023-08-08T08:46:47","date_gmt":"2023-08-08T08:46:47","guid":{"rendered":"http:\/\/192.168.0.142\/?p=597"},"modified":"2023-08-09T13:27:53","modified_gmt":"2023-08-09T13:27:53","slug":"grpc-server-app-using-go","status":"publish","type":"post","link":"http:\/\/192.168.0.142\/grpc-server-app-using-go\/","title":{"rendered":"gRPC server app using Go"},"content":{"rendered":"\n

TL;DR<\/strong><\/p>\n\n\n\n

The source code is available in https:\/\/github.com\/rinoymjoseph\/learning-go<\/a><\/p>\n\n\n\n

Create a directory<\/strong><\/p>\n\n\n\n

mkdir grpc-demo<\/code><\/pre>\n\n\n\n

Initialize a go project named ‘grpc-demo’<\/strong><\/p>\n\n\n\n

 go mod init rinoymjoseph.github.com\/grpc-demo<\/code><\/pre>\n\n\n\n

Create a directory to place proto file<\/strong><\/p>\n\n\n\n

mkdir calc<\/code><\/pre>\n\n\n\n
syntax= \"proto3\";\noption go_package= \"rinoymjoseph.github.com\/grpc-demo\/calc\";\n\nservice calc {\n    rpc Add(AddRequest) returns (AddResponse);\n}\n\nmessage AddRequest {\n    int32 num1 = 1;\n    int32 num2 = 2;\n}\n\nmessage AddResponse {\n    int64 sum = 1;\n}<\/code><\/pre>\n\n\n\n

Install protoc-gen-go and protoc-gen-go-grpc. Add GOPATH\/bin to path<\/strong><\/p>\n\n\n\n

go install google.golang.org\/protobuf\/cmd\/protoc-gen-go@latest\ngo install google.golang.org\/grpc\/cmd\/protoc-gen-go-grpc@latest\nexport PATH=\"$PATH:$(go env GOPATH)\/bin\"<\/code><\/pre>\n\n\n\n

 Generate the gRPC stubs that can be used to implement the service and consume from clients<\/strong><\/p>\n\n\n\n

protoc --go_out=calc \\\n    --go_opt=paths=source_relative \\\n    --go-grpc_out=calc \\\n    --go-grpc_opt=paths=source_relative \\\n    calc\/calc.proto <\/code><\/pre>\n\n\n\n

This creates two files in calc folder. calc_grpc.pb.go and calc.pb.go.<\/strong><\/p>\n\n\n\n

Add the main.go file<\/strong><\/p>\n\n\n\n

package main\n\nimport (\n\t\"context\"\n\t\"log\"\n\t\"net\"\n\n\t\"google.golang.org\/grpc\"\n\t\"rinoymjoseph.github.com\/grpc-demo\/calc\"\n)\n\ntype calcServer struct {\n\tcalc.UnimplementedCalcServer\n}\n\nfunc (s calcServer) Add(ctx context.Context, req *calc.AddRequest) (*calc.AddResponse, error) {\n\treturn &calc.AddResponse{\n\t\tSum: int64(req.Num1 + req.Num2),\n\t}, nil\n}\n\nfunc main() {\n\tlis, err := net.Listen(\"tcp\", \":8090\")\n\tif err != nil {\n\t\tlog.Fatalf(\"cannot create listener: %s\", err)\n\t}\n\tserverRegistrar := grpc.NewServer()\n\tservice := &calcServer{}\n\n\tcalc.RegisterCalcServer(serverRegistrar, service)\n\terr = serverRegistrar.Serve(lis)\n\tif err != nil {\n\t\tlog.Fatalf(\"impossible to serve: %s\", err)\n\t}\n}\n<\/code><\/pre>\n\n\n\n

The project structure<\/strong><\/p>\n\n\n

\n
\"gRPC<\/figure><\/div>\n\n\n

Get the packages<\/strong><\/p>\n\n\n\n

go get .\ngo mod tidy<\/code><\/pre>\n\n\n\n

Run the gRPC server<\/strong><\/p>\n\n\n\n

go run main.go<\/code><\/pre>\n\n\n\n

Postman can be used for testing the gRPC methods<\/strong><\/p>\n\n\n\n

\"Postman<\/figure>\n","protected":false},"excerpt":{"rendered":"

TL;DR The source code is available in https:\/\/github.com\/rinoymjoseph\/learning-go Create a directory Initialize a go project named ‘grpc-demo’ Create a directory to place proto file Install protoc-gen-go and protoc-gen-go-grpc. Add GOPATH\/bin to path  Generate the gRPC stubs that can be used to implement the service and consume from clients This creates two files in calc folder. … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":717,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56],"tags":[57,62],"_links":{"self":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts\/597"}],"collection":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/comments?post=597"}],"version-history":[{"count":34,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts\/597\/revisions"}],"predecessor-version":[{"id":718,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts\/597\/revisions\/718"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/media\/717"}],"wp:attachment":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/media?parent=597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/categories?post=597"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/tags?post=597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}