Mencantumkan cluster Google Cloud Managed Service for Apache Kafka Anda

Untuk mencantumkan cluster, Anda dapat menggunakan Google Cloud konsol, Google Cloud CLI, library klien, atau Managed Kafka API. Anda tidak dapat menggunakan Apache Kafka API open source untuk mencantumkan cluster.

Peran dan izin yang diperlukan untuk mencantumkan cluster Anda

Untuk mendapatkan izin yang diperlukan guna membuat daftar cluster, minta administrator untuk memberi Anda peran IAM Managed Kafka Viewer (roles/managedkafka.viewer) di project Anda. Untuk mengetahui informasi selengkapnya tentang cara memberikan peran, lihat Mengelola akses ke project, folder, dan organisasi.

Peran bawaan ini berisi izin yang diperlukan untuk mencantumkan cluster Anda. Untuk melihat izin yang benar-benar diperlukan, perluas bagian Izin yang diperlukan:

Izin yang diperlukan

Izin berikut diperlukan untuk mencantumkan cluster Anda:

  • Mencantumkan cluster: managedkafka.clusters.list
  • Dapatkan detail cluster: managedkafka.clusters.get

Anda mungkin juga bisa mendapatkan izin ini dengan peran khusus atau peran bawaan lainnya.

Untuk mengetahui informasi selengkapnya tentang peran Managed Kafka Viewer, lihat Peran standar Managed Service for Apache Kafka.

Mencantumkan cluster Anda

Untuk mencantumkan semua cluster Anda dalam sebuah project, ikuti langkah-langkah berikut:

