创建和管理精简版预留

本文档介绍了如何使用和管理 Pub/Sub Lite 预留。本文档还介绍了容量单元的概念,以及如何确定您需要多少个容量单元来满足应用的吞吐量。

精简版预留概览

精简版预留可在一个区域内的一个或多个精简版主题之间预配和动态共享吞吐量容量。这些主题可以是区域级精简版主题,也可以是可用区级精简版主题。与设置各个精简版主题的吞吐量容量相比,使用精简版预留是一种更理想的方案。使用精简版预留后,您需要管理和监控的资源会减少。此外,您无需选择各个精简版主题的吞吐量容量。

如需使用精简版预留,您需要将与精简版预留在同一区域内的精简版主题与特定精简版预留关联。您可以将一个精简版预留同时附加到可用区级精简版主题和区域级精简版主题。附加到精简版预留的精简版主题中的所有分区都会动态从精简版预留中预配的吞吐量容量中消耗。如果您为精简版主题指定了精简版预留,还可以添加速率限制,以限制精简版主题的每个分区的吞吐量容量。

容量单位概览

Lite 预留的吞吐量容量以容量单元形式预配。

一个容量单元对应以下吞吐量:

精简版主题类型 发布吞吐量(MiBps) 订阅吞吐量(以 MiBps 为单位)
可用区级 1 2
区域 0.25 0.5

预订名称命名准则

Pub/Sub Lite 资源名称用于唯一标识 Pub/Sub Lite 资源(例如主题、订阅或预订)。资源名称必须采用以下格式:

projects/project-identifier/collection/ID

  • project-identifier:必须是 Google Cloud 控制台中提供的项目 ID 或项目编号。例如,my-cool-project 是项目 ID。 123456789123 是项目编号。

  • collection:必须是 topicssubscriptionsreservations 中的一个。

  • ID:必须符合以下准则:

    • 不以字符串 goog 开头
    • 以字母开头
    • 包含 3 到 255 个字符
    • 仅包含以下字符:字母 [A-Za-z]、数字 [0-9]、短划线 -、下划线 _、英文句点 .、波形符 ~、加号 + 和百分号 %

    您可以在资源名称中使用上述列表中的特殊字符,而无需进行网址编码。不过,在网址中使用任何其他特殊字符时,您必须确保对这些字符正确进行编码或解码。例如,mi-tópico 是无效的 ID,不过,mi-t%C3%B3pico 是有效的。在进行 REST 调用时,此格式非常重要。

使用精简版预留的准则

对于区域级精简版主题,使用精简版预留预配吞吐量是必需的;对于可用区级精简版主题,则是可选的。精简版预留是区域级资源。

在规划配置 Lite 预订时,请注意以下事项:

  • Lite 预留所需的容量单元数量必须等于您预计在任何给定分钟内,该 Lite 预留中的所有主题和订阅的峰值吞吐量。

  • 当精简版预留中所有精简版主题的总吞吐量达到其容量时,发布和传送会被限流。Pub/Sub Lite 服务器会停止向客户端发送消息,并且消息会在发布者客户端中缓冲。您可以为 reservation/throughput_capacity/utilization 设置提醒,以便在预留有被节流的危险时通知您,以便您增加预留大小。

  • 您可以根据需要随时更新预订的吞吐量。更改会在 1 分钟内生效。不过,如果您降低预订的吞吐量,系统仍会在 24 小时内继续按较高金额向您收费。Pub/Sub Lite 本质上是一种按容量付费的系统,可实现即时扩容和缩容。

  • 附加到精简版预留的精简版主题都会从同一个吞吐量容量池中消耗资源。因此,精简版主题可能会突然使用大量吞吐量容量,导致其他精简版主题无法发布或接收消息。如果您需要完全隔离精简版主题,可以将其附加到独立的预订中。

分区对精简版预留的影响

一个精简版预留中所有精简版主题的分区总数不得大于预留的容量单位数量。例如,您无法将容量为 10 个容量单位的精简版预留附加到分区总数超过 10 的精简版主题或一组精简版主题。

