kratos学习

简介: kratos学习

环境

安装protoc编译器

  1. Releases · protocolbuffers/protobuf (github.com) 下载
  2. 解压
  3. 将可执行文件路径添加到PATH中
  4. 安装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 log
  completion  Generate the autocompletion script for the specified shell
  help        Help about any command
  new         Create a service template
  proto       Generate the proto files
  run         Run project
  upgrade     Upgrade the kratos tools

Flags:
  -h, --help      help for kratos
  -v, --version   version for kratos

Use "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 service

import (
    "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 最佳实践

相关文章
|
2月前
|
Java 中间件 Nacos
开箱即用的 GoWind Admin|风行,企业级前后端一体中后台框架:kratos-bootstrap 入门教程(类比 Spring Boot)
kratos-bootstrap 是 GoWind Admin 的核心引导框架,类比 Spring Boot,提供应用初始化、配置管理、组件集成等一站式解决方案。通过标准化流程与多源配置支持,开发者可快速构建企业级中后台服务,专注业务开发,降低微服务复杂度。
323 3
|
JSON Go 数据库
Golang微服务框架居然可以开发单体应用?—— Kratos单体架构实践
微服务框架也是可以用于开发单体架构(monolith architecture)的应用。并且,单体应用也是最小的、最原始的、最初的项目状态,经过渐进式的开发演进,单体应用能够逐步的演变成微服务架构,并且不断的细分服务粒度。微服务框架开发的单体架构应用,既然是一个最小化的实施,那么它只需要使用到微服务框架最小的技术,也就意味着它只需要用到微服务框架最少的知识点,拿它来学习微服务框架是极佳的。
1714 0
|
11月前
|
JSON 前端开发 测试技术
2025年 5 个好用的 Socket.IO 调试工具推荐
在实时通信应用开发中,Socket.IO 是一个非常流行的框架,但调试其应用可能较为棘手。为此,推荐5个好用的 Socket.IO 调试工具:Apifox、WebSocket King、Socket.IO Test Client、Socket.IO Inspector 和 Postman。其中,Apifox 集成设计、调试、测试和文档生成于一体,支持多种版本的 Socket.IO 客户端,提供强大的接口管理和团队协作功能;
|
算法 关系型数据库 MySQL
分布式唯一ID生成:深入理解Snowflake算法在Go中的实现
在分布式系统中,确保每个节点生成的 ID 唯一且高效至关重要。Snowflake 算法由 Twitter 开发,通过 64 位 long 型数字生成全局唯一 ID,包括 1 位标识位、41 位时间戳、10 位机器 ID 和 12 位序列号。该算法具备全局唯一性、递增性、高可用性和高性能,适用于高并发场景,如电商促销时的大量订单生成。本文介绍了使用 Go 语言的 `bwmarrin/snowflake` 和 `sony/sonyflake` 库实现 Snowflake 算法的方法。
879 1
分布式唯一ID生成:深入理解Snowflake算法在Go中的实现
|
API Go 网络架构
Kratos 大乱炖 —— 整合其他Web框架:Gin、FastHttp、Hertz
Kratos默认的RPC框架使用的是gRPC,支持REST和protobuf两种通讯协议。其API都是使用protobuf定义的,REST协议是通过[grpc-gateway](https://github.com/grpc-ecosystem/grpc-gateway)转译实现的。使用protobuf定义API是具有极大优点的,具有很强的可读性、可维护性,以及工程性。工程再大,人员再多,也不会乱。 一切看起来都是很美好的。那么,问题来了,我们现在使用的是其他的Web框架,迁移就会有成本,有风险,不可能一下子就把历史存在的代码一口气转换过来到Kratos框架。那我可以在Kratos中整合其他
1330 0
|
机器学习/深度学习 前端开发 Go
Golang微服务框架kratos实现SSE服务
我也是最近才知道SSE的,问了下周围的人,发现知道的人也着实不多的。我是怎么知道SSE的呢?我看了下OpenAI的API,有一个Stream模式,就是使用的SSE实现的。说白了,这就是一个HTTP长连接通过服务端持续发送数据到前端的协议。在网络不稳定的情况下,它比Websocket要更好。
1023 0
|
缓存 PyTorch API
Transformers 4.37 中文文档(四十)(2)
Transformers 4.37 中文文档(四十)
418 1
|
Java 开发者
Java的三元表达式用法
Java的三元表达式用法
1593 1
|
算法 前端开发 Android开发
Android文字基线Baseline算法的使用讲解,Android开发面试题
Android文字基线Baseline算法的使用讲解,Android开发面试题
Android文字基线Baseline算法的使用讲解,Android开发面试题

热门文章

最新文章