【问题标题】:Caused by: io.grpc.StatusRuntimeException: NOT_FOUND: Resource not found引起:io.grpc.StatusRuntimeException:NOT_FOUND:找不到资源
【发布时间】:2017-06-05 13:12:20
【问题描述】:

我尝试写一个pubsub的测试:

@Test
public void sendTopic() throws Exception {

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

    CustomPublisher customPublisher = new CustomPublisher();
    customPublisher.publish("123");
}

和:

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();
}

和:

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();
    }
}

当我运行代码时,我得到了这个错误:

Caused by: io.grpc.StatusRuntimeException: NOT_FOUND: Resource not found (resource=add-partner-request).

我错过了什么?

【问题讨论】:

    标签: java publish-subscribe google-cloud-pubsub


    【解决方案1】:

    任何名为“add-partner-request”的实体均未成功创建或不属于该项目。如果 "add-partner-request" 是主题,那么您实际上需要创建主题; TopicName.create(projectId, topicId) 行不足以创建主题本身。通常,人们会在 Cloud Pub/Sub portion of the Cloud console 或通过 gcloud 命令创建主题,例如,

    gcloud pubsub topics create add-partner-request
    

    确保您在控制台中登录的项目是代码中使用的项目。您还应该在通过--project 标志创建主题时明确设置项目或验证默认项目是否正确:

    gcloud config list --format='text(core.project)'
    

    对于测试,通常在代码中创建和删除。例如,创建一个主题:

    Topic topic = null;
    ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
    TopicAdminClient topicAdminClient = TopicAdminClient.create();
    try {
      topic = topicAdminClient.createTopic(topicName);
    } catch (APIException e) {
      System.out.println("Issue creating topic!");
    }
    

    如果“add-partner-request”是订阅名称,那么同样适用。 gcloud 命令会有所改变:

    gcloud pubsub subscriptions create add-partner-request --topic=<topic name>
    

    在 Java 中创建订阅的命令如下:

    Subscription subscription = null;
    ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
    SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create();
    try {
      subscription = subscriptionAdminClient.createSubscription(
              subscriptionName, topicName, PushConfig.newBuilder().build(), 600);
    } catch (APIException e) {
      System.out.println("Issue creating topic!");
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        我认为您忘记在项目中创建一个名为“add-partner-request”的主题。 您可以使用以下代码创建它:

        try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
          // projectId <=  unique project identifier, eg. "my-project-id"
          TopicName topicName = TopicName.create(projectId, "add-partner-request");
          Topic topic = topicAdminClient.createTopic(topicName);
          return topic;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-01-28
          • 2015-05-03
          • 2013-01-03
          • 2014-03-20
          • 2015-04-13
          • 2021-07-06
          • 1970-01-01
          相关资源
          最近更新 更多