1,Golang开发实战讲解

47 阅读6分钟

// HelloRequest 请求内容 message HelloRequest { string name = 1; string message = 2; }

// HelloResponse 响应内容 message HelloResponse{ string name = 1; string message = 2; }




> 

> 在文件夹 **/grpc\_proto**中执行命令:protoc -I . --go\_out=plugins=grpc:. .\hello.proto

> 

> 

> 或者编写set.bat批处理文件,方便使用,内容如下:

> 

> 

> protoc -I . --go\_out=plugins=grpc:.\grpc\_proto .\grpc\_proto\hello.proto

> 

> 

> 




### 2.服务端



package main

import ( "context" "fmt" "google.golang.org/grpc" "google.golang.org/grpc/grpclog" "net" "oslee/grpc_study/1base/grpc_proto/hello_grpc" )

// HelloServer 得有一个结构体,需要实现这个服务的全部方法,叫什么名字不重要 type HelloServer struct { }

func (HelloServer) SayHello(ctx context.Context, request *hello_grpc.HelloRequest) (*hello_grpc.HelloResponse, error) { fmt.Println("入参:", request.Name, request.Message) return &hello_grpc.HelloResponse{ Name: "server", Message: "hello " + request.Name, }, nil }

func main() { // 监听端口 listen, err := net.Listen("tcp", ":8080") if err != nil { grpclog.Fatalf("Failed to listen: %v", err) }

// 创建一个gRPC服务器实例。
s := grpc.NewServer()
server := HelloServer{}
// 将server结构体注册为gRPC服务。
hello_grpc.RegisterHelloServiceServer(s, &server)
fmt.Println("grpc server running :8080")
// 开始处理客户端请求。
err = s.Serve(listen)

}


### 3.客户端



package main

import ( "context" "fmt" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" "log" "oslee/grpc_study/1base/grpc_proto/hello_grpc" )

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: "client", Message: "hello", }) fmt.Println(result, err) }


调用结果如下:



## 



## 



## 四、proto文件详解



### 1.proto语法




> 

> * service 对应的就是go里面的接口,可以作为服务端,客户端

> * rpc 对应的就是结构体中的方法

> * message对应的也是结构体

> 

> 

> 




### 2.数据类型



#### **基本数据类型**




> 

> 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.MessageState  

>    sizeCache     protoimpl.SizeCache  

>    unknownFields protoimpl.UnknownFields

> 

> 

>   A1  float64 `protobuf:"fixed64,1,opt,name=a1,proto3" json:"a1,omitempty"`  

>    A2  float32 `protobuf:"fixed32,2,opt,name=a2,proto3" json:"a2,omitempty"`  

>    A3  int32   `protobuf:"varint,3,opt,name=a3,proto3" json:"a3,omitempty"`  

>    A4  uint32  `protobuf:"varint,4,opt,name=a4,proto3" json:"a4,omitempty"`  

>    A5  uint64  `protobuf:"varint,5,opt,name=a5,proto3" json:"a5,omitempty"`  

>    A6  int32   `protobuf:"zigzag32,6,opt,name=a6,proto3" json:"a6,omitempty"`  

>    A7  int64   `protobuf:"zigzag64,7,opt,name=a7,proto3" json:"a7,omitempty"`  

>    A8  uint32  `protobuf:"fixed32,8,opt,name=a8,proto3" json:"a8,omitempty"`  

>    A9  uint64  `protobuf:"fixed64,9,opt,name=a9,proto3" json:"a9,omitempty"`  

>    A10 int32   `protobuf:"fixed32,10,opt,name=a10,proto3" json:"a10,omitempty"`  

>    A11 int64   `protobuf:"fixed64,11,opt,name=a11,proto3" json:"a11,omitempty"`  

>    A12 bool    `protobuf:"varint,12,opt,name=a12,proto3" json:"a12,omitempty"`  

>    A13 string  `protobuf:"bytes,13,opt,name=a13,proto3" json:"a13,omitempty"`  

>    A14 []byte  `protobuf:"bytes,14,opt,name=a14,proto3" json:"a14,omitempty"`  

>  }

> 

> 

> 



标量类型




| .proto Type | 解释 | Go Type |
| --- | --- | --- |
| double |  | float64 |
| float |  | float32 |
| int32 | 使用变长编码,对于负值的效率很低,如果你的域有可能有负值,请使用sint64替代 | int32 |
| uint32 | 使用变长编码 | uint32 |
| uint64 | 使用变长编码 | uint64 |
| sint32 | 使用变长编码,这些编码在负值时比int32高效的多 | int32 |
| sint64 | 使用变长编码,有符号的整型值。编码时比通常的int64高效 | int64 |
| fixed32 | 总是4个字节,如果数值总是比总是比228大的话,这个类型会比uint32高效。 | uint32 |
| fixed64 | 总是8个字节,如果数值总是比总是比256大的话,这个类型会比uint64高效。 | uint64 |
| sfixed32 | 总是4个字节 | int32 |
| sfixed64 | 总是8个字节 | int64 |
| bool |  | bool |
| string | 一个字符串必须是UTF-8编码或者7-bit ASCII编码的文本 | string |
| bytes | 可能包含任意顺序的字节数据 | []byte |


标量类型如果没有被赋值,则不会被序列化,解析时,会赋予默认值


* strings:空字符串
* bytes:空序列
* bools:false
* 数值类型:0



#### **数组类型**




> 

> message ArrayRequest {  

>    repeated int64 a1 = 1;  

>    repeated string a2 = 2;  

>    repeated Request request\_list = 3;  

>  }

> 

> 

> 



对应go类型 




> 

