文章目录
- 1.环境准备
- 2.验证码服务
- 2.1 kratos 初始化验证码服务项目
- 2.2 使用 Protobuf 定义验证码生成接口
- 2.3 业务逻辑代码实现
1.环境准备
protoc和protoc-gen-go插件安装和kratos工具安装
-
protoc下载
- 下载二进制文件:https://github.com/protocolbuffers/protobuf/releases 下拉到最下方下载系统对应的版本,将解压出来bin目录下的protoc.exe拷贝到GOPATH的bin目录下。
- 查看protoc版本:protoc --version。
-
protoc-gen-go插件安装
- protoc是针对所有语言开发的,protoc-gen-go是专门针对go语言开发,因为是使用go语言开发的,所以直接使用go install就可以进行安装,安装后在GOPATH的bin沐目录下就会有对应的二进制文件。
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
,版本验证protoc-gen-go --version
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
,版本验证protoc-gen-go-grpc --version
-
kratos工具安装
- kratos也是go语言编写的,帮助我们快速生成代码的,直接go install安装即可,安装完成也会在GOPATH的bin目录中生成二进制文件。
- 安装命令:
go install github.com/go-kratos/kratos/cmd/kratos/v2@latest
- 版本验证:
kratos -v
2.验证码服务
2.1 kratos 初始化验证码服务项目
kratos new kratos-demo03
PS E:\java\go_code> kratos new kratos-demo03
🚀 Creating service kratos-demo03, layout repo is https://github.com/go-kratos/kratos-layout.git, please wait a moment.
Already up to date.
CREATED E:\java\go_code\kratos-demo03\.gitignore (590 bytes)
CREATED E:\java\go_code\kratos-demo03\Dockerfile (483 bytes)
CREATED E:\java\go_code\kratos-demo03\LICENSE (1087 bytes)
CREATED E:\java\go_code\kratos-demo03\Makefile (2608 bytes)
CREATED E:\java\go_code\kratos-demo03\README.md (1113 bytes)
CREATED E:\java\go_code\kratos-demo03\api\helloworld\v1\error_reason.pb.go (5126 bytes)
CREATED E:\java\go_code\kratos-demo03\api\helloworld\v1\error_reason.proto (306 bytes)
CREATED E:\java\go_code\kratos-demo03\api\helloworld\v1\greeter.pb.go (8299 bytes)
CREATED E:\java\go_code\kratos-demo03\api\helloworld\v1\greeter.proto (711 bytes)
CREATED E:\java\go_code\kratos-demo03\api\helloworld\v1\greeter_grpc.pb.go (3667 bytes)
CREATED E:\java\go_code\kratos-demo03\api\helloworld\v1\greeter_http.pb.go (2213 bytes)
CREATED E:\java\go_code\kratos-demo03\cmd\kratos-demo03\main.go (1834 bytes)
CREATED E:\java\go_code\kratos-demo03\cmd\kratos-demo03\wire.go (645 bytes)
CREATED E:\java\go_code\kratos-demo03\cmd\kratos-demo03\wire_gen.go (1121 bytes)
CREATED E:\java\go_code\kratos-demo03\configs\config.yaml (306 bytes)
CREATED E:\java\go_code\kratos-demo03\go.mod (1301 bytes)
CREATED E:\java\go_code\kratos-demo03\go.sum (9022 bytes)
CREATED E:\java\go_code\kratos-demo03\internal\biz\README.md (7 bytes)
CREATED E:\java\go_code\kratos-demo03\internal\biz\biz.go (134 bytes)
CREATED E:\java\go_code\kratos-demo03\internal\biz\greeter.go (1285 bytes)
CREATED E:\java\go_code\kratos-demo03\internal\conf\conf.pb.go (21434 bytes)
CREATED E:\java\go_code\kratos-demo03\internal\conf\conf.proto (805 bytes)
CREATED E:\java\go_code\kratos-demo03\internal\data\README.md (8 bytes)
CREATED E:\java\go_code\kratos-demo03\internal\data\data.go (500 bytes)
CREATED E:\java\go_code\kratos-demo03\internal\data\greeter.go (880 bytes)
CREATED E:\java\go_code\kratos-demo03\internal\server\grpc.go (867 bytes)
CREATED E:\java\go_code\kratos-demo03\internal\server\http.go (872 bytes)
CREATED E:\java\go_code\kratos-demo03\internal\server\server.go (158 bytes)
CREATED E:\java\go_code\kratos-demo03\internal\service\README.md (11 bytes)
CREATED E:\java\go_code\kratos-demo03\internal\service\greeter.go (723 bytes)
CREATED E:\java\go_code\kratos-demo03\internal\service\service.go (142 bytes)
CREATED E:\java\go_code\kratos-demo03\openapi.yaml (1169 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\README.md (15 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\errors\errors.proto (428 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\google\api\annotations.proto (1082 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\google\api\client.proto (3495 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\google\api\field_behavior.proto (3090 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\google\api\http.proto (15515 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\google\api\httpbody.proto (2748 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\google\protobuf\any.proto (6067 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\google\protobuf\api.proto (7942 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\google\protobuf\compiler\plugin.proto (8937 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\google\protobuf\descriptor.proto (39418 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\google\protobuf\duration.proto (5011 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\google\protobuf\empty.proto (2481 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\google\protobuf\field_mask.proto (8430 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\google\protobuf\source_context.proto (2389 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\google\protobuf\struct.proto (3874 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\google\protobuf\timestamp.proto (6606 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\google\protobuf\type.proto (6313 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\google\protobuf\wrappers.proto (4165 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\openapi\v3\annotations.proto (2254 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\openapi\v3\openapi.proto (22754 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\validate\README.md (84 bytes)
CREATED E:\java\go_code\kratos-demo03\third_party\validate\validate.proto (32133 bytes)
🍺 Project creation succeeded kratos-demo03
$ cd kratos-demo03
$ go generate ./...
$ go build -o ./bin/ ./...
$ ./bin/kratos-demo03 -conf ./configs
🤝 Thanks for using Kratos
📚 Tutorial: https://go-kratos.dev/docs/getting-started/start
🤝 Thanks for using Kratos
📚 Tutorial: https://go-kratos.dev/docs/getting-started/start
# 进入项目目录,拉取依赖\
cd kratos-demo03
go mod tidy
# 安装wire工具:kratos使用了依赖注入来生成相关的代码,所以运行项目前也需要安装
go get github.com/google/wire/cmd/wire
# 生成了wire_gen.go
go generate ./...
# 项目运行
kratos run
以上的layout的目录布局,仅仅是kratos推荐的项目目录布局方式
2.2 使用 Protobuf 定义验证码生成接口
- 定义 protobuf 文件说明接口
- 利用 protoc 基于 protobuf 生成必要代码
- 将生成的代码整合到项目中
- 完善业务逻辑
kratos proto add api/verifyCode/verifyCode.proto
syntax = "proto3";
package api.verifyCode;
// 生成的go代码所在的包
option go_package = "kratos-demo03/api/verifyCode;verifyCode";
// 定义 VerifyCode 服务
service VerifyCode {
rpc GetVerifyCode (GetVerifyCodeRequest) returns (GetVerifyCodeReply);
}
// 类型常量
enum TYPE {
DEFAULT = 0;
DIGIT = 1;
LETTER = 2;
MIXED = 3;
};
// 定义 GetVerifyCodeRequest 消息
message GetVerifyCodeRequest {
// 验证码长度
uint32 length = 1;
// 验证码类型
TYPE type = 2;
}
// 定义 GetVerifyCodeReply 消息
message GetVerifyCodeReply {
// 生成的验证码
string code = 1;
}
基于verifyCode.proto生成 client(Stub)相关代码
kratos proto client api/verifyCode/verifyCode.proto
- api/verifyCode/verifyCode.pb.go:类型定义代码
- api/verifyCode/verifyCode_grpc.pb.go:gRPC服务定义代码
基于verifyCode.proto文件生成 grpc服务代码
kratos proto server api/verifyCode/verifyCode.proto -t internal/service
- -t 选项指定生成文件所在位置,代码会生成在internal/service目录中的
internal/service/verifycode.go
internal/service/verifycode.go
该文件定义了最基本的 VerifyCode 服务和对应的 GetVerifyCode 方法
package service
import (
"context"
pb "kratos-demo03/api/verifyCode"
)
type VerifyCodeService struct {
pb.UnimplementedVerifyCodeServer
}
func NewVerifyCodeService() *VerifyCodeService {
return &VerifyCodeService{}
}
func (s *VerifyCodeService) GetVerifyCode(ctx context.Context, req *pb.GetVerifyCodeRequest) (*pb.GetVerifyCodeReply, error) {
return &pb.GetVerifyCodeReply{}, nil
}
将生成的服务代码注册到 gRPC 服务中
- 更新 internal/service/service.go 文件
- 告知 wire 依赖注入系统,如果需要 VerifyCodeService 的话,使用NewVerifyCodeService 函数来构建
将 VerifyCodeService 注册到 gRPC 服务中
- 更新 internal/server/grpc.go 文件
package server
import (
v1 "kratos-demo03/api/helloworld/v1"
"kratos-demo03/api/verifyCode"
"kratos-demo03/internal/conf"
"kratos-demo03/internal/service"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/transport/grpc"
)
// NewGRPCServer new a gRPC server.
func NewGRPCServer(c *conf.Server, greeter *service.GreeterService, verifyCodeService *service.VerifyCodeService, logger log.Logger) *grpc.Server {
var opts = []grpc.ServerOption{
grpc.Middleware(
recovery.Recovery(),
),
}
if c.Grpc.Network != "" {
opts = append(opts, grpc.Network(c.Grpc.Network))
}
if c.Grpc.Addr != "" {
opts = append(opts, grpc.Address(c.Grpc.Addr))
}
if c.Grpc.Timeout != nil {
opts = append(opts, grpc.Timeout(c.Grpc.Timeout.AsDuration()))
}
srv := grpc.NewServer(opts...)
v1.RegisterGreeterServer(srv, greeter)
verifyCode.RegisterVerifyCodeServer(srv, verifyCodeService)
return srv
}
生成依赖注入代码:go generate ./...
kratos run
建立ApiPost的项目
2.3 业务逻辑代码实现
- GetVerifyCode方法中添加code返回
- 添加RandCode方法,返回测试字符串"result"
- kratos启动测试
package service
import (
"context"
pb "kratos-demo03/api/verifyCode"
)
type VerifyCodeService struct {
pb.UnimplementedVerifyCodeServer
}
func NewVerifyCodeService() *VerifyCodeService {
return &VerifyCodeService{}
}
func (s *VerifyCodeService) GetVerifyCode(ctx context.Context, req *pb.GetVerifyCodeRequest) (*pb.GetVerifyCodeReply, error) {
return &pb.GetVerifyCodeReply{
Code: RandCode(int(req.Length), req.Type),
}, nil
}
func RandCode(l int, t pb.TYPE) string {
return "result"
}
// RandCode 开放的被调用的方法,用于区分类型
func RandCode(l int, t pb.TYPE) string {
switch t {
case pb.TYPE_DEFAULT:
fallthrough
case pb.TYPE_DIGIT:
return randCode("0123456789", l)
case pb.TYPE_LETTER:
return randCode("abcdefghijklmnopqrstuvwxyz", l)
case pb.TYPE_MIXED:
return randCode("0123456789abcdefghijklmnopqrstuvwxyz", l)
}
return ""
}
// randCode 随机的核心方法
func randCode(chars string, l int) string {
charsLen := len(chars)
result := make([]byte, l)
for i := 0; i < l; i++ {
// 核心函数 生成[0,n]的整型随机数
randIndex := rand.Intn(charsLen)
result[i] = chars[randIndex]
}
return string(result)
}