initial import
This commit is contained in:
12
example/kmq/consumer/config.json
Normal file
12
example/kmq/consumer/config.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"Name": "kmq",
|
||||
"Brokers": [
|
||||
"172.16.56.64:19092",
|
||||
"172.16.56.65:19092",
|
||||
"172.16.56.66:19092"
|
||||
],
|
||||
"Group": "adhoc",
|
||||
"Topic": "kevin",
|
||||
"Offset": "first",
|
||||
"NumProducers": 1
|
||||
}
|
||||
20
example/kmq/consumer/queue.go
Normal file
20
example/kmq/consumer/queue.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"zero/core/conf"
|
||||
"zero/kq"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var c kq.KqConf
|
||||
conf.MustLoad("config.json", &c)
|
||||
|
||||
q := kq.MustNewQueue(c, kq.WithHandle(func(k, v string) error {
|
||||
fmt.Printf("=> %s\n", v)
|
||||
return nil
|
||||
}))
|
||||
defer q.Stop()
|
||||
q.Start()
|
||||
}
|
||||
51
example/kmq/producer/produce.go
Normal file
51
example/kmq/producer/produce.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"zero/core/cmdline"
|
||||
"zero/kq"
|
||||
)
|
||||
|
||||
type message struct {
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
Payload string `json:"message"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
pusher := kq.NewPusher([]string{
|
||||
"172.16.56.64:19092",
|
||||
"172.16.56.65:19092",
|
||||
"172.16.56.66:19092",
|
||||
}, "kevin")
|
||||
|
||||
ticker := time.NewTicker(time.Millisecond)
|
||||
for round := 0; round < 3; round++ {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
count := rand.Intn(100)
|
||||
m := message{
|
||||
Key: strconv.FormatInt(time.Now().UnixNano(), 10),
|
||||
Value: fmt.Sprintf("%d,%d", round, count),
|
||||
Payload: fmt.Sprintf("%d,%d", round, count),
|
||||
}
|
||||
body, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(string(body))
|
||||
if err := pusher.Push(string(body)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cmdline.EnterToContinue()
|
||||
}
|
||||
Reference in New Issue
Block a user