grpc test - grpcurl

2022. 5. 19. 00:00ASPNET/Grpc

반응형

Grpc  Server 를 만들었는데 Test 를 위해 client 를 계속 만들기는 힘들다.

 

두가지 방법으로 테스트를 할수 있다. 

첫번째 방법은 아래와 같다. 

 

테스트를 하기 전에 해당 Test tool 을 설치 하기 위해 Go language sdk 를 설치 하자

https://go.dev/dl/

 

Downloads - The Go Programming Language

Downloads After downloading a binary release suitable for your system, please follow the installation instructions. If you are building from source, follow the source installation instructions. See the release history for more information about Go releases

go.dev

 

1. grpcurl 을 이용하여 test 하는 방법이 있다. 

cmd 를 이용해서 test 가능 하며 통합테스트를 자동화 하려고 할때 이용하면 좋다. 

https://github.com/fullstorydev/grpcurl#installation

 

GitHub - fullstorydev/grpcurl: Like cURL, but for gRPC: Command-line tool for interacting with gRPC servers

Like cURL, but for gRPC: Command-line tool for interacting with gRPC servers - GitHub - fullstorydev/grpcurl: Like cURL, but for gRPC: Command-line tool for interacting with gRPC servers

github.com

 

go 를 이용하지 않고 직접 다운로드 하기

https://repology.org/project/grpcurl/information

 

windows 라면 32bit 인지 64bit 인지에 따라 아래를 다운로드 하자

 

 

grpcurl 폴더를 환경 변수의 Path 에 등록 시키자

 

1. 내 pc 마우스 오른쪽 클릭 --> 속성 --> 고급 시스템 설정 --> 환경 변수 

2. 환경 변수 --> 시스템 변수 (Path 편집)

 

3. 새로 만들기 --> Grpcurl 설치 경로 추가 --> 확인

 

 

여기 까지 했다면 Test 를 위한 기본 설정은 되었다. 

이전강좌에서 만든 grpc server 를 열자

https://yogingang.tistory.com/230

 

개발자 명령 프롬프트에서 .csproj 가 있는 폴더로 이동하여 아래를 실행하자

dotnet add package Grpc.AspNetCore.Server.Reflection

 

Program.cs 수정

using GrpcServerWithASPNet.Services;

var builder = WebApplication.CreateBuilder(args);

// Additional configuration is required to successfully run gRPC on macOS.
// For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682

// Add services to the container.
builder.Services.AddGrpc();
builder.Services.AddGrpcReflection(); // <--- 추가
builder.Services.AddCors(o => o.AddPolicy("AllowAll", builder =>
{
    builder.AllowAnyOrigin()
           .AllowAnyMethod()
           .AllowAnyHeader()
           .WithExposedHeaders("Grpc-Status", "Grpc-Message", "Grpc-Encoding", "Grpc-Accept-Encoding");
}));

var app = builder.Build();

if(app.Environment.IsDevelopment()) // <--- 추가
    app.MapGrpcReflectionService(); // <--- 추가

// Configure the HTTP request pipeline.
app.MapGrpcService<GreeterService>().RequireCors("AllowAll"); ;
app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");

app.Run();

 

이제 grpc server 를 실행하자

 

windows cmd 창을 열어서 아래 명령을 내려보자

$ grpcurl -help

제대로 되지 않는다면 path 를 잘 설정했는지 grpcurl 은 잘 설치했는지 확인하자

 

 

서비스 검색

grpc server 가 실행된 ip 와 port 를 확인하자 

localhost:7253 인것을 알수 있다. 

 

다음 명령을 실행하자

grpcurl localhost:7253 describe

 

 

실행 결과

아래 명령을 실행 하여 input type 을 알아오자

 

grpcurl localhost:7253 describe greet.HelloRequest

 

grpcurl localhost:7253 greet.Greeter/SayHello

 

parameter 를 추가 해서 test 하려면

grpcurl -d "{\"name\":\"world\"}" localhost:7253 greet.Greeter/SayHello

 

정말 유용하다.(???)

 

하지만 너무 불편하다. 

그래서 두번째 방법이 있다. 

다음 시간에 grpc 를 test 할 수 있는 두번째 방법인 gRPCui 에 대해 알아보겠다. 

 

관련영상

https://youtu.be/B1G8S9Twzn4

 

 

반응형

'ASPNET > Grpc' 카테고리의 다른 글

gRPC - Configuration  (0) 2022.05.23
grpc test - grpcui  (0) 2022.05.20
grpc with aspnet core  (0) 2022.05.18
grpc - console client  (0) 2022.05.17
grpc - console server  (0) 2022.05.16