Go Language
Golang에서 gRPC 사용하기 (Client / Server)
히진
2022. 9. 13. 17:46
반응형

해당 포스팅에선 protobuf 작성 및 해당 protobuf 컴파일하여 Client / Server Stub 생성 후 사용하는 예제까지
진행 하려고 합니다.
실습 환경
OS : MacOS
go version : go version go1.19 darwin/arm64
protoc --version : libprotoc 3.21.5
1. gRPC 설치하기
go get -u google.golang.org/gpc
go get -u google.golang.org/grpc/cmd/protoc-gen-go-grpc
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc
2. protobuf 작성
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syntax = "proto3"; | |
option go_package = ".;student"; | |
service StudentInfo { | |
rpc addStudent(Student) returns (StudentNum); | |
rpc getStudent(StudentNum) returns (Student); | |
} | |
message Student { | |
string num = 1; | |
string name = 2; | |
string birth = 3; | |
} | |
message StudentNum { | |
string num = 1; | |
} |
3. protobuf 컴파일 및 스텁 생성
protoc -I protobuf/ protobuf/studentInfo.proto --go_out=./pb/student --go-grpc_out=./pb/student
4. Client 코드 작성
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"google.golang.org/grpc" | |
pb "grpcTest/pb/student" | |
"log" | |
"time" | |
) | |
const ( | |
address = "localhost:8081" | |
) | |
func main() { | |
//Make connection | |
conn, err := grpc.Dial(address, grpc.WithInsecure()) | |
if err != nil { | |
log.Fatalf("connect err: %v", err) | |
} | |
defer conn.Close() | |
//Make StudentInfo Client | |
c := pb.NewStudentInfoClient(conn) | |
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | |
defer cancel() | |
//Student Info | |
studentInfo := &pb.Student{ | |
Name: "Heejin Kim", | |
Birth: "1993-11-09", | |
} | |
//Add StudentInfo | |
r, err := c.AddStudent(ctx, studentInfo) | |
if err != nil { | |
log.Fatalf("Fail AddStudent: %v", err) | |
} | |
log.Printf("Add Student num: %v ", r.Num) | |
//Get Student Info | |
student, err := c.GetStudent(ctx, &pb.StudentNum{Num: r.Num}) | |
if err != nil { | |
log.Fatalf("Could not get product: %v", err) | |
} | |
log.Printf("Get Student %v", student.String()) | |
} |
5. Server 코드 작성
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"google.golang.org/grpc" | |
"google.golang.org/grpc/codes" | |
"google.golang.org/grpc/status" | |
pb "grpcTest/pb/student" | |
"log" | |
"net" | |
"strconv" | |
) | |
const ( | |
port = ":8081" | |
) | |
type server struct { | |
studentMap map[string]*pb.Student | |
pb.UnimplementedStudentInfoServer | |
} | |
func (s *server) AddStudent(ctx context.Context, in *pb.Student) (*pb.StudentNum, error) { | |
if s.studentMap == nil { | |
s.studentMap = make(map[string]*pb.Student) | |
} | |
in.Num = strconv.Itoa(len(s.studentMap) + 1) | |
s.studentMap[in.Num] = in | |
log.Printf("Student %v : %v - Added.", in.Num, in.Name) | |
return &pb.StudentNum{Num: in.Num}, status.New(codes.OK, "").Err() | |
} | |
func (s *server) GetStudent(ctx context.Context, in *pb.StudentNum) (*pb.Student, error) { | |
student, exists := s.studentMap[in.Num] | |
if exists && student != nil { | |
log.Printf("Student %v : %v - Retrieved.", student.Num, student.Name) | |
return student, status.New(codes.OK, "").Err() | |
} | |
return nil, status.Errorf(codes.NotFound, "Not Found Student.", in.Num) | |
} | |
func main() { | |
lis, err := net.Listen("tcp", port) | |
if err != nil { | |
log.Fatalf("failed to listen: %v", err) | |
} | |
s := grpc.NewServer() | |
pb.RegisterStudentInfoServer(s, &server{}) | |
if err := s.Serve(lis); err != nil { | |
log.Fatalf("failed to serve: %v", err) | |
} | |
} |
6. 실행 예제
Client


Server

반응형