引起:io.grpc.StatusRuntimeException:NOT_FOUND:找不到资源

Ela*_*nda 5 java publish-subscribe google-cloud-pubsub

我尝试编写pubsub的测试:

@Test
public void sendTopic() throws Exception {

    CustomSubscriber customSubscriber = new CustomSubscriber();
    customSubscriber.startAndWait();

    CustomPublisher customPublisher = new CustomPublisher();
    customPublisher.publish("123");
}
Run Code Online (Sandbox Code Playgroud)

和:

public CustomSubscriber() {
    this.subscriptionName = SubscriptionName.create(SdkServiceConfig.s.GCP_PROJECT_ID, SdkServiceConfig.s.TOPIC_ID );
    this.receiveMsgAction = (message, consumer) -> {
        // handle incoming message, then ack/nack the received message
        System.out.println("Id : " + message.getMessageId());
        System.out.println("Data : " + message.getData().toStringUtf8());
        consumer.ack();
    };
    this.afterStopAction = new ApiFutureEmpty();
}

// [TARGET startAsync()]
public void startAndWait() throws Exception {
    Subscriber subscriber = createSubscriberWithCustomCredentials();
    subscriber.startAsync();

    // Wait for a stop signal.
    afterStopAction.get();
    subscriber.stopAsync().awaitTerminated();
}
Run Code Online (Sandbox Code Playgroud)

和:

public ApiFuture<String> publish(String message) throws Exception {
    ByteString data = ByteString.copyFromUtf8(message);
    PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
    ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);

    ApiFutures.addCallback(messageIdFuture, new ApiFutureCallback<String>() {
        public void onSuccess(String messageId) {
            System.out.println("published with message id: " + messageId);
        }

        public void onFailure(Throwable t) {
            System.out.println("failed to publish: " + t);
        }
    });
    return messageIdFuture;
}

/**
 * Example of creating a {@code Publisher}.
 */
// [TARGET newBuilder(TopicName)]
// [VARIABLE "my_project"]
// [VARIABLE "my_topic"]
public void createPublisher(String projectId, String topicId) throws Exception {
    TopicName topic = TopicName.create(projectId, topicId);
    try {
        publisher = createPublisherWithCustomCredentials(topic);

    } finally {
        // When finished with the publisher, make sure to shutdown to free up resources.
        publisher.shutdown();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,我收到此错误:

Caused by: io.grpc.StatusRuntimeException: NOT_FOUND: Resource not found (resource=add-partner-request).
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

小智 0

我假设 TOPIC_ID 是您的主题的名称;您实际上需要引用订阅。您可以从 GCP 控制台轻松创建订阅,然后在 SubscriptionName.create(project,yoursubscriptionname) 中引用该名称