在云主机上调用docker-compose脚本:
version: '3'
services:
zookeeper:
image: zookeeper
ports:
- "2182:2181"
networks:
- hbl_net
kafka:
image: wurstmeister/kafka
depends_on:
- zookeeper
links:
- zookeeper
networks:
- hbl_net
ports:
- "9095:9092"
environment:
KAFKA_BROKER_ID: 5
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://124.70.146.126:9095 #宿主机监听端口
volumes:
- /var/run/docker.sock:/var/run/docker.sock
networks:
hbl_net:
driver: bridge # 生成一个桥接网络,用于容器内部通信,注意实际生成的网络名称会带有docker-compose.yml文件所在文件夹的前缀,比如我的.yml文件放在了hbl文件夹下,所以执行后生>成的网络名为hbl_hbl_net
# external: true 如果外部已有网络就用这个配置
go mod里面指定版本:require github.com/Shopify/sarama v1.19.0
版本高了Windows用不了
生产者代码:
package main
import (
"fmt"
"github.com/Shopify/sarama"
)
func main() {
producer_test()
}
func producer_test() {
fmt.Printf("producer_test\n")
config := sarama.NewConfig()
config.Producer.RequiredAcks = sarama.WaitForAll
config.Producer.Partitioner = sarama.NewRandomPartitioner
config.Producer.Return.Successes = true
config.Producer.Return.Errors = true
config.Version = sarama.V0_11_0_2
producer, err := sarama.NewAsyncProducer([]string{"124.70.146.126:9095"}, config)
if err != nil {
fmt.Printf("producer_test create producer error :%s\n", err.Error())
return
}
defer producer.AsyncClose()
// send message
msg := &sarama.ProducerMessage{
Topic: "test",
Key: sarama.StringEncoder("go_test"),
}
value := "this is message"
for {
fmt.Scanln(&value)
msg.Value = sarama.ByteEncoder(value)
fmt.Printf("input [%s]\n", value)
// send to chain
producer.Input() <- msg
select {
case suc := <-producer.Successes():
fmt.Printf("offset: %d, timestamp: %s", suc.Offset, suc.Timestamp.String())
case fail := <-producer.Errors():
fmt.Printf("err: %s\n", fail.Err.Error())
}
}
}
消费者代码:
package main
import (
"fmt"
"github.com/Shopify/sarama"
)
func main() {
consumer_test()
}
func consumer_test() {
fmt.Printf("consumer_test")
config := sarama.NewConfig()
config.Consumer.Return.Errors = true
config.Version = sarama.V0_11_0_2
// consumer
consumer, err := sarama.NewConsumer([]string{"124.70.146.126:9095"}, config)
if err != nil {
fmt.Printf("consumer_test create consumer error %s\n", err.Error())
return
}
defer consumer.Close()
partition_consumer, err := consumer.ConsumePartition("test", 0, sarama.OffsetOldest)
if err != nil {
fmt.Printf("try create partition_consumer error %s\n", err.Error())
return
}
defer partition_consumer.Close()
for {
select {
case msg := <-partition_consumer.Messages():
fmt.Printf("msg offset: %d, partition: %d, timestamp: %s, value: %s\n",
msg.Offset, msg.Partition, msg.Timestamp.String(), string(msg.Value))
case err := <-partition_consumer.Errors():
fmt.Printf("err :%s\n", err.Error())
}
}
}