grpcmd - "grpc" 명령어.
Go Report Card
GitHub go.mod Go version
GitHub Actions Workflow Status
GitHub Release
GitHub Downloads (all assets, all releases)
GitHub Downloads (all assets, latest release)
grpcmd는 gRPC를 위한 간단하고 사용하기 쉬우며 개발자 친화적인 CLI 도구입니다.
(gRPC GUI 데스크톱 앱을 찾고 있다면 다음을 확인하세요: grpcmd-gui)
데모
Demo
기능 개요
- 사용 가능한 서비스 및 메서드 보기 (Reflection 사용)
- gRPC 메서드 설명
grpc :50051 UnaryMethod
- 모호성 발생 시 짧은 메서드 이름 지원 및 오류 처리.
- 메서드 인수에 대한 셸 자동 완성(shell completion)을 지원합니다.
- 메서드와 해당 요청/응답 메시지에 대한 설명을 출력합니다.
- gRPC 메서드 호출
grpc :50051 UnaryMethod '{"name":"Bob"}'
- 요청과 응답 모두 스트리밍(streaming)을 지원합니다.
- 헤더, 데이터 및 트레일러를 출력합니다.
- gRPC 오류 발생 시,
종료 코드 = 64 + gRPC 상태 코드가 됩니다.
- gRPC 서버 시작 (Reflection을 사용하여 grpcmd.GrpcmdService 구현)
설치
Homebrew
brew install grpcmd/tap/grpcmd
위 명령어는 tap grpcmd/tap에서 grpcmd 패키지를 설치합니다.
바이너리
macOS, Linux 및 Windows용 바이너리 파일은 Releases 페이지에서도 다운로드할 수 있습니다.
사용법
gRPC 서버 시작
출력:
Listening on address: :50051
다음 실행을 시도해 보세요:
grpc :50051 UnaryMethod '{"name": "Bob"}'
이렇게 하면 [GrpcmdService]를 구현하는 gRPC 서버가 (Reflection을 사용하여) 시작됩니다. 이 서비스 내의 메서드들은 들어오는 헤더를 나가는 헤더로도 첨부합니다.
옵션
주소 (Address)
기본적으로 gRPC 서버는 :50051 주소에서 요청을 수신합니다. 다른 주소에서 수신하려면, 해당 주소를 첫 번째 인수로 간단히 전달하면 됩니다. 예를 들어:
grpc server localhost:12345
사용 가능한 서비스 및 메서드 목록 보기 (List Available Services and Methods)
Output:
grpcmd.GrpcmdService
BidirectionalStreamingMethod
ClientStreamingMethod
ServerStreamingMethod
UnaryMethod
grpc.reflection.v1.ServerReflection
ServerReflectionInfo
grpc.reflection.v1alpha.ServerReflection
ServerReflectionInfo
메서드 설명하기 (Describe a Method)
Output:
rpc UnaryMethod ( .GrpcmdRequest ) returns ( .GrpcmdResponse );
message GrpcmdRequest {
string name = 1;
}
message GrpcmdResponse {
string message = 1;
}
GrpcmdRequest Template:
{
"name": ""
}
Unary 메서드 호출하기 (Call a Unary Method)
grpc :50051 UnaryMethod '{"name": "Bob"}'
Output:
content-type: application/grpc
{
"message": "Hello, Bob!"
}
status-code: 0 (OK)
Client Streaming 메서드 호출하기 (Call a Client Streaming Method)
grpc :50051 ClientStreamingMethod '{"name": "Bob"}{ "name": "Alice"}'
또는
grpc :50051 ClientStreamingMethod '{"name": "Bob"}' '{"name": "Alice"}'
Output:
content-type: application/grpc
{
"message": "Hello, Bob + Alice!"
}
status-code: 0 (OK)
Server Streaming 메서드 호출하기 (Call a Server Streaming Method)
grpc :50051 ServerStreamingMethod '{"name": "Bob"}'
Output:
content-type: application/grpc
{
"message": "Hello, "
}
{
"message": "B"
}
{
"message": "o"
}
{
"message": "b"
}
{
"message": "!"
}
status-code: 0 (OK)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Bidirectional Streaming 메서드 호출하기 (Call a Bidirectional Streaming Method)
grpc :50051 BidirectionalStreamingMethod '{"name": "Bob"}{ "name": "Alice"}'
또는
grpc :50051 BidirectionalStreamingMethod '{"name": "Bob"}' '{"name": "Alice"}'
Output:
content-type: application/grpc
{
"message": "Hello, Bob!"
}
{
"message": "Hello, Alice!"
}
status-code: 0 (OK)
추가 문서(Additional Documentation)
Proto 파일(Proto Files)
Reflection 대신 .proto 파일을 사용하려면, 쉼표로 구분된 하나 이상의 파일 위치를 --protos (단축어: -p) 플래그에 전달할 수 있습니다. 예시:
grpc --protos a.proto --protos b.proto,c.proto :50051
Proto Import 경로(Proto Import Paths)
.proto 파일에 import 구문이 포함되어 있다면, 가져오기(import)가 제대로 작동하도록 검색 경로를 설정하고 싶을 것입니다. 이를 위해 쉼표로 구분된 하나 이상의 디렉터리 위치를 --paths (단축어: -P) 플래그에 전달할 수 있습니다. 예시:
grpc --protos a.proto --paths ../protos/ :50051
빈 요청 전송(Sending an Empty Request)
빈 요청을 보내려면, 요청 데이터에 비어 있는 JSON 인수를 전달하기만 하면 됩니다. 예시:
grpc :50051 UnaryMethod {}
추가 헤더가 포함된 요청을 보내려면, 요청 데이터 앞에 하나 이상의 key: value 인수를 전달하기만 하면 됩니다. 예시:
grpc :50051 UnaryMethod 'custom-header: custom-value' {}
또는
grpc :50051 UnaryMethod header-1:value-1 header-2:value-2 {}
종료 코드(Exit Codes)
성공 시 종료 코드는 0입니다. 그러나 응답에 OK가 아닌 gRPC 상태 코드(status code)가 있는 경우, 종료 코드는 gRPC 상태 코드 + 64와 같습니다. 다른 애플리케이션 오류는 64보다 작은 종료 코드를 가집니다. 예를 들어, 제공된 주소에 연결하지 못하면 종료 코드가 1이 됩니다.
짧은 메서드 이름 모호성 해소(Disambiguating Short Method Names)
grpcmd는 완전히 한정된(fully-qualified) 및 네임스페이스가 지정된(namespaced) 메서드 이름을 요구하는 대신 짧은 메서드 이름(short method names)을 사용할 수 있도록 지원합니다. 만약 하나의 짧은 메서드 이름이 두 개 이상의 네임스페이스나 패키지에 존재하는 경우, 도구는 오류를 발생시킵니다. 예시:
grpc :50051 ServerReflectionInfo
Error while describing method ServerReflectionInfo:
Ambiguous method ServerReflectionInfo. Matching methods:
grpc.reflection.v1.ServerReflection.ServerReflectionInfo
grpc.reflection.v1alpha.ServerReflection.ServerReflectionInfo
grpc server :50051을 실행할 때 방화벽 팝업이 나타날 수 있습니다. 예를 들어, macOS에서 이러한 경우가 발생합니다. 그 이유는 주소 :50051이 모든 네트워크 인터페이스의 포트 50051에서 리스닝(listen)하기 때문입니다. 이를 해결하려면 주소에 루프백 인터페이스를 지정할 수 있습니다. 예를 들면 다음과 같습니다:
grpc server localhost:50051
grpc server 127.0.0.1:50051
서버에 연결할 때는 선호하는 경우 여전히 grpc :50051로 시작할 수 있습니다.
쉘 완성 설정 (Setup Shell Completion)
Homebrew
Homebrew를 사용하여 grpcmd 패키지를 설치했다면, 쉘 완성 스크립트가 해당 디렉터리에 설치되어야 합니다. 만약 brew 완성을 설정하지 않았다면, 이 가이드를 따르십시오. 이 가이드는 bash, zsh, fish에 대한 지침을 포함하고 있습니다.
수동 설정 (Manual)
쉘 완성 기능을 수동으로 활성화하려면 사용 중인 쉘에 따라 다음 명령어를 실행하십시오. 참고: 다음 명령어는 현재 세션에만 영향을 미칩니다.
Bash
source <(grpc completion bash)
Zsh
source <(grpc completion zsh)
fish
grpc completion fish | source
PowerShell
bash completion powershell | Out-String | Invoke-Expression