msg_broker.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package command
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "google.golang.org/grpc/reflection"
  7. "github.com/chrislusf/seaweedfs/weed/util/grace"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/messaging/broker"
  10. "github.com/chrislusf/seaweedfs/weed/pb"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
  13. "github.com/chrislusf/seaweedfs/weed/security"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  15. )
  16. var (
  17. messageBrokerStandaloneOptions MessageBrokerOptions
  18. )
  19. type MessageBrokerOptions struct {
  20. filer *string
  21. ip *string
  22. port *int
  23. cpuprofile *string
  24. memprofile *string
  25. }
  26. func init() {
  27. cmdMsgBroker.Run = runMsgBroker // break init cycle
  28. messageBrokerStandaloneOptions.filer = cmdMsgBroker.Flag.String("filer", "localhost:8888", "filer server address")
  29. messageBrokerStandaloneOptions.ip = cmdMsgBroker.Flag.String("ip", util.DetectedHostAddress(), "broker host address")
  30. messageBrokerStandaloneOptions.port = cmdMsgBroker.Flag.Int("port", 17777, "broker gRPC listen port")
  31. messageBrokerStandaloneOptions.cpuprofile = cmdMsgBroker.Flag.String("cpuprofile", "", "cpu profile output file")
  32. messageBrokerStandaloneOptions.memprofile = cmdMsgBroker.Flag.String("memprofile", "", "memory profile output file")
  33. }
  34. var cmdMsgBroker = &Command{
  35. UsageLine: "msgBroker [-port=17777] [-filer=<ip:port>]",
  36. Short: "start a message queue broker",
  37. Long: `start a message queue broker
  38. The broker can accept gRPC calls to write or read messages. The messages are stored via filer.
  39. The brokers are stateless. To scale up, just add more brokers.
  40. `,
  41. }
  42. func runMsgBroker(cmd *Command, args []string) bool {
  43. util.LoadConfiguration("security", false)
  44. return messageBrokerStandaloneOptions.startQueueServer()
  45. }
  46. func (msgBrokerOpt *MessageBrokerOptions) startQueueServer() bool {
  47. grace.SetupProfiling(*messageBrokerStandaloneOptions.cpuprofile, *messageBrokerStandaloneOptions.memprofile)
  48. filerAddress := pb.ServerAddress(*msgBrokerOpt.filer)
  49. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.msg_broker")
  50. cipher := false
  51. for {
  52. err := pb.WithGrpcFilerClient(false, filerAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  53. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  54. if err != nil {
  55. return fmt.Errorf("get filer %s configuration: %v", filerAddress, err)
  56. }
  57. cipher = resp.Cipher
  58. return nil
  59. })
  60. if err != nil {
  61. glog.V(0).Infof("wait to connect to filer %s grpc address %s", *msgBrokerOpt.filer, filerAddress.ToGrpcAddress())
  62. time.Sleep(time.Second)
  63. } else {
  64. glog.V(0).Infof("connected to filer %s grpc address %s", *msgBrokerOpt.filer, filerAddress.ToGrpcAddress())
  65. break
  66. }
  67. }
  68. qs, err := broker.NewMessageBroker(&broker.MessageBrokerOption{
  69. Filers: []pb.ServerAddress{filerAddress},
  70. DefaultReplication: "",
  71. MaxMB: 0,
  72. Ip: *msgBrokerOpt.ip,
  73. Port: *msgBrokerOpt.port,
  74. Cipher: cipher,
  75. }, grpcDialOption)
  76. // start grpc listener
  77. grpcL, err := util.NewListener(util.JoinHostPort("", *msgBrokerOpt.port), 0)
  78. if err != nil {
  79. glog.Fatalf("failed to listen on grpc port %d: %v", *msgBrokerOpt.port, err)
  80. }
  81. grpcS := pb.NewGrpcServer(security.LoadServerTLS(util.GetViper(), "grpc.msg_broker"))
  82. messaging_pb.RegisterSeaweedMessagingServer(grpcS, qs)
  83. reflection.Register(grpcS)
  84. grpcS.Serve(grpcL)
  85. return true
  86. }