gRPC+Protocol Buffer Go微服務實戰 - 用戶服務開發

概要用戶服務基本是每個互聯網產品里必備的一個服務了 , 因為沒有用戶基本是什么也干不了 。所以他的重要性不言而喻 。本文主要介紹下如何開發一個用戶微服務,以及他的詳細開發流程 。
目錄

  • Go微服務實戰 - 從0到1搭建一個類Instagram應用(持續更新)
  • Go微服務實戰 - 用戶服務(gRPC+Protocol Buffer)
  • Go微服務實戰 - 關系服務服務(gRPC+Protocol Buffer)
  • Go微服務實戰 - 動態服務(gRPC+Protocol Buffer)
  • Go微服務實戰 - 聚合服務(http)
調用流程圖
gRPC+Protocol Buffer Go微服務實戰 - 用戶服務開發

文章插圖
技術棧
  • Go
  • Eagle 開發框架
  • Redis
  • MySQL
  • Gorm
  • docker
  • kubernetes
接口開發使用proto方式定義,主要包含以下接口
gRPC+Protocol Buffer Go微服務實戰 - 用戶服務開發

文章插圖
開發步驟這里詳細的記錄了開發的步驟,方便參看本項目的同學知道其實現過程 。
1、生成proto模板文件
eagle proto add api/user/v1/user.proto內容如下
syntax = "proto3";package api.user.v1;option go_package = "github.com/go-microservice/user-service/api/user/v1;v1";option java_multiple_files = true;option java_package = "api.user.v1";service UserService { rpc CreateUser (CreateUserRequest) returns (CreateUserReply); rpc UpdateUser (UpdateUserRequest) returns (UpdateUserReply); rpc DeleteUser (DeleteUserRequest) returns (DeleteUserReply); rpc GetUser (GetUserRequest) returns (GetUserReply); rpc ListUser (ListUser1Request) returns (ListUserReply);}message CreateUser1Request {}message CreateUser1Reply {}message UpdateUserRequest {}message UpdateUserReply {}message DeleteUserRequest {}message DeleteUserReply {}message GetUserRequest {}message GetUserReply {}message ListUserRequest {}message ListUserReply {}2、為proto填充業務方法及字段定義
vim api/user/v1/user.proto3、生成pb文件
# 生成所有protomake grpc# 或者# 生成指定proto的pb文件eagle proto client api/user/v1/user.proto# Outputll api/user/v1/user.pb.go #新增user.protouser_grpc.pb.go #新增會生成兩個文件 api/user/v1/user.pb.goapi/user/v1/user.pb.go
4、生成server骨架代碼
# 生成骨架代碼eagle proto server api/user/v1/user.proto# 默認會輸出到 internal/service# 如果需要指定到對應的目錄,可以使用 -t 參數, eg:# eagle proto server -t internal/logic# 查看internal/service/user_svc.go5、注冊服務到gRPC Server
// internal/server/grpc.go import (...v1 "github.com/go-microservice/user-service/api/user/v1"...)...// NewGRPCServer creates a gRPC serverfunc NewGRPCServer( cfg *app.ServerConfig, // 新增 svc *service.UserServiceServer,) *grpc.Server { grpcServer := grpc.NewServer(grpc.Network("tcp"),grpc.Address(cfg.WriteTimeout),grpc.Timeout(cfg.WriteTimeout), ) // register biz service // 新增 v1.RegisterUserServiceServer(grpcServer, svc) return grpcServer}6、在生成的server中編寫業務邏輯
// vim internal/service/user_svc.gopackage serviceimport ( "context" pb "github.com/go-microservice/moment-service/api/user/v1")var ( _ pb.UserServiceServer = (*UserServiceServer)(nil))type UserServiceServer struct { pb.UnimplementedUserServiceServer}func NewUserServiceServer() *UserServiceServer { return &UserServiceServer{}}func (s *UserServiceServer) CreateUser(ctx context.Context, req *pb.CreateUserRequest) (*pb.CreateUserReply, error) { return &pb.CreateUserReply{}, nil}func (s *UserServiceServer) UpdateUser(ctx context.Context, req *pb.UpdateUserRequest) (*pb.UpdateUserReply, error) { return &pb.UpdateUserReply{}, nil}func (s *UserServiceServer) DeleteUser(ctx context.Context, req *pb.DeleteUserRequest) (*pb.DeleteUserReply, error) { return &pb.DeleteUserReply{}, nil}func (s *UserServiceServer) GetUser(ctx context.Context, req *pb.GetUserRequest) (*pb.GetUserReply, error) { return &pb.GetUserReply{}, nil}func (s *UserServiceServer) ListUser(ctx context.Context, req *pb.ListUserRequest) (*pb.ListUserReply, error) { return &pb.ListUserReply{}, nil}后面就可以在這里補充具體的業務邏輯處理了 。
7、啟動服務
# 在根目錄下運行go run main.go確保運行gRPC server
// main.go...eagle.WithServer( // init gRPC server gs,),...8、接口調試
調試工具,這里使用 [grpcurl](https://github.com/fullstorydev/grpcurl)
# 查看服務列表grpcurl -plaintext localhost:9090 list# Outputapi.user.v1.UserServicegrpc.health.v1.Healthgrpc.reflection.v1alpha.ServerReflection# 訪問列表grpcurl -plaintext -d '{"user_id":2}' localhost:9090 api.user.v1.UserService/ListUser

推薦閱讀