Go framework-Kratos

一、Go framework

框架Github开源时间开源方
Kratoshttps://github.com/go-kratos/kratos2019Bilibili
go-kithttps://github.com/go-kit/kit/2015团队开源
go-zerohttps://github.com/tal-tech/go-zero2020团队开源
TarsGohttps://github.com/TarsCloud/TarsGo2018腾讯
Jupiterhttps://github.com/douyu/jupiter2020斗鱼开源
Istiohttps://github.com/istio/istio2017Google、IBM、Lyft开源
Kitexhttps://github.com/cloudwego/kitex2020字节跳动
Go-micro(m3o)https://github.com/asim/go-micro2015Micro Sercives,Inc
Dubbo-gohttps://github.com/apache/dubbo-go2019阿里

1、Kratos 介绍

        Kratos 是一套由 Bilibili 开源的轻量级 Go 微服务框架,包含大量微服务相关框架及工具。Kratos(奎托斯)是希腊神话中的战神,其主要经历是由凡人成为战神并展开弑神屠杀。

Kratos官网

1.1、Kratos 框架开发依赖安装

1、Go语言环境,All releases - The Go Programming Language

$ go version
go version go1.20.6 windows/amd64

$ go env -w GO111MODULE=on

$ go env -w GOPROXY=https://goproxy.cn,direct

2、protoc,Protocol Compiler 编辑器 下载Releases · protocolbuffers/protobuf · GitHub

下载完直接解压,配置到path环境变量中

$ protoc --version
libprotoc 3.21.6

3、protoc-gen-go,Protoc 的插件,用于生成 Go 代码

go 语言开发的插件,使用 go install 安装

# 安装最新版本
$ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

# 测试
$ protoc-gen-go --version
protoc-gen-go v1.31.0
$ protoc-gen-go-grpc -version
protoc-gen-go-grpc 1.3.0

4、kratos,kratos 框架配套的脚手架工具

$ go install github.com/go-kratos/kratos/cmd/kratos/v2@latest

# 测试
$ kratos -v
kratos version v2.7.0

2、初始化 Kratos 项目

1、使用 Kratos完成第一个验证码校验服务verifyCode。

D:\GIT_workspace\go-valet-driving-system\backend>kratos new verifyCode
🚀 Creating service verifyCode, layout repo is https://github.com/go-kratos/kratos-layout.git, please wait a moment.

Cloning into 'C:\Users\Administrator\.kratos\repo\github.com\go-kratos/kratos-layout@main'...

