Skip to content

Commit a246ddb

Browse files
committedDec 7, 2020
grpc test
1 parent 95ae572 commit a246ddb

File tree

6 files changed

+910
-0
lines changed

6 files changed

+910
-0
lines changed
 

‎grpc/book.proto

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
syntax = "proto3";
2+
package book;
3+
4+
message Book {
5+
string name = 1;
6+
int32 isbn = 2;
7+
}
8+
9+
service BookService {
10+
rpc GetBook(Book) returns (Book) {}
11+
}

‎grpc/book/book.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package book
2+
3+
import (
4+
log "github.com/sirupsen/logrus"
5+
6+
"golang.org/x/net/context"
7+
)
8+
9+
// Server interface for our service methods
10+
type Server struct {
11+
}
12+
13+
// GetBook logs Book from client and returns new Book
14+
func (s *Server) GetBook(ctx context.Context, input *Book) (*Book, error) {
15+
16+
log.WithFields(log.Fields{
17+
"Name": input.Name,
18+
"Isbn": input.Isbn,
19+
}).Info("Book data received from client")
20+
21+
return &Book{Name: "The Great Gatsby", Isbn: 90393}, nil
22+
}

‎grpc/book/book.pb.go

+243
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎grpc/go.mod

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module grpc_using_go
2+
3+
go 1.15
4+
5+
require (
6+
github.com/fullstorydev/grpcurl v1.7.0 // indirect
7+
github.com/golang/protobuf v1.4.3
8+
github.com/sirupsen/logrus v1.7.0
9+
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb
10+
golang.org/x/sys v0.0.0-20201204225414-ed752295db88 // indirect
11+
golang.org/x/text v0.3.4 // indirect
12+
google.golang.org/genproto v0.0.0-20201204160425-06b3db808446 // indirect
13+
google.golang.org/grpc v1.34.0
14+
google.golang.org/protobuf v1.25.0
15+
)

‎grpc/go.sum

+550
Large diffs are not rendered by default.

‎grpc/main.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
3+
import (
4+
"net"
5+
"os"
6+
7+
"grpc_using_go/book"
8+
9+
log "github.com/sirupsen/logrus"
10+
"google.golang.org/grpc"
11+
"google.golang.org/grpc/reflection"
12+
)
13+
14+
/*
15+
sudo apt install protobuf-compiler
16+
go get -u github.com/golang/protobuf/protoc-gen-go
17+
18+
go install github.com/fullstorydev/grpcurl/cmd/grpcurl
19+
*/
20+
21+
func main() {
22+
log.SetFormatter(&log.TextFormatter{
23+
FullTimestamp: true,
24+
})
25+
26+
var port string
27+
var ok bool
28+
port, ok = os.LookupEnv("PORT")
29+
if ok {
30+
log.WithFields(log.Fields{
31+
"PORT": port,
32+
}).Info("PORT env var defined")
33+
34+
} else {
35+
port = "9000"
36+
log.WithFields(log.Fields{
37+
"PORT": port,
38+
}).Warn("PORT env var not defined. Going with default")
39+
40+
}
41+
42+
l, err := net.Listen("tcp", ":"+port)
43+
if err != nil {
44+
log.WithFields(log.Fields{
45+
"Error": err.Error(),
46+
}).Fatal("Failed to listen")
47+
}
48+
49+
s := book.Server{}
50+
51+
grpcServer := grpc.NewServer()
52+
53+
/*
54+
grpcurl -plaintext localhost:9000 list
55+
grpcurl -plaintext localhost:9000 describe
56+
grpcurl -d '{"name": "To Kill a Mockingbird", "isbn": 12345}' -plaintext localhost:9000 book.BookService/GetBook
57+
*/
58+
reflection.Register(grpcServer)
59+
60+
book.RegisterBookServiceServer(grpcServer, &s)
61+
62+
log.Info("gRPC server started at ", port)
63+
if err := grpcServer.Serve(l); err != nil {
64+
log.WithFields(log.Fields{
65+
"Error": err.Error(),
66+
}).Fatal("Failed to serve")
67+
}
68+
69+
}

0 commit comments

Comments
 (0)
Please sign in to comment.