技能创建器
skill-creator
创建、改进和评估自定义技能,把专业流程转化为可重复使用的 AI 能力。
google-pubsub【数据与研究】Google Cloud Pub/Sub messaging service. Covers topics, subscriptions, push/pull delivery, and dead-letter handling. Use for GCP-native event-driven architectures and real-time analytics. USE WHEN: user mentions "google pub/sub", "pubsub", "gcp messaging", "push subscription", "pull subscription", asks about "cloud pub/sub", "ordering keys", "bigquer…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
name: google-pubsub description: | Google Cloud Pub/Sub messaging service. Covers topics, subscriptions, push/pull delivery, and dead-letter handling. Use for GCP-native event-driven architectures and real-time analytics.
USE WHEN: user mentions "google pub/sub", "pubsub", "gcp messaging", "push subscription", "pull subscription", asks about "cloud pub/sub", "ordering keys", "bigquery subscription"
sqs; Azure-native - use azure-service-bus; event streaming - use kafka or Dataflow; on-premise - use rabbitmq or activemq; multi-cloud - use kafka or pulsar
allowed-tools: Read, Grep, Glob, Write, EditFull Reference: See advanced.md for Java/Python/Go producer patterns, Spring Cloud GCP consumer, push subscription handlers, and Terraform for DLT, IAM, VPC Service Controls, and monitoring alerts.
Deep Knowledge: Use
mcp__documentation__fetch_docswith technology:google-pubsubfor comprehensive documentation.
# docker-compose.yml
services:
pubsub:
image: gcr.io/google.com/cloudsdktool/google-cloud-cli:emulators
command: gcloud beta emulators pubsub start --host-port=0.0.0.0:8085
ports:
- "8085:8085"
docker-compose up -d
# Set emulator env
export PUBSUB_EMULATOR_HOST=localhost:8085
# Create topic and subscription
gcloud pubsub topics create orders
gcloud pubsub subscriptions create order-processor --topic=orders
# Test
gcloud pubsub topics publish orders --message='{"id":"123"}'
gcloud pubsub subscriptions pull order-processor --auto-ack
| Concept | Description |
|---|---|
| Topic | Named resource for publishing messages |
| Subscription | Named resource for receiving messages |
| Publisher | Sends messages to topic |
| Subscriber | Receives messages from subscription |
| Ack Deadline | Time to acknowledge before redelivery |
| Message Retention | How long unacked messages are kept |
| Type | Description | Use Case |
|---|---|---|
| Pull | Subscriber requests messages | Batch processing, variable load |
| Push | Pub/Sub sends to endpoint | Serverless, webhooks |
| BigQuery | Direct export to BigQuery | Analytics pipelines |
| Cloud Storage | Direct export to GCS | Data archival |
Publisher ──▶ Topic ──▶ Subscription 1 (Pull) ──▶ Subscriber
──▶ Subscription 2 (Push) ──▶ Cloud Run
──▶ Subscription 3 ──▶ BigQuery
import { PubSub } from '@google-cloud/pubsub';
const pubsub = new PubSub({
projectId: 'my-project',
});
const topic = pubsub.topic('orders');
// Publish single message
const messageId = await topic.publishMessage({
data: Buffer.from(JSON.stringify(order)),
attributes: {
'correlation-id': correlationId,
'order-type': order.type,
},
orderingKey: order.customerId, // For ordered delivery
});
// Batch publishing (automatic batching)
const publishOptions = {
batching: {
maxMessages: 100,
maxMilliseconds: 10,
},
};
const batchTopic = pubsub.topic('orders', publishOptions);
const subscription = pubsub.subscription('order-processor', {
flowControl: {
maxMessages: 100,
maxExtensionMinutes: 10,
},
ackDeadline: 30,
});
const messageHandler = async (message) => {
try {
const order = JSON.parse(message.data.toString());
const correlationId = message.attributes['correlation-id'];
await processOrder(order);
message.ack();
} catch (error) {
console.error('Processing failed:', error);
message.nack(); // Will be redelivered
}
};
subscription.on('message', messageHandler);
subscription.on('error', (error) => console.error('Subscription error:', error));
// Graceful shutdown
process.on('SIGTERM', async () => {
await subscription.close();
});
# Create DLT
gcloud pubsub topics create orders-dlq
gcloud pubsub subscriptions create orders-dlq-sub --topic=orders-dlq
# Create subscription with DLT
gcloud pubsub subscriptions create order-processor \
--topic=orders \
--dead-letter-topic=orders-dlq \
--max-delivery-attempts=5
Use alternative messaging solutions when:
| Anti-Pattern | Why It's Bad | Solution |
|---|---|---|
| No dead letter topic | Failed messages lost | Configure DLT for all subscriptions |
| Short ack deadline | Duplicate processing | Set deadline > max processing time |
| No retry policy | Immediate redelivery on failure | Configure exponential backoff |
| Synchronous publish | Poor throughput | Use batching and async publish |
| Pull without flow control | Consumer overwhelmed | Set max_messages limit |
| No message ordering when needed | Out of order processing | Use ordering keys |
| Large message payloads | Higher costs, poor performance | Use Cloud Storage with reference |
| No IAM least privilege | Security risk | Use service accounts with minimal roles |
| Issue | Likely Cause | Fix |
|---|---|---|
| Messages not received | No subscription or wrong topic | Create subscription, verify topic |
| Duplicate messages | Ack deadline expired | Increase ack deadline or process faster |
| Messages in DLT | Max delivery attempts exceeded | Check processing logic, review DLT |
| Permission denied | Missing IAM roles | Grant Publisher/Subscriber roles |
| Ordering not working | No ordering key or wrong subscription | Set ordering key, enable message ordering |
| High latency | Batching delay or network | Reduce batch delay, check network |
| Push subscription failing | Endpoint down or auth failure | Check endpoint health, verify auth |
| Backlog growing | Slow consumers | Add consumers or optimize processing |
| Metric | Alert Threshold |
|---|---|
| num_undelivered_messages | > 10000 |
| oldest_unacked_message_age | > 3600s |
| num_outstanding_messages | > 10000 |
| dead_letter_message_count | > 0 |
| publish_latencies | p99 > 1s |
Deep Knowledge: Use
mcp__documentation__fetch_docswith technology:google-pubsubfor comprehensive documentation.
Available topics: basics, producers, consumers, production