module declares its path as: github.com/IBM/sarama but was required as: gith

简介: module declares its path as: github.com/IBM/sarama but was required as: gith

看来在您当前的项目中已经存在一个模块,声明其路径为 github.com/IBM/sarama,但我们需要使用的是 github.com/Shopify/sarama。这可能会导致依赖冲突。要解决这个问题,您可以采取以下步骤:

移除或更新现有的 go.mod 文件: 确保您的项目使用的是正确的 sarama 库。

更新 go.mod 文件: 确保您的 go.mod 文件引用的是 github.com/Shopify/sarama。您可以手动编辑 go.mod 文件,或者使用 go get 命令。

首先,删除现有的 go.mod 和 go.sum 文件:

rm go.mod go.sum


然后,重新初始化 Go 模块并获取正确的 sarama 库:

go mod init your_module_name
go get github.com/Shopify/sarama


以下是一个完整的示例,包括 go.mod 文件和 main.go 文件。

1. 创建 go.mod 文件
go mod init your_module_name


然后在 go.mod 文件中确保引用 github.com/Shopify/sarama:

module your_module_name
go 1.18
require github.com/Shopify/sarama v1.34.1


2. 创建 main.go 文件

package main

import (
  "context"
  "fmt"
  "log"
  "os"
  "os/signal"

  "github.com/Shopify/sarama"
)

func main() {
  // Kafka broker addresses
  brokers := []string{"localhost:9092"}
  // Kafka topic
  topic := "your_topic"
  // Consumer group ID
  group := "your_group"

  // Sarama logger configuration
  sarama.Logger = log.New(os.Stdout, "[sarama] ", log.LstdFlags)

  // Sarama configuration
  config := sarama.NewConfig()
  config.Version = sarama.V0_9_0_0 // Kafka version
  config.Consumer.Group.Rebalance.Strategy = sarama.BalanceStrategyRoundRobin
  config.Consumer.Offsets.Initial = sarama.OffsetOldest

  // Create new consumer group
  consumerGroup, err := sarama.NewConsumerGroup(brokers, group, config)
  if err != nil {
    log.Fatalf("Error creating consumer group: %v", err)
  }
  defer func() {
    if err := consumerGroup.Close(); err != nil {
      log.Fatalf("Error closing consumer group: %v", err)
    }
  }()

  // Create a new consumer instance
  consumer := Consumer{
    ready: make(chan bool),
  }

  // Handle termination signals
  ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
  defer cancel()

  // Run the consumer group in a separate goroutine
  go func() {
    for {
      if err := consumerGroup.Consume(ctx, []string{topic}, &consumer); err != nil {
        log.Fatalf("Error consuming messages: %v", err)
      }
      // Check if context was canceled, signaling termination
      if ctx.Err() != nil {
        return
      }
      consumer.ready = make(chan bool)
    }
  }()

  // Wait for consumer to be ready
  <-consumer.ready
  log.Println("Sarama consumer up and running! Press Ctrl+C to stop.")

  // Wait for termination signal
  <-ctx.Done()
  log.Println("Terminating Sarama consumer...")
}

// Consumer represents a Sarama consumer group consumer
type Consumer struct {
  ready chan bool
}

// Setup is run at the beginning of a new session, before ConsumeClaim
func (consumer *Consumer) Setup(sarama.ConsumerGroupSession) error {
  // Mark the consumer as ready
  close(consumer.ready)
  return nil
}

// Cleanup is run at the end of a session, once all ConsumeClaim goroutines have exited
func (consumer *Consumer) Cleanup(sarama.ConsumerGroupSession) error {
  return nil
}

// ConsumeClaim must start a consumer loop of ConsumerGroupClaim's Messages().
func (consumer *Consumer) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
  for message := range claim.Messages() {
    log.Printf("Message claimed: value = %s, timestamp = %v, topic = %s", string(message.Value), message.Timestamp, message.Topic)
    session.MarkMessage(message, "")
  }
  return nil
}

