如何在代码中获取容器注册表 pubsub 通知(java 或任何其他语言)
How to get container registry pubsub notifications inside code (java or any other lang)
我的目标是在任何镜像 updated/inserted/deleted 来自注册表时,在代码中从 google 容器注册表获取通知。
我正在学习教程 - https://cloud.google.com/container-registry/docs/configuring-notifications
我可以使用 google 控制台使用命令从注册表中提取通知消息 - gcloud alpha pubsub subscriptions pull SUBSCRIPTION
但我希望这些通知消息以代码形式传递(在 java 中)。
如果有人可以向我提供对任何有帮助的文章或教程的参考。
收到 dsesto 的评论后,我添加了以下代码。当我第一次 运行 时,这段代码给了我一些信息。但在那之后,我保留了应用程序 运行ning 并尝试从容器注册表中获取 delete/insert 图像,但它没有给出任何消息。
有什么建议么。
package com.avaya.ipoffice.mcm.googleconnect;
import org.springframework.stereotype.Service;
import com.google.cloud.pubsub.v1.AckReplyConsumer;
import com.google.cloud.pubsub.v1.MessageReceiver;
import com.google.cloud.pubsub.v1.Subscriber;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.PubsubMessage;
@Service
public class RecieveMessagesUtil {
public static void main(String... args) throws Exception {
String projectId = "xxxxx";
String subscriptionId = "prashantsub";
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
// Instantiate an asynchronous message receiver
MessageReceiver receiver = new MessageReceiver() {
@Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer 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();
}
};
Subscriber subscriber = null;
try {
// Create a subscriber for "my-subscription-id" bound to the message receiver
subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
subscriber.startAsync();
// ...
} catch (Exception e) {
System.out.println("Exception while subscribing" + e);
} finally {
// stop receiving messages
if (subscriber != null) {
subscriber.stopAsync();
}
}
}
}
我建议查看 Cloud Pub/Sub documentation, particularly, the subscriber guide,它为您提供了在 Java 中接收消息所需的代码。
根据您的问题,我了解到您已经成功设置了 notifications system of Container Registry using Pub/Sub topics and subscriptions, given that you said that you are already able to retrieve the messages from your Pub/Sub subscription using command gcloud pubsub subscriptions pull
。因此,看起来您的担忧主要与以编程方式从订阅中提取消息有关。
首先,我建议您查看有关 Subscribers in Pub/Sub 的文档页面。尤其是,看看 Pull 与 Push 的比较,你会更好地了解可用的可能性,你应该首先决定是使用 Pull Subscription(例如您在调用 gcloud
命令时使用的那个,应用程序在其中发起请求)还是使用 Push订阅(其中 Pub/Sub 向订阅者应用程序发起请求,例如 App Engine 应用程序)。
一旦清楚(并假设您选择已在 gcloud
中使用的 Pull 订阅),您可以查看有关如何执行 Asynchronous Pull operations, with a Java-based example. Additionally, you can have a look at the complete subscriber example available in GitHub 的文档.
最后,您应该看看 Client Libraries documentation, more specifically the Pub/Sub Java reference,在那里您可以找到用于以编程方式与 Pub/Sub 一起工作的 Pub/Sub 客户端库的完整文档。
我的目标是在任何镜像 updated/inserted/deleted 来自注册表时,在代码中从 google 容器注册表获取通知。
我正在学习教程 - https://cloud.google.com/container-registry/docs/configuring-notifications
我可以使用 google 控制台使用命令从注册表中提取通知消息 - gcloud alpha pubsub subscriptions pull SUBSCRIPTION
但我希望这些通知消息以代码形式传递(在 java 中)。
如果有人可以向我提供对任何有帮助的文章或教程的参考。
收到 dsesto 的评论后,我添加了以下代码。当我第一次 运行 时,这段代码给了我一些信息。但在那之后,我保留了应用程序 运行ning 并尝试从容器注册表中获取 delete/insert 图像,但它没有给出任何消息。 有什么建议么。
package com.avaya.ipoffice.mcm.googleconnect;
import org.springframework.stereotype.Service;
import com.google.cloud.pubsub.v1.AckReplyConsumer;
import com.google.cloud.pubsub.v1.MessageReceiver;
import com.google.cloud.pubsub.v1.Subscriber;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.PubsubMessage;
@Service
public class RecieveMessagesUtil {
public static void main(String... args) throws Exception {
String projectId = "xxxxx";
String subscriptionId = "prashantsub";
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
// Instantiate an asynchronous message receiver
MessageReceiver receiver = new MessageReceiver() {
@Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer 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();
}
};
Subscriber subscriber = null;
try {
// Create a subscriber for "my-subscription-id" bound to the message receiver
subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
subscriber.startAsync();
// ...
} catch (Exception e) {
System.out.println("Exception while subscribing" + e);
} finally {
// stop receiving messages
if (subscriber != null) {
subscriber.stopAsync();
}
}
}
}
我建议查看 Cloud Pub/Sub documentation, particularly, the subscriber guide,它为您提供了在 Java 中接收消息所需的代码。
根据您的问题,我了解到您已经成功设置了 notifications system of Container Registry using Pub/Sub topics and subscriptions, given that you said that you are already able to retrieve the messages from your Pub/Sub subscription using command gcloud pubsub subscriptions pull
。因此,看起来您的担忧主要与以编程方式从订阅中提取消息有关。
首先,我建议您查看有关 Subscribers in Pub/Sub 的文档页面。尤其是,看看 Pull 与 Push 的比较,你会更好地了解可用的可能性,你应该首先决定是使用 Pull Subscription(例如您在调用 gcloud
命令时使用的那个,应用程序在其中发起请求)还是使用 Push订阅(其中 Pub/Sub 向订阅者应用程序发起请求,例如 App Engine 应用程序)。
一旦清楚(并假设您选择已在 gcloud
中使用的 Pull 订阅),您可以查看有关如何执行 Asynchronous Pull operations, with a Java-based example. Additionally, you can have a look at the complete subscriber example available in GitHub 的文档.
最后,您应该看看 Client Libraries documentation, more specifically the Pub/Sub Java reference,在那里您可以找到用于以编程方式与 Pub/Sub 一起工作的 Pub/Sub 客户端库的完整文档。