计算精简版预留的容量单位

Lite 预留的大小由容量单元数量决定。以下示例说明了如何估算各个 Lite 主题所需的容量。

  1. 确定发布吞吐量峰值。

    估算附加到精简版预留的所有精简版主题的峰值吞吐量。您可以随时更新预留的大小,因此只需对吞吐量进行粗略估算即可。

  2. 确定订阅吞吐量峰值。

    订阅吞吐量峰值的计算方式与发布吞吐量峰值类似。不过,请注意,您的订阅者不一定需要以发布商的峰值速率接收消息。如果可以接受处理延迟,您可以使用 Pub/Sub Lite 缓冲消息,并以稳定的速率处理消息。与此同时,您可能希望订阅容量大于发布容量,以便应对订阅方偶尔出现的服务中断。

  3. 确定所需的容量单位总数。

    上一部分中的表格指定了容量单元可针对不同操作容纳的 MiBps 数量。以下是计算示例 Lite 预订所需容量单元的公式:

    • 可用区级精简版主题的峰值发布吞吐量 = Pz MiBps
    • 区域级精简版主题的峰值发布吞吐量 = Pr MiBps
    • 可用区级精简版主题的峰值订阅吞吐量 = Sz MiBps
    • 区域级精简版主题的订阅吞吐量峰值 = Sr MiBps

      所需容量单位数 = (Pz / 1 MiBps) + (Pr/0.25 MiBps) + (Sz/2 MiBps) + (Sr/0.5 MiBps)

      如果数字不是整数,则必须向上取整。

在不使用精简版预留的情况下配置吞吐量容量

  • 主题发布吞吐量容量:您可以指定 4 到 16 MiBps 的吞吐量容量,值必须为整数。

  • 主题订阅吞吐量容量:您可以指定 4 到 32 MiBps 的吞吐量容量,值必须为整数。

对于订阅吞吐量,我们建议您预配吞吐量容量,使其等于预期发布吞吐量与主题中的订阅数的乘积。

为单个主题预配的发布者和订阅者容量不可互换。处于这个原因,我们建议始终使用预留。

为主题直接预配容量与使用预留在结算方面会有差异。如需了解详情,请参阅 Pub/Sub 精简版价格页面

创建精简版预留

精简版预留必须与其关联的任何精简版主题位于同一项目和区域。如需查看可用的可用区和区域的列表,请参阅 Pub/Sub 精简版位置

您可以使用 Google Cloud 控制台、Google Cloud CLI 或 Pub/Sub Lite API 创建精简版预留。

控制台

  1. 前往精简版预留页面。

    前往“精简版预留”页面

  2. 点击创建精简版预留

  3. 请选择区域。

  4. 输入精简版预留 ID

  5. 预配吞吐量容量单位的数量。

  6. 点击创建

gcloud

要创建精简版预留,请使用 gcloud pubsub lite-reservations create 命令:

gcloud pubsub lite-reservations create myRes \
  --location=us-central1 \
  --throughput-capacity=INTEGER_NUMBER_OF_UNITS
gcloud pubsub lite-topics create myTopic \
  --throughput-reservation=myRez \
  --zone=ZONE \
  --location=us-central1a \
  --partitions \
  --storage

Go

在运行此示例之前,请按照 Pub/Sub Lite 客户端库中的 Go 设置说明进行操作。