运行代码

确保 Kafka 服务器已启动,并且指定的主题存在,然后运行以上 Go 程序来消费 Kafka 消息。

go run main.go


这段代码将在控制台中打印消费到的 Kafka 消息的值、时间戳和主题信息。这样就可以避免模块路径冲突,并确保使用正确的 sarama 库。

相关文章
|
消息中间件 Kafka Go
使用github.com/IBM/sarama 编写消费kafka的功能
使用github.com/IBM/sarama 编写消费kafka的功能
no required module provides package github.com/gorilla/mux
no required module provides package github.com/gorilla/mux
605 0
|
人工智能 负载均衡 数据中心
GitHub 开源负载均衡组件 GLB Director;IBM 利用 AI 开发黑客程序,可攻破最高级别防御措施
1、GitHub 开源负载均衡组件 GLB Director GitHub 发布了开源负载均衡组件 GitHub Load Balancer Director(GLB) Director,GLB 是 GitHub 针对裸机数据中心的可扩展负载均衡解决方案,它支持大多数 GitHub 的对外服务,并且还为诸如高可用 MySQL 集群这样最为关键的内部系统提供负载均衡服务。
2819 0
|
JSON Kubernetes 安全
找到啦,我们已上车,Github 27000+ star,研发团队必备开源工具项目,真丝滑!!!
Trivy 是一款高效灵活的开源安全扫描工具,支持容器镜像、文件系统、Kubernetes 等多目标扫描,具备快速、易用、集成性强等特点,适用于 DevSecOps 全流程安全检测。
442 0
|
11月前
|
人工智能 JavaScript 前端开发
Github 2024-10-28 开源项目周报 Top15
本周GitHub热门项目涵盖Svelte、Open Interpreter、PowerShell等,涉及Web开发、AI助手、自动化工具等领域,Python、JavaScript为主流语言,展现开源技术活跃生态。(239字)
957 19
|
11月前
|
人工智能 JavaScript 前端开发
Github 2024-11-04 开源项目周报 Top14
本周GitHub热门项目涵盖屏幕截图转代码、网页监控、低代码开发等。Python与TypeScript主导,亮点项目包括AI生成代码工具、开源社交应用Bluesky及机器人框架LeRobot,展现AI与自动化技术的快速发展趋势。
546 15
|
11月前
|
人工智能 JavaScript Docker
Github 2024-11-11 开源项目周报 Top15
本周GitHub热门项目涵盖多领域:Python与TypeScript领跑,包括屏幕截图转代码、本地文件共享、PDF处理、AI开发代理等。亮点项目如screenshot-to-code、LocalSend、OpenHands及Diagrams,兼具创新与实用性,广受开发者关注。
972 13
|
11月前
|
人工智能 算法 JavaScript
Github 2024-10-14 开源项目周报 Top14
本周GitHub热门项目共14个,Python项目占7席。涵盖算法实现、生成式AI、金融分析、目标检测等领域,包括TheAlgorithms系列、OpenBB金融平台、Ultralytics YOLO11、Manim动画框架等,展现开源技术多元发展态势。
415 8
|
11月前
|
人工智能 Rust JavaScript
Github 2024-10-07 开源项目周报 Top15
本周GitHub热门项目共15个,Python项目占比最高达7个。榜首为Python算法实现集合TheAlgorithms/Python,Star数超17万;其他亮点包括Godot游戏引擎、OpenBB金融平台、ToolJet低代码框架及新兴AI相关项目如Crawl4AI、Llama Stack等,涵盖游戏、金融、AI、理财等多个领域。
456 4
|
11月前
|
人工智能 Rust 算法
Github 2024-09-30 开源项目周报 Top15
本周GitHub热门项目揭晓:Python主导,AutoGPT居首,涵盖AI、编程、数学动画等领域,助力开发者探索前沿技术。
414 4