《go微服务框架Kratos 》连载五:使用protbuf创建http服务器
一、介绍
前几张我们已经学了kratos的基本框架。本章我们来深入剖析一下原理。
kratos框架之所以能够使用protbuf创建http服务器,多亏了框架自带的protoc-gen-go-http
插件。
那么我们是否可以在其他地方使用这个插件呢,答案是可以,今天我们就试一下。
本文章代码地址在 https://github.com/hisheng/kratos-http
1.1 准备目录
我们新建一个 kratos-http目录,并且go模块初始化。
创建目录:
mkdir kratos-http && cd kratos-http
go项目初始化:
我们在kratos-http根目录执行一下代码
go mod init github.com/hisheng/kratos-http
1.2 安装protoc-gen-go以及http扩展protoc-gen-go-http
我们在kratos-http根目录执行一下代码,安装扩展。
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2@latest
二、创建protobuf文件
2.1 准备目录
我们在kratos-http根目录,创建一个api目录来存放我们的proto文件
mkdir api && cd api
2.2 创建user.proto
此时我们新建一个 user.proto 文件用来实现我们的http服务。
touch user.proto
我们user.proto的代码如下:
syntax = "proto3";
package api;
import "google/api/annotations.proto";
option go_package = "github/hisheng/kratos-http/api";
service User {
rpc CreateUser (CreateUserRequest) returns (CreateUserReply){
option (google.api.http) = {
post: "/user",
body: "*",
};
};
rpc ListUser (ListUserRequest) returns (ListUserReply){
option (google.api.http) = {
get: "/users",
};
};
}
message CreateUserRequest {}
message CreateUserReply {}
message ListUserRequest {}
message ListUserReply {}
2.3 解决proto文件 cannot import "google/api/annotations.proto"
此时我们在proto文件里,引入了一个第三方的proto文件,此文件没有报错怎么办?
我们一般这类的第三方文件依赖发到third_party目录。
2.3.1 引入google proto库。
打开https://github.com/hisheng/kratos-http
我们项目代码地址。
直接把third_party目录复制下来就可以了。
此时我们的目录如下:
➜ kratos-http git:(master) tree
.
├── api
│ └── user.proto
├── go.mod
└── third_party
└── google
├── api
│ ├── annotations.proto
│ ├── client.proto
│ ├── field_behavior.proto
│ ├── http.proto
│ └── httpbody.proto
└── protobuf
├── any.proto
├── api.proto
├── compiler
│ └── plugin.proto
├── descriptor.proto
├── duration.proto
......
6 directories, 19 files
2.3.2 配置goland,protobuf的第三方import地址
我们打开 goland的 “语言和框架”->"Protocol Buffers"
然后把我们自己的项目的 third_party目录加一下。
此时我们的proto文件显示正常了。
《go微服务框架Kratos 》连载五:使用protbuf创建http服务器《go微服务框架Kratos 》连载五:使用protbuf创建http服务器《go微服务框架Kratos 》连载五:使用protbuf创建http服务器《go微服务框架Kratos 》连载五:使用protbuf创建http服务器《go微服务框架Kratos 》连载五:使用protbuf创建http服务器《go微服务框架Kratos 》连载五:使用protbuf创建http服务器《go微服务框架Kratos 》连载五:使用protbuf创建http服务器《go微服务框架Kratos 》连载五:使用protbuf创建http服务器《go微服务框架Kratos 》连载五:使用protbuf创建http服务器