[32mCREATED[0m D:\GIT_workspace\go-valet-driving-system\backend\verifyCode\.gitignore (590 bytes)
[32mCREATED[0m D:\GIT_workspace\go-valet-driving-system\backend\verifyCode\Dockerfile (483 bytes)
........
.......
[32mCREATED[0m D:\GIT_workspace\go-valet-driving-system\backend\verifyCode\third_party\validate\validate.proto (32133 bytes)

🍺 Project creation succeeded [32mverifyCode[0m
💻 Use the following command to start the project 👇:

[37m$ cd verifyCode[0m
[37m$ go generate ./...[0m
[37m$ go build -o ./bin/ ./... [0m
[37m$ ./bin/verifyCode -conf ./configs
[0m
                        🤝 Thanks for using Kratos
        📚 Tutorial: https://go-kratos.dev/docs/getting-started/start

该布局提供了最基本的目录结构和一个用于测试的 HTTP 和 gRPC 接口。

2、进入项目目录,拉取依赖

$ go mod tidy

D:\GIT_workspace\go-valet-driving-system\backend>cd verifyCode

D:\GIT_workspace\go-valet-driving-system\backend\verifyCode>go mod tidy
go: downloading github.com/google/wire v0.5.0
go: downloading github.com/go-kratos/kratos/v2 v2.7.0
....

3、运行项目前,需要先生成相应源码,主要是使用了 wire 的依赖注入相关代码:

$ go get github.com/google/wire/cmd/wire
$ go generate ./...

D:\GIT_workspace\go-valet-driving-system\backend\verifyCode>go get github.com/google/wire/cmd/wire
go: downloading github.com/google/subcommands v1.0.1
go: downloading golang.org/x/tools v0.6.0
go: downloading golang.org/x/mod v0.8.0

D:\GIT_workspace\go-valet-driving-system\backend\verifyCode>go generate ./...
wire: verifyCode/cmd/verifyCode: wrote D:\GIT_workspace\go-valet-driving-system\backend\verifyCode\cmd\verifyCode\wire_gen.go

4、运行项目,使用 kratos 工具:

$ kratos run

D:\GIT_workspace\go-valet-driving-system\backend\verifyCode>kratos run
2023/08/24 23:32:51 maxprocs: Leaving GOMAXPROCS=8: CPU quota undefined
DEBUG msg=config loaded: config.yaml format: yaml
INFO ts=2023-08-24T23:32:52+08:00 caller=http/server.go:317 service.id=MS-TGOOFNKABBOB service.name= service.version= trace.id= span.id= msg=[HTTP] server listening on: [::]:8000
INFO ts=2023-08-24T23:32:52+08:00 caller=grpc/server.go:212 service.id=MS-TGOOFNKABBOB service.name= service.version= trace.id= span.id= msg=[gRPC] server listening on: [::]:9000

#以上信息,表示项目运行成功,正在监听 HTTP 8000, gRPC 9000 端口。

测试:http://localhost:8000/helloworld/lwz

helloworld:是kratos生成时默认生成的接口

 使用kratos创建项目的步骤

$ kratos new projectName_xxx
$ cd projectName_xxx
$ go mod tidy
$ go get github.com/google/wire/cmd/wire
$ go generate ./...
$ kratos run

3、使用 Protobuf 、生成 Go 代码

可以看到api\helloworld\v1\greeter.proto文件。

使用 .proto 文件定义接口,基于 .proto 文件生成基础代码。

3.1、增加proto文件模板

$ kratos proto add xxx_path/xxx.proto

D:\GIT_workspace\go-valet-driving-system\backend\verifyCode>kratos proto add api/verifyCode/verifyCode.proto

D:\GIT_workspace\go-valet-driving-system\backend\verifyCode>

3.2、修改proto文件模板

默认生成带增删改查接口,看需求删改。

syntax = "proto3";

package api.verifyCode;

// 生成的go代码所在的包
option go_package = "verifyCode/api/verifyCode;verifyCode";
option java_multiple_files = true;
option java_package = "api.verifyCode";

// 定义 VerifyCode 服务
service VerifyCode {
	rpc CreateVerifyCode (CreateVerifyCodeRequest) returns (CreateVerifyCodeReply);
	rpc UpdateVerifyCode (UpdateVerifyCodeRequest) returns (UpdateVerifyCodeReply);
	rpc DeleteVerifyCode (DeleteVerifyCodeRequest) returns (DeleteVerifyCodeReply);
	rpc GetVerifyCode (GetVerifyCodeRequest) returns (GetVerifyCodeReply);
	rpc ListVerifyCode (ListVerifyCodeRequest) returns (ListVerifyCodeReply);
}

message CreateVerifyCodeRequest {}
message CreateVerifyCodeReply {}

message UpdateVerifyCodeRequest {}
message UpdateVerifyCodeReply {}

message DeleteVerifyCodeRequest {}
message DeleteVerifyCodeReply {}

message GetVerifyCodeRequest {}
message GetVerifyCodeReply {}

message ListVerifyCodeRequest {}
message ListVerifyCodeReply {}

修改,只保留获取验证码

syntax = "proto3";

package api.verifyCode;
// 生成的go代码所在的包
option go_package = "verifyCode/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;
}

3.3、根据修改完的模板文件生成客户端代码

$ kratos proto client xxx_path/xxxx.proto

D:\GIT_workspace\go-valet-driving-system\backend\verifyCode>kratos proto client api/verifyCode/verifyCode.proto
go install github.com/go-kratos/kratos/cmd/kratos/v2@latest
go install github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2@latest
go: downloading github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2 v2.0.0-20230823024326-a09f4d8ebba9
go: downloading github.com/go-kratos/kratos/cmd/protoc-gen-go-http v0.0.0-20210217095515-c4e4aa563867
go: downloading google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd
go: downloading google.golang.org/protobuf v1.28.0
go install github.com/go-kratos/kratos/cmd/protoc-gen-go-errors/v2@latest
go: downloading github.com/go-kratos/kratos/cmd/protoc-gen-go-errors v0.0.0-20210217095515-c4e4aa563867
go: downloading github.com/go-kratos/kratos/cmd/protoc-gen-go-errors/v2 v2.0.0-20230823024326-a09f4d8ebba9
go: downloading golang.org/x/text v0.3.8
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/google/gnostic/cmd/protoc-gen-openapi@latest
go: downloading github.com/google/gnostic v0.6.9
go: downloading google.golang.org/protobuf v1.27.1
go: downloading google.golang.org/genproto v0.0.0-20220107163113-42d7afdf6368
go: downloading gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
go: downloading github.com/golang/protobuf v1.5.2
proto: api/verifyCode/verifyCode.proto

代码会生成在 api/verifyCode/ 目录中:

# 类型定义代码
api/verifyCode/verifyCode.pb.go
# gRPC服务定义代码
api/verifyCode/verifyCode_grpc.pb.go
# 注意 http 代码只会在 proto 文件中声明了 http 时才会生成。
api/verifyCode/verifyCode_http.pb.go

注意:生成的以上代码不需要手动编辑。

3.4、根据修改完的模板文件生成服务端代码

$ kratos proto server api/verifyCode/verifyCode.proto -t internal/service

# -t 选项指定生成文件所在位置,代码会生成在 internal/service 目录中verifycode.go

D:\GIT_workspace\go-valet-driving-system\backend\verifyCode> kratos proto server api/verifyCode/verifyCode.proto -t internal/service
internal\service\verifycode.go

internal/service/verifycode.go

package service

import (
	"context"

	pb "verifyCode/api/verifyCode"
)

type VerifyCodeService struct {
	pb.UnimplementedVerifyCodeServer
}

func NewVerifyCodeService() *VerifyCodeService {
	return &VerifyCodeService{}
}

func (s *VerifyCodeService) CreateVerifyCode(ctx context.Context, req *pb.CreateVerifyCodeRequest) (*pb.CreateVerifyCodeReply, error) {
	return &pb.CreateVerifyCodeReply{}, nil
}
func (s *VerifyCodeService) UpdateVerifyCode(ctx context.Context, req *pb.UpdateVerifyCodeRequest) (*pb.UpdateVerifyCodeReply, error) {
	return &pb.UpdateVerifyCodeReply{}, nil
}
func (s *VerifyCodeService) DeleteVerifyCode(ctx context.Context, req *pb.DeleteVerifyCodeRequest) (*pb.DeleteVerifyCodeReply, error) {
	return &pb.DeleteVerifyCodeReply{}, nil
}
func (s *VerifyCodeService) GetVerifyCode(ctx context.Context, req *pb.GetVerifyCodeRequest) (*pb.GetVerifyCodeReply, error) {
	return &pb.GetVerifyCodeReply{}, nil
}
func (s *VerifyCodeService) ListVerifyCode(ctx context.Context, req *pb.ListVerifyCodeRequest) (*pb.ListVerifyCodeReply, error) {
	return &pb.ListVerifyCodeReply{}, nil
}

至此,通过编写 .proto 文件来生成接口基础代码的工作就完成了。

接下来还需要将生成的服务代码注册到 gRPC 服务中。

4、将生成的服务代码注册到 gRPC 服务中

4.1、更新 internal/service/service.go 文件

// ProviderSet is service providers.
var ProviderSet = wire.NewSet(NewGreeterService, NewVerifyCodeService)

        在以上的 `wire.NewSet()` 调用中,添加第二个参数 NewVerifyCodeService。这个函数是用来生成 VerifyCodeService 服务的,定义在internal/service/verifycode.go 中。以上代码的意思就是告知 wire 依赖注入系统,如果需要 VerifyCodeService 的话,使用 NewVerifyCodeService 函数来构建。

4.2、更新 internal/server/grpc.go 文件:

在 NewGRPCServer 函数中:
        1.增加一个参数
        2.在函数体中,增加一行代码
用于将 VerifyCodeService 注册到 gRPC 服务中:
internal/server/grpc.go

Web framework-Gin

Go framework-Beego

Golang学习+深入(一)

年轻不怕输!

谁能比别人领先一步掌握新技术,谁就在竞争中赢得了先机。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/89024.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

计算机竞赛 基于人工智能的图像分类算法研究与实现 - 深度学习卷积神经网络图像分类

文章目录 0 简介1 常用的分类网络介绍1.1 CNN1.2 VGG1.3 GoogleNet 2 图像分类部分代码实现2.1 环境依赖2.2 需要导入的包2.3 参数设置(路径,图像尺寸,数据集分割比例)2.4 从preprocessedFolder读取图片并返回numpy格式(便于在神经网络中训练)2.5 数据预…

Linux 定时任务 crontab 用法学习整理

一、linux版本 lsb_release -a 二、crontab 用法学习 2.1,crontab 简介 linux中crontab命令用于设置周期性被执行的指令,该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行。cron 系统调度进程。…

简述docker的网络模式

Docker 提供了多种网络模式,用于控制容器之间以及容器与主机之间的网络通信。以下是 Docker 的一些常见网络模式 briage模式: docker容器启动时默认就是该模式,在该模式下,docker容器会连接到一个名为docker0的虚拟以太网桥上,通…

脑机接口里程碑!一天2篇Nature!

2023年8月23日,《Nature》期刊一口气发表了两项独立的脑机接口方向的研究。 一项来自加州大学旧金山分校华裔科学家张复伦团队,另一项来自斯坦福大学的神经科学家弗朗西斯威利特(Francis Willett)团队。两项研究都旨在帮助那些因脑损伤和疾病而失去语言能…

Ubuntu虚拟机网络无法连接的几种解决方法

虚拟机网络无法连接的几种解决方法 问题状况描述可能的解决方案 问题状况描述 Ubuntu虚拟机没有网络,无法ping通互联网,左上角网络连接图标消失等情况可能的解决方案 1.重启虚拟机网络编辑器 2.重启虚拟机网络适配器 3.重启虚拟机网络服务器1.重启网络…

day-31 代码随想录算法训练营(19)贪心part01

455.分发饼干 思路一:贪心思路,大饼干先分给大胃口 思路二:小饼干先分给小胃口 376.摆动序列 分析摆动:记 presub 为前面与当前数之差,lastsub 为当前与后面数之差 思路: 1.正常摆动时,需要 …

5个流程图模板网站,帮你轻松绘制专业流程图

在复杂的项目管理和团队协作中,流程图成为了一个必不可少的工具。从零开始创建流程图可能会很耗时,同时也需要一定的技能。使用模板可以让流程图方便制作又保持高颜值,降低制作的成本,一款模板众多、功能强大、具有丰富编辑工具的…

python自动化入门之Python编写脚本实现自动化爬虫详解

想知道如何使用Python轻松高效地获取网络上的信息? 本篇文章将探索Python自动化爬虫,并展示如何编写实用的脚本。 1. 什么是Python爬虫? 爬虫顾名思义,就是像蜘蛛一样在网络上爬行,抓取各种有用信息的一种程序。而Pyt…

Qt应用开发(拓展篇)——示波器/图表 QCustomPlot

一、介绍 QCustomPlot是一个用于绘图和数据可视化的Qt C小部件。它没有进一步的依赖关系,提供友好的文档帮助。这个绘图库专注于制作好看的,出版质量的2D绘图,图形和图表,以及为实时可视化应用程序提供高性能。 QCustomPl…

【点云分割】points3d框架学习01 —— 安装和配置

安装 $ pip install torch1.12.1cu113 torchvision0.13.1cu113 torchaudio0.12.1 --extra-index-url https://download.pytorch.org/whl/cu113 $ pip install torch-points3d $ pip install ipython $ pip install trame $ pip install h5py $ pip install gdown案例 from to…

Docker拉取并配置Grafana

Linux下安装Docker请参考:Linux安装Docker 安装准备 新建挂载目录 /opt/grafana/data目录,准备用来挂载放置grafana的数据 /opt/grafana/plugins目录,准备用来放置grafana的插件 /opt/grafana/config目录,准备用来挂载放置graf…

Python Opencv实践 - 图像直方图自适应均衡化

import cv2 as cv import numpy as np import matplotlib.pyplot as pltimg cv.imread("../SampleImages/cat.jpg", cv.IMREAD_GRAYSCALE) print(img.shape)#整幅图像做普通的直方图均衡化 img_hist_equalized cv.equalizeHist(img)#图像直方图自适应均衡化 #1. 创…

yolov5的xml文件转txt文件格式(详细解释与完整代码供应)

文章目录 前言一、yolov5训练数据格式介绍1、txt的类别对应说明2、txt的文件说明3、txt文件格式3、yolov5训练文件形式 二、xml文件读取代码解读三、xml文件转txt文件1、xml转txt代码解读2、保存txt文件代码解读 四、完整代码 前言 本文章实现xml数据格式转yolov5的txt格式&am…

ORB-SLAM2算法11之地图点MapPoint

文章目录 0 引言1 MapPoint类1.1 构造函数1.2 成员函数1.2.1 AddObservation1.2.2 EraseObservation1.2.3 SetBadFlag1.2.4 Replace1.2.5 ComputeDistinctiveDescriptors1.2.6 UpdateNormalAndDepth1.2.7 PredictScale 2 MapPoint类用途 0 引言 ORB-SLAM2算法7详细了解了Syste…

网络协议详解之STP

目录 一、STP协议(生成树) 1.1 生成树协议核心知识点: 1.2 生成树协议与导致问题: 生成树含义: 1.3 802.1D 规则: 802.1D 缺点: 1.4 PVST cisco私有 1.5 PVST 1.6 快速生成树 快速的原…

uniapp 微信小程序:RecorderManager 录音DEMO

uniapp 微信小程序:RecorderManager 录音DEMO 简介index.vue参考资料 简介 使用 RecorderManager 实现录音。及相关的基本操作。(获取文件信息,上传文件) 此图包含Demo中用于上传测试的服务端程序upload.exe,下载后用…

【Axure原型分享】能统计中英文字数的多行输入框

今天和大家分享能统计中英文字数的多行输入框的原型模板,在输入框里输入内容后,能够动态根据输入框的内容,统计出字符数量,包括总字数、中文字数、英文字数、数字字数、其他标点符号的字数,具体效果可以观看下方视频或…

网络安全(黑客)自学剖析

想自学网络安全(黑客技术)首先你得了解什么是网络安全!什么是黑客! 网络安全可以基于攻击和防御视角来分类,我们经常听到的 “红队”、“渗透测试” 等就是研究攻击技术,而“蓝队”、“安全运营”、“安全…

04_Redis与mysql数据双写一致性案例

04——redis与mysql数据双写一致性 一、canal 是什么 canal[ka’nel,中文翻译为水道/管道/沟渠/运河,主要用途是用于MySQL数据库增量日志数据的订阅、消费和解析,是阿里巴巴开发并开源的,采用Java语言开发; 历史背景是早期阿里巴巴因为杭州和…

【Python爬虫】使用代理ip进行网站爬取

前言 使用代理IP进行网站爬取可以有效地隐藏你的真实IP地址,让网站难以追踪你的访问行为。本文将介绍Python如何使用代理IP进行网站爬取的实现,包括代理IP的获取、代理IP的验证、以及如何把代理IP应用到爬虫代码中。 1. 使用代理IP的好处 在进行网站爬…