Přeskočit na obsah
_CORE
AI & Agentic Systems Core Informační Systémy Cloud & Platform Engineering Data Platforma & Integrace Security & Compliance QA, Testing & Observability IoT, Automatizace & Robotika Mobile & Digital Banky & Finance Pojišťovnictví Veřejná správa Obrana & Bezpečnost Zdravotnictví Energetika & Utility Telco & Média Průmysl & Výroba Logistika & E-commerce Retail & Loyalty
Reference Technologie Blog Knowledge Base O nás Spolupráce Kariéra
Pojďme to probrat

gRPC Service v Go

01. 01. 2024 1 min čtení intermediate

gRPC je high-performance RPC framework od Googlu. Protobuf serializace, HTTP/2, streaming, code generation.

Protobuf definice

syntax = “proto3”; package user; option go_package = “./pb”; service UserService { rpc GetUser(GetUserRequest) returns (User); rpc ListUsers(ListUsersRequest) returns (stream User); } message User { int32 id = 1; string name = 2; string email = 3; } message GetUserRequest { int32 id = 1; } message ListUsersRequest { int32 limit = 1; }

Go server

type server struct { pb.UnimplementedUserServiceServer } func (s *server) GetUser(ctx context.Context, req *pb.GetUserRequest) (*pb.User, error) { user, err := db.FindUser(req.Id) if err != nil { return nil, status.Errorf(codes.NotFound, “user not found”) } return &pb.User{Id: user.ID, Name: user.Name, Email: user.Email}, nil } func main() { lis, _ := net.Listen(“tcp”, “:50051”) s := grpc.NewServer() pb.RegisterUserServiceServer(s, &server{}) s.Serve(lis) }

Klíčový takeaway

gRPC pro microservices komunikaci — rychlejší než REST, typově bezpečné, streaming. Protobuf = schema.

grpcgoprotobufmicroservices