You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.go 3.2 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. "os"
  6. "sync"
  7. "gitlink.org.cn/cloudream/agent/internal/config"
  8. "gitlink.org.cn/cloudream/agent/internal/event"
  9. "gitlink.org.cn/cloudream/common/utils/ipfs"
  10. log "gitlink.org.cn/cloudream/common/utils/logger"
  11. "gitlink.org.cn/cloudream/db"
  12. agentserver "gitlink.org.cn/cloudream/proto"
  13. "google.golang.org/grpc"
  14. rasvr "gitlink.org.cn/cloudream/rabbitmq/server/agent"
  15. cmdsvc "gitlink.org.cn/cloudream/agent/internal/services/cmd"
  16. grpcsvc "gitlink.org.cn/cloudream/agent/internal/services/grpc"
  17. sccli "gitlink.org.cn/cloudream/rabbitmq/client/scanner"
  18. )
  19. // TODO 此数据是否在运行时会发生变化?
  20. var AgentIpList []string
  21. func main() {
  22. // TODO 放到配置里读取
  23. AgentIpList = []string{"pcm01", "pcm1", "pcm2"}
  24. err := config.Init()
  25. if err != nil {
  26. fmt.Printf("init config failed, err: %s", err.Error())
  27. os.Exit(1)
  28. }
  29. err = log.Init(&config.Cfg().Logger)
  30. if err != nil {
  31. fmt.Printf("init logger failed, err: %s", err.Error())
  32. os.Exit(1)
  33. }
  34. scanner, err := sccli.NewScannerClient(&config.Cfg().RabbitMQ)
  35. if err != nil {
  36. log.Fatalf("new scanner client failed, err: %s", err.Error())
  37. }
  38. db, err := db.NewDB(&config.Cfg().DB)
  39. if err != nil {
  40. log.Fatalf("new db failed, err: %s", err.Error())
  41. }
  42. ipfs, err := ipfs.NewIPFS(&config.Cfg().IPFS)
  43. if err != nil {
  44. log.Fatalf("new ipfs failed, err: %s", err.Error())
  45. }
  46. //处置协调端、客户端命令(可多建几个)
  47. wg := sync.WaitGroup{}
  48. wg.Add(4)
  49. eventExecutor := event.NewExecutor(scanner, db, ipfs)
  50. go serveEventExecutor(&eventExecutor, &wg)
  51. // 启动命令服务器
  52. // TODO 需要设计AgentID持久化机制
  53. agtSvr, err := rasvr.NewAgentServer(cmdsvc.NewService(ipfs, &eventExecutor), config.Cfg().ID, &config.Cfg().RabbitMQ)
  54. if err != nil {
  55. log.Fatalf("new agent server failed, err: %s", err.Error())
  56. }
  57. agtSvr.OnError = func(err error) {
  58. log.Warnf("agent server err: %s", err.Error())
  59. }
  60. go serveAgentServer(agtSvr, &wg)
  61. go reportStatus(&wg) //网络延迟感知
  62. //面向客户端收发数据
  63. listenAddr := config.Cfg().GRPCListenAddress
  64. lis, err := net.Listen("tcp", listenAddr)
  65. if err != nil {
  66. log.Fatalf("listen on %s failed, err: %s", listenAddr, err.Error())
  67. }
  68. s := grpc.NewServer()
  69. agentserver.RegisterFileTransportServer(s, grpcsvc.NewService(ipfs))
  70. go serveGRPC(s, lis, &wg)
  71. wg.Wait()
  72. }
  73. func serveAgentServer(server *rasvr.AgentServer, wg *sync.WaitGroup) {
  74. log.Info("start serving command server")
  75. err := server.Serve()
  76. if err != nil {
  77. log.Errorf("command server stopped with error: %s", err.Error())
  78. }
  79. log.Info("command server stopped")
  80. wg.Done()
  81. }
  82. func serveEventExecutor(executor *event.Executor, wg *sync.WaitGroup) {
  83. log.Info("start serving event executor")
  84. err := executor.Execute()
  85. if err != nil {
  86. log.Errorf("event executor stopped with error: %s", err.Error())
  87. }
  88. log.Info("event executor stopped")
  89. wg.Done()
  90. }
  91. func serveGRPC(s *grpc.Server, lis net.Listener, wg *sync.WaitGroup) {
  92. log.Info("start serving grpc")
  93. err := s.Serve(lis)
  94. if err != nil {
  95. log.Errorf("grpc stopped with error: %s", err.Error())
  96. }
  97. log.Info("grpc stopped")
  98. wg.Done()
  99. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。