电子商务网站建设规划的内容/电商运营转行后悔了
文章目录
- 环境
- 安装protoc编译器
- 创建项目
- 学习
- 脚手架
- New
- proto
- 添加proto文件
- 生成 Proto 代码
- 生成 Service 代码
- 参考资料
环境
安装protoc编译器
- 到 Releases · protocolbuffers/protobuf (github.com) 下载
- 解压
- 将可执行文件路径添加到PATH中
- 安装Go插件
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
安装
go install github.com/go-kratos/kratos/cmd/kratos/v2@latest
创建项目
# 国内拉取失败可使用gitee源
kratos new helloworld -r https://gitee.com/go-kratos/kratos-layout.git
# 亦可使用自定义的模板
kratos new helloworld -r xxx-layout.git
# 同时也可以通过环境变量指定源
KRATOS_LAYOUT_REPO=xxx-layout.git
kratos new helloworld
使用 -b 指定分支
kratos new helloworld -b main
使用 --nomod 添加服务,共用 go.mod ,大仓模式
kratos new helloworld
cd helloworld
kratos new app/user --nomod
生成pb和wire
# 生成所有proto源码、wire等等
go generate ./...
运行
kratos run
学习
- 脚手架使用
- 项目结构
- 依赖注入
- 日志库
- 配置文件读取
脚手架
# kratos -h
Kratos: An elegant toolkit for Go microservices.Usage:kratos [command]Available Commands:changelog Get a kratos change logcompletion Generate the autocompletion script for the specified shellhelp Help about any commandnew Create a service templateproto Generate the proto filesrun Run projectupgrade Upgrade the kratos toolsFlags:-h, --help help for kratos-v, --version version for kratosUse "kratos [command] --help" for more information about a command.
- changelog: 查看kratos更新日志
- completion: 可以为bash/fish/powershell/zsh提供命令补全
- new: 根据模板创建一个服务,默认使用https://github.com/go-kratos/kratos-layout.git
- proto: 生成proto文件
- run: 运行项目,实际调用的go run命令
New
# 通过 kratos 命令创建项目模板:
kratos new helloworld# 使用gitee源
kratos new helloworld -r https://gitee.com/go-kratos/kratos-layout.git# 使用自定义的模板
kratos new helloworld -r xxx-layout.git# 同时也可以通过环境变量指定源
KRATOS_LAYOUT_REPO=xxx-layout.git
kratos new helloworld# 使用 -b 指定分支
kratos new helloworld -b main# 使用 --nomod 添加服务,共用 go.mod ,大仓模式
kratos new helloworld
cd helloworld
kratos new app/user --nomod
proto
添加proto文件
kratos-layout 项目中对 proto 文件进行了版本划分,放在了 v1 子目录下
kratos proto add api/helloworld/v1/demo.proto
syntax = "proto3";package api.helloworld.v1;option go_package = "helloworld/api/helloworld/v1;v1";
option java_multiple_files = true;
option java_package = "api.helloworld.v1";service Demo {rpc CreateDemo (CreateDemoRequest) returns (CreateDemoReply);rpc UpdateDemo (UpdateDemoRequest) returns (UpdateDemoReply);rpc DeleteDemo (DeleteDemoRequest) returns (DeleteDemoReply);rpc GetDemo (GetDemoRequest) returns (GetDemoReply);rpc ListDemo (ListDemoRequest) returns (ListDemoReply);
}message CreateDemoRequest {}
message CreateDemoReply {}message UpdateDemoRequest {}
message UpdateDemoReply {}message DeleteDemoRequest {}
message DeleteDemoReply {}message GetDemoRequest {}
message GetDemoReply {}message ListDemoRequest {}
message ListDemoReply {}
生成 Proto 代码
# 可以直接通过 make 命令生成
make api# 或使用 kratos cli 进行生成
kratos proto client api/helloworld/v1/demo.proto
生成两个文件
api/helloworld/v1/demo.pb.go
api/helloworld/v1/demo_grpc.pb.go
生成 Service 代码
通过 proto 文件,可以直接生成对应的 Service 实现代码:
使用 -t
指定生成目录
kratos proto server api/helloworld/v1/demo.proto -t internal/service
package serviceimport ("context"pb "helloworld/api/helloworld/v1"
)type DemoService struct {pb.UnimplementedDemoServer
}func NewDemoService() *DemoService {return &DemoService{}
}func (s *DemoService) CreateDemo(ctx context.Context, req *pb.CreateDemoRequest) (*pb.CreateDemoReply, error) {return &pb.CreateDemoReply{}, nil
}
func (s *DemoService) UpdateDemo(ctx context.Context, req *pb.UpdateDemoRequest) (*pb.UpdateDemoReply, error) {return &pb.UpdateDemoReply{}, nil
}
func (s *DemoService) DeleteDemo(ctx context.Context, req *pb.DeleteDemoRequest) (*pb.DeleteDemoReply, error) {return &pb.DeleteDemoReply{}, nil
}
func (s *DemoService) GetDemo(ctx context.Context, req *pb.GetDemoRequest) (*pb.GetDemoReply, error) {return &pb.GetDemoReply{}, nil
}
func (s *DemoService) ListDemo(ctx context.Context, req *pb.ListDemoRequest) (*pb.ListDemoReply, error) {return &pb.ListDemoReply{}, nil
}
参考资料
Go工程化 - Project Layout 最佳实践
推荐一个零声学院免费教程,个人觉得老师讲得不错,分享给大家:Linux,Nginx,ZeroMQ,MySQL,Redis,
fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,
TCP/IP,协程,DPDK等技术内容,点击立即学习