import (
	"context"
	"fmt"
	"io"

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

func createReservation(w io.Writer, projectID, region, reservationID string, throughputCapacity int) error {
	// projectID := "my-project-id"
	// region := "us-central1"
	// reservationID := "my-reservation"
	// throughputCapacity := 4
	ctx := context.Background()
	client, err := pubsublite.NewAdminClient(ctx, region)
	if err != nil {
		return fmt.Errorf("pubsublite.NewAdminClient: %w", err)
	}
	defer client.Close()

	reservationPath := fmt.Sprintf("projects/%s/locations/%s/reservations/%s", projectID, region, reservationID)
	res, err := client.CreateReservation(ctx, pubsublite.ReservationConfig{
		Name:               reservationPath,
		ThroughputCapacity: throughputCapacity,
	})
	if err != nil {
		return fmt.Errorf("client.CreateReservation got err: %w", err)
	}
	fmt.Fprintf(w, "Created reservation: %s\n", res.Name)
	return nil
}

Java

在运行此示例之前,请按照 Pub/Sub Lite 客户端库中的 Java 设置说明进行操作。

import com.google.api.gax.rpc.AlreadyExistsException;
import com.google.cloud.pubsublite.AdminClient;
import com.google.cloud.pubsublite.AdminClientSettings;
import com.google.cloud.pubsublite.CloudRegion;
import com.google.cloud.pubsublite.ProjectNumber;
import com.google.cloud.pubsublite.ReservationName;
import com.google.cloud.pubsublite.ReservationPath;
import com.google.cloud.pubsublite.proto.Reservation;
import java.util.concurrent.ExecutionException;

public class CreateReservationExample {
  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    long projectNumber = Long.parseLong("123456789");
    String cloudRegion = "your-cloud-region";
    String reservationId = "your-reservation-id";
    // Each unit of throughput capacity supports up to 1 MiB/s of published messages or
    // 2 MiB/s of subscribed messages.
    int throughputCapacity = 4;

    createReservationExample(projectNumber, cloudRegion, reservationId, throughputCapacity);
  }

  public static void createReservationExample(
      long projectNumber, String cloudRegion, String reservationId, int throughputCapacity)
      throws Exception {

    ReservationPath reservationPath =
        ReservationPath.newBuilder()
            .setProject(ProjectNumber.of(projectNumber))
            .setLocation(CloudRegion.of(cloudRegion))
            .setName(ReservationName.of(reservationId))
            .build();

    Reservation reservation =
        Reservation.newBuilder()
            .setName(reservationPath.toString())
            .setThroughputCapacity(throughputCapacity)
            .build();

    AdminClientSettings adminClientSettings =
        AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();

    try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
      Reservation response = adminClient.createReservation(reservation).get();
      System.out.println(response.getAllFields() + " created successfully.");
    } catch (ExecutionException e) {
      try {
        throw e.getCause();
      } catch (AlreadyExistsException alreadyExists) {
        System.out.println("This reservation already exists.");
      } catch (Throwable throwable) {
        throwable.printStackTrace();
      }
    }
  }
}

Python

在运行此示例之前,请按照 Pub/Sub Lite 客户端库中的 Python 设置说明进行操作。

from google.api_core.exceptions import AlreadyExists
from google.cloud.pubsublite import AdminClient, Reservation
from google.cloud.pubsublite.types import CloudRegion, ReservationPath

# TODO(developer):
# project_number = 1122334455
# cloud_region = "us-central1"
# reservation_id = "your-reservation-id"
# Each unit of throughput capacity supports up to 1 MiB/s of published messages
# or 2 MiB/s of subscribed messages. Must be a positive integer.
# throughput_capacity = 4

cloud_region = CloudRegion(cloud_region)
reservation_path = ReservationPath(project_number, cloud_region, reservation_id)

reservation = Reservation(
    name=str(reservation_path),
    throughput_capacity=throughput_capacity,
)

client = AdminClient(cloud_region)
try:
    response = client.create_reservation(reservation)
    print(f"{response.name} created successfully.")
except AlreadyExists:
    print(f"{reservation_path} already exists.")

更新精简版预留

您可以更新为 Lite 预留配置的容量单元数量。

您可以使用 Google Cloud 控制台、Google Cloud CLI 或 Pub/Sub Lite API 更新精简版预留。

控制台

  1. 前往精简版预留页面。

    前往“精简版预留”页面

  2. 点击精简版预留 ID。

  3. 在精简版订阅详情页面中,点击修改

gcloud

要更新精简版预留,请使用 gcloud pubsub lite-reservations update 命令:

gcloud pubsub lite-reservations update RESERVATION_ID \
  --location=REGION \
  --throughput-capacity=THROUGHPUT_CAPACITY

