【问题标题】:How to prepare Pub/Sub emulator for tests?如何准备 Pub/Sub 模拟器进行测试?
【发布时间】:2019-07-26 22:08:22
【问题描述】:

我启动 gcloud sdk docker

docker run -ti --rm --expose=8085 -p 8085:8085 google/cloud-sdk:latest

然后我运行:

gcloud beta emulators pubsub start --project=my-project  --host-port=0.0.0.0:8085

然后停止服务器然后:

gcloud beta emulators pubsub env-init

给予:

导出 PUBSUB_EMULATOR_HOST=0.0.0.0:8085

但没有项目 ID。如何为测试设置项目?如何创建主题和订阅?

版本:

gcloud  version

给予:

Google Cloud SDK 236.0.0
...
pubsub-emulator 2019.02.22

【问题讨论】:

    标签: google-cloud-platform gcloud google-cloud-pubsub


    【解决方案1】:

    我们在使用 Pub/Sub 模拟器时发现的一个可能的问题是文档说:

    在这种情况下,项目 ID 可以是任何有效字符串;它不是 需要代表一个真正的 GCP 项目,因为 Cloud Pub/Sub 模拟器在本地运行。

    此上下文中的

    任何有效字符串不是任何字符串,而是一个有效字符串,这意味着它看起来像一个有效的 GC 项目 ID。在我们的测试中,这是专门匹配 REGEX 模式的字符串:

    /^[a-z]-[a-z]-\d{6}$/

    一旦提供了有效项目 ID,模拟器就会像宣传的那样工作。如果您在 GC 中有一个沙盒项目,您可以使用该 ID,或者您可以自己创建与该模式匹配的 ID。一旦你做到了这一点,你就可以关注Testing apps locally with the emulator 文档的其余部分。

    【讨论】:

      【解决方案2】:

      您将在第二个命令中使用项目 my-project 启动 pubsub 模拟器。一旦运行,不要杀死它,让它运行。

      要创建主题和订阅,您必须使用其中一个 SDK。我使用 Java SDK 创建了一个演示项目:https://github.com/nhartner/pubsub-emulator-demo/

      相关代码是这样的:

      @Component
      public class TestPubSubConfig {
      
          private final TransportChannelProvider channelProvider;
          private final CredentialsProvider credentialsProvider;
      
          private String projectId;
          private String topicName = "test-topic";
          private String subscriptionName = "test-subscription";
      
          TestPubSubConfig(@Autowired @Value("${spring.cloud.gcp.pubsub.emulator-host}") String emulatorHost,
                           @Autowired @Value("${spring.cloud.gcp.project-id}") String projectId) throws IOException {
              this.projectId = projectId;
              ManagedChannel channel = ManagedChannelBuilder.forTarget(emulatorHost).usePlaintext().build();
              channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));
              credentialsProvider = NoCredentialsProvider.create();
              createTopic(topicName);
              createSubscription(topicName, subscriptionName);
          }
      
          @Bean
          public Publisher testPublisher() throws IOException {
              return Publisher.newBuilder(ProjectTopicName.of(projectId, topicName))
                      .setChannelProvider(channelProvider)
                      .setCredentialsProvider(credentialsProvider)
                      .build();
          }
      
          private void createSubscription(String topicName, String subscriptionName) throws IOException {
              ProjectTopicName topic = ProjectTopicName.of(projectId, topicName);
              ProjectSubscriptionName subscription = ProjectSubscriptionName.of(projectId, subscriptionName);
      
              try {
                  subscriptionAdminClient()
                          .createSubscription(subscription, topic, PushConfig.getDefaultInstance(), 100);
              }
              catch (AlreadyExistsException e) {
                  // this is fine, already created
              }
          }
      
          private void createTopic(String topicName) throws IOException {
              ProjectTopicName topic = ProjectTopicName.of(projectId, topicName);
              try {
                  topicAdminClient().createTopic(topic);
              }
              catch (AlreadyExistsException e) {
                  // this is fine, already created
              }
          }
      
          private TopicAdminClient topicAdminClient() throws IOException {
              return TopicAdminClient.create(
                      TopicAdminSettings.newBuilder()
                              .setTransportChannelProvider(channelProvider)
                              .setCredentialsProvider(credentialsProvider).build());
          }
      
      
          private SubscriptionAdminClient subscriptionAdminClient() throws IOException {
              return SubscriptionAdminClient.create(SubscriptionAdminSettings.newBuilder()
                      .setTransportChannelProvider(channelProvider)
                      .setCredentialsProvider(credentialsProvider)
                      .build());
      
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2015-10-22
        • 2021-11-26
        • 2021-07-29
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        • 2015-07-27
        • 1970-01-01
        • 2016-08-14
        相关资源
        最近更新 更多