RPC 작업 EPUProtobuf 사용 및 사용자 정의 플러그인 생성

王林
풀어 주다: 2024-09-09 20:30:10
원래의
270명이 탐색했습니다.

RPC Action EPUsing Protobuf and Creating a Custom Plugin

이전 글에서는 net/rpc 패키지를 사용하여 간단한 RPC 인터페이스를 구현하고 net/rpc와 함께 제공되는 Gob 인코딩과 JSON 인코딩을 사용해 Golang의 기본 사항을 학습했습니다. RPC. 이번 게시물에서는 net/rpc를 protobuf와 결합하고 protobuf 플러그인을 만들어 코드 생성에 도움을 드릴 예정이므로 시작해 보겠습니다.

이 글은 Medium MPP 기획에 처음 게재되었습니다. 미디엄 사용자라면 미디엄에서 저를 팔로우해주세요. 정말 감사합니다.

작업 중에 gRPC와 protobuf를 사용했을 텐데 바인딩되어 있지는 않습니다. gRPC는 JSON을 사용하여 인코딩할 수 있고, protobuf는 다른 언어로 구현할 수 있습니다.

프로토콜 버퍼(Protobuf)는 구조화된 데이터를 직렬화하는 데 사용되는 무료 오픈 소스 크로스 플랫폼 데이터 형식입니다. 네트워크를 통해 서로 통신하는 프로그램을 개발하거나 데이터를 저장하는 데 유용합니다. 이 방법에는 일부 데이터의 구조를 설명하는 인터페이스 설명 언어와 구조화된 데이터를 나타내는 바이트 스트림을 생성하거나 파싱하기 위해 해당 설명에서 소스 코드를 생성하는 프로그램이 포함됩니다.

protobuf 사용 예

먼저 "문자열" 메시지를 정의하는 proto 파일 hello-service.proto를 작성합니다

syntax = "proto3";
package api;
option  go_package="api";

message String {
  string value = 1;
}
로그인 후 복사

그런 다음 protoc 유틸리티를 사용하여 메시지 문자열에 대한 Go 코드를 생성합니다

protoc --go_out=. hello-service.proto
로그인 후 복사

그런 다음 protobuf 파일에서 생성된 문자열을 사용하도록 Hello 함수의 인수를 수정합니다.

type HelloServiceInterface = interface {  
    Hello(request api.String, reply *api.String) error  
}  
로그인 후 복사

사용법은 예전과 별반 다르지 않고, 심지어 문자열을 직접 사용하는 것만큼 편리하지도 않습니다. 그렇다면 왜 protobuf를 사용해야 할까요? 앞서 말했듯이 Protobuf를 사용하여 언어 독립적인 RPC 서비스 인터페이스와 메시지를 정의한 다음 protoc 도구를 사용하여 다양한 언어로 코드를 생성하는 것이 Protobuf의 진정한 가치입니다. 예를 들어 gRPC 코드를 생성하려면 공식 플러그인 protoc-gen-go를 사용하세요.

protoc --go_out=plugins=grpc. hello-service.proto
로그인 후 복사

protoc용 플러그인 시스템

protobuf 파일에서 코드를 생성하려면 protoc을 설치해야 하지만 protoc은 대상 언어가 무엇인지 모르기 때문에 코드 생성을 도와주는 플러그인이 필요합니다. protoc의 플러그인 시스템은 어떻게 작동하나요? 위의 grpc를 예로 들어보겠습니다.

여기에 --go_out 매개변수가 있습니다. 우리가 호출하는 플러그인은 protoc-gen-go이므로 매개변수는 go_out이라고 합니다. 이름이 XXX인 경우 매개변수는 XXX_out이 됩니다.

