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 库。

相关文章
|
12月前
|
消息中间件 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
429 0
|
人工智能 负载均衡 数据中心
GitHub 开源负载均衡组件 GLB Director;IBM 利用 AI 开发黑客程序,可攻破最高级别防御措施
1、GitHub 开源负载均衡组件 GLB Director GitHub 发布了开源负载均衡组件 GitHub Load Balancer Director(GLB) Director,GLB 是 GitHub 针对裸机数据中心的可扩展负载均衡解决方案,它支持大多数 GitHub 的对外服务,并且还为诸如高可用 MySQL 集群这样最为关键的内部系统提供负载均衡服务。
2558 0
|
1月前
|
JSON Kubernetes 安全
找到啦,我们已上车,Github 27000+ star,研发团队必备开源工具项目,真丝滑!!!
Trivy 是一款高效灵活的开源安全扫描工具,支持容器镜像、文件系统、Kubernetes 等多目标扫描,具备快速、易用、集成性强等特点,适用于 DevSecOps 全流程安全检测。
|
30天前
|
人工智能 编解码 JSON
不看后悔!GitHub 开源 MultiTalk .8k star 强大的人语音+图像绑定项目
MultiTalk 是 GitHub 上的开源项目,具备音频驱动、多人对话视频生成功能。支持多路音频与图像绑定,实现高同步唇动与角色互动,适用于教学、虚拟人及短视频创作,已获 8k 星标。
150 0
|
30天前
|
安全 数据可视化 项目管理
精品,Github 5000+ star,小型研发团队必备商业开源项目
DooTask 是一款开源在线项目任务管理工具,具备文档协作、流程图、任务分发、IM沟通等功能,支持私有部署与数据加密,已在 GitHub 获得 5000+ 星标,适合中小团队提升协作效率。
|
30天前
|
人工智能 自然语言处理 JavaScript
Github又一AI黑科技项目,打造全栈架构,只需一个统一框架?
Motia 是一款现代化后端框架,融合 API 接口、后台任务、事件系统与 AI Agent,支持 JavaScript、TypeScript、Python 多语言协同开发。它提供可视化 Workbench、自动观测追踪、零配置部署等功能,帮助开发者高效构建事件驱动的工作流,显著降低部署与运维成本,提升 AI 项目落地效率。
157 0
|
7月前
|
开发工具 git C++
【够用就好002】外行第一次发布github项目仓库
#deepseek#自学 记录外行学习代码的历程 今天是上传自己的工具代码到github仓库,一直以来是伸手党克隆别人的项目,今天在deepseek的辅导下上传自己的内容。
|
1月前
|
数据采集 人工智能 搜索推荐
完蛋啦,爆火Github项目,用微信聊天记录打造专属AI数字分身,我都不敢相信!!
WeClone 是一个基于微信或 Telegram 聊天记录微调大语言模型的开源项目,可打造专属 AI 数字分身。支持文本、图片等多模态数据,具备语言风格迁移和语音克隆功能,实现“说话像你”的AI角色。项目提供完整训练流程,支持本地部署,保护隐私,适用于个人数字分身、纪念机器人、客服助手等场景。
211 0
|
1月前
|
存储 人工智能 自然语言处理
让你拥有一个AI大脑,这个32.1k Github项目是你不错的选择,支持PDF、Markdown、代码、视频成为你的知识内容
Quivr 是开源全栈 RAG 平台,助你打造“第二大脑”,支持多文档类型与多种 LLM,实现智能搜索与聊天。具备语义检索、本地部署、隐私保护等功能,适用于个人知识管理与企业知识库,界面简洁易用,是高效智能问答的理想选择。

热门文章

最新文章