Exploring Proto files: parsing definitions and parameters revealed

Preface: apply bct involves serviceand again proto. I don’t understand it well. Let’s study it~
Set up a flag here and take the network engineer certificate or embedded engineer certificate at the end of the year.

NOTICE  2023-09-26 10:56:46.336009+0800: Stopping tcpdump since packet capture is not required by the remaining tests

NOTICE  2023-09-26 10:56:46.336774+0800: Terminating BCT.
*** Beginning Network Interoperability Test ***
Please enter the device's service type in the form 
	"_<service>._<proto>.local.", e.g. "_printer._tcp.local."
	(or try again with "-M"):

Proto files are a language developed by Google for defining data structures and service interfaces, often used for data exchange and communication between different platforms.

Proto files are composed of three main components: message, service and enumeration.

Messages are the main way to define data structures, similar to classes in object-oriented programming. You can define fields in the message, specify the field type and name, and add metadata such as comments.

A service defines a set of methods that can be called remotely. Each method has input and output parameters, and multiple methods can be defined in a service.

An enumeration defines a set of named integer constants, which can be used as value ranges for message fields or options for service methods.

In addition to the above basic components, Proto files also support import statements for importing other Proto files, which can achieve modularization and reuse.

Proto files use concise syntax to define data structures and interfaces, and also support advanced features such as extensions and custom options, making them very suitable for cross-platform data exchange and communication scenarios.

It should be noted that the Proto file is only a definition language and does not directly correspond to the specific implementation code. You need to use the corresponding compiler or tool to convert the Proto file into the specific code of the target platform.


Optimize Proto file definition and parameter description

1. Proto file naming convention:
package name.service name.proto

2. Proto syntax keywords:

  • syntax: protocol type. There are currently two sets of protocols, proto3 and proto2. It is recommended to use proto3, which must be placed in the first line of the Proto file.
  • package: package name, must be placed on the second line.
  • service: defines a collection of methods in the Proto file, similar to a method interface.
  • message: Define the message structure, similar to the structure of the Go language, in which the receiving and return parameters of the method are defined.
  • returns: defines the return response, used with service.
  • rpc: Define methods, used with service.

Variable type:

  • Optional: Used together with message, indicating that the field is optional and may or may not be passed.
  • repeated: used with message, indicating that the field is received or returned as an array.

3. Example:

syntax = "proto3";
package user;

service HelloService {
  rpc SayHello (HelloRequest) returns (HelloResponse){}
}

message HelloRequest {}

message HelloResponse {
  string msg = 1;
}

Compile command:

protoc --go_out=plugins=grpc:. server.hello.proto

Server side example:

package main

import (
	"flag"
	"fmt"
	"log"
	"net"

	user "grpc.zozoo.net/protos"
	"grpc.zozoo.net/impl"
	"google.golang.org/grpc"
)

func main() {
    
    
	flag.Parse()
	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 9000))
	if err != nil {
    
    
		log.Fatalf("failed to listen: %v", err)
	}

	log.Println("端口监听成功:9000")

	grpcServer := grpc.NewServer()
	user.RegisterHelloServiceServer(grpcServer, &impl.HelloService{
    
    })
	grpcServer.Serve(lis)
}

Client example:

package main

import (
	"context"
	"log"

	"google.golang.org/grpc"

	user "grpc.zozoo.net/protos"
)

func main() {
    
    
	conn, err := grpc.Dial(":9000", grpc.WithInsecure())
	if err != nil {
    
    
		log.Fatalf("failed to listen: %v", err)
	}

	// 调用RPC方法
	hello := user.NewHelloServiceClient(conn)
	rsp, err := hello.SayHello(context.TODO(), &user.HelloRequest{
    
    })
	if err != nil {
    
    
		log.Fatalf("方法调用失败 err  %v", err)
	}

	log.Println(rsp.Msg)
	defer conn.Close()
}

Guess you like

Origin blog.csdn.net/weixin_43233219/article/details/133300199