๐Ÿ”Œ

gRPC & Protobuf ์น˜ํŠธ์‹œํŠธ

Protocol Buffers proto3 ๋ฌธ๋ฒ•, 4๊ฐ€์ง€ RPC ํ†ต์‹  ํŒจํ„ด(Unary/Streaming), protoc ์ปดํŒŒ์ผ, Spring Boot/Go/Node.js ์—ฐ๋™, ์ธํ„ฐ์…‰ํ„ฐ, ์—๋Ÿฌ ๋ชจ๋ธ ๋ฐ grpcurl ๋””๋ฒ„๊น… ๊ฐ€์ด๋“œ

--views
๋‹จ์ˆ˜:
#grpc#protobuf#proto3#rpc#http2#microservices#spring-boot#golang#backend#api์ตœ์ข… ์—…๋ฐ์ดํŠธ: 2026-10-03

1. gRPC vs REST ์•„ํ‚คํ…์ฒ˜ ๋น„๊ต & ํ•ต์‹ฌ ํŠน์ง•

๐Ÿ’ป ์‹œ์Šคํ…œ ์„ค์น˜ ๋ฐ ์‹คํ–‰ ์š”๊ตฌ์‚ฌ์–‘ (System Requirements)

ํ•ญ๋ชฉ์ตœ์†Œ ์‚ฌ์–‘ (Minimum)๊ถŒ์žฅ ์‚ฌ์–‘ (Recommended)์ฐธ๊ณ  ๋ฐ ๋น„๊ณ 
์šด์˜์ฒด์ œ (OS)Linux, macOS, Windows 10/11Ubuntu 22.04 LTS, macOS (Apple Silicon)HTTP/2 ํ”„๋กœํ† ์ฝœ ์ง€์› OS
CPU1 ์ฝ”์–ด ์ด์ƒ2 ์ฝ”์–ด ~ 4 ์ฝ”์–ด ์ด์ƒProtocol Buffers ์ด์ง„ ์ง๋ ฌํ™”/์—ญ์ง๋ ฌํ™” ๊ณ ์† ์ฒ˜๋ฆฌ
๋ฉ”๋ชจ๋ฆฌ (RAM)1 GB ์ด์ƒ2 GB ~ 4 GB ์ด์ƒ๋™์‹œ ์ŠคํŠธ๋ฆฌ๋ฐ RPC ์„ธ์…˜ ์ˆ˜์— ๋น„๋ก€
๋””์Šคํฌ (Storage)1 GB ์ด์ƒ์˜ ์—ฌ์œ  ๊ณต๊ฐ„5 GB ์ด์ƒprotoc ์ปดํŒŒ์ผ๋Ÿฌ ๋ฐ ์–ธ์–ด๋ณ„ gRPC ํ”Œ๋Ÿฌ๊ทธ์ธ
๋„คํŠธ์›Œํฌ / ๋Ÿฐํƒ€์ž„Protocol Buffers ์ปดํŒŒ์ผ๋Ÿฌ (protoc v3/v25+)Go 1.21+, Java 17+, Node.js 18+, Python 3.10+ ์ค‘ ํƒ 1HTTP/2 ์ง€์› ๋„คํŠธ์›Œํ‚น (๊ธฐ๋ณธ ํฌํŠธ 50051)

Google์ด ๊ฐœ๋ฐœํ•œ ์˜คํ”ˆ์†Œ์Šค ๊ณ ์„ฑ๋Šฅ ์›๊ฒฉ ํ”„๋กœ์‹œ์ € ํ˜ธ์ถœ(RPC) ํ”„๋ ˆ์ž„์›Œํฌ๋กœ, ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ๊ฐ„ ํ†ต์‹ ๊ณผ ์‹ค์‹œ๊ฐ„ ์ŠคํŠธ๋ฆฌ๋ฐ์— ์ตœ์ ํ™”๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

