gRPC를 사용하여 Cloud Storage에 연결

gRPC는 Google에서 개발된 고성능 오픈소스 범용 RPC 프레임워크입니다. 이를 통해 프로토콜 버퍼를 사용하여 서비스를 정의할 수 있습니다. gRPC를 사용하여 Cloud Storage와 상호작용할 수 있습니다. gRPC는 Google 프런트엔드(GFE)를 우회하고 Compute Engine 인스턴스와 Cloud Storage 버킷 간에 직접 연결을 사용합니다.

다음과 같은 지원되는 클라이언트를 통해 gRPC를 사용해서 Cloud Storage에 연결할 수 있습니다.

클라이언트 라이브러리에서 gRPC 사용 설정

C++

시작하기 전에

  1. 다음 버전이 설치되었는지 확인합니다.

    • gRPC 버전 1.65.1 이상

    • C++ 클라이언트 라이브러리 버전 v2.30.0 이상

    • C++ 버전 14 이상

    설치 안내는 C++ 개발 환경 설정을 참조하세요.

  2. 인증을 설정합니다.

C++ 클라이언트 라이브러리 구성

  1. gcs::MakeGrpcClient()를 사용하여 gRPC 클라이언트를 만듭니다.

      namespace gcs = google::cloud::storage;
      void App() {
       auto client = gcs::MakeGrpcClient();
    
      // application code
    
      }
    

    C++ 클라이언트 라이브러리는 애플리케이션이 Google Cloud에서 실행되는 것을 감지했을 때 자동으로 직접 연결을 사용합니다.

  2. gRPC를 사용하도록 C++ 클라이언트 라이브러리를 구성하려면 Cloud Storage gRPC 클라이언트가 CMake 또는 Bazel에 대한 빌드 시스템 구성을 업데이트하도록 설정합니다.

    CMake

    1. 컴파일 시 Cloud Storage gRPC 클라이언트 플러그인을 사용 설정합니다.

      cmake -DGOOGLE_CLOUD_CPP_ENABLE=storage_grpc [other options here]
      
    2. 코드베이스에서 target_link_libraries() 명령어에 대해 google-cloud-cpp::storagegoogle-cloud-cpp::storage_grpc로 바꿉니다.

      예를 들어 gRPC의 빠른 시작 프로그램은 다음 코드를 사용합니다.

      add_executable(quickstart_grpc quickstart_grpc.cc)
      target_link_libraries(quickstart_grpc google-cloud-cpp::storage_grpc)
      

    Bazel

    종속 항목을 @google_cloud_cpp//:storage에서 @google_cloud_cpp//:storage_grpc로 바꿉니다.

    예를 들어 gRPC의 빠른 시작 프로그램은 다음 코드를 사용합니다.

      cc_binary(
          name = "quickstart",
          srcs = [
              "quickstart.cc",
          ],
          deps = [
              "@com_github_googleapis_google_cloud_cpp//:storage_grpc",
          ],
      )
    

Java

시작하기 전에

  1. 다음 버전이 설치되었는지 확인합니다.

    • Java 클라이언트 라이브러리:

      • com.google.cloud:google-cloud-storage:2.43.1 이상
      • com.google.cloud:libraries-bom:26.48 이상
    • Java 8 이상

    설치 안내는 Java 개발 환경 설정을 참조하세요.

  2. 인증을 설정합니다.

BOM을 사용하도록 프로젝트 업데이트

프로젝트에 호환 가능한 Google Cloud 클라이언트 라이브러리 버전이 포함되도록 하려면 Google Cloud 라이브러리 자재명세서(BOM)에 지정된 버전을 사용합니다. BOM을 사용하도록 프로젝트를 업데이트하려면 다음 방법을 사용합니다.

독립형 Cloud Storage

다른Google Cloud 라이브러리 없이 Cloud Storage 클라이언트 라이브러리를 독립적으로 사용 중이면 Cloud Storage 클라이언트 라이브러리와 관련된 BOM을 사용합니다.

Maven

pom.xml 파일의 dependencyManagement 섹션에서 BOM을 가져옵니다.

다음 예시는 BOM을 가져오고 google-cloud-storage 아티팩트를 포함하는 방법을 보여줍니다.

<dependencyManagement>
  <dependencies>
  <dependency>
   <groupId>com.google.cloud</groupId>
   <artifactId>google-cloud-storage-bom</artifactId>
   <version>2.43.1</version>
   <type>pom</type>
   <scope>import</scope>
  </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
   <groupId>com.google.cloud</groupId>
   <artifactId>google-cloud-storage</artifactId>
  </dependency>
</dependencies>

Gradle

com.google.cloud:google-cloud-storage-bom에 대한 플랫폼 종속성을 추가합니다.

implementation platform('com.google.cloud:google-cloud-storage-bom:2.43.1')
implementation 'com.google.cloud:google-cloud-storage'

다른 Google Cloud 라이브러리가 포함된 Cloud Storage

다른 Google Cloud 라이브러리와 함께 Cloud Storage 클라이언트 라이브러리를 사용 중이면 Google Cloud 클라이언트 라이브러리 BOM을 사용합니다.

Maven

pom.xml 파일의 dependencyManagement 섹션에서 BOM을 가져옵니다.

