-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (72 loc) · 1.82 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"Eyup-Devop/kafka-consumer/kafkaconsumer"
"github.com/confluentinc/confluent-kafka-go/v2/kafka"
)
var (
server *http.Server
consumer *kafkaconsumer.KafkaConsumer
)
func main() {
brokerList := []string{"127.0.0.1:19092"}
topicList := []string{"example-topic"}
consumer = kafkaconsumer.NewConsumer(strings.Join(brokerList, ","), topicList, "group1", nil)
err := consumer.Init()
if err != nil {
log.Println(err)
panic(err)
}
go ProduceTestMessages(brokerList, "group1", "example-topic")
consumer.StartConsume()
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM)
go startHttpServer(":9090", httpHandle)
<-sigchan
}
func ProduceTestMessages(br []string, grp string, topic string) {
producer, _ := kafka.NewProducer(&kafka.ConfigMap{"bootstrap.servers": strings.Join(br, ",")})
for i := 0; i < 10; i++ {
time.Sleep(time.Second * 2)
producer.Produce(&kafka.Message{
TopicPartition: kafka.TopicPartition{Topic: &topic, Partition: kafka.PartitionAny},
Value: []byte("Example Message - " + string(rune(i))),
}, nil)
}
}
func startHttpServer(addr string, handler http.HandlerFunc) {
http.HandleFunc("/", handler)
log.Println("listening on", addr)
server = &http.Server{
Addr: addr,
}
server.ListenAndServe()
log.Println("server stopped")
}
func httpHandle(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
consumer.Close()
SendShutdownSignal()
server.Shutdown(context.Background())
}
}
func SendShutdownSignal() bool {
pid := os.Getpid()
process, err := os.FindProcess(pid)
if err != nil {
log.Println(err)
return false
}
if err := process.Signal(os.Interrupt); err != nil {
log.Println(err)
return false
}
return true
}