[gRPC vs REST API ํ•ต์‹ฌ ๋น„๊ต]
โ€ข ํ”„๋กœํ† ์ฝœ     : HTTP/2 ์ „์šฉ (๋ฐ”์ด๋„ˆ๋ฆฌ ํ”„๋ ˆ์ด๋ฐ, ๋ฉ€ํ‹ฐํ”Œ๋ ‰์‹ฑ, ์–‘๋ฐฉํ–ฅ ์ŠคํŠธ๋ฆฌ๋ฐ) vs HTTP/1.1 or 2
โ€ข ํŽ˜์ด๋กœ๋“œ ํฌ๋งท: Protocol Buffers (๋ฐ”์ด๋„ˆ๋ฆฌ ์ง๋ ฌํ™”, JSON ๋Œ€๋น„ 3~10๋ฐฐ ๋น ๋ฅด๊ณ  ์šฉ๋Ÿ‰ 70% ์ ˆ๊ฐ) vs JSON / XML
โ€ข ๊ณ„์•ฝ ๋ฐฉ์‹     : ์—„๊ฒฉํ•œ ํƒ€์ž…์˜ .proto ์Šคํ‚ค๋งˆ ๊ธฐ๋ฐ˜ IDL vs OpenAPI / Swagger (์„ ํƒ ์‚ฌํ•ญ)
โ€ข ํ†ต์‹  ํŒจํ„ด     : Unary, Server Stream, Client Stream, Bi-directional Stream vs ๋‹จ๋ฐฉํ–ฅ Request-Response
โ€ข ๋ธŒ๋ผ์šฐ์ € ์—ฐ๋™: gRPC-Web ๋ฐ Envoy ํ”„๋ก์‹œ ํ•„์š” vs ๋ธŒ๋ผ์šฐ์ € ๋„ค์ดํ‹ฐ๋ธŒ ์ง์ ‘ ์ง€์› (fetch, XHR)
โ€ข ์ฃผ ์‚ฌ์šฉ์ฒ˜     : MSA ๋‚ด๋ถ€ ๊ณ ์† ํ†ต์‹ , ๋Œ€์šฉ๋Ÿ‰ ์‹ค์‹œ๊ฐ„ IoT/์ŠคํŠธ๋ฆฌ๋ฐ, ๋ชจ๋ฐ”์ผ ๋ฐฑ์—”๋“œ vs ๊ณต๊ฐœ Public API

2. Protocol Buffers v3 (proto3) ๋ฌธ๋ฒ• ์™„๋ฒฝ ๊ฐ€์ด๋“œ

์Šคํ‚ค๋งˆ ํŒŒ์ผ(.proto)์— ๋ฉ”์‹œ์ง€ ๊ตฌ์กฐ์™€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์—„๊ฒฉํ•˜๊ฒŒ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

syntax = "proto3"; // proto3 ๋ฌธ๋ฒ• ๋ช…์‹œ (ํ•„์ˆ˜)

package user.service.v1; // ํŒจํ‚ค์ง€ ๋„ค์ž„์ŠคํŽ˜์ด์Šค

// โ˜• ์–ธ์–ด๋ณ„ ์ƒ์„ฑ ์˜ต์…˜
option java_multiple_files = true;
option java_package = "com.lucky.grpc.user.v1";
option go_package = "github.com/lucky/service/user/v1;userv1";

// ๐Ÿ“ฆ Google ๋‚ด์žฅ ๊ณต์šฉ ํƒ€์ž… ์ž„ํฌํŠธ
import "google/protobuf/timestamp.proto";
import "google/protobuf/empty.proto";

// ๐Ÿ‘ค [1] ๊ธฐ๋ณธ ๋ฉ”์‹œ์ง€ ์ •์˜
message User {
  // ํ•„๋“œ ๋ฒˆํ˜ธ(1~15)๋Š” 1๋ฐ”์ดํŠธ๋กœ ์ธ์ฝ”๋”ฉ๋˜๋ฏ€๋กœ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํ•ต์‹ฌ ํ•„๋“œ์— ํ• ๋‹น
  int64 id = 1;
  string username = 2;
  string email = 3;
  bool is_active = 4;
  Role role = 5;

  // ๐Ÿ“‹ ๋ฐฐ์—ด/๋ฆฌ์ŠคํŠธ (repeated)
  repeated string tags = 6;

  // ๐Ÿ—บ๏ธ Key-Value ๋งต (map<KeyType, ValueType>)
  map<string, string> metadata = 7;

  // โฐ ํƒ€์ž„์Šคํƒฌํ”„
  google.protobuf.Timestamp created_at = 8;

  // ๐Ÿ”€ oneof: ์—ฌ๋Ÿฌ ํ•„๋“œ ์ค‘ ๋‹จ ํ•˜๋‚˜๋งŒ ๊ฐ’ ์„ค์ • ๊ฐ€๋Šฅ (๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ)
  oneof auth_method {
    string password_hash = 9;
    string oauth_token = 10;
  }

  // ๐Ÿšซ reserved: ์‚ญ์ œ๋œ ํ•„๋“œ ๋ฒˆํ˜ธ๋‚˜ ์ด๋ฆ„์„ ์žฌ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•˜๋„๋ก ์˜๊ตฌ ๊ณ ์ • (ํ•˜์œ„ ํ˜ธํ™˜์„ฑ)
  reserved 11, 12, 20 to 30;
  reserved "temp_token", "legacy_id";
}

// ๐Ÿท๏ธ [2] ์—ด๊ฑฐํ˜•(Enum) ์ •์˜
enum Role {
  // proto3์˜ Enum 0๋ฒˆ ์ธ๋ฑ์Šค๋Š” ๋ฐ˜๋“œ์‹œ ๊ธฐ๋ณธ๊ฐ’(UNSPECIFIED)์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  ROLE_UNSPECIFIED = 0;
  ROLE_USER = 1;
  ROLE_ADMIN = 2;
  ROLE_MANAGER = 3;
}

3. gRPC ์„œ๋น„์Šค ์ •์˜ & 4๊ฐ€์ง€ RPC ํ†ต์‹  ํŒจํ„ด

ํ•˜๋‚˜์˜ ์„œ๋น„์Šค ๋‚ด์—์„œ ๋‹จํ•ญ ๋ฐ 3๊ฐ€์ง€ ํ˜•ํƒœ์˜ ์ŠคํŠธ๋ฆฌ๋ฐ ํ†ต์‹  ๋ฐฉ์‹์„ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

service UserService {
  // 1๏ธโƒฃ [๋‹จํ•ญ RPC (Unary RPC)]
  // 1๊ฐœ์˜ ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  1๊ฐœ์˜ ์‘๋‹ต์„ ๋ฐ›๋Š” ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ํ†ต์‹  ๋ฐฉ์‹ (REST ๋Œ€์ฒด)
  rpc GetUser (UserRequest) returns (UserResponse);

  // 2๏ธโƒฃ [์„œ๋ฒ„ ์ŠคํŠธ๋ฆฌ๋ฐ RPC (Server Streaming)]
  // ํด๋ผ์ด์–ธํŠธ์˜ 1๊ฐœ ์š”์ฒญ์— ๋Œ€ํ•ด ์„œ๋ฒ„๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์‘๋‹ต ๋ฉ”์‹œ์ง€๋ฅผ ์ง€์†์ ์œผ๋กœ ์ŠคํŠธ๋ฆฌ๋ฐ (๋‹ค์šด๋กœ๋“œ/ํ”ผ๋“œ)
  rpc StreamUserLogs (LogRequest) returns (stream LogEntry);

  // 3๏ธโƒฃ [ํด๋ผ์ด์–ธํŠธ ์ŠคํŠธ๋ฆฌ๋ฐ RPC (Client Streaming)]
  // ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ ์กฐ๊ฐ๋“ค์„ ์—ฐ์† ์ „์†กํ•˜๊ณ , ์„œ๋ฒ„๊ฐ€ ์ฒ˜๋ฆฌ๋ฅผ ๋งˆ์นœ ๋’ค ์ตœ์ข… 1ํšŒ ์‘๋‹ต (ํŒŒ์ผ ์—…๋กœ๋“œ)
  rpc UploadAuditLogs (stream AuditLog) returns (UploadSummary);

  // 4๏ธโƒฃ [์–‘๋ฐฉํ–ฅ ์ŠคํŠธ๋ฆฌ๋ฐ RPC (Bidirectional Streaming)]
  // ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„๊ฐ€ ๋…๋ฆฝ์ ์ธ ์ŠคํŠธ๋ฆผ์„ ์—ด์–ด ์–ธ์ œ๋“  ๋น„๋™๊ธฐ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ์ฃผ๊ณ ๋ฐ›์Œ (์‹ค์‹œ๊ฐ„ ์ฑ„ํŒ…/IoT)
  rpc ChatRoom (stream ChatMessage) returns (stream ChatMessage);
}

message UserRequest { int64 id = 1; }
message UserResponse { User user = 1; }
message LogRequest { int64 user_id = 1; }
message LogEntry { string text = 1; int64 timestamp = 2; }
message AuditLog { string action = 1; }
message UploadSummary { int32 count = 1; bool success = 2; }
message ChatMessage { string sender = 1; string message = 2; }

4. ์ฝ”๋“œ ์ƒ์„ฑ (protoc ์ปดํŒŒ์ผ๋Ÿฌ & ํ”Œ๋Ÿฌ๊ทธ์ธ)

.proto ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ํƒ€๊ฒŸ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์˜ ๋ชจ๋ธ ํด๋ž˜์Šค์™€ gRPC ํด๋ผ์ด์–ธํŠธ/์„œ๋ฒ„ ์Šค์ผˆ๋ ˆํ†ค ์ฝ”๋“œ๋ฅผ ์ž๋™ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

# ๐Ÿ› ๏ธ [1] protoc CLI ๊ธฐ๋ณธ ์ปดํŒŒ์ผ ๋ช…๋ น์–ด
# Go ์ฝ”๋“œ ์ƒ์„ฑ (protoc-gen-go, protoc-gen-go-grpc ํ”Œ๋Ÿฌ๊ทธ์ธ ํ•„์š”)
protoc --proto_path=. \
       --go_out=. --go_opt=paths=source_relative \
       --go-grpc_out=. --go-grpc_opt=paths=source_relative \
       user.proto

# Python ์ฝ”๋“œ ์ƒ์„ฑ
python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. user.proto

# ๐Ÿ“ฆ [2] Go ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์น˜ (์‚ฌ์ „ ์ค€๋น„)
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

Gradle ๋นŒ๋“œ ์„ค์ • (build.gradle.kts - Java/Kotlin)

plugins {
    id("com.google.protobuf") version "0.9.4"
}

protobuf {
    protoc { artifact = "com.google.protobuf:protoc:3.25.1" }
    plugins {
        create("grpc") { artifact = "io.grpc:protoc-gen-grpc-java:1.60.0" }
    }
    generateProtoTasks {
        all().forEach { task ->
            task.plugins { create("grpc") }
        }
    }
}

5. Spring Boot & Java ์—ฐ๋™ (@GrpcService & @GrpcClient)

Spring Boot 3 ํ™˜๊ฒฝ์—์„œ net.devh:grpc-spring-boot-starter๋ฅผ ์ด์šฉํ•œ ์„œ๋ฒ„ ๋ฐ ํด๋ผ์ด์–ธํŠธ ๊ตฌํ˜„์ž…๋‹ˆ๋‹ค.

// ๐Ÿš€ [1] gRPC ์„œ๋ฒ„ ๊ตฌํ˜„์ฒด (Spring Boot)
package com.lucky.grpc.server;

import com.lucky.grpc.user.v1.*;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;

@GrpcService // Spring Bean ๋“ฑ๋ก ๋ฐ gRPC ์„œ๋ฒ„์— ์„œ๋น„์Šค ์ž๋™ ๋…ธ์ถœ
public class UserGrpcServiceImpl extends UserServiceGrpc.UserServiceImplBase {

    @Override
    public void getUser(UserRequest request, StreamObserver<UserResponse> responseObserver) {
        long userId = request.getId();
        
        User user = User.newBuilder()
                .setId(userId)
                .setUsername("lucky")
                .setEmail("lucky@example.com")
                .setIsActive(true)
                .build();

        UserResponse response = UserResponse.newBuilder().setUser(user).build();

        // ๊ฒฐ๊ณผ ์ „์†ก ๋ฐ ์™„๋ฃŒ ์•Œ๋ฆผ
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }

