Poisoner
Poisoner

Reputation: 61

how to access kafka installed in docker with golang on host

I need to use golang to access kafka,so i installed a kafka & zookepper in docker.

1.here is kafka install script:

# pull images
docker pull wurstmeister/zookeeper 
docker pull wurstmeister/kafka

# run kafka & zookepper
docker run -d --name zookeeper -p 2181 -t wurstmeister/zookeeper  
docker run --name kafka -e HOST_IP=localhost -e KAFKA_ADVERTISED_PORT=9092 -e KAFKA_BROKER_ID=1 -e ZK=zk -p 9092:9092 --link zookeeper:zk -t wurstmeister/kafka  

# enter container
docker exec -it ${CONTAINER ID} /bin/bash  
cd opt/kafka_2.11-0.10.1.1/ 

# make a tpoic
bin/kafka-topics.sh --create --zookeeper zookeeper:2181 --replication-factor 1 --partitions 1 --topic mykafka 

# start a producer in terminal-1
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic mykafka 

# start another terminal-2 and start a consumer
bin/kafka-console-consumer.sh --zookeeper zookeeper:2181 --topic mykafka --from-beginning 

when i type some message in producer, the consumer will get it immediately. so i assumed that the kafka is working fine

2.Now i need to create a consumer with golang to access kafka.

here is my golang demo code:

import "github.com/bsm/sarama-cluster"
func Consumer(){
    // init (custom) config, enable errors and notifications
    config := cluster.NewConfig()
    config.Consumer.Return.Errors = true
    config.Group.Return.Notifications = true

    // init consumer
    brokers := []string{"192.168.9.100:9092"}
    topics := []string{"mykafka"}
    consumer, err := cluster.NewConsumer(brokers, "my-group-id", topics, config)
    if err != nil {
        panic(err)
    }
    defer consumer.Close()

    // trap SIGINT to trigger a shutdown.
    signals := make(chan os.Signal, 1)
    signal.Notify(signals, os.Interrupt)

    // consume messages, watch errors and notifications
    for {
        select {
        case msg, more := <-consumer.Messages():
            if more {
                fmt.Fprintf(os.Stdout, "%s/%d/%d\t%s\t%s\n", msg.Topic, msg.Partition, msg.Offset, msg.Key, msg.Value)
                consumer.MarkOffset(msg, "")    // mark message as processed
            }
        case err, more := <-consumer.Errors():
            if more {
                log.Printf("Error: %s\n", err.Error())
            }
        case ntf, more := <-consumer.Notifications():
            if more {
                log.Printf("Rebalanced: %+v\n", ntf)
            }
        case <-signals:
            return
    }
}

}

actually this demo code is copied from a github repo's demo:sarama-cluster

When running the code, i got an error:

kafka: client has run out of available brokers to talk to (Is your cluster reachable?)

i did use a port map when start kafka,but just can't access it in golang

is there a way to use curl to access kafka? i'v tried:

curl http://192.168.99.10:9092

and kafka report an error:

[2017-08-02 06:39:15,232] WARN Unexpected error from /192.168.99.1; closing connection (org.apache.kafka.common.network.Selector)
org.apache.kafka.common.network.InvalidReceiveException: Invalid receive (size = 1195725856 larger than 104857600)
    at org.apache.kafka.common.network.NetworkReceive.readFromReadableChannel(NetworkReceive.java:95)
    at org.apache.kafka.common.network.NetworkReceive.readFrom(NetworkReceive.java:75)
    at org.apache.kafka.common.network.KafkaChannel.receive(KafkaChannel.java:203)
    at org.apache.kafka.common.network.KafkaChannel.read(KafkaChannel.java:167)
    at org.apache.kafka.common.network.Selector.pollSelectionKeys(Selector.java:379)
    at org.apache.kafka.common.network.Selector.poll(Selector.java:326)
    at kafka.network.Processor.poll(SocketServer.scala:499)
    at kafka.network.Processor.run(SocketServer.scala:435)
    at java.lang.Thread.run(Thread.java:748)

BTW:

i use windows 7

dcoker machine's ip :192.168.99.100

it's drived me crazy

Is there some advice or solution? appreciate!!!

Upvotes: 0

Views: 3372

Answers (3)

Quan Van
Quan Van

Reputation: 339

If you want to create a consumer to listen a topic from Kafka, let's try that way. I used confluent-kafka-go from the tutorial: https://github.com/confluentinc/confluent-kafka-go

This is the code on main.go file:

import (
    "fmt"
    "gopkg.in/confluentinc/confluent-kafka-go.v1/kafka"
)

func main() {

    c, err := kafka.NewConsumer(&kafka.ConfigMap{
        "bootstrap.servers": "localhost",
        "group.id":          "myGroup",
        "auto.offset.reset": "earliest",
    })

    if err != nil {
        panic(err)
    }

    c.SubscribeTopics([]string{"myTopic", "^aRegex.*[Tt]opic"}, nil)

    for {
        msg, err := c.ReadMessage(-1)
        if err == nil {
            fmt.Printf("Message on %s: %s\n", msg.TopicPartition, string(msg.Value))
        } else {
            // The client will automatically try to recover from all errors.
            fmt.Printf("Consumer error: %v (%v)\n", err, msg)
        }
    }

    c.Close()
}

If you use docker to build: follow this comment to add suitable packages

For Debian and Ubuntu based distros, install librdkafka-dev from the standard repositories or using Confluent's Deb repository.

For Redhat based distros, install librdkafka-devel using Confluent's YUM repository.

For MacOS X, install librdkafka from Homebrew. You may also need to brew install pkg-config if you don't already have it. brew install librdkafka pkg-config.

For Alpine: apk add librdkafka-dev pkgconf confluent-kafka-go is not supported on Windows.

With Alpine, please remember that install the community version, because it cannot install librdkafka with max version 1.1.0 (not use Alpine community version)

Good luck!

Upvotes: 2

Poisoner
Poisoner

Reputation: 61

I'v found the reason. because the kafka's settings is not correct

this is server.properties:

############################# Socket Server Settings #############################

# The address the socket server listens on. It will get the value returned from 
# java.net.InetAddress.getCanonicalHostName() if not configured.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
#listeners=PLAINTEXT://:9092

# Hostname and port the broker will advertise to producers and consumers. If not set, 
# it uses the value for "listeners" if configured.  Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
#advertised.listeners=PLAINTEXT://your.host.name:9092

if the listeners is not set , kafka will only receive request from java.net.InetAddress.getCanonicalHostName() which means localhost

so i shuld set :

listeners = PLAINTEXT://0.0.0.0:9092

this will work

Upvotes: 0

pan
pan

Reputation: 1967

Not sure, if it is possible to use with curl with kafka. But you can use the kafka-console-consumer.

kafka-console-consumer.bat --bootstrap-server 192.168.9.100:9092 --topic mykafka --from-beginning

Upvotes: 0

Related Questions