替换以下内容:

  • RESERVATION_ID:精简版预留的 ID
  • REGION:预留的区域
  • THROUGHPUT_CAPACITY:预留吞吐量容量。

Go

在运行此示例之前,请按照 Pub/Sub Lite 客户端库中的 Go 设置说明进行操作。

import (
	"context"
	"fmt"
	"io"

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

func updateReservation(w io.Writer, projectID, region, reservationID string, throughputCapacity int) error {
	// projectID := "my-project-id"
	// region := "us-central1"
	// reservationID := "my-reservation-id"
	// throughputCapacity := 8
	ctx := context.Background()
	client, err := pubsublite.NewAdminClient(ctx, region)
	if err != nil {
		return fmt.Errorf("pubsublite.NewAdminClient: %w", err)
	}
	defer client.Close()

	reservationPath := fmt.Sprintf("projects/%s/locations/%s/reservations/%s", projectID, region, reservationID)
	config := pubsublite.ReservationConfigToUpdate{
		Name:               reservationPath,
		ThroughputCapacity: throughputCapacity,
	}
	updatedCfg, err := client.UpdateReservation(ctx, config)
	if err != nil {
		return fmt.Errorf("client.UpdateReservation got err: %w", err)
	}
	fmt.Fprintf(w, "Updated reservation: %#v\n", updatedCfg)
	return nil
}

Java

在运行此示例之前,请按照 Pub/Sub Lite 客户端库中的 Java 设置说明进行操作。

import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.pubsublite.AdminClient;
import com.google.cloud.pubsublite.AdminClientSettings;
import com.google.cloud.pubsublite.CloudRegion;
import com.google.cloud.pubsublite.ProjectNumber;
import com.google.cloud.pubsublite.ReservationName;
import com.google.cloud.pubsublite.ReservationPath;
import com.google.cloud.pubsublite.proto.Reservation;
import java.util.concurrent.ExecutionException;

public class UpdateReservationExample {
  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    long projectNumber = Long.parseLong("123456789");
    String cloudRegion = "your-cloud-region";
    String reservationId = "your-reservation-id";
    // Each unit of throughput capacity supports up to 1 MiB/s of published messages or
    // 2 MiB/s of subscribed messages.
    int throughputCapacity = 8;

    updateReservationExample(projectNumber, cloudRegion, reservationId, throughputCapacity);
  }

  public static void updateReservationExample(
      long projectNumber, String cloudRegion, String reservationId, int throughputCapacity)
      throws Exception {

    ReservationPath reservationPath =
        ReservationPath.newBuilder()
            .setProject(ProjectNumber.of(projectNumber))
            .setLocation(CloudRegion.of(cloudRegion))
            .setName(ReservationName.of(reservationId))
            .build();

    com.google.protobuf.FieldMask fieldMask =
        com.google.protobuf.FieldMask.newBuilder().addPaths("throughput_capacity").build();

    Reservation reservation =
        Reservation.newBuilder()
            .setName(reservationPath.toString())
            .setThroughputCapacity(throughputCapacity)
            .build();

    AdminClientSettings adminClientSettings =
        AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();

    try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
      Reservation reservationBeforeUpdate = adminClient.getReservation(reservationPath).get();
      System.out.println("Before update: " + reservationBeforeUpdate.getAllFields());

      Reservation reservationAfterUpdate =
          adminClient.updateReservation(reservation, fieldMask).get();
      System.out.println("After update: " + reservationAfterUpdate.getAllFields());
    } catch (ExecutionException e) {
      try {
        throw e.getCause();
      } catch (NotFoundException notFound) {
        System.out.println("This reservation is not found.");
      } catch (Throwable throwable) {
        throwable.printStackTrace();
      }
    }
  }
}

Python

在运行此示例之前,请按照 Pub/Sub Lite 客户端库中的 Python 设置说明进行操作。

from google.api_core.exceptions import NotFound
from google.cloud.pubsublite import AdminClient, Reservation
from google.cloud.pubsublite.types import CloudRegion, ReservationPath
from google.protobuf.field_mask_pb2 import FieldMask

# TODO(developer):
# project_number = 1122334455
# cloud_region = "us-central1"
# reservation_id = "your-reservation-id"
# Each unit of throughput capacity supports up to 1 MiB/s of published messages
# or 2 MiB/s of subscribed messages. Must be a positive integer.
# throughput_capacity = 8

cloud_region = CloudRegion(cloud_region)
reservation_path = ReservationPath(project_number, cloud_region, reservation_id)

# Defines which fields to update in the reservation.
field_mask = FieldMask(paths=["throughput_capacity"])

reservation = Reservation(
    name=str(reservation_path), throughput_capacity=throughput_capacity
)

client = AdminClient(cloud_region)
try:
    response = client.get_reservation(reservation_path)
    print(f"Before update: {response}")

    response = client.update_reservation(reservation, field_mask)
    print(f"After update: {response}")
except NotFound:
    print(f"{reservation_path} is not found.")

获取精简版预留的详细信息

您可以使用 Google Cloud 控制台、Google Cloud CLI 或 Pub/Sub Lite API 获取有关精简版预留的详细信息。

控制台

  1. 前往精简版预留页面。

    前往“精简版预留”页面

  2. 点击精简版预留 ID。

gcloud

要获取精简版预留的详细信息,请使用 gcloud pubsub lite-reservations describe 命令:

gcloud pubsub lite-reservations describe RESERVATION_ID \
  --location=REGION

替换以下内容:

  • RESERVATION_ID:精简版预留的 ID
  • REGION:预留的区域

Go

在运行此示例之前,请按照 Pub/Sub Lite 客户端库中的 Go 设置说明进行操作。

import (
	"context"
	"fmt"
	"io"

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

func getReservation(w io.Writer, projectID, region, reservationID string) error {
	// projectID := "my-project-id"
	// region := "us-central1"
	// reservationID := "my-reservation"
	ctx := context.Background()
	client, err := pubsublite.NewAdminClient(ctx, region)
	if err != nil {
		return fmt.Errorf("pubsublite.NewAdminClient: %w", err)
	}
	defer client.Close()

	reservationPath := fmt.Sprintf("projects/%s/locations/%s/reservations/%s", projectID, region, reservationID)
	res, err := client.Reservation(ctx, reservationPath)
	if err != nil {
		return fmt.Errorf("client.Reservation got err: %w", err)
	}
	fmt.Fprintf(w, "Got reservation: %#v\n", res)
	return nil
}

Java

在运行此示例之前,请按照 Pub/Sub Lite 客户端库中的 Java 设置说明进行操作。

import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.pubsublite.AdminClient;
import com.google.cloud.pubsublite.AdminClientSettings;
import com.google.cloud.pubsublite.CloudRegion;
import com.google.cloud.pubsublite.ProjectNumber;
import com.google.cloud.pubsublite.ReservationName;
import com.google.cloud.pubsublite.ReservationPath;
import com.google.cloud.pubsublite.proto.Reservation;
import java.util.concurrent.ExecutionException;

public class GetReservationExample {
  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    long projectNumber = Long.parseLong("123456789");
    String cloudRegion = "your-cloud-region";
    String reservationId = "your-reservation-id";

    getReservationExample(projectNumber, cloudRegion, reservationId);
  }

  public static void getReservationExample(
      long projectNumber, String cloudRegion, String reservationId) throws Exception {
    ReservationPath reservationPath =
        ReservationPath.newBuilder()
            .setProject(ProjectNumber.of(projectNumber))
            .setLocation(CloudRegion.of(cloudRegion))
            .setName(ReservationName.of(reservationId))
            .build();

    AdminClientSettings adminClientSettings =
        AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();

    try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
      Reservation response = adminClient.getReservation(reservationPath).get();
      long throughputCapacity = response.getThroughputCapacity();
      System.out.println(
          response.getAllFields()
              + "\nhas "
              + throughputCapacity
              + " units of throughput capacity.");
    } catch (ExecutionException e) {
      try {
        throw e.getCause();
      } catch (NotFoundException notFound) {
        System.out.println("This reservation is not found.");
      } catch (Throwable throwable) {
        throwable.printStackTrace();
      }
    }
  }
}

