สร้าง microservices ด้วย Go + gRPC

Sharing is caring!

ในยุคของสถาปัตยกรรมแบบ Microservices การสื่อสารระหว่างบริการย่อย (services) ถือเป็นสิ่งสำคัญ gRPC คือหนึ่งในเครื่องมือที่ได้รับความนิยมสูง เนื่องจากมีความเร็วสูง ใช้ Protocol Buffers (Protobuf) ที่มีขนาดเล็ก และรองรับ bi-directional streaming ได้ดี โดยเฉพาะเมื่อนำมาใช้ร่วมกับภาษา Go ซึ่งมีจุดเด่นเรื่อง performance และ concurrency ยิ่งทำให้การสร้างระบบ microservices มีประสิทธิภาพมากขึ้น

🔍 gRPC คืออะไร?

  • gRPC ย่อมาจาก Google Remote Procedure Call
  • ทำงานบน HTTP/2
  • ใช้ Protobuf ในการ serialize ข้อมูล
  • รองรับ Streaming, Load Balancing, TLS

🔧 ติดตั้ง gRPC กับ Go

go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
export PATH="$PATH:$(go env GOPATH)/bin"
  

📁 ตัวอย่างโครงสร้างโปรเจกต์

.
├── proto/
│   └── user.proto
├── server/
│   └── main.go
├── client/
│   └── main.go
  

📜 ตัวอย่างไฟล์ user.proto

syntax = "proto3";

package user;
option go_package = "example.com/microservice/proto/userpb";

service UserService {
  rpc GetUser(GetUserRequest) returns (UserResponse);
}

message GetUserRequest {
  int32 id = 1;
}

message UserResponse {
  int32 id = 1;
  string name = 2;
}
  

🔄 สร้างโค้ด Go จาก .proto

protoc --go_out=. --go-grpc_out=. proto/user.proto
  

🖥️ สร้าง gRPC Server (server/main.go)

package main

import (
  "context"
  "log"
  "net"

  pb "example.com/microservice/proto/userpb"
  "google.golang.org/grpc"
)

type server struct {
  pb.UnimplementedUserServiceServer
}

func (s *server) GetUser(ctx context.Context, req *pb.GetUserRequest) (*pb.UserResponse, error) {
  return &pb.UserResponse{
    Id:   req.Id,
    Name: "User #" + fmt.Sprint(req.Id),
  }, nil
}

func main() {
  lis, err := net.Listen("tcp", ":50051")
  if err != nil {
    log.Fatalf("failed to listen: %v", err)
  }
  s := grpc.NewServer()
  pb.RegisterUserServiceServer(s, &server{})
  log.Println("gRPC server running at :50051")
  if err := s.Serve(lis); err != nil {
    log.Fatalf("failed to serve: %v", err)
  }
}
  

📡 สร้าง gRPC Client (client/main.go)

package main

import (
  "context"
  "log"
  "time"

  pb "example.com/microservice/proto/userpb"
  "google.golang.org/grpc"
)

func main() {
  conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())
  if err != nil {
    log.Fatalf("could not connect: %v", err)
  }
  defer conn.Close()

  client := pb.NewUserServiceClient(conn)
  ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  defer cancel()

  res, err := client.GetUser(ctx, &pb.GetUserRequest{Id: 1})
  if err != nil {
    log.Fatalf("error calling GetUser: %v", err)
  }

  log.Printf("Response: ID=%d, Name=%s", res.Id, res.Name)
}
  

⚙️ ข้อดีของ gRPC กับ Microservices

  • ประสิทธิภาพสูง (ใช้ HTTP/2 และ binary data)
  • เชื่อมต่อหลายภาษาได้ (Multi-language)
  • มีเครื่องมือตรวจสอบ schema ที่ชัดเจนจาก proto file
  • รองรับ load balancing และ service discovery

📌 ใช้งานจริงใน Production

  • แนะนำให้ใช้ TLS encryption
  • ใช้ gRPC Gateway หากต้องการ expose REST API
  • ใส่ context timeout สำหรับแต่ละ RPC call
  • สามารถ combine กับ Kubernetes เพื่อสร้าง service mesh ได้

🧠 กรณีศึกษา

  • Netflix ใช้ gRPC ในระบบ streaming backend
  • Spotify ใช้ gRPC กับบริการ machine learning
  • บริษัทไทยจำนวนมากใช้ Go + gRPC ร่วมกับ Kubernetes

📚 สรุป

gRPC เป็นเทคโนโลยีที่มีความเหมาะสมมากสำหรับระบบ microservices โดยเฉพาะเมื่อใช้งานร่วมกับภาษา Go ที่มีความเร็วและรองรับการทำงานแบบ concurrent ได้ดี คุณสามารถเริ่มต้นได้ทันทีด้วย proto file และ library มาตรฐาน

🔗 แหล่งอ้างอิง


🔍 ข้อความสำหรับ SEO

Go gRPC microservices | สร้างระบบ microservice ด้วย Go | gRPC คืออะไร | gRPC กับ Go ภาษาไทย | Protobuf Go | gRPC client server go

Leave a Reply

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *