grpc를 공부하려고 해도 어디서 부터 시작해야 할 지 막막하신 분들 많지요? 우선 helloworld 서버와 grpcurl을 이용해서 커맨드라인에서 호출해 보는 것부터 시작하는 것은 어떨까요?
일단 실행되는 것 보기
우선 서버 클라이언트 프로그램을 다운받습니다. 여러가지 버전이 있지만 우선은 go 버전의 것을 선택했습니다.
go get google.golang.org/grpc/examples/helloworld/greeter_client
go get google.golang.org/grpc/examples/helloworld/greeter_server
서버 실행해보기
which greeter_server
greeter_server
그리고 클라이언트 실행해보기
which greeter_client
greeter_client
그리고 다운받은 프로그램 삭제하기
go clean -i -n google.golang.org/grpc/examples/helloworld/greeter_client...
go clean -i -n google.golang.org/grpc/examples/helloworld/greeter_server...
grpcurl 설치하기
grpcurl
은 scoop에 있습니다.
C:\Users\Administrator\temp11>scoop search grpcurl
'main' bucket:
grpcurl (1.7.0)
C:\Users\Administrator\temp11>scoop install grpcurl
Updating Scoop...
Creating shim for 'grpcurl'.
'grpcurl' (1.7.0) was installed successfully!
C:\Users\Administrator\temp11>
C:\Users\Administrator\temp11>which grpcurl
C:\Users\Administrator\scoop\shims\grpcurl.EXE
grpc 소스 다운받기
go 소소는 (요즘은 아무데나 받아도 module이 있으니까 상관없지만,) 환경변수의 GOPATH에 받는 것이 좋습니다.
git clone https://github.com/grpc/grpc-go.git %GOPATH%/src/google.golang.org/grpc
cd %GOPATH%/src/google.golang.org/grpc
그런데 greeter_server가 reflection을 지원하지 않는다
아. go 버전의 서버가 reflection을 지원하지 않는 버전이었습니다.
https://github.com/grpc/grpc-go/blob/master/examples/helloworld/greeter_server/main.go
그래서 reflection을 지원하도록 소스코드를 살짝 변경합니다.
- 라인 29에
"google.golang.org/grpc/reflection"
- 라인 54에
reflection.Register(s)
수정된 화면은 다음과 같습니다.
이제 수정된 코드를 빌드합니다.
go build -o greeter_server.exe main.go
grpcurl 실행하기
list 옵션을 이용하면 어떤 서비스를 제공하는 지를 알 수 있습니다.
C:\Users\Administrator\temp11>grpcurl localhost:50051 list
Failed to dial target host "localhost:50051": tls: first record does not look like a TLS handshake
TLS handshake 를 하려면 인증서 작업을 해줘야 하기 때문에 조금 복잡해지므로, 그냥 -plaintext
옵션을 사용하여 암호화 하지 않고 호출합니다.
C:\Users\Administrator\temp11>grpcurl -plaintext localhost:50051 list
grpc.reflection.v1alpha.ServerReflection
helloworld.Greeter
C:\Users\Administrator\temp11>grpcurl -plaintext -d "{ \"name\": \"World\" }" localhost:50051 helloworld.Greeter/SayHello
{
"message": "Hello World"
}
이상으로 grpcurl의 간략한 사용법을 살펴보았습니다. 느낌은 예전의 wsdl사용하는 것과 같은 느낌입니다. 어떤 서비스를 제공하는지 service description도 제공하고, 그리고 메서드도 제공하고.
자세한 사용법은 마이크로소프트의 문서를 참조해도 좋을 것 같습니다.