Python

在运行此示例之前,请按照 Pub/Sub Lite 客户端库中的 Python 设置说明进行操作。

from google.api_core.exceptions import NotFound
from google.cloud.pubsublite import AdminClient
from google.cloud.pubsublite.types import CloudRegion, ReservationPath

# TODO(developer):
# project_number = 1122334455
# cloud_region = "us-central1"
# reservation_id = "your-reservation-id"

cloud_region = CloudRegion(cloud_region)
reservation_path = ReservationPath(project_number, cloud_region, reservation_id)

client = AdminClient(cloud_region)
try:
    response = client.get_reservation(reservation_path)
    print(
        f"{response.name} has {response.throughput_capacity} units of throughput capacity."
    )
except NotFound:
    print(f"{reservation_path} is not found.")

列出精简版预留

控制台

gcloud

要查看项目中的精简版预留列表,请使用 gcloud pubsub lite-reservations list 命令:

gcloud pubsub lite-reservations list \
  --location=REGION

替换以下内容:

  • REGION:预留的区域

Go

在运行此示例之前,请按照 Pub/Sub Lite 客户端库中的 Go 设置说明进行操作。

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/pubsublite"
	"google.golang.org/api/iterator"
)

func listReservations(w io.Writer, projectID, region string) error {
	// projectID := "my-project-id"
	// region := "us-central1"
	ctx := context.Background()
	client, err := pubsublite.NewAdminClient(ctx, region)
	if err != nil {
		return fmt.Errorf("pubsublite.NewAdminClient: %w", err)
	}
	defer client.Close()

	parent := fmt.Sprintf("projects/%s/locations/%s", projectID, region)
	resIter := client.Reservations(ctx, parent)
	for {
		res, err := resIter.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("resIter.Next() got err: %w", err)
		}
		fmt.Fprintf(w, "Got reservation config: %v", res)
	}
	return nil
}

Java

在运行此示例之前,请按照 Pub/Sub Lite 客户端库中的 Java 设置说明进行操作。

import com.google.cloud.pubsublite.AdminClient;
import com.google.cloud.pubsublite.AdminClientSettings;
import com.google.cloud.pubsublite.CloudRegion;
import com.google.cloud.pubsublite.LocationPath;
import com.google.cloud.pubsublite.ProjectNumber;
import com.google.cloud.pubsublite.proto.Reservation;
import java.util.List;

public class ListReservationsExample {

  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    long projectNumber = Long.parseLong("123456789");
    String cloudRegion = "your-cloud-region";

    listReservationsExample(projectNumber, cloudRegion);
  }

  public static void listReservationsExample(long projectNumber, String cloudRegion)
      throws Exception {

    AdminClientSettings adminClientSettings =
        AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();

    LocationPath locationPath =
        LocationPath.newBuilder()
            .setProject(ProjectNumber.of(projectNumber))
            .setLocation(CloudRegion.of(cloudRegion))
            .build();

    try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
      List<Reservation> reservations = adminClient.listReservations(locationPath).get();
      for (Reservation reservation : reservations) {
        System.out.println(reservation.getAllFields());
      }
      System.out.println(reservations.size() + " reservation(s) listed in " + locationPath + ".");
    }
  }
}

Python

在运行此示例之前,请按照 Pub/Sub Lite 客户端库中的 Python 设置说明进行操作。

from google.cloud.pubsublite import AdminClient
from google.cloud.pubsublite.types import LocationPath

# TODO(developer):
# project_number = 1122334455
# cloud_region = "us-central1"

location_path = LocationPath(project_number, cloud_region)

client = AdminClient(cloud_region)
response = client.list_reservations(location_path)

for reservation in response:
    print(reservation)

print(f"{len(response)} reservation(s) listed in your project and location.")

删除精简版预留

