Mercurial > hg > Docker > GRPCTest
changeset 0:568ffe87c352
impl docker-compose go-grpc project example
https://tech.libinc.co.jp/entry/2019/11/07/111548
author | anatofuz |
---|---|
date | Mon, 16 Dec 2019 19:19:32 +0900 |
parents | |
children | ec2218a31c56 |
files | Dockerfile client.go docker-compose.yml go.mod pb/cat.proto scripts/client.sh scripts/server.sh server.go |
diffstat | 8 files changed, 137 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Dockerfile Mon Dec 16 19:19:32 2019 +0900 @@ -0,0 +1,20 @@ +FROM golang:1.13.1 + +RUN apt-get update && apt-get install -y unzip + +# Install protobuf +# @see https://github.com/yoshi42662/go-grpc/blob/master/server/Dockerfile +RUN mkdir -p /tmp/protoc && \ + curl -L https://github.com/protocolbuffers/protobuf/releases/download/v3.10.0/protoc-3.10.0-linux-x86_64.zip > /tmp/protoc/protoc.zip && \ + cd /tmp/protoc && \ + unzip protoc.zip && \ + cp /tmp/protoc/bin/protoc /usr/local/bin && \ + chmod go+rx /usr/local/bin/protoc && \ + cd /tmp && \ + rm -r /tmp/protoc + +WORKDIR /study-grpc +COPY . /study-grpc + +RUN go get -u google.golang.org/grpc +RUN go get -u github.com/golang/protobuf/protoc-gen-go
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client.go Mon Dec 16 19:19:32 2019 +0900 @@ -0,0 +1,27 @@ +package main + +import ( + "context" + "fmt" + "google.golang.org/grpc" + "log" + "os" + cat "study-grpc/pb" +) + +func main() { + port := os.Getenv("SERVER") + conn, err := grpc.Dial(port, grpc.WithInsecure()) + if err != nil { + log.Fatal("connection error:", err) + } + defer conn.Close() + + client := cat.NewCatClient(conn) + message := &cat.GetMyCatMessage{TargetCat: "mike"} + res, err := client.GetMyCat(context.Background(), message) + if err != nil { + log.Fatal(err) + } + fmt.Printf("result:%s\n", res) +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/docker-compose.yml Mon Dec 16 19:19:32 2019 +0900 @@ -0,0 +1,21 @@ +version: '3.7' + +services: + server: + build: . + container_name: "server" + ports: + - 1234:1234 + volumes: + - .:/study-grpc + command: bash scripts/server.sh + tty: true + client: + build: . + container_name: "client" + environment: + SERVER: server:1234 + volumes: + - .:/study-grpc + command: bash scripts/client.sh + tty: true
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/go.mod Mon Dec 16 19:19:32 2019 +0900 @@ -0,0 +1,8 @@ +module study-grpc + +go 1.13 + +require ( + github.com/golang/protobuf v1.3.2 + google.golang.org/grpc v1.24.0 +)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pb/cat.proto Mon Dec 16 19:19:32 2019 +0900 @@ -0,0 +1,14 @@ +syntax = "proto3"; + +service Cat { + rpc GetMyCat (GetMyCatMessage) returns (MyCatResponse) {} +} + +message GetMyCatMessage { + string target_cat = 1; +} + +message MyCatResponse { + string name = 1; + string kind = 2; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/client.sh Mon Dec 16 19:19:32 2019 +0900 @@ -0,0 +1,5 @@ +ls +go build client.go +ls -l +ls -l scripts +./client
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/server.sh Mon Dec 16 19:19:32 2019 +0900 @@ -0,0 +1,2 @@ +go build server.go +./server
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/server.go Mon Dec 16 19:19:32 2019 +0900 @@ -0,0 +1,40 @@ +package main + +import ( + "context" + "errors" + "google.golang.org/grpc" + "log" + "net" + cat "study-grpc/pb" +) + +type myCatService struct{} + +func (s *myCatService) GetMyCat(ctx context.Context, message *cat.GetMyCatMessage) (*cat.MyCatResponse, error) { + switch message.TargetCat { + case "tama": + return &cat.MyCatResponse{ + Name: "tama", + Kind: "Maine Coon", + }, nil + case "mike": + return &cat.MyCatResponse{ + Name: "mike", + Kind: "Norwegian Forest Cat", + }, nil + default: + return nil, errors.New("Not Found YourCat..") + } +} + +func main() { + port, err := net.Listen("tcp", ":1234") + if err != nil { + log.Println(err.Error()) + return + } + s := grpc.NewServer() + cat.RegisterCatServer(s, &myCatService{}) + s.Serve(port) +}