golang整合kafka之生产者和消费者案例

docker 本地环境搭建

docker-compose.yaml内容如下,创建完成后,使用docker-compose up -d 命令启动。

version: '3.8'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    container_name: zookeeper
    restart: always
    ports:
      - "2181:2181"
  kafka:
    image: wurstmeister/kafka
    container_name: kafka
    restart: always
    depends_on:
      - zookeeper
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: 192.168.18.135
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_AUTO_CREATE_TOPICS_ENABLE: "false"

生产者和消费者案例

  • 生产者
package main

import (
	"fmt"
	"github.com/Shopify/sarama"
)

你可能感兴趣的:(kafka,golang,kafka)