    // ์„œ๋ฒ„ ์ŠคํŠธ๋ฆฌ๋ฐ ์˜ˆ์ œ
    @Override
    public void streamUserLogs(LogRequest request, StreamObserver<LogEntry> responseObserver) {
        for (int i = 1; i <= 5; i++) {
            LogEntry entry = LogEntry.newBuilder()
                    .setText("๋กœ๊ทธ ์ด๋ฒคํŠธ #" + i + " for User " + request.getUserId())
                    .setTimestamp(System.currentTimeMillis())
                    .build();
            responseObserver.onNext(entry); // ์—ฌ๋Ÿฌ ๋ฒˆ ํ˜ธ์ถœ
        }
        responseObserver.onCompleted(); // ์ŠคํŠธ๋ฆผ ์ข…๋ฃŒ ์•Œ๋ฆผ
    }
}
// ๐Ÿ’ป [2] Spring Boot ํด๋ผ์ด์–ธํŠธ ํ˜ธ์ถœ (Stub ์ฃผ์ž…)
package com.lucky.grpc.client;

import com.lucky.grpc.user.v1.*;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

@Service
public class UserClientService {

    // ๋™๊ธฐ ๋ธ”๋กœํ‚น Stub (๊ฐ€์žฅ ํ”ํ•˜๊ฒŒ ์‚ฌ์šฉ)
    @GrpcClient("user-service")
    private UserServiceGrpc.UserServiceBlockingStub userBlockingStub;

    public User fetchUser(long id) {
        UserRequest request = UserRequest.newBuilder().setId(id).build();
        UserResponse response = userBlockingStub.getUser(request);
        return response.getUser();
    }
}

application.yml ์„ค์ •

grpc:
  server:
    port: 9090 # gRPC ์„œ๋ฒ„ ๋ฆฌ์Šค๋‹ ํฌํŠธ
  client:
    user-service:
      address: 'static://localhost:9090'
      negotiation-type: plaintext # ๋กœ์ปฌ ๊ฐœ๋ฐœ ์‹œ TLS ๋ฏธ์ ์šฉ

6. Go (Golang) ์„œ๋ฒ„ & ํด๋ผ์ด์–ธํŠธ ๊ตฌํ˜„

Go๋Š” gRPC์˜ ๋„ค์ดํ‹ฐ๋ธŒ 1๋“ฑ ์‹œ๋ฏผ ์–ธ์–ด๋กœ ์ดˆ๊ฒฝ๋Ÿ‰ ๊ณ ์„ฑ๋Šฅ ํ†ต์‹ ์— ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

// ๐Ÿš€ [1] Go gRPC ์„œ๋ฒ„ ๊ตฌํ˜„ (main.go)
package main

import (
	"context"
	"log"
	"net"

	pb "myproject/user/v1"
	"google.golang.org/grpc"
)

type server struct {
	pb.UnimplementedUserServiceServer
}

func (s *server) GetUser(ctx context.Context, in *pb.UserRequest) (*pb.UserResponse, error) {
	log.Printf("์š”์ฒญ ์ˆ˜์‹  User ID: %d", in.GetId())
	return &pb.UserResponse{
		User: &pb.User{
			Id:       in.GetId(),
			Username: "lucky_go",
			Email:    "go@example.com",
			IsActive: true,
		},
	}, nil
}

func main() {
	lis, err := net.Listen("tcp", ":50051")
	if err != nil {
		log.Fatalf("ํฌํŠธ ๋ฐ”์ธ๋”ฉ ์‹คํŒจ: %v", err)
	}

	s := grpc.NewServer()
	pb.RegisterUserServiceServer(s, &server{})
	log.Println("gRPC Go Server ๋ฆฌ์Šค๋‹ ์‹œ์ž‘ :50051")
	if err := s.Serve(lis); err != nil {
		log.Fatalf("์„œ๋ฒ„ ์‹คํ–‰ ์‹คํŒจ: %v", err)
	}
}
// ๐Ÿ’ป [2] Go gRPC ํด๋ผ์ด์–ธํŠธ ํ˜ธ์ถœ
package main

