[英]io.grpc.StatusRuntimeException: UNIMPLEMENTED: Method not found
[英]Caused by: io.grpc.StatusRuntimeException: NOT_FOUND: Resource not found
我嘗試編寫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).
我錯過了什么?
無論什么實體被命名為“add-partner-request”都沒有成功創建或不屬於該項目。 如果“add-partner-request”是主題,那么您實際上需要創建主題; TopicName.create(projectId, topicId)
行不足以創建主題本身。 通常,可以在Cloud控制台的Cloud Pub / Sub部分中創建主題,或者通過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!");
}
我假設TOPIC_ID是你主題的名字; 你實際上需要引用訂閱。 您可以從GCP控制台輕松創建訂閱,然后在SubscriptionName.create(project,yoursubscriptionname)中引用該名稱
我認為您忘記使用以下名稱“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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.