찾다
백엔드 개발GolangRPC 작업 EPUProtobuf 사용 및 사용자 정의 플러그인 생성

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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
마스터 링 GO 줄 : '문자열'패키지에 대한 깊은 다이빙마스터 링 GO 줄 : '문자열'패키지에 대한 깊은 다이빙May 12, 2025 am 12:05 AM

텍스트 데이터를 처리하는 도구를 제공하고 기본 문자열에서 고급 정규 표현식에 이르기까지 텍스트 데이터를 처리 할 수있는 도구를 제공하기 때문에 이동 중 "문자열"패키지에주의해야합니다. 1) "Strings"패키지는 성능 문제를 피하기 위해 문자열을 스플 라이스하는 데 사용되는 결합 기능과 같은 효율적인 스트링 작업을 제공합니다. 2) 포함 함수와 같은 고급 함수가 포함되어있어 문자열에 특정 문자 세트가 포함되어 있는지 확인합니다. 3) 교체 함수는 문자열의 하위 문자열을 대체하는 데 사용되며 교체 순서 및 사례 감도에주의를 기울여야합니다. 4) 분할 함수는 분리기에 따라 문자열을 분할 할 수 있으며 종종 정규 발현 처리에 사용됩니다. 5) 사용 할 때 성능을 고려해야합니다.

Go의 '인코딩/이진'패키지 : 이진 작업을위한 이동Go의 '인코딩/이진'패키지 : 이진 작업을위한 이동May 12, 2025 am 12:03 AM

"인코딩/바이너리"패키지 인테이블 링 베이너리 데이터, 1) ItsupportsBothlittle-endianandbig-endianByteorders, CruialCross-SystemCompatibility .2) ThePackagealLowworkingwithcus

바이트 슬라이스 조작 자습서 : '바이트'패키지 마스터 링바이트 슬라이스 조작 자습서 : '바이트'패키지 마스터 링May 12, 2025 am 12:02 AM

바이트 패키지를 GO에서 마스터하면 코드의 효율성과 우아함을 향상시키는 데 도움이 될 수 있습니다. 1) 바이트 패키지는 이진 데이터를 구문 분석, 네트워크 프로토콜 및 메모리 관리에 중요합니다. 2) BYTES.BUFFER를 사용하여 점차적으로 바이트 슬라이스를 작성하십시오. 3) 바이트 패키지는 바이트 슬라이스 검색, 교체 및 세분화 기능을 제공합니다. 4) BYTES.READER 유형은 특히 I/O 작업에서 바이트 슬라이스의 데이터를 읽는 데 적합합니다. 5) Bytes 패키지는 GO의 가비지 수집기와 협력하여 빅 데이터 처리의 효율성을 향상시킵니다.

'문자열'패키지를 사용하여 GO에서 문자열을 조작하는 방법은 무엇입니까?'문자열'패키지를 사용하여 GO에서 문자열을 조작하는 방법은 무엇입니까?May 12, 2025 am 12:01 AM

문자열을 조작 할 때 "문자열"패키지를 사용할 수 있습니다. 1) Strings.trimSpace를 사용하여 문자열의 양쪽 끝에서 공백 문자를 제거하십시오. 2) strings.split을 사용하여 지정된 구분 기호에 따라 문자열을 슬라이스로 분할하십시오. 3) 문자열 슬라이스를 문자열을 통해 하나의 문자열로 병합합니다. 4) 문자열에 포함하여 문자열에 특정 하위 문자열이 포함되어 있는지 확인하십시오. 5) STRINGS.REPLACEALL을 사용하여 글로벌 교체를 수행하십시오. 사용할 때 성능과 잠재적 인 함정에주의하십시오.

'바이트'패키지를 사용하여 바이트 슬라이스를 이동하는 방법 (단계별)'바이트'패키지를 사용하여 바이트 슬라이스를 이동하는 방법 (단계별)May 12, 2025 am 12:01 AM

thebytespackageingoishlytectivefificbyteslicemanipulation, ontomingfortionsforctionsforctions, splitting, andbuffering

Go Bytes 패키지 : 대안은 무엇입니까?Go Bytes 패키지 : 대안은 무엇입니까?May 11, 2025 am 12:11 AM

thealternativeStogo'sbytespackageinincludestringspackage, bufiopackage, and customstructs.1) thestringspackagecanbeusedforbytemanipulationbytestostringsandback.2) thebufiopackageisidealforhandlinglargestreamsofficationsificationsificationsly

바이트 슬라이스 조작 GO : '바이트'패키지의 전력바이트 슬라이스 조작 GO : '바이트'패키지의 전력May 11, 2025 am 12:09 AM

"바이트"패키지 인테이션 전신 formanipulatingByteslices, CruialForbinaryData, NetworkProtocols 및 Filei/O.itoffersFunctions likeIndexForsearching, BufferforHandlingLargedAtasets, ReaderForsimulatingstreamReading 및 조종 전망

Go Strings 패키지 : 문자열 조작에 대한 포괄적 인 안내서Go Strings 패키지 : 문자열 조작에 대한 포괄적 인 안내서May 11, 2025 am 12:08 AM

GO'SSTRINGSPACKAGEISCRUCIALFORFOFFICIENTSTRINGMENIPULATION, OffingerToolSlikestrings.split (), strings.join (), strings.Replaceall () 및 strings.contains () .split () DivideStringIntoSubstrings; 2) strings.join () stringsintring;

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

VSCode Windows 64비트 다운로드

VSCode Windows 64비트 다운로드

Microsoft에서 출시한 강력한 무료 IDE 편집기

mPDF

mPDF

mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.

맨티스BT

맨티스BT

Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.