protoc이 실행되면 먼저 protobuf 파일을 구문 분석하고 프로토콜 버퍼로 인코딩된 설명 데이터 세트를 생성합니다. 먼저 go 플러그인이 protoc에 포함되어 있는지 여부를 확인한 다음 $PATH에서 protoc-gen-go를 찾으려고 시도하고 찾을 수 없으면 오류를 보고한 다음 protoc-gen-go를 실행합니다. protoc-gen-go 명령을 실행하고 설명 데이터를 stdin을 통해 플러그인 명령으로 보냅니다. 플러그인은 파일 콘텐츠를 생성한 후 프로토콜 버퍼로 인코딩된 데이터를 stdout에 입력하여 protoc에 특정 파일을 생성하라고 지시합니다.

플러그인=grpc는 protoc-gen-go를 호출하기 위해 함께 제공되는 플러그인입니다. 사용하지 않으면 Go에서만 메시지가 생성되는데, 이 플러그인을 사용하면 grpc 관련 코드를 생성할 수 있습니다.

protoc 플러그인 사용자 정의

protobuf에 Hello 인터페이스 타이밍을 추가하면 protoc 플러그인을 맞춤설정하여 코드를 직접 생성할 수 있나요?

syntax = "proto3";  
package api;  
option  go_package="./api";  
service HelloService {  
  rpc Hello (String) returns (String) {}  
}  
message String {  
  string value = 1;
}
로그인 후 복사

목적

이 기사의 목표는 다음과 같은 RPC 서버측 및 클라이언트측 코드를 생성하는 데 사용할 플러그인을 만드는 것이었습니다.

// HelloService_rpc.pb.go
type HelloServiceInterface interface {  
    Hello(String, *String) error  
}  

func RegisterHelloService(  
    srv *rpc.Server, x HelloServiceInterface,  
) error {  
    if err := srv.RegisterName("HelloService", x); err != nil {  
       return err  
    }  
    return nil  
}  

type HelloServiceClient struct {  
    *rpc.Client  
}  

var _ HelloServiceInterface = (*HelloServiceClient)(nil)  

func DialHelloService(network, address string) (  
    *HelloServiceClient, error,  
) {  
    c, err := rpc.Dial(network, address)  
    if err != nil {  
       return nil, err  
    }  
    return &HelloServiceClient{Client: c}, nil  
}  

func (p *HelloServiceClient) Hello(  
    in String, out *String,  
) error {  
    return p.Client.Call("HelloService.Hello", in, out)  
}
로그인 후 복사

이렇게 하면 비즈니스 코드가 다음과 같이 변경됩니다

// service
func main() {  
    listener, err := net.Listen("tcp", ":1234")  
    if err != nil {  
       log.Fatal("ListenTCP error:", err)  
    }  
    _ = api.RegisterHelloService(rpc.DefaultServer, new(HelloService))  
    for {  
       conn, err := listener.Accept()  
       if err != nil {  
          log.Fatal("Accept error:", err)  
       }  
       go rpc.ServeConn(conn)  
    }  
}  

type HelloService struct{}  

func (p *HelloService) Hello(request api.String, reply *api.String) error {  
    log.Println("HelloService.proto Hello")  
    *reply = api.String{Value: "Hello:" + request.Value}  
    return nil  
}
// client.go
func main() {  
    client, err := api.DialHelloService("tcp", "localhost:1234")  
    if err != nil {  
       log.Fatal("net.Dial:", err)  
    }  
    reply := &api.String{}  
    err = client.Hello(api.String{Value: "Hello"}, reply)  
    if err != nil {  
       log.Fatal(err)  
    }  
    log.Println(reply)  
}
로그인 후 복사

생성된 코드에 따르면 작업량은 이미 훨씬 적고 오류 가능성도 매우 낮습니다. 좋은 시작입니다.

위의 API 코드를 기반으로 템플릿 파일을 꺼낼 수 있습니다.

const tmplService = `  
import (  
    "net/rpc")  
type {{.ServiceName}}Interface interface {  
func Register{{.ServiceName}}(  
    if err := srv.RegisterName("{{.ServiceName}}", x); err != nil {        return err    }    return nil}  
    *rpc.Client}  
func Dial{{.ServiceName}}(network, address string) (  
{{range $_, $m := .MethodList}}  
    return p.Client.Call("{{$root.ServiceName}}.{{$m.MethodName}}", in, out)}  
`
로그인 후 복사

