104 lines
2.8 KiB
Go
104 lines
2.8 KiB
Go
|
|
// 验证 Pulsar 消息的简单脚本
|
|||
|
|
// 使用方法: go run scripts/verify_pulsar_messages.go
|
|||
|
|
|
|||
|
|
package main
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"fmt"
|
|||
|
|
"log"
|
|||
|
|
"time"
|
|||
|
|
|
|||
|
|
"github.com/apache/pulsar-client-go/pulsar"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
const (
|
|||
|
|
pulsarURL = "pulsar://localhost:6650"
|
|||
|
|
topic = "persistent://public/default/operation"
|
|||
|
|
timeout = 10 * time.Second
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
func main() {
|
|||
|
|
fmt.Println("🔍 Pulsar Message Verification Tool")
|
|||
|
|
fmt.Println("=====================================")
|
|||
|
|
fmt.Printf("Pulsar URL: %s\n", pulsarURL)
|
|||
|
|
fmt.Printf("Topic: %s\n", topic)
|
|||
|
|
fmt.Println()
|
|||
|
|
|
|||
|
|
// 创建 Pulsar 客户端
|
|||
|
|
client, err := pulsar.NewClient(pulsar.ClientOptions{
|
|||
|
|
URL: pulsarURL,
|
|||
|
|
})
|
|||
|
|
if err != nil {
|
|||
|
|
log.Fatalf("❌ Failed to create Pulsar client: %v", err)
|
|||
|
|
}
|
|||
|
|
defer client.Close()
|
|||
|
|
fmt.Println("✅ Connected to Pulsar")
|
|||
|
|
|
|||
|
|
// 创建消费者(使用唯一的 subscription)
|
|||
|
|
subName := fmt.Sprintf("verify-sub-%d", time.Now().Unix())
|
|||
|
|
consumer, err := client.Subscribe(pulsar.ConsumerOptions{
|
|||
|
|
Topic: topic,
|
|||
|
|
SubscriptionName: subName,
|
|||
|
|
Type: pulsar.Shared,
|
|||
|
|
// 从最早的未确认消息开始读取
|
|||
|
|
SubscriptionInitialPosition: pulsar.SubscriptionPositionEarliest,
|
|||
|
|
})
|
|||
|
|
if err != nil {
|
|||
|
|
log.Fatalf("❌ Failed to create consumer: %v", err)
|
|||
|
|
}
|
|||
|
|
defer consumer.Close()
|
|||
|
|
fmt.Printf("✅ Consumer created: %s\n\n", subName)
|
|||
|
|
|
|||
|
|
// 接收消息
|
|||
|
|
fmt.Println("📩 Listening for messages (timeout: 10s)...")
|
|||
|
|
fmt.Println("----------------------------------------")
|
|||
|
|
|
|||
|
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
|||
|
|
defer cancel()
|
|||
|
|
|
|||
|
|
messageCount := 0
|
|||
|
|
for {
|
|||
|
|
msg, err := consumer.Receive(ctx)
|
|||
|
|
if err != nil {
|
|||
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|||
|
|
break
|
|||
|
|
}
|
|||
|
|
log.Printf("⚠️ Error receiving message: %v", err)
|
|||
|
|
continue
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
messageCount++
|
|||
|
|
fmt.Printf("\n📨 Message #%d:\n", messageCount)
|
|||
|
|
fmt.Printf(" Key: %s\n", msg.Key())
|
|||
|
|
fmt.Printf(" Payload Size: %d bytes\n", len(msg.Payload()))
|
|||
|
|
fmt.Printf(" Publish Time: %v\n", msg.PublishTime())
|
|||
|
|
fmt.Printf(" Topic: %s\n", msg.Topic())
|
|||
|
|
fmt.Printf(" Message ID: %v\n", msg.ID())
|
|||
|
|
|
|||
|
|
// 确认消息
|
|||
|
|
consumer.Ack(msg)
|
|||
|
|
|
|||
|
|
// 最多显示 10 条消息
|
|||
|
|
if messageCount >= 10 {
|
|||
|
|
fmt.Println("\n⚠️ Reached 10 messages limit, stopping...")
|
|||
|
|
break
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
fmt.Println("\n========================================")
|
|||
|
|
if messageCount == 0 {
|
|||
|
|
fmt.Println("❌ No messages found in Pulsar")
|
|||
|
|
fmt.Println("\nPossible reasons:")
|
|||
|
|
fmt.Println(" 1. No operations have been published yet")
|
|||
|
|
fmt.Println(" 2. All messages have been consumed by other consumers")
|
|||
|
|
fmt.Println(" 3. Wrong topic name")
|
|||
|
|
fmt.Println("\nTo test, run the E2E test:")
|
|||
|
|
fmt.Println(" go test ./api/persistence -v -run TestE2E_DBAndTrustlog_WithPulsarConsumer")
|
|||
|
|
} else {
|
|||
|
|
fmt.Printf("✅ Found %d messages in Pulsar\n", messageCount)
|
|||
|
|
}
|
|||
|
|
fmt.Println("========================================")
|
|||
|
|
}
|
|||
|
|
|