如何准备发布/订阅模拟器进行测试?

时间:2019-03-05 12:29:04

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

我启动了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

2 个答案:

答案 0 :(得分:0)

您要在第二个命令中使用项目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());

    }

}

答案 1 :(得分:0)

与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文档的其余部分。