이전 글에서는 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을 출시했습니다.
- First of all, we create a go language project, such as protoc-gen-go-spprpc
- 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.
- 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
- https://taoshu.in/go/create-protoc-plugin.html
- https://chai2010.cn/advanced-go-programming-book/ch4-rpc/ch4-02-pb-intro.html
위 내용은 RPC 작업 EPUProtobuf 사용 및 사용자 정의 플러그인 생성의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

보안 통신에 널리 사용되는 오픈 소스 라이브러리로서 OpenSSL은 암호화 알고리즘, 키 및 인증서 관리 기능을 제공합니다. 그러나 역사적 버전에는 알려진 보안 취약점이 있으며 그 중 일부는 매우 유해합니다. 이 기사는 데비안 시스템의 OpenSSL에 대한 일반적인 취약점 및 응답 측정에 중점을 둘 것입니다. DebianopensSL 알려진 취약점 : OpenSSL은 다음과 같은 몇 가지 심각한 취약점을 경험했습니다. 심장 출혈 취약성 (CVE-2014-0160) :이 취약점은 OpenSSL 1.0.1 ~ 1.0.1F 및 1.0.2 ~ 1.0.2 베타 버전에 영향을 미칩니다. 공격자는이 취약점을 사용하여 암호화 키 등을 포함하여 서버에서 무단 읽기 민감한 정보를 사용할 수 있습니다.

이 기사는 프로파일 링 활성화, 데이터 수집 및 CPU 및 메모리 문제와 같은 일반적인 병목 현상을 식별하는 등 GO 성능 분석을 위해 PPROF 도구를 사용하는 방법을 설명합니다.

이 기사는 GO에서 단위 테스트 작성, 모범 사례, 조롱 기술 및 효율적인 테스트 관리를위한 도구를 다루는 것에 대해 논의합니다.

이 기사는 단위 테스트를 위해 이동 중에 모의와 스터브를 만드는 것을 보여줍니다. 인터페이스 사용을 강조하고 모의 구현의 예를 제공하며 모의 집중 유지 및 어설 션 라이브러리 사용과 같은 모범 사례에 대해 설명합니다. 기사

이 기사에서는 GO의 제네릭에 대한 사용자 정의 유형 제약 조건을 살펴 봅니다. 인터페이스가 일반 함수에 대한 최소 유형 요구 사항을 정의하여 유형 안전 및 코드 재사성을 향상시키는 방법에 대해 자세히 설명합니다. 이 기사는 또한 한계와 모범 사례에 대해 설명합니다

이 기사는 코드의 런타임 조작, 직렬화, 일반 프로그래밍에 유리한 런타임 조작에 사용되는 GO의 반사 패키지에 대해 설명합니다. 실행 속도가 느리고 메모리 사용이 높아짐, 신중한 사용 및 최고와 같은 성능 비용을 경고합니다.

이 기사는 추적 도구를 사용하여 GO 응용 프로그램 실행 흐름을 분석합니다. 수동 및 자동 계측 기술, Jaeger, Zipkin 및 OpenTelemetry와 같은 도구 비교 및 효과적인 데이터 시각화를 강조합니다.

이 기사는 테스트 케이스 테이블을 사용하여 여러 입력 및 결과로 기능을 테스트하는 방법 인 GO에서 테이블 중심 테스트를 사용하는 것에 대해 설명합니다. 가독성 향상, 중복 감소, 확장 성, 일관성 및 A와 같은 이점을 강조합니다.


핫 AI 도구

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

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

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

PhpStorm 맥 버전
최신(2018.2.1) 전문 PHP 통합 개발 도구

Atom Editor Mac 버전 다운로드
가장 인기 있는 오픈 소스 편집기

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

Dreamweaver Mac版
시각적 웹 개발 도구

뜨거운 주제