다음 예시는 BOM을 가져오고 libraries-bom 아티팩트를 포함하는 방법을 보여줍니다.

<dependencyManagement>
  <dependencies>
  <dependency>
   <groupId>com.google.cloud</groupId>
   <artifactId>libraries-bom</artifactId>
   <version>26.48.0</version>
   <type>pom</type>
   <scope>import</scope>
  </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
   <groupId>com.google.cloud</groupId>
   <artifactId>google-cloud-storage</artifactId>
  </dependency>
</dependencies>

Gradle

com.google.cloud:libraries-bom에 대한 플랫폼 종속성을 추가합니다.

implementation platform('com.google.cloud:libraries-bom:26.48.0')
implementation 'com.google.cloud:google-cloud-storage'

gRPC 클라이언트 만들기

다음 샘플에는 gRPC 중심 빌더가 사용됩니다. gRPC Java 클라이언트는 애플리케이션이 Google Cloud에서 실행되는 것을 감지했을 때 자동으로 직접 연결을 사용합니다.

// Imports the Google Cloud client library
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class QuickstartGrpcSample {
  public static void main(String... args) throws Exception {

    // Create an instance of options which will use the Google Cloud Storage gRPC API for all
    // operations
    StorageOptions options = StorageOptions.grpc().build();

    // Instantiates a client in a try-with-resource to automatically cleanup underlying resources
    try (Storage storage = options.getService()) {
      // The name for the new bucket
      String bucketName = args[0]; // "my-new-bucket";

      // Creates the new bucket using a request to the gRPC API
      Bucket bucket = storage.create(BucketInfo.of(bucketName));

      System.out.printf("Bucket %s created.%n", bucket.getName());
    }
  }
}

Go

시작하기 전에

  1. 개발 환경을 설정합니다.

  2. Cloud Storage Go 클라이언트 라이브러리 버전 1.46.0 이상을 사용해야 합니다.

  3. 인증을 설정합니다.

gRPC 클라이언트 만들기

클라이언트를 사용하려면 애플리케이션에서 NewClient 대신 NewGRPCClient 생성자를 호출해야 합니다.


// Sample storage-quickstart creates a Google Cloud Storage bucket using
// gRPC API.
package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"cloud.google.com/go/storage"
)

func main() {
	ctx := context.Background()

	// Use your Google Cloud Platform project ID and Cloud Storage bucket
	projectID := "project-id"
	bucketName := "bucket-name"

	// Creates a gRPC enabled client.
	client, err := storage.NewGRPCClient(ctx)
	if err != nil {
		log.Fatalf("Failed to create client: %v", err)
	}
	defer client.Close()

	// Creates the new bucket.
	ctx, cancel := context.WithTimeout(ctx, time.Second*10)
	defer cancel()
	if err := client.Bucket(bucketName).Create(ctx, projectID, nil); err != nil {
		log.Fatalf("Failed to create bucket: %v", err)
	}

	fmt.Printf("Bucket %v created.\n", bucketName)
}

Go 클라이언트 라이브러리는 애플리케이션이 Google Cloud에서 실행되는 것을 감지했을 때 자동으로 직접 연결을 사용합니다.

gRPC 클라이언트 사용 방법은 gRPC API를 참조하세요.

VPC 서비스 제어를 구성합니다.

제한된 가상 IP 주소가 있는 가상 프라이빗 클라우드를 사용(VPC 서비스 제어)하여 네트워크 보안을 개선하는 경우, Compute Engine 인스턴스와 Cloud Storage 버킷 사이의 성능을 최적화하기 위해 직접 연결을 사용 설정하도록 방화벽 규칙을 업데이트해야 합니다.

이렇게 하려면 다음 CIDR 블록에 대해 모든 포트에서 트래픽을 허용하도록 허용 목록 방화벽 규칙을 추가합니다.

  • IPv4 트래픽: 34.126.0.0/18
  • IPv6 트래픽: 2001:4860:8040::/42

위의 규칙 외에도 199.36.153.4/30에 대한 기존 허용 목록 규칙을 유지합니다.

방화벽 규칙 수정에 대한 제한이 있고 이를 업데이트할 수 없으면, google-c2p://storage.googleapis.com을 사용하는 대신 트래픽이 storage.googleapis.com을 통과하도록 구성하여 직접 연결을 피할 수 있습니다.

예를 들어 C++의 경우 google-c2p:///storage.googleapis.com 대신 .set<google::cloud::EndpointOption>(storage.googleapis.com)을 사용합니다.

Cloud Monitoring에서 gRPC 관련 측정항목을 생성하도록 Cloud Storage 클라이언트 라이브러리를 구성할 수 있습니다. gRPC 관련 측정항목을 사용하면 다음을 수행할 수 있습니다.

  • Cloud Storage에 대한 gRPC 요청의 성능 모니터링 및 최적화

  • 문제 해결 및 디버깅

  • 애플리케이션의 사용량 및 동작에 대한 인사이트를 얻습니다.

gRPC 관련 측정항목을 생성하는 방법은 클라이언트 측 측정항목 사용을 참조하세요.

측정항목 수집이 필요하지 않은 사용 사례의 경우에는 측정항목 수집을 선택 해제할 수 있습니다. 자세한 내용은 클라이언트 측 측정항목 선택 해제를 참조하세요.

제한사항

다음 단계