produce.go 911 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "math/rand"
  7. "strconv"
  8. "time"
  9. "zero/core/cmdline"
  10. "zero/kq"
  11. )
  12. type message struct {
  13. Key string `json:"key"`
  14. Value string `json:"value"`
  15. Payload string `json:"message"`
  16. }
  17. func main() {
  18. pusher := kq.NewPusher([]string{
  19. "172.16.56.64:19092",
  20. "172.16.56.65:19092",
  21. "172.16.56.66:19092",
  22. }, "kevin")
  23. ticker := time.NewTicker(time.Millisecond)
  24. for round := 0; round < 3; round++ {
  25. select {
  26. case <-ticker.C:
  27. count := rand.Intn(100)
  28. m := message{
  29. Key: strconv.FormatInt(time.Now().UnixNano(), 10),
  30. Value: fmt.Sprintf("%d,%d", round, count),
  31. Payload: fmt.Sprintf("%d,%d", round, count),
  32. }
  33. body, err := json.Marshal(m)
  34. if err != nil {
  35. log.Fatal(err)
  36. }
  37. fmt.Println(string(body))
  38. if err := pusher.Push(string(body)); err != nil {
  39. log.Fatal(err)
  40. }
  41. }
  42. }
  43. cmdline.EnterToContinue()
  44. }