主要内容:
ProtocolBuffers(后简称protobuf)是谷歌开发的一个,平台无关,语言无关的,可扩展,轻量级高效的序列化结构的数据格式。用于将自定义数据结构序列化成字节流,和将字节流反序列化为数据结构。它很适合作为不同应用间互相通信的数据格式,只要通信双方都使用proto文件定义数据结构和接口,再转换为各自的语言,即可实现双方的高效通信。
目前官方支持Java、Go、C++、JavaScript、Python、C#、PHP等语言,如果要获得语言支持,必须安装ProtoBuf本身和Protobuf不同语言的支持两个插件/库。对于Go语言来说,就是以下两个插件:
gomodule GeeCache
go 1.22.1
require (
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
要下载的话,需要以下命令:
go get github.com/golang/protobuf/proto // protobuf本身 go get github.com/golang/protobuf/protoc-gen-go // protobuf生成go代码的插件 go get google.golang.org/grpc/cmd/protoc-gen-go-grpc // grpc插件
一个proto文件里可以定义多个数据结构和针对这些数据结构的接口,如下:
proto// test.proto syntax = "proto3"; // 指定proto版本 没有意外情况下都指定为3 option go_package = "."; // 指定生成的代码的包位置 message Userinfo { // 这个可以视作一个和结构体差不多的东西 message里可以放字段 可以嵌套一个message 可以放枚举值 string name = 1; // 这个1 2 3是字段编号 同一个message里必须唯一存在 范围是从1开始到2的29次(枚举是从0开始) 从19000到19999是官方预留值 不能使用 // 推荐1~15放常用的字段 读写更快 int32 age = 2; repeated string hobby = 3; // repeated表示该字段可以重复多个 到语言那边就可以转换为数组类的数据结构 } message Reply { int32 grade = 1; } service GetUserInfo { // RPC服务 rpc Get (Userinfo) returns (Reply); // 接口 }
该文件执行protoc --go_out=./ src/test.proto
(这个命令最后的位置写要转换的proto文件位置)后在proto文件内指定的包位置生成一个go文件(示例文件生成的文件名为test.pb.go),如下:
go// test.pb.go
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.33.0
// protoc v5.26.1
// source: src/test.proto
package __
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type Userinfo struct { // message转换
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
Age int32 `protobuf:"varint,2,opt,name=age,proto3" json:"age,omitempty"`
Hobby []string `protobuf:"bytes,3,rep,name=hobby,proto3" json:"hobby,omitempty"`
}
func (x *Userinfo) Reset() {
*x = Userinfo{}
if protoimpl.UnsafeEnabled {
mi := &file_src_test_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Userinfo) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Userinfo) ProtoMessage() {}
func (x *Userinfo) ProtoReflect() protoreflect.Message {
mi := &file_src_test_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Userinfo.ProtoReflect.Descriptor instead.
func (*Userinfo) Descriptor() ([]byte, []int) {
return file_src_test_proto_rawDescGZIP(), []int{0}
}
func (x *Userinfo) GetName() string {
if x != nil {
return x.Name
}
return ""
}
func (x *Userinfo) GetAge() int32 {
if x != nil {
return x.Age
}
return 0
}
func (x *Userinfo) GetHobby() []string {
if x != nil {
return x.Hobby
}
return nil
}
type Reply struct { // message转换
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Grade int32 `protobuf:"varint,1,opt,name=grade,proto3" json:"grade,omitempty"`
}
func (x *Reply) Reset() {
*x = Reply{}
if protoimpl.UnsafeEnabled {
mi := &file_src_test_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Reply) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Reply) ProtoMessage() {}
func (x *Reply) ProtoReflect() protoreflect.Message {
mi := &file_src_test_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Reply.ProtoReflect.Descriptor instead.
func (*Reply) Descriptor() ([]byte, []int) {
return file_src_test_proto_rawDescGZIP(), []int{1}
}
func (x *Reply) GetGrade() int32 {
if x != nil {
return x.Grade
}
return 0
}
var File_src_test_proto protoreflect.FileDescriptor
var file_src_test_proto_rawDesc = []byte{
0x0a, 0x0e, 0x73, 0x72, 0x63, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x22, 0x46, 0x0a, 0x08, 0x55, 0x73, 0x65, 0x72, 0x69, 0x6e, 0x66, 0x6f, 0x12, 0x12, 0x0a, 0x04,
0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65,
0x12, 0x10, 0x0a, 0x03, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x61,
0x67, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x68, 0x6f, 0x62, 0x62, 0x79, 0x18, 0x03, 0x20, 0x03, 0x28,
0x09, 0x52, 0x05, 0x68, 0x6f, 0x62, 0x62, 0x79, 0x22, 0x1d, 0x0a, 0x05, 0x52, 0x65, 0x70, 0x6c,
0x79, 0x12, 0x14, 0x0a, 0x05, 0x67, 0x72, 0x61, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05,
0x52, 0x05, 0x67, 0x72, 0x61, 0x64, 0x65, 0x32, 0x27, 0x0a, 0x0b, 0x47, 0x65, 0x74, 0x55, 0x73,
0x65, 0x72, 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x18, 0x0a, 0x03, 0x47, 0x65, 0x74, 0x12, 0x09, 0x2e,
0x55, 0x73, 0x65, 0x72, 0x69, 0x6e, 0x66, 0x6f, 0x1a, 0x06, 0x2e, 0x52, 0x65, 0x70, 0x6c, 0x79,
0x42, 0x03, 0x5a, 0x01, 0x2e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_src_test_proto_rawDescOnce sync.Once
file_src_test_proto_rawDescData = file_src_test_proto_rawDesc
)
func file_src_test_proto_rawDescGZIP() []byte {
file_src_test_proto_rawDescOnce.Do(func() {
file_src_test_proto_rawDescData = protoimpl.X.CompressGZIP(file_src_test_proto_rawDescData)
})
return file_src_test_proto_rawDescData
}
var file_src_test_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_src_test_proto_goTypes = []interface{}{
(*Userinfo)(nil), // 0: Userinfo
(*Reply)(nil), // 1: Reply
}
var file_src_test_proto_depIdxs = []int32{
0, // 0: GetUserInfo.Get:input_type -> Userinfo
1, // 1: GetUserInfo.Get:output_type -> Reply
1, // [1:2] is the sub-list for method output_type
0, // [0:1] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
}
func init() { file_src_test_proto_init() }
func file_src_test_proto_init() {
if File_src_test_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_src_test_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Userinfo); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_src_test_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Reply); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_src_test_proto_rawDesc,
NumEnums: 0,
NumMessages: 2,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_src_test_proto_goTypes,
DependencyIndexes: file_src_test_proto_depIdxs,
MessageInfos: file_src_test_proto_msgTypes,
}.Build()
File_src_test_proto = out.File
file_src_test_proto_rawDesc = nil
file_src_test_proto_goTypes = nil
file_src_test_proto_depIdxs = nil
}
可以看到两个message被转换为结构体,同时还有对应字段的get方法,还有固定的String、Reset、ProtoMessage、ProtoReflect五个方法,但是并没有rpc对应的方法。因为默认情况下不生成rpc服务的代码,需要输入protoc --go-grpc_out=./ test.proto
才能输出rpc相关的代码(它需要提前先生成不带rpc的那部分代码才可以用):
go// test_grpc.pb.go
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.3.0
// - protoc v5.26.1
// source: test.proto
package __
import (
context "context"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7
const (
GetUserInfo_Get_FullMethodName = "/GetUserInfo/Get"
)
// GetUserInfoClient is the client API for GetUserInfo service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type GetUserInfoClient interface {
Get(ctx context.Context, in *Userinfo, opts ...grpc.CallOption) (*Reply, error)
}
type getUserInfoClient struct {
cc grpc.ClientConnInterface
}
func NewGetUserInfoClient(cc grpc.ClientConnInterface) GetUserInfoClient {
return &getUserInfoClient{cc}
}
func (c *getUserInfoClient) Get(ctx context.Context, in *Userinfo, opts ...grpc.CallOption) (*Reply, error) {
out := new(Reply)
err := c.cc.Invoke(ctx, GetUserInfo_Get_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// GetUserInfoServer is the server API for GetUserInfo service.
// All implementations must embed UnimplementedGetUserInfoServer
// for forward compatibility
type GetUserInfoServer interface {
Get(context.Context, *Userinfo) (*Reply, error)
mustEmbedUnimplementedGetUserInfoServer()
}
// UnimplementedGetUserInfoServer must be embedded to have forward compatible implementations.
type UnimplementedGetUserInfoServer struct {
}
func (UnimplementedGetUserInfoServer) Get(context.Context, *Userinfo) (*Reply, error) {
return nil, status.Errorf(codes.Unimplemented, "method Get not implemented")
}
func (UnimplementedGetUserInfoServer) mustEmbedUnimplementedGetUserInfoServer() {}
// UnsafeGetUserInfoServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to GetUserInfoServer will
// result in compilation errors.
type UnsafeGetUserInfoServer interface {
mustEmbedUnimplementedGetUserInfoServer()
}
func RegisterGetUserInfoServer(s grpc.ServiceRegistrar, srv GetUserInfoServer) {
s.RegisterService(&GetUserInfo_ServiceDesc, srv)
}
func _GetUserInfo_Get_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(Userinfo)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(GetUserInfoServer).Get(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: GetUserInfo_Get_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(GetUserInfoServer).Get(ctx, req.(*Userinfo))
}
return interceptor(ctx, in, info, handler)
}
// GetUserInfo_ServiceDesc is the grpc.ServiceDesc for GetUserInfo service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var GetUserInfo_ServiceDesc = grpc.ServiceDesc{
ServiceName: "GetUserInfo",
HandlerType: (*GetUserInfoServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Get",
Handler: _GetUserInfo_Get_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "test.proto",
}
要引入Protobuf,它能发挥的作用就是作为节点之间的通信的数据结构。而分布式缓存中,互相要传的有作为请求的key,group和作为返回的value。据此可以定义proto文件:
proto// src/geecache/geecachepb/geecachepb.proto syntax = "proto3"; option go_package = "src/geecache/geecachepb"; package geecachepb; message Request { string group = 1; string key = 2; } message Response { bytes value = 1; } service GroupCache { rpc Get(Request) returns (Response); }
其对应的go文件:
go// src/geecache/geecachepb/geecachepb.pb.go
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.33.0
// protoc v5.26.1
// source: src/geecache/geecachepb/geecachepb.proto
package geecachepb
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type Request struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Group string `protobuf:"bytes,1,opt,name=group,proto3" json:"group,omitempty"`
Key string `protobuf:"bytes,2,opt,name=key,proto3" json:"key,omitempty"`
}
func (x *Request) Reset() {
*x = Request{}
if protoimpl.UnsafeEnabled {
mi := &file_src_geecache_geecachepb_geecachepb_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Request) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Request) ProtoMessage() {}
func (x *Request) ProtoReflect() protoreflect.Message {
mi := &file_src_geecache_geecachepb_geecachepb_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Request.ProtoReflect.Descriptor instead.
func (*Request) Descriptor() ([]byte, []int) {
return file_src_geecache_geecachepb_geecachepb_proto_rawDescGZIP(), []int{0}
}
func (x *Request) GetGroup() string {
if x != nil {
return x.Group
}
return ""
}
func (x *Request) GetKey() string {
if x != nil {
return x.Key
}
return ""
}
type Response struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Value []byte `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"`
}
func (x *Response) Reset() {
*x = Response{}
if protoimpl.UnsafeEnabled {
mi := &file_src_geecache_geecachepb_geecachepb_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Response) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Response) ProtoMessage() {}
func (x *Response) ProtoReflect() protoreflect.Message {
mi := &file_src_geecache_geecachepb_geecachepb_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Response.ProtoReflect.Descriptor instead.
func (*Response) Descriptor() ([]byte, []int) {
return file_src_geecache_geecachepb_geecachepb_proto_rawDescGZIP(), []int{1}
}
func (x *Response) GetValue() []byte {
if x != nil {
return x.Value
}
return nil
}
var File_src_geecache_geecachepb_geecachepb_proto protoreflect.FileDescriptor
var file_src_geecache_geecachepb_geecachepb_proto_rawDesc = []byte{
0x0a, 0x28, 0x73, 0x72, 0x63, 0x2f, 0x67, 0x65, 0x65, 0x63, 0x61, 0x63, 0x68, 0x65, 0x2f, 0x67,
0x65, 0x65, 0x63, 0x61, 0x63, 0x68, 0x65, 0x70, 0x62, 0x2f, 0x67, 0x65, 0x65, 0x63, 0x61, 0x63,
0x68, 0x65, 0x70, 0x62, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0a, 0x67, 0x65, 0x65, 0x63,
0x61, 0x63, 0x68, 0x65, 0x70, 0x62, 0x22, 0x31, 0x0a, 0x07, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73,
0x74, 0x12, 0x14, 0x0a, 0x05, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
0x52, 0x05, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x02,
0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x20, 0x0a, 0x08, 0x52, 0x65, 0x73,
0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01,
0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x32, 0x3e, 0x0a, 0x0a, 0x47,
0x72, 0x6f, 0x75, 0x70, 0x43, 0x61, 0x63, 0x68, 0x65, 0x12, 0x30, 0x0a, 0x03, 0x47, 0x65, 0x74,
0x12, 0x13, 0x2e, 0x67, 0x65, 0x65, 0x63, 0x61, 0x63, 0x68, 0x65, 0x70, 0x62, 0x2e, 0x52, 0x65,
0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x67, 0x65, 0x65, 0x63, 0x61, 0x63, 0x68, 0x65,
0x70, 0x62, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x19, 0x5a, 0x17, 0x73,
0x72, 0x63, 0x2f, 0x67, 0x65, 0x65, 0x63, 0x61, 0x63, 0x68, 0x65, 0x2f, 0x67, 0x65, 0x65, 0x63,
0x61, 0x63, 0x68, 0x65, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_src_geecache_geecachepb_geecachepb_proto_rawDescOnce sync.Once
file_src_geecache_geecachepb_geecachepb_proto_rawDescData = file_src_geecache_geecachepb_geecachepb_proto_rawDesc
)
func file_src_geecache_geecachepb_geecachepb_proto_rawDescGZIP() []byte {
file_src_geecache_geecachepb_geecachepb_proto_rawDescOnce.Do(func() {
file_src_geecache_geecachepb_geecachepb_proto_rawDescData = protoimpl.X.CompressGZIP(file_src_geecache_geecachepb_geecachepb_proto_rawDescData)
})
return file_src_geecache_geecachepb_geecachepb_proto_rawDescData
}
var file_src_geecache_geecachepb_geecachepb_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_src_geecache_geecachepb_geecachepb_proto_goTypes = []interface{}{
(*Request)(nil), // 0: geecachepb.Request
(*Response)(nil), // 1: geecachepb.Response
}
var file_src_geecache_geecachepb_geecachepb_proto_depIdxs = []int32{
0, // 0: geecachepb.GroupCache.Get:input_type -> geecachepb.Request
1, // 1: geecachepb.GroupCache.Get:output_type -> geecachepb.Response
1, // [1:2] is the sub-list for method output_type
0, // [0:1] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
}
func init() { file_src_geecache_geecachepb_geecachepb_proto_init() }
func file_src_geecache_geecachepb_geecachepb_proto_init() {
if File_src_geecache_geecachepb_geecachepb_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_src_geecache_geecachepb_geecachepb_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Request); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_src_geecache_geecachepb_geecachepb_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Response); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_src_geecache_geecachepb_geecachepb_proto_rawDesc,
NumEnums: 0,
NumMessages: 2,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_src_geecache_geecachepb_geecachepb_proto_goTypes,
DependencyIndexes: file_src_geecache_geecachepb_geecachepb_proto_depIdxs,
MessageInfos: file_src_geecache_geecachepb_geecachepb_proto_msgTypes,
}.Build()
File_src_geecache_geecachepb_geecachepb_proto = out.File
file_src_geecache_geecachepb_geecachepb_proto_rawDesc = nil
file_src_geecache_geecachepb_geecachepb_proto_goTypes = nil
file_src_geecache_geecachepb_geecachepb_proto_depIdxs = nil
}
之后再对请求和接收请求相关的方法和接口进行更改:
go// src/geecache/peers.go
// PeerCacheValueGetter 接口 根据key和给定的group获取缓存值
type PeerCacheValueGetter interface {
GetCacheFromPeer(in *pb.Request, out *pb.Response) error
}
go// src/geecache/geecache.go
// getFromPeer 从远程节点获得缓存
func (g *Group) getFromPeer(peer PeerCacheValueGetter, key string) (ByteView, error) {
req := &pb.Request{ // 初始化proto数据结构
Key: key,
Group: g.name,
}
resp := &pb.Response{}
err := peer.GetCacheFromPeer(req, resp)
if err != nil {
return ByteView{}, err
}
return ByteView{cacheBytes: resp.Value}, nil
}
gosrc/geecache/http.go
// GetCacheFromPeer 实现PeerCacheValueGetter接口 从远程节点获得缓存
func (h *httpClient) GetCacheFromPeer(in *pb.Request, out *pb.Response) error {
url := fmt.Sprintf("%v%v/%v", h.baseURL, url.QueryEscape(in.GetGroup()), url.QueryEscape(in.GetKey()))
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("server returned error: %v", resp.Status)
}
bytes, err := io.ReadAll(resp.Body) // 读取响应体 原有的ioutil.ReadAll方法被弃用
if err != nil {
err = fmt.Errorf("reading response body error: %v", err)
}
if err = proto.Unmarshal(bytes, out); err != nil { // 反序列化 向out中写入
return fmt.Errorf("decoding response body: %v", err)
}
return nil
}
// ServeHTTP HTTP服务端的Handler方法 对有效的缓存请求进行响应
func (pool *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !strings.HasPrefix(r.URL.Path, pool.basePath) { // 检查请求是否有效
panic("HTTPPool is serving unexpected path: " + r.URL.Path)
}
pool.Log("%s %s", r.Method, r.URL.Path) // log记录该次请求的信息
parts := strings.SplitN(r.URL.Path[len(pool.basePath):], "/", 2)
if len(parts) != 2 { // 检查请求是否有效
http.Error(w, "bad request", http.StatusBadRequest) // 400
return
}
groupName := parts[0]
key := parts[1]
group := GetGroup(groupName)
if group == nil { // 请求的缓存不存在
http.Error(w, "no such group:" + groupName, http.StatusNotFound) // 404
return
}
view, err := group.GetFromCache(key) // fixme
if err != nil { // 缓存请求失败
http.Error(w, err.Error(), http.StatusInternalServerError) // 500
return
}
body, err := proto.Marshal(&pb.Response{Value: view.GetByteCopy()}) // 序列化
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/octet-stream")
_, err = w.Write(body)
if err != nil { // 写入响应失败
http.Error(w, "write into responseWriter error: " + err.Error(), http.StatusInternalServerError) // 500
return
}
}
到此,极客兔兔给的全部的代码已经实现,之后的内容是个人对该项目进行的改进,目前规划可能的改进方向:
本文作者:御坂19327号
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!