> type ArrayRequest struct {  

>    A1          []int64   

>    A2          []string     

>    RequestList []\*Request  

>  }

> 

> 

> 




#### map类型


键只能是基本类型




> 

> message MapRequest {  

>    map<int64, string> m\_i\_s = 1;  

>    map<string, bool> m\_i\_b = 2;  

>    map<string, ArrayRequest> m\_i\_arr = 3;  

>  }

> 

> 

> 



对应go类型 




> 

> type MapRequest struct {

> 

> 

>   MIS   map[int64]string  

>    MIB   map[string]bool  

>    MIArr map[string]\*ArrayRequest  

>  }

> 

> 

> 




#### 嵌套类型




> 

> message Q1 {  

>    message Q2{  

>      string name2 = 2;  

>    }  

>    string name1 = 1;  

>    Q2 q2 = 2;  

>  }

> 

> 

> 



对应go类型




> 

> type Q1 struct {  

>    state         protoimpl.MessageState  

>    sizeCache     protoimpl.SizeCache  

>    unknownFields protoimpl.UnknownFields  

>    Name1 string `protobuf:"bytes,1,opt,name=name1,proto3" json:"name1,omitempty"`  

>    Q2    \*Q1\_Q2 `protobuf:"bytes,2,opt,name=q2,proto3" json:"q2,omitempty"`  

>  }

> 

> 

> 



个人习惯不嵌套,分开写



#### 编写风格




> 

> * 文件名建议下划线,例如:my\_student.proto

> * 包名和目录名对应

> * 服务名、方法名、消息名均为大驼峰

> * 字段名为下划线

> 

> 

> 




### 3.多服务


**proto文件**




> 

> 

> ```

> syntax = "proto3"; // 指定proto版本

> // 指定golang包名

> option go_package = "/duo_server";

> 

> service VideoService {

>   rpc Look(Request)returns(Response){}

> }

> 

> service OrderService {

>   rpc Buy(Request)returns(Response){}

> }

> 

> message Request{

>   string name = 1;

> }

> message Response{

>   string name = 1;

> }

> ```

> 

> 



**服务端**



package main

import ( "context" "fmt" "google.golang.org/grpc" "log" "net" "oslee/grpc_study/2duo_server/grpc_proto/duo_server" )

type VideoServer struct { }

func (VideoServer) Look(ctx context.Context, request *duo_server.Request) (res *duo_server.Response, err error) { fmt.Println("video:", request) return &duo_server.Response{ Name: "server", }, nil }

type OrderServer struct { }

func (OrderServer) Buy(ctx context.Context, request *duo_server.Request) (res *duo_server.Response, err error) { fmt.Println("order:", request) return &duo_server.Response{ Name: "server", }, nil }

func main() { listen, err := net.Listen("tcp", ":8080") if err != nil { log.Fatal(err) } s := grpc.NewServer() duo_server.RegisterVideoServiceServer(s, &VideoServer{}) duo_server.RegisterOrderServiceServer(s, &OrderServer{}) fmt.Println("grpc server程序运行在:8080") err = s.Serve(listen) }


**客户端**



package main

import ( "context" "fmt" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" "log" "oslee/grpc_study/2duo_server/grpc_proto/duo_server" )

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()

orderClient := duo_server.NewOrderServiceClient(conn)
res, err := orderClient.Buy(context.Background(), &duo_server.Request{
	Name: "client",
})
fmt.Println(res, err)

videoClient := duo_server.NewVideoServiceClient(conn)
res, err = videoClient.Look(context.Background(), &duo_server.Request{
	Name: "client",
})
fmt.Println(res, err)

}


### 4.多个proto文件




> 

> 当项目大起来之后,会有很多个service,rpc,message

> 

> 

> 我们会将不同服务放在不同的proto文件中,还可以放一些公共的proto文件

> 

> 

> 本质就是生成go文件,需要在一个包内

> 

> 

> 



**proto文件**




> 

> **common.proto**

> 

> 

> 

> ```

> syntax = "proto3";

> package proto;

> option go_package = "/proto";

> 

> 

> message Request{

>   string name = 1;

> }

> message Response{

>   string name = 1;

> }

> 

> ```

> 

> **order.proto**

> 

> 

> 

> ```

> syntax = "proto3";

> package proto;

> option go_package = "/proto";

> import "common.proto";

> 

> service OrderService {

>   rpc Look(Request)returns(Response){}

> }

> ```

> 

> **video.proto**

> 

> 

> 

> ```

> syntax = "proto3";

> package proto;

> option go_package = "/proto";

> import "common.proto";

> 

> service VideoService {

>   rpc Look(Request)returns(Response){}

> }

> ```

> 

> 



**服务端**



package main

import ( "context" "fmt" "google.golang.org/grpc" "log" "net" "oslee/grpc_study/3duo_proto/grpc_proto/proto" )

type VideoServer struct { }

func (VideoServer) Look(ctx context.Context, request *proto.Request) (res *proto.Response, err error) { fmt.Println("video:", request) return &proto.Response{ Name: "server", }, nil }

type OrderServer struct { }

func (OrderServer) Look(ctx context.Context, request *proto.Request) (res *proto.Response, err error) { fmt.Println("order:", request) return &proto.Response{ Name: "server", }, nil }

func main() { listen, err := net.Listen("tcp", ":8080") if err != nil { log.Fatal(err) } s := grpc.NewServer() proto.RegisterVideoServiceServer(s, &VideoServer{}) proto.RegisterOrderServiceServer(s, &OrderServer{}) fmt.Println("grpc server程序运行在:8080") err = s.Serve(listen) }


**客户端**



package main

img img img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取