Konsol

  1. Di konsol Google Cloud , buka halaman Clusters.

    Buka Cluster

  2. Cluster yang Anda buat dalam project akan dicantumkan. Di halaman ini, Anda dapat melihat properti cluster berikut:

    • Nama cluster: ID unik untuk cluster Managed Service for Apache Kafka Anda. Anda dapat menggunakannya untuk mereferensikan cluster dalam berbagai operasi.
    • Status cluster: Menunjukkan status operasional cluster saat ini, seperti Aktif.
    • Region: Lokasi geografis tempat cluster Anda dihosting.
    • Memori: Jumlah total memori yang dialokasikan ke cluster. Hal ini menentukan kapasitas untuk menangani traffic dan penyimpanan pesan.
    • vCPU: Jumlah vCPU yang ditetapkan ke cluster. Hal ini memengaruhi kemampuan pemrosesan cluster dan kemampuannya untuk menangani operasi serentak.
    • Label: Pasangan nilai kunci yang dapat Anda lampirkan ke cluster untuk tujuan pengorganisasian, pemfilteran, dan otomatisasi.

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. Jalankan perintah gcloud managed-service-for-apache-kafka clusters list:

    gcloud managed-kafka clusters list --location=LOCATION \
        --limit=LIMIT
    

    Ganti kode berikut:

    • LOCATION: Lokasi cluster.
    • LIMIT: Jumlah maksimum cluster yang akan dicantumkan.
  3. REST

    Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

    • PROJECT_ID: Project ID Google Cloud Anda
    • LOCATION: lokasi cluster

    Metode HTTP dan URL:

    GET https://managedkafka.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/clusters

    Untuk mengirim permintaan Anda, perluas salah satu opsi berikut:

    Anda akan melihat respons JSON seperti berikut:

    {
      "clusters": [
        {
          "name": "projects/PROJECT_ID/locations/LOCATION/clusters/CLUSTER_ID",
          "createTime": "CREATE_TIME",
          "updateTime": "UPDATE_TIME",
          "capacityConfig": {
            "vcpuCount": "CPU_COUNT",
            "memoryBytes": "MEMORY"
          },
          "rebalanceConfig": {},
          "gcpConfig": {
            "accessConfig": {
              "networkConfigs": [
                {
                  "subnet": "projects/PROJECT_ID/locations/LOCATION/subnetworks/SUBNET_ID"
                }
              ]
            }
          },
          "state": "ACTIVE",
          "satisfiesPzi": false,
          "satisfiesPzs": false,
          "tlsConfig": {
            "trustConfig": {}
          },
          "updateOptions": {}
        }
      ]
    }
    

    Go

    Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Go di Menginstal library klien. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Managed Service for Apache Kafka Go API.

    Untuk melakukan autentikasi ke Managed Service untuk Apache Kafka, siapkan Kredensial Default Aplikasi(ADC). Untuk mengetahui informasi selengkapnya, lihat Menyiapkan ADC untuk lingkungan pengembangan lokal.

    import (
    	"context"
    	"fmt"
    	"io"
    
    	"cloud.google.com/go/managedkafka/apiv1/managedkafkapb"
    	"google.golang.org/api/iterator"
    	"google.golang.org/api/option"
    
    	managedkafka "cloud.google.com/go/managedkafka/apiv1"
    )
    
    func listClusters(w io.Writer, projectID, region string, opts ...option.ClientOption) error {
    	// projectID := "my-project-id"
    	// region := "us-central1"
    	ctx := context.Background()
    	client, err := managedkafka.NewClient(ctx, opts...)
    	if err != nil {
    		return fmt.Errorf("managedkafka.NewClient got err: %w", err)
    	}
    	defer client.Close()
    
    	locationPath := fmt.Sprintf("projects/%s/locations/%s", projectID, region)
    	req := &managedkafkapb.ListClustersRequest{
    		Parent: locationPath,
    	}
    	clusterIter := client.ListClusters(ctx, req)
    	for {
    		res, err := clusterIter.Next()
    		if err == iterator.Done {
    			break
    		}
    		if err != nil {
    			return fmt.Errorf("clusterIter.Next() got err: %w", err)
    		}
    		fmt.Fprintf(w, "Got cluster: %v", res)
    	}
    	return nil
    }
    

    Java

    Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Java di Menginstal library klien. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Managed Service for Apache Kafka Java API.

    Untuk melakukan autentikasi ke Managed Service for Apache Kafka, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan ADC untuk lingkungan pengembangan lokal.

    import com.google.api.gax.rpc.ApiException;
    import com.google.cloud.managedkafka.v1.Cluster;
    import com.google.cloud.managedkafka.v1.LocationName;
    import com.google.cloud.managedkafka.v1.ManagedKafkaClient;
    import java.io.IOException;
    
    public class ListClusters {
    
      public static void main(String[] args) throws Exception {
        // TODO(developer): Replace these variables before running the example.
        String projectId = "my-project-id";
        String region = "my-region"; // e.g. us-east1
        listClusters(projectId, region);
      }
    
      public static void listClusters(String projectId, String region) throws Exception {
        try (ManagedKafkaClient managedKafkaClient = ManagedKafkaClient.create()) {
          LocationName locationName = LocationName.of(projectId, region);
          // This operation is being handled synchronously.
          for (Cluster cluster : managedKafkaClient.listClusters(locationName).iterateAll()) {
            System.out.println(cluster.getAllFields());
          }
        } catch (IOException | ApiException e) {
          System.err.printf("managedKafkaClient.listClusters got err: %s", e.getMessage());
        }
      }
    }
    

    Python

    Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Python di Menginstal library klien. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Managed Service for Apache Kafka Python API.

    Untuk melakukan autentikasi ke Managed Service for Apache Kafka, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan ADC untuk lingkungan pengembangan lokal.

    from google.cloud import managedkafka_v1
    
    # TODO(developer)
    # project_id = "my-project-id"
    # region = "us-central1"
    
    client = managedkafka_v1.ManagedKafkaClient()
    
    request = managedkafka_v1.ListClustersRequest(
        parent=client.common_location_path(project_id, region),
    )
    
    response = client.list_clusters(request=request)
    for cluster in response:
        print("Got cluster:", cluster)
    

Apa langkah selanjutnya?

Apache Kafka® adalah merek dagang terdaftar milik The Apache Software Foundation atau afiliasinya di Amerika Serikat dan/atau negara lain.