Kratos 一套轻量级 Go 微服务框架,包含大量微服务相关功能及工具。
github链接:https://github.com/go-kratos/kratos
前提条件:
- [安装golang]() https://golang.org/dl/
- 安装protoc https://github.com/protocolbuffers/protobuf
- 安装 protoc-gen-go https://github.com/protocolbuffers/protobuf-go
protobuf
Protobuf(Protocol Buffer)是google 的一种数据交换的格式,它独立于语言,独立于平台。它是一种二进制的格式,比使用 xml 进行数据交换快许多。可以把它用于分布式应用之间的数据通信或者异构环境下的数据交换。作为一种效率和兼容性都很优秀的二进制数据传输格式,可以用于诸如网络传输、配置文件、数据存储等诸多领域
protoc
protoc是protobuf文件(.proto)的编译器,可以借助这个工具把 .proto 文件转译成各种编程语言对应的源码,包含数据类型定义、调用接口等。
protoc在设计上把protobuf和不同的语言解耦了,底层用c++来实现protobuf结构的存储,然后通过插件的形式来生成不同语言的源码。
可以把protoc的编译过程分成简单的两个步骤:
1)解析.proto文件,转译成protobuf的原生数据结构在内存中保存;
2)把protobuf相关的数据结构传递给相应语言的编译插件,由插件负责根据接收到的protobuf原生结构渲染输出特定语言的模板。
protoc-gen-go
protoc-gen-go是protobuf编译插件系列中的Go版本
由于众所周知的原因,这里可能需要一个代理才能下载一些,如果你恰好也用clash for windows ,
1. 开启clash的允许局域网(默认端口是7890)
2. 点击允许局域网右边的icon (network ineterface)
1. 以太网Address:1x.1x.7x.7x 记住这个ip 就是你的本机ip
3. 去虚拟机里设置http代理和https代理
set http_proxy=http://1x.1x.7x.7x:7890
set https_proxy=http://1x.1x.7x.7x:7890
查看系统版本:lsb_release -a,如果你恰好也是ubuntu的fans
Distributor ID: Ubuntu
Description: Ubuntu 22.04 LTS
Release: 22.04
Codename: jammy
准备工作:
- 配置好golang的GOPATH
- 安装好扩展 sudo apt install autoconf automake libtool curl make libprotobuf-dev protobuf-compiler gcc
-
安装protoc
- sudo wget https://github.com/protocolbuffers/protobuf/releases/download/v3.19.4/protobuf-all-3.19.4.tar.gz
- tar -zxvf protobuf-all-3.19.4.tar.gz
- cd protobuf-3.19.4/
- git submodule update --init --recursive
- ./autogen.sh
- ./configure
- sudo make
- sudo make install
-
查看是否安装成功
- protoc --version
- 出现 libprotoc 3.19.4 表示成功
-
安装protoc-gen-go
go install github.com/golang/protobuf/protoc-gen-go\@latest
go install google.golang.org/protobuf/cmd/protoc-gen-go\@latest 这个好使一点
是否安装成功:去\$GOPATH 下的bin目录下查看是否有protoc-gen-go 文件,存在证明安装成功
-
安装kratos
-
go install 安装
-
go install github.com/go-kratos/kratos/cmd/kratos/v2\@latest 这里其实安装的是kratos的命令
kratos upgrade
-
-
源码编译安装
- git clone https://github.com/go-kratos/kratos
cd kratos
make install
- git clone https://github.com/go-kratos/kratos
-
6. 创建一个服务 官网链接 https://go-kratos.dev/docs/getting-started/start
# 创建项目模板
go env -w GO111MODULE=on
kratos new helloworld (# 使用默认模板创建项目
# 如在国内环境拉取失败, 可 -r 指定源
kratos new helloworld -r https://gitee.com/go-kratos/kratos-layout.git
# 进入项目目录
cd helloworld
# 拉取项目依赖
go mod download)
cd helloworld
# 拉取项目依赖
go mod download
# 生成proto模板
kratos proto add api/helloworld/helloworld.proto
# 生成proto源码
kratos proto client api/helloworld/helloworld.proto
# 生成server模板
kratos proto server api/helloworld/helloworld.proto -t internal/service
# 生成所有proto源码、wire等等
go generate ./...
# 运行程序
kratos run
测试一下接口
curl 'http://127.0.0.1:8000/helloworld/kratos'
# 输出:
{
"message": "Hello kratos"
}
看起来一切顺利\~
哪有什么一帆风顺的事儿,记录一下踩过的坑
1 go install的时候一直超时,配置代理
2 kratos 命令识别不了,没有安装CLI的kratos ( go install github.com/go-kratos/kratos/cmd/kratos/v2\@latest)
3 直接clone git上的protoc 安装不了,还是使用源码安装的
4 go install 安装一些东西的时候会默认放到\$GOPATH下,这里配置的是go所在的项目的bin文件夹里面,开始环境变量没配置好
5 go build 超时 用go env -w GOPROXY=https://goproxy.cn
6 通过clone的源码,然后去go build 没能尝试成功
如果一直不成功 参考一下这篇文章 https://www.clownfish.site/post/2022-09-25-ubuntu20.04%E5%AE%89%E8%A3%85protoc-gen-go/
我没参考\~