import (
	"context"
	"log"
	"time"

	pb "myproject/user/v1"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

func main() {
	// gRPC ์ฑ„๋„ ์—ฐ๊ฒฐ ์ƒ์„ฑ
	conn, err := grpc.Dial("localhost:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("์—ฐ๊ฒฐ ์‹คํŒจ: %v", err)
	}
	defer conn.Close()

	client := pb.NewUserServiceClient(conn)

	// ์ปจํ…์ŠคํŠธ์— 2์ดˆ ํƒ€์ž„์•„์›ƒ ์ง€์ • (ํ•„์ˆ˜ ๊ถŒ์žฅ!)
	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
	defer cancel()

	resp, err := client.GetUser(ctx, &pb.UserRequest{Id: 42})
	if err != nil {
		log.Fatalf("RPC ํ˜ธ์ถœ ์‹คํŒจ: %v", err)
	}
	log.Printf("์กฐํšŒ๋œ ์œ ์ € ์ด๋ฆ„: %s", resp.GetUser().GetUsername())
}

7. Node.js / TypeScript ๊ตฌํ˜„ (@grpc/grpc-js)

Node.js์—์„œ proto ํŒŒ์ผ์„ ๋™์ ์œผ๋กœ ๋กœ๋“œํ•˜๊ฑฐ๋‚˜ ์ปดํŒŒ์ผ๋œ ์ฝ”๋“œ๋กœ ํ†ต์‹ ํ•ฉ๋‹ˆ๋‹ค.

import * as grpc from '@grpc/grpc-js';
import * as protoLoader from '@grpc/proto-loader';

// ๐Ÿ“‚ [1] .proto ํŒŒ์ผ ๋™์  ๋กœ๋“œ
const packageDefinition = protoLoader.loadSync('user.proto', {
  keepCase: true,
  longs: String,
  enums: String,
  defaults: true,
  oneofs: true,
});
const userProto: any = grpc.loadPackageDefinition(packageDefinition).user.service.v1;

// ๐Ÿš€ [2] ์„œ๋ฒ„ ๊ตฌ๋™
const server = new grpc.Server();
server.addService(userProto.UserService.service, {
  GetUser: (call: any, callback: any) => {
    const userId = call.request.id;
    callback(null, {
      user: { id: userId, username: 'lucky_node', email: 'node@test.com' },
    });
  },
});

server.bindAsync('0.0.0.0:50051', grpc.ServerCredentials.createInsecure(), () => {
  console.log('Node.js gRPC Server Running on port 50051');
  server.start();
});

// ๐Ÿ’ป [3] ํด๋ผ์ด์–ธํŠธ ํ˜ธ์ถœ
const client = new userProto.UserService('localhost:50051', grpc.credentials.createInsecure());
client.GetUser({ id: 10 }, (err: any, response: any) => {
  if (err) console.error(err);
  else console.log('์‘๋‹ต:', response.user);
});

8. ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ(Metadata) & ์ธํ„ฐ์…‰ํ„ฐ(Interceptor)

์ธ์ฆ ํ† ํฐ(JWT) ์ „๋‹ฌ, ๋ถ„์‚ฐ ์ถ”์ (Tracing), ๊ณตํ†ต ๋กœ๊น…์„ HTTP/2 ํ—ค๋” ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ์™€ ์ธํ„ฐ์…‰ํ„ฐ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

// ๐Ÿ”’ [1] ํด๋ผ์ด์–ธํŠธ: ์•„์›ƒ๋ฐ”์šด๋“œ ํ—ค๋”์— Bearer ํ† ํฐ ์ฃผ์ž… (Go)
import "google.golang.org/grpc/metadata"

// ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์ƒ์„ฑ
md := metadata.Pairs("authorization", "Bearer my-jwt-token", "x-request-id", "uuid-1234")
ctx := metadata.NewOutgoingContext(context.Background(), md)

// ์š”์ฒญ ์‹œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์ปจํ…์ŠคํŠธ ์ „๋‹ฌ
resp, err := client.GetUser(ctx, &pb.UserRequest{Id: 1})
// ๐Ÿ›ก๏ธ [2] ์„œ๋ฒ„: ์ธํ„ฐ์…‰ํ„ฐ๋กœ ์ธ์ฆ ํ† ํฐ ๊ฒ€์ฆ (Java)
package com.lucky.grpc.interceptor;

import io.grpc.*;
import net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor;

@GrpcGlobalServerInterceptor
public class AuthServerInterceptor implements ServerInterceptor {
    private static final Metadata.Key<String> AUTH_KEY =
            Metadata.Key.of("authorization", Metadata.ASCII_STRING_MARSHALLER);

    @Override
    public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
            ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
        
        String authHeader = headers.get(AUTH_KEY);
        if (authHeader == null || !authHeader.startsWith("Bearer ")) {
            // ์ธ์ฆ ์‹คํŒจ ์‹œ ์ฆ‰์‹œ UNAUTHENTICATED ์—๋Ÿฌ ๋ฆฌํ„ด
            call.close(Status.UNAUTHENTICATED.withDescription("์ธ์ฆ ํ† ํฐ์ด ๋ˆ„๋ฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."), headers);
            return new ServerCall.Listener<ReqT>() {};
        }

        // ์ •์ƒ ์š”์ฒญ ๊ณ„์† ์ง„ํ–‰
        return next.startCall(call, headers);
    }
}

