golang 的 rabbitmq 消费项目
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

93 linhas
2.2 KiB

  1. package main
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. "applet/app/utils"
  6. "applet/app/utils/logx"
  7. "applet/processor"
  8. "errors"
  9. "fmt"
  10. "os"
  11. "os/signal"
  12. "strings"
  13. "syscall"
  14. )
  15. //系统初始化
  16. func init() {
  17. cfg.InitCfg() //配置初始化
  18. cfg.InitLog() //日志初始化
  19. cfg.InitCache() //缓存初始化
  20. cfg.InitMq() //队列初始化
  21. if cfg.Debug { //判断是否是debug
  22. if err := db.InitDB(cfg.DB); err != nil { //主数据库初始化
  23. panic(err)
  24. }
  25. channel := make(chan int, 0) //开辟管道,缓冲为
  26. go db.InitDBs(channel)
  27. <-channel
  28. }
  29. fmt.Println("init success")
  30. }
  31. // @title 智莺生活移动端接口
  32. // @version 1.0
  33. // @description 移动端接口
  34. // @termsOfService 智莺生活后端组
  35. // @contact.name sherlockwhite
  36. // @host localhost:5000
  37. // @securityDefinitions.apikey MasterID
  38. // @in header
  39. // @name MasterID
  40. // @BasePath /
  41. func main() {
  42. if cfg.CurlDebug {
  43. utils.CurlDebug = true
  44. }
  45. //r := router.Init() //创建路由
  46. //srv := &http.Server{ //设置http服务参数
  47. // Addr: cfg.SrvAddr, //指定ip和端口
  48. // Handler: r, //指定路由
  49. //}
  50. //go func() { //协程启动监听队列
  51. // fmt.Println("Listening and serving HTTP on " + cfg.SrvAddr)
  52. // if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
  53. // log.Fatalf("listen: %s\n", err)
  54. // }
  55. //}()
  56. //退出go守护进程
  57. //quit := make(chan os.Signal)
  58. //signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
  59. //<-quit
  60. //log.Println("Shutting down server...")
  61. //ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  62. //defer cancel()
  63. //if err := srv.Shutdown(ctx); err != nil {
  64. // log.Fatal("Server forced to shutdown:", err)
  65. //}
  66. //log.Println("Server exiting")
  67. //获取需要监听多少个队列
  68. if cfg.RabbitMqAddr == "" {
  69. panic(errors.New("请填写MQ参数"))
  70. }
  71. forever := make(chan bool)
  72. for _, workQueueName := range strings.Split(cfg.MqWorkQueueName, ",") {
  73. if workQueueName == "" {
  74. continue
  75. }
  76. fmt.Printf("监听%s队列", workQueueName)
  77. fmt.Println()
  78. go processor.WorkReceive(workQueueName)
  79. }
  80. //processor.TestWorkSend()
  81. <-forever
  82. quit := make(chan os.Signal)
  83. signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
  84. <-quit
  85. _ = logx.Info("Server exiting...")
  86. }