合肥网站建设认准 晨飞网络,山东做网站建设公司,网店服务平台,旅游网站建设参考文献GRPC介绍
目录
单体架构微服务架构问题原始的grpc 服务端客户端原生rpc的问题 grpc的hello world 服务端客户端 proto文件proto语法 数据类型 基本数据类型其他数据类型 编写风格多服务
单体架构
只能对整体扩容一荣俱荣#xff0c;一损俱损代码耦合#xff0c;项目的开…GRPC介绍
目录
单体架构微服务架构问题原始的grpc 服务端客户端原生rpc的问题 grpc的hello world 服务端客户端 proto文件proto语法 数据类型 基本数据类型其他数据类型 编写风格多服务
单体架构
只能对整体扩容一荣俱荣一损俱损代码耦合项目的开发者需要知道整个项目的流程
微服务架构
针对单体架构的问题出现了微服务架构
可以按照服务进行单独扩容各个服务之间可以独立开发独立部署
问题
代码冗余服务之间的调用很麻烦
为什么要使用grpc grpc使用的意义
原始的grpc
服务端
package mainimport (fmtnetnet/httpnet/rpc
)type Server struct {
}
type Req struct {Num1 intNum2 int
}
type Res struct {Num int
}func (s Server) Add(req Req, res *Res) error {res.Num req.Num1 req.Num2return nil
}func main() {// 注册rpc服务rpc.Register(new(Server))rpc.HandleHTTP()listen, err : net.Listen(tcp, :8080)if err ! nil {fmt.Println(err)return}http.Serve(listen, nil)
}客户端
package mainimport (fmtnet/rpc
)type Req struct {Num1 intNum2 int
}
type Res struct {Num int
}func main() {req : Req{1, 2}client, err : rpc.DialHTTP(tcp, :8080)if (err ! nil) {fmt.Println(err)return}var res Resclient.Call(Server.Add, req, res)fmt.Println(res)
}原生rpc的问题
编写相对复杂需要自己去关注实现过程没有代码提示容易写错。
grpc的hello world
服务端
编写一个结构体名字叫什么不重要重要的是得实现protobuf中的所有方法监听端口注册服务
package mainimport (contextfmtgoogle.golang.org/grpcgoogle.golang.org/grpc/grpcloggrpc_study/grpc_proto/hello_grpcnet
)type HelloServiceServer struct {
}func (s HelloServiceServer) SayHello(ctx context.Context, request *hello_grpc.HelloRequest) (res *hello_grpc.HelloResponse, err error) {fmt.Println(请求来了, request)return hello_grpc.HelloResponse{Message: Hello Xiaoyu_Wang,Name: Server,}, nil
}func main() {// 监听端口listen, err : net.Listen(tcp, :8080)if err ! nil {grpclog.Fatalf(Failed to listen: %v, err)}// 创建一个gRPC服务器实例。s : grpc.NewServer()server : HelloServiceServer{}// 将server结构体注册为gRPC服务。hello_grpc.RegisterHelloServiceServer(s, server)fmt.Println(grpc server running :8080)// 开始处理客户端请求。err s.Serve(listen)
}客户端
建立连接调用方法
package mainimport (contextfmtgoogle.golang.org/grpcgoogle.golang.org/grpc/credentials/insecuregrpc_study/grpc_proto/hello_grpclog
)func main() {addr : :8080// 使用 grpc.Dial 创建一个到指定地址的 gRPC 连接。// 此处使用不安全的证书来实现 SSL/TLS 连接conn, err : grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))if err ! nil {log.Fatalf(fmt.Sprintf(grpc connect addr [%s] 连接失败 %s, addr, err))}defer conn.Close()// 初始化客户端client : hello_grpc.NewHelloServiceClient(conn)result, err : client.SayHello(context.Background(), hello_grpc.HelloRequest{Name: Xiaoyu_Wang,Message: ok,})fmt.Println(result, err)
}proto文件
syntax proto3; // 指定proto版本
package hello_grpc; // 指定默认包名// 指定golang包名
option go_package /hello_grpc;//定义rpc服务
service HelloService {// 定义函数rpc SayHello (HelloRequest) returns (HelloResponse) {}
}// HelloRequest 请求内容
message HelloRequest {string name 1; // 消息号string message 2;
}// HelloResponse 响应内容
message HelloResponse{string name 1;string message 2;
}proto语法
service 对应的就是go里面的接口可以作为服务端客户端rpc 对应的就是结构体中的方法message对应的也是结构体
数据类型
基本数据类型
message Request {double a1 1;float a2 2;int32 a3 3;uint32 a4 4;uint64 a5 5;sint32 a6 6;sint64 a7 7;fixed32 a8 8;fixed64 a9 9;sfixed32 a10 10;sfixed64 a11 11;bool a12 12;string a13 13;bytes a14 14;
}对应的go类型
type Request struct {state protoimpl.MessageStatesizeCache protoimpl.SizeCacheunknownFields protoimpl.UnknownFieldsA1 float64 protobuf:fixed64,1,opt,namea1,proto3 json:a1,omitemptyA2 float32 protobuf:fixed32,2,opt,namea2,proto3 json:a2,omitemptyA3 int32 protobuf:varint,3,opt,namea3,proto3 json:a3,omitemptyA4 uint32 protobuf:varint,4,opt,namea4,proto3 json:a4,omitemptyA5 uint64 protobuf:varint,5,opt,namea5,proto3 json:a5,omitemptyA6 int32 protobuf:zigzag32,6,opt,namea6,proto3 json:a6,omitemptyA7 int64 protobuf:zigzag64,7,opt,namea7,proto3 json:a7,omitemptyA8 uint32 protobuf:fixed32,8,opt,namea8,proto3 json:a8,omitemptyA9 uint64 protobuf:fixed64,9,opt,namea9,proto3 json:a9,omitemptyA10 int32 protobuf:fixed32,10,opt,namea10,proto3 json:a10,omitemptyA11 int64 protobuf:fixed64,11,opt,namea11,proto3 json:a11,omitemptyA12 bool protobuf:varint,12,opt,namea12,proto3 json:a12,omitemptyA13 string protobuf:bytes,13,opt,namea13,proto3 json:a13,omitemptyA14 []byte protobuf:bytes,14,opt,namea14,proto3 json:a14,omitempty
}其他数据类型
数组类型
message ArrayRequest {repeated int64 a1 1;repeated string a2 2;repeated Request request_list 3;
}type ArrayRequest struct {A1 []int64 A2 []string RequestList []*Request
}map类型
message MapRequest {mapint64, string m_i_s 1;mapstring, bool m_i_b 2;mapstring, ArrayRequest m_i_arr 3;
}type MapRequest struct {MIS map[int64]stringMIB map[string]boolMIArr map[string]*ArrayRequest
}嵌套类型
message Q1 {message Q2{string name2 2;}string name1 1;Q2 q2 2;
}type Q1 struct {state protoimpl.MessageStatesizeCache protoimpl.SizeCacheunknownFields protoimpl.UnknownFieldsName1 string protobuf:bytes,1,opt,namename1,proto3 json:name1,omitemptyQ2 *Q1_Q2 protobuf:bytes,2,opt,nameq2,proto3 json:q2,omitempty
}编写风格
文件名建议下划线例如my_student.proto包名和目录名对应服务名、方法名、消息名均为大驼峰字段名为下划线
多服务
syntax proto3;option go_package /duo_grpc;service VideoService {rpc Look (Request) returns (Response) {}
}message Request{string name 1;
}message Response{string name 1;
}service OderService {rpc Buy (Request) returns (Response) {}
}