import (
"os"
"time"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func InitZapLogger(flieName string) *zap.SugaredLogger {
writeSyncer := getLogWriter(flieName)
encoder := getTimeInfoEncoder()
core := zapcore.NewCore(encoder, writeSyncer, zapcore.DebugLevel)
// Logger = zap.New(core)
// 添加将调用函数信息记录到日志中的功能
zapLogger := zap.New(core, zap.AddCaller())
sugarLogger := zapLogger.Sugar()
return sugarLogger
}
// 日志写入文件
func getLogWriter(fileName string) zapcore.WriteSyncer {
file := CreateFileAll(fileName)
return zapcore.AddSync(file)
}
// 编码器-更改时间编码并添加调用者详细信息
func getTimeInfoEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
return zapcore.NewConsoleEncoder(encoderConfig)
}
测试修改topic就可以
func getLocalConsumerPulsarClient() pulsar.Client {
localPulsarClient, err := pulsar.NewClient(pulsar.ClientOptions{
URL: "pulsar://xxxxx",
OperationTimeout: 30 * time.Second,
ConnectionTimeout: 30 * time.Second,
})
if err != nil {
log.Fatalf("Could not instantiate Pulsar client [PulsarClient]: %v", err)
}
return localPulsarClient
}
func createCosumer(client pulsar.Client) pulsar.Consumer {
localPulsarconsumer, err := client.Subscribe(pulsar.ConsumerOptions{
Topic: "non-persistent://xxxxx",
SubscriptionName: "test-sub",
Type: pulsar.Exclusive,
})
if err != nil {
log.Fatal(err)
}
return localPulsarconsumer
}
func localConsumerTask() {
client := getLocalConsumerPulsarClient()
consumer := createCosumer(client)
logger := InitZapLogger(fmt.Sprintf("./log/consumer-dev-v1-%s.txt", time.Now().Format("2006-01-02_15-04-05")))
pulsarCusmer(consumer, logger)
}
var (
LocalPulsarClient pulsar.Client // 常规配置
LocalTestPulsarProducer pulsar.Producer // 默认配置
)
func getLocalPulsarClient() {
var err error
LocalPulsarClient, err = pulsar.NewClient(pulsar.ClientOptions{
URL: "pulsar://xxxx",
OperationTimeout: 30 * time.Second,
ConnectionTimeout: 30 * time.Second,
})
if err != nil {
log.Fatalf("Could not instantiate Pulsar client [PulsarClient]: %v", err)
}
}
func createLocalProducer(client pulsar.Client) {
var err error
LocalTestPulsarProducer, err = client.CreateProducer(pulsar.ProducerOptions{
Topic: "non-persistent://xxxx",
})
if err != nil {
log.Fatal(err)
}
}
func pulsarLocalPublish(producer pulsar.Producer, msg []byte, logger *zap.SugaredLogger) {
_, err := producer.Send(context.Background(), &pulsar.ProducerMessage{
Payload: msg,
})
logger.Info(string(msg))
if err != nil {
fmt.Println("Failed to publish message", err)
}
}
// 循环发送
func localCycPublishTask() {
getLocalPulsarClient()
createLocalProducer(LocalPulsarClient)
logger := InitZapLogger(fmt.Sprintf("./log/localCycPublish-dev-v1-%s.txt", time.Now().Format("2006-01-02_15-04-05")))
for i := 0; i < 1000; i++ {
msg := fmt.Sprintf("send-%d", i)
pulsarLocalPublish(LocalTestPulsarProducer, []byte(msg), logger)
}
}
结论:这样的代码下,非持久化发送之后,消费者接收会出现接收不全的情况。
持久化发送就是正常的