|
|
@@ -9,11 +9,11 @@ import (
|
|
|
|
"syscall"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/confluentinc/confluent-kafka-go/kafka"
|
|
|
|
|
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
|
|
|
|
kafka "github.com/segmentio/kafka-go"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@@ -76,23 +76,31 @@ func (c *Client) readPump(topic string) {
|
|
|
|
"bootstrap servers": *bootstrapServers,
|
|
|
|
"bootstrap servers": *bootstrapServers,
|
|
|
|
"group": group}).Info("create kafka client")
|
|
|
|
"group": group}).Info("create kafka client")
|
|
|
|
|
|
|
|
|
|
|
|
consumer, err := kafka.NewConsumer(&kafka.ConfigMap{
|
|
|
|
// consumer, err := kafka.NewConsumer(&kafka.ConfigMap{
|
|
|
|
"bootstrap.servers": *bootstrapServers,
|
|
|
|
// "bootstrap.servers": *bootstrapServers,
|
|
|
|
"group.id": group,
|
|
|
|
// "group.id": group,
|
|
|
|
"session.timeout.ms": 6000,
|
|
|
|
// "session.timeout.ms": 6000,
|
|
|
|
"auto.offset.reset": "latest"})
|
|
|
|
// "auto.offset.reset": "latest"})
|
|
|
|
|
|
|
|
reader := kafka.NewReader(kafka.ReaderConfig{
|
|
|
|
|
|
|
|
Brokers: []string{*bootstrapServers},
|
|
|
|
|
|
|
|
Topic: topic,
|
|
|
|
|
|
|
|
Partition: 0,
|
|
|
|
|
|
|
|
MinBytes: 10e1,
|
|
|
|
|
|
|
|
MaxBytes: 10e6, // 10MB
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
reader.SetOffset(-1) //lastoffset
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Fprintf(os.Stderr, "Failed to create consumer: %s\n", err)
|
|
|
|
// // fmt.Fprintf(os.Stderr, "Failed to create consumer: %s\n", err)
|
|
|
|
log.WithError(err).Fatal("Failed to create consumer")
|
|
|
|
// log.WithError(err).Fatal("Failed to create consumer")
|
|
|
|
os.Exit(1)
|
|
|
|
// os.Exit(1)
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"Consumer": consumer,
|
|
|
|
"reader": reader,
|
|
|
|
}).Info("Created ")
|
|
|
|
}).Info("Created ")
|
|
|
|
|
|
|
|
|
|
|
|
consumer.SubscribeTopics([]string{topic}, nil)
|
|
|
|
// consumer.SubscribeTopics([]string{topic}, nil)
|
|
|
|
|
|
|
|
|
|
|
|
run := true
|
|
|
|
run := true
|
|
|
|
|
|
|
|
|
|
|
@@ -103,28 +111,37 @@ func (c *Client) readPump(topic string) {
|
|
|
|
run = false
|
|
|
|
run = false
|
|
|
|
log.Fatal("break ")
|
|
|
|
log.Fatal("break ")
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
ev := consumer.Poll(100)
|
|
|
|
// ev := consumer.Poll(100)
|
|
|
|
if ev == nil {
|
|
|
|
// if ev == nil {
|
|
|
|
continue
|
|
|
|
// continue
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
switch e := ev.(type) {
|
|
|
|
// switch e := ev.(type) {
|
|
|
|
case *kafka.Message:
|
|
|
|
// case *kafka.Message:
|
|
|
|
log.WithFields(log.Fields{"partition": e.TopicPartition, "message": string(e.Value)}).Info("got message")
|
|
|
|
// log.WithFields(log.Fields{"partition": e.TopicPartition, "message": string(e.Value)}).Info("got message")
|
|
|
|
c.hub.broadcast <- e.Value
|
|
|
|
// c.hub.broadcast <- e.Value
|
|
|
|
if e.Headers != nil {
|
|
|
|
// if e.Headers != nil {
|
|
|
|
log.WithFields(log.Fields{"header": e.Headers}).Info(" with header")
|
|
|
|
// log.WithFields(log.Fields{"header": e.Headers}).Info(" with header")
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// case kafka.Error:
|
|
|
|
|
|
|
|
// // Errors should generally be considered as informational, the client will try to automatically recover
|
|
|
|
|
|
|
|
// log.WithFields(log.Fields{"error": e}).Error("got error")
|
|
|
|
|
|
|
|
// default:
|
|
|
|
|
|
|
|
// log.WithFields(log.Fields{"event": e}).Info("ignored")
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
m, err := reader.ReadMessage(context.Background())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{"error": err}).Error("read error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case kafka.Error:
|
|
|
|
// fmt.Printf("message at offset %d: %s = %s\n", m.Offset, string(m.Key), string(m.Value))
|
|
|
|
// Errors should generally be considered as informational, the client will try to automatically recover
|
|
|
|
log.WithFields(log.Fields{"offset": m.Offset, "message": string(m.Value)}).Info("got message")
|
|
|
|
log.WithFields(log.Fields{"error": e}).Error("got error")
|
|
|
|
c.hub.broadcast <- m.Value
|
|
|
|
default:
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{"event": e}).Info("ignored")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
consumer.Close()
|
|
|
|
// consumer.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// writePump pumps messages from the hub to the websocket connection.
|
|
|
|
// writePump pumps messages from the hub to the websocket connection.
|
|
|
@@ -263,10 +280,9 @@ func main() {
|
|
|
|
|
|
|
|
|
|
|
|
// Run our server in a goroutine so that it doesn't block.
|
|
|
|
// Run our server in a goroutine so that it doesn't block.
|
|
|
|
go func() {
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{"address": *addr}).Info("starting server")
|
|
|
|
if err := srv.ListenAndServe(); err != nil {
|
|
|
|
if err := srv.ListenAndServe(); err != nil {
|
|
|
|
log.WithFields(log.Fields{"error": err}).Error("startup error")
|
|
|
|
log.WithFields(log.Fields{"error": err}).Error("startup error")
|
|
|
|
} else {
|
|
|
|
|
|
|
|
log.Info("proxy started at :: ", addr)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|