9. ์—๋Ÿฌ ํ•ธ๋“ค๋ง & gRPC ํ‘œ์ค€ ์ƒํƒœ ์ฝ”๋“œ

REST HTTP ์ƒํƒœ ์ฝ”๋“œ(404, 500 ๋“ฑ)์™€ 1:1๋กœ ๋Œ€์‘๋˜๋Š” gRPC ํ‘œ์ค€ ์ƒํƒœ ์ฝ”๋“œ ์ฒด๊ณ„์ž…๋‹ˆ๋‹ค.

[gRPC ํ‘œ์ค€ ์ƒํƒœ ์ฝ”๋“œ (Status Codes)]
โ€ข OK (0)                  : ์„ฑ๊ณต (HTTP 200)
โ€ข CANCELLED (1)           : ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ž‘์—… ์ทจ์†Œ (HTTP 499)
โ€ข UNKNOWN (2)             : ์•Œ ์ˆ˜ ์—†๋Š” ์„œ๋ฒ„ ๋‚ด๋ถ€ ์˜ค๋ฅ˜
โ€ข INVALID_ARGUMENT (3)    : ํŒŒ๋ผ๋ฏธํ„ฐ/์ž…๋ ฅ๊ฐ’ ์œ ํšจ์„ฑ ๊ฒ€์ฆ ์‹คํŒจ (HTTP 400)
โ€ข DEADLINE_EXCEEDED (4)   : ํƒ€์ž„์•„์›ƒ ์ดˆ๊ณผ (HTTP 504)
โ€ข NOT_FOUND (5)           : ๋Œ€์ƒ ๋ฆฌ์†Œ์Šค๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Œ (HTTP 404)
โ€ข ALREADY_EXISTS (6)      : ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ฆฌ์†Œ์Šค ์ƒ์„ฑ ์‹œ๋„ (HTTP 409)
โ€ข PERMISSION_DENIED (7)   : ๊ถŒํ•œ ๋ถ€์กฑ (HTTP 403)
โ€ข RESOURCE_EXHAUSTED (8)  : Rate Limit / ์ฟผํ„ฐ ์ดˆ๊ณผ (HTTP 429)
โ€ข UNAUTHENTICATED (16)    : ์ธ์ฆ๋˜์ง€ ์•Š์€ ์‚ฌ์šฉ์ž/ํ† ํฐ ๋งŒ๋ฃŒ (HTTP 401)
// โš ๏ธ ์„œ๋ฒ„์—์„œ ๋น„์ฆˆ๋‹ˆ์Šค ์˜ˆ์™ธ ๋˜์ง€๊ธฐ ์˜ˆ์‹œ (Java)
if (user == null) {
    responseObserver.onError(
        Status.NOT_FOUND
            .withDescription("ID " + userId + "์— ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์šฉ์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
            .asRuntimeException()
    );
    return;
}

10. CLI ๋””๋ฒ„๊น… & ํ…Œ์ŠคํŠธ ๋„๊ตฌ (grpcurl)

Postman GUI ๋˜๋Š” ํ„ฐ๋ฏธ๋„์˜ cURL์ฒ˜๋Ÿผ gRPC ์„œ๋ฒ„๋ฅผ ์†์‰ฝ๊ฒŒ ํ˜ธ์ถœํ•˜๊ณ  ํ…Œ์ŠคํŠธํ•˜๋Š” ํ•„์ˆ˜ CLI ๋„๊ตฌ์ž…๋‹ˆ๋‹ค.

# ๐Ÿ› ๏ธ [1] grpcurl ์„ค์น˜ (macOS: brew install grpcurl / Go: go install ... / Windows: winget)
winget install fullstorydev.grpcurl

# ๐Ÿ” [2] ์„œ๋ฒ„์—์„œ ์ œ๊ณตํ•˜๋Š” ์„œ๋น„์Šค ๋ชฉ๋ก ์กฐํšŒ (์„œ๋ฒ„ ๋ฆฌํ”Œ๋ ‰์…˜ ํ™œ์„ฑํ™” ํ•„์š”)
grpcurl -plaintext localhost:9090 list

# ๐Ÿ“‹ [3] ํŠน์ • ์„œ๋น„์Šค์˜ ์ƒ์„ธ ๋ฉ”์„œ๋“œ ๋ฐ ์Šคํ‚ค๋งˆ ๊ตฌ์กฐ ํ™•์ธ
grpcurl -plaintext localhost:9090 describe user.service.v1.UserService

# โšก [4] ๋‹จํ•ญ RPC ํ˜ธ์ถœ (JSON ๋ฌธ์ž์—ด ํŽ˜์ด๋กœ๋“œ ์ „๋‹ฌ)
grpcurl -plaintext \
        -d '{"id": 42}' \
        localhost:9090 \
        user.service.v1.UserService/GetUser

# ๐Ÿ”’ [5] ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ(ํ—ค๋”) ํฌํ•จ ํ˜ธ์ถœ
grpcurl -plaintext \
        -H 'authorization: Bearer my-secret-token' \
        -d '{"id": 100}' \
        localhost:9090 \
        user.service.v1.UserService/GetUser

11. ๋ธŒ๋ผ์šฐ์ € ์—ฐ๋™: gRPC-Web & Envoy Proxy

์›น ๋ธŒ๋ผ์šฐ์ €๋Š” HTTP/2 ๋ฐ”์ด๋„ˆ๋ฆฌ ํ”„๋ ˆ์ž„์„ ์ง์ ‘ ์กฐ์ž‘ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์ค‘๊ฐ„์— Envoy Proxy๋ฅผ ๋‘์–ด gRPC-Web ํ”„๋กœํ† ์ฝœ์„ ๋„ค์ดํ‹ฐ๋ธŒ gRPC๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

# ๐ŸŒ Envoy Proxy ๋ผ์šฐํŒ… ์„ค์ • ํŒŒ์ผ (envoy.yaml)
static_resources:
  listeners:
  - name: grpc_web_listener
    address:
      socket_address: { address: 0.0.0.0, port_value: 8080 } # ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ ‘์†ํ•  ํฌํŠธ
    filter_chains:
    - filters:
      - name: envoy.filters.network.http_connection_manager
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
          stat_prefix: grpc_web
          http_filters:
          - name: envoy.filters.http.grpc_web # โ˜… gRPC-Web ๋ณ€ํ™˜ ํ•„ํ„ฐ
          - name: envoy.filters.http.cors     # โ˜… ๋ธŒ๋ผ์šฐ์ € CORS ํ—ˆ์šฉ ํ•„ํ„ฐ
          - name: envoy.filters.http.router
          route_config:
            name: local_route
            virtual_hosts:
            - name: local_service
              domains: ["*"]
              cors:
                allow_origin_string_match: [{ prefix: "*" }]
                allow_methods: GET, PUT, DELETE, POST, OPTIONS
                allow_headers: keep-alive,user-agent,cache-control,content-type,content-transfer-encoding,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout
              routes:
              - match: { prefix: "/" }
                route:
                  cluster: backend_grpc_service
                  timeout: 0s

  clusters:
  - name: backend_grpc_service
    connect_timeout: 0.25s
    type: logical_dns
    http2_protocol_options: {} # ๋ฐฑ์—”๋“œ์™€ HTTP/2 ํ†ต์‹ 
    load_assignment:
      cluster_name: backend_grpc_service
      endpoints:
      - lb_endpoints:
        - endpoint:
            socket_address: { address: 127.0.0.1, port_value: 9090 } # ์‹ค์ œ ๋ฐฑ์—”๋“œ gRPC ํฌํŠธ
๐Ÿ’ฌ

์˜๊ฒฌ ๋ฐ ์งˆ๋ฌธ

0
0 / 1500