Clawdidr
Clawdidr

Reputation: 617

Error while trying to publish to a topic using GCP Emulator

I'm trying to use GCP Pub/Sub emulator. I created topic and subscription, set PROJECT_ID variable but when I try to publish a message, I'm getting this exception:

java.util.concurrent.ExecutionException: org.springframework.cloud.gcp.pubsub.core.PubSubDeliveryException: 
Publishing to fake_facl_sellerorder_topic topic failed.; 
nested exception is com.google.api.gax.rpc.NotFoundException: 
io.grpc.StatusRuntimeException:
NOT_FOUND: Requested project not found or user does not have access to it (project=project-emulator-123). 
Make sure to specify the unique project identifier and not the Google Cloud Console display name.

As this is running only in my local, I don't understand why it says that the PROJECT_ID cannot be found or I do not have access to it.

Upvotes: 0

Views: 2104

Answers (2)

Dmytro Boichenko
Dmytro Boichenko

Reputation: 5407

I resolved the same error by setting up the PUBSUB_EMULATOR_HOST environment variable on the client side.

As for example

PUBSUB_EMULATOR_HOST=localhost:8085

Upvotes: 0

Oskar H.
Oskar H.

Reputation: 1806

This issue is already a bit old, but for anyone who comes here later. This is what I had to do:

You have to make sure you point to emulator in all the builders separately. Simple Spring Bean version below:

@Bean
public ManagedChannel managedChannel(IntegrationServiceLibraryProperties properties) {
    return ManagedChannelBuilder.forTarget("localhost:8085").usePlaintext().build();
}

@Bean
public TransportChannelProvider transportChannelProvider(ManagedChannel managedChannel) {
    return FixedTransportChannelProvider.create(
            GrpcTransportChannel.create(managedChannel));
}

@Bean
public CredentialsProvider credentialsProvider() {
    return NoCredentialsProvider.create();
}

ManagedChannel is required for the TransportChannelProvider, the NoCredentialsProvider allows us to talk to local emulator without credentials. The next step is to make sure all the builders use these beans: TopicAdminClient, SubscriptionAdminClient, Publisher and Subscriber.

@Bean
public TopicAdminClient topicAdminClient(
    CredentialsProvider credentialsProvider,
    TransportChannelProvider transportChannelProvider) throws IOException {
    return TopicAdminClient.create(
        TopicAdminSettings.newBuilder()
            .setTransportChannelProvider(transportChannelProvider)
            .setCredentialsProvider(credentialsProvider)
            .build());
}

@Bean
public SubscriptionAdminClient subscriptionAdminClient(
      CredentialsProvider credentialsProvider,
      TransportChannelProvider transportChannelProvider) throws IOException {
    return SubscriptionAdminClient.create(
        SubscriptionAdminSettings.newBuilder()
            .setTransportChannelProvider(transportChannelProvider)
            .setCredentialsProvider(credentialsProvider)
            .build());
}

public Publisher createPublisher(
      String topicName,
      CredentialsProvider credentialsProvider,
      TransportChannelProvider transportChannelProvider) throws IOException {
    return Publisher.newBuilder(topicName)
        .setCredentialsProvider(credentialsProvider)
        .setChannelProvider(transportChannelProvider)
    .build();
 }

public Subscriber createSubscriber(
      String subscriptionName,
      CredentialsProvider credentialsProvider,
      TransportChannelProvider transportChannelProvider) {
    return Subscriber
        .newBuilder(subscriptionName, (PubsubMessage message, AckReplyConsumer consumer) -> { 
      // handle message
    })
    .setChannelProvider(transportChannelProvider)
    .setCredentialsProvider(credentialsProvider)
    .build();
}

For me for example I was forgetting it in the subscriber and therefore the cycle failed.

Upvotes: 1

Related Questions