전체 템플릿이 명확하고 그 안에 MethodName, ServiceName 등과 같은 몇 가지 자리 표시자가 있습니다. 이에 대해서는 나중에 다루겠습니다.

플러그인을 개발하는 방법은 무엇입니까?

Google은 플러그인 개발의 어려움을 크게 줄여주는 새로운 패키지 google.golang.org/protobuf/compile R/protogen을 도입하는 Go 언어 API 1을 출시했습니다.

  1. First of all, we create a go language project, such as protoc-gen-go-spprpc
  2. Then we need to define a protogen.Options, then call its Run method, and pass in a func(*protogen.Plugin) error callback. This is the end of the main process code.
  3. We can also set the ParamFunc parameter of protogen.Options, so that protogen will automatically parse the parameters passed by the command line for us. Operations such as reading and decoding protobuf information from standard input, encoding input information into protobuf and writing stdout are all handled by protogen. What we need to do is to interact with protogen.Plugin to implement code generation logic.

The most important thing for each service is the name of the service, and then each service has a set of methods. For the method defined by the service, the most important thing is the name of the method, as well as the name of the input parameter and the output parameter type. Let's first define a ServiceData to describe the meta information of the service:

// ServiceData 
type ServiceData struct {  
    PackageName string  
    ServiceName string  
    MethodList  []Method  
}
// Method 
type Method struct {  
    MethodName     string  
    InputTypeName  string  
    OutputTypeName string  
}
로그인 후 복사

Then comes the main logic, and the code generation logic, and finally the call to tmpl to generate the code.

func main() {  
    protogen.Options{}.Run(func(gen *protogen.Plugin) error {  
       for _, file := range gen.Files {  
          if !file.Generate {  
             continue  
          }  
          generateFile(gen, file)  
       }  
       return nil  
    })  
}  

// generateFile function definition
func generateFile(gen *protogen.Plugin, file *protogen.File) {  
    filename := file.GeneratedFilenamePrefix + "_rpc.pb.go"  
    g := gen.NewGeneratedFile(filename, file.GoImportPath)  
    tmpl, err := template.New("service").Parse(tmplService)  
    if err != nil {  
       log.Fatalf("Error parsing template: %v", err)  
    }  
    packageName := string(file.GoPackageName)  
// Iterate over each service to generate code
    for _, service := range file.Services {  
       serviceData := ServiceData{  
          ServiceName: service.GoName,  
          PackageName: packageName,  
       }  
       for _, method := range service.Methods {  
          inputType := method.Input.GoIdent.GoName  
          outputType := method.Output.GoIdent.GoName  

          serviceData.MethodList = append(serviceData.MethodList, Method{  
             MethodName:     method.GoName,  
             InputTypeName:  inputType,  
             OutputTypeName: outputType,  
          })  
       }  
// Perform template rendering
       err = tmpl.Execute(g, serviceData)  
       if err != nil {  
          log.Fatalf("Error executing template: %v", err)  
       }  
    }  
}
로그인 후 복사

Debug plugin

Finally, we put the compiled binary execution file protoc-gen-go-spprpc in $PATH, and then run protoc to generate the code we want.

protoc --go_out=.. --go-spprpc_out=.. HelloService.proto
로그인 후 복사

Because protoc-gen-go-spprpc has to depend on protoc to run, it's a bit tricky to debug. We can use

fmt.Fprintf(os.Stderr, "Fprintln: %v\n", err)
로그인 후 복사

To print the error log to debug.

Summary

That's all there is to this article. We first implemented an RPC call using protobuf and then created a protobuf plugin to help us generate the code. This opens the door for us to learn protobuf + RPC, and is our path to a thorough understanding of gRPC. I hope everyone can master this technology.

Reference

  1. https://taoshu.in/go/create-protoc-plugin.html
  2. https://chai2010.cn/advanced-go-programming-book/ch4-rpc/ch4-02-pb-intro.html

위 내용은 RPC 작업 EPUProtobuf 사용 및 사용자 정의 플러그인 생성의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!