如何准备Pub / Sub模拟器进行测试?

问题描述 投票:0回答:1

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

得到:

export 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个回答
1
投票

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

    }

}
© www.soinside.com 2019 - 2024. All rights reserved.