您可以使用 Google Cloud 控制台、Google Cloud CLI 或 Pub/Sub Lite API 删除精简版预留。要删除的预留不得包含任何主题。您可以将现有主题关联到其他预留,然后再删除预留。

控制台

  1. 前往精简版预留页面。

    前往“精简版预留”页面

  2. 点击精简版预留 ID。

  3. 在精简版预留详情页面中,点击删除

  4. 在显示的对话框中,点击删除以确认您要删除精简版预留。

gcloud

要删除精简版预留,请使用 gcloud pubsub lite-reservations delete 命令:

gcloud pubsub lite-reservations delete RESERVATION_ID \
  --location=REGION

替换以下内容:

  • RESERVATION_ID:精简版预留的 ID
  • REGION:预留的区域

Go

在运行此示例之前,请按照 Pub/Sub Lite 客户端库中的 Go 设置说明进行操作。

import (
	"context"
	"fmt"
	"io"

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

func deleteReservation(w io.Writer, projectID, region, reservationID string) error {
	// projectID := "my-project-id"
	// region := "us-central1"
	// reservationID := "my-reservation"
	ctx := context.Background()
	client, err := pubsublite.NewAdminClient(ctx, region)
	if err != nil {
		return fmt.Errorf("pubsublite.NewAdminClient: %w", err)
	}
	defer client.Close()

	reservationPath := fmt.Sprintf("projects/%s/locations/%s/reservations/%s", projectID, region, reservationID)
	err = client.DeleteReservation(ctx, reservationPath)
	if err != nil {
		return fmt.Errorf("client.DeleteReservation got err: %w", err)
	}
	fmt.Fprint(w, "Deleted reservation")
	return nil
}

Java

在运行此示例之前,请按照 Pub/Sub Lite 客户端库中的 Java 设置说明进行操作。

import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.pubsublite.AdminClient;
import com.google.cloud.pubsublite.AdminClientSettings;
import com.google.cloud.pubsublite.CloudRegion;
import com.google.cloud.pubsublite.ProjectNumber;
import com.google.cloud.pubsublite.ReservationName;
import com.google.cloud.pubsublite.ReservationPath;
import java.util.concurrent.ExecutionException;

public class DeleteReservationExample {
  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    long projectNumber = Long.parseLong("123456789");
    String cloudRegion = "your-cloud-region";
    String reservationId = "your-reservation-id";

    deleteReservationExample(projectNumber, cloudRegion, reservationId);
  }

  public static void deleteReservationExample(
      long projectNumber, String cloudRegion, String reservationId) throws Exception {

    ReservationPath reservationPath =
        ReservationPath.newBuilder()
            .setProject(ProjectNumber.of(projectNumber))
            .setLocation(CloudRegion.of(cloudRegion))
            .setName(ReservationName.of(reservationId))
            .build();

    AdminClientSettings adminClientSettings =
        AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();

    // If a reservation has topics attached, you must delete the topics before deleting
    // the reservation.
    try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
      adminClient.deleteReservation(reservationPath).get();
      System.out.println(reservationPath + " deleted successfully.");
    } catch (ExecutionException e) {
      try {
        throw e.getCause();
      } catch (NotFoundException notFound) {
        System.out.println("This reservation is not found.");
      } catch (Throwable throwable) {
        throwable.printStackTrace();
      }
    }
  }
}

Python

在运行此示例之前,请按照 Pub/Sub Lite 客户端库中的 Python 设置说明进行操作。

from google.api_core.exceptions import NotFound
from google.cloud.pubsublite import AdminClient
from google.cloud.pubsublite.types import CloudRegion, ReservationPath

# TODO(developer):
# project_number = 1122334455
# cloud_region = "us-central1"
# reservation_id = "your-reservation-id"

cloud_region = CloudRegion(cloud_region)
reservation_path = ReservationPath(project_number, cloud_region, reservation_id)

client = AdminClient(cloud_region)
try:
    client.delete_reservation(reservation_path)
    print(f"{reservation_path} deleted successfully.")
except NotFound:
    print(f"{reservation_path} not found.")

后续步骤