func NewConsulClusterNode(config *ConsulNodeConfig) (*ConsulClusterNode, error) { if config == nil { config = &ConsulNodeConfig{} } err := config.Normalize() if err != nil { return nil, err } consul := NewConsulClient(config.ConsulToken) ct, err := NewConsulRoutingTable(config.RoutingPrefix, config.AdvertiseID(), consul) if err != nil { return nil, err } cn, err := NewClusterNode(config.DataPath, vega.NewRouter(ct)) if err != nil { return nil, err } serv, err := vega.NewService(config.ListenAddr(), cn) if err != nil { cn.Close() return nil, err } ccn := &ConsulClusterNode{ clusterNode: cn, Config: config, routes: ct, service: serv, } for _, name := range cn.disk.MailboxNames() { ccn.Declare(name) } return ccn, nil }
func main() { flag.Parse() cfg := &cluster.ConsulNodeConfig{ ListenPort: *fClusterPort, DataPath: *fData, AdvertiseAddr: *fAdvertise, RoutingPrefix: *fRoutingPrefix, ConsulToken: *fToken, } node, err := cluster.NewConsulClusterNode(cfg) if err != nil { log.Fatalf("unable to create node: %s", err) os.Exit(1) } go node.Accept() var h *vega.HTTPService var local *vega.Service if *fHttpPort != 0 { h = vega.NewHTTPService( fmt.Sprintf("127.0.0.1:%d", *fHttpPort), node) err = h.Listen() if err != nil { log.Fatalf("unable to create http server: %s", err) os.Exit(1) } h.BackgroundTimeouts() go h.Accept() } if *fPort != 0 { local, err = vega.NewService(fmt.Sprintf("127.0.0.1:%d", *fPort), node) if err != nil { log.Fatalf("Unable to create local server: %s", err) os.Exit(1) } go local.AcceptInsecure() } fmt.Printf("! Booted vegad:\n") fmt.Printf("* LocalPort: %d\n", *fPort) fmt.Printf("* ClusterPort: %d\n", cfg.ListenPort) fmt.Printf("* DataPath: %s\n", cfg.DataPath) fmt.Printf("* AdvertiseId: %s\n", cfg.AdvertiseID()) if h == nil { fmt.Printf("* HTTP Server disabled\n") } else { fmt.Printf("* HTTP Server: %d\n", *fHttpPort) } sig := make(chan os.Signal) signal.Notify(sig, os.Interrupt) <-sig fmt.Printf("\nGracefully shutting down...\n") if h != nil { h.Close() } if local != nil { local.Close() } node.Cleanup() node.Close() }
func TestClusterPubSubBetweenNodes(t *testing.T) { dir, err := ioutil.TempDir("", "mailbox") if err != nil { panic(err) } defer os.RemoveAll(dir) cn, err := NewMemClusterNode(dir) if err != nil { panic(err) } defer cn.Close() dir2, err := ioutil.TempDir("", "mailbox") if err != nil { panic(err) } defer os.RemoveAll(dir2) cn2, err := NewMemClusterNode(dir2) if err != nil { panic(err) } defer cn2.Close() // Setup 2 service objects s1, err := vega.NewService(cPort, cn) if err != nil { panic(err) } defer s1.Close() go s1.Accept() s2, err := vega.NewService(cPort2, cn2) if err != nil { panic(err) } defer s2.Close() go s2.Accept() // Wire up a client going to s1 toS1, err := vega.NewClient(cPort) if err != nil { panic(err) } toS1.Declare("a") err = toS1.Push(":subscribe", &vega.Message{ReplyTo: "a", CorrelationId: "foo"}) require.NoError(t, err) cn2.AddRoute(":publish", toS1) // Push data into cn2 and see it show up in cn toS2, err := vega.NewClient(cPort2) if err != nil { panic(err) } msg := &vega.Message{CorrelationId: "foo", Body: []byte("between nodes")} err = toS2.Push(":publish", msg) require.NoError(t, err) // debugf("polling\n") ret, err := toS1.Poll("a") if err != nil { panic(err) } require.NotNil(t, ret) assert.True(t, msg.Equal(ret.Message), "message did not route properly") }
func TestClusterRoutesViaNetwork(t *testing.T) { dir, err := ioutil.TempDir("", "mailbox") if err != nil { panic(err) } defer os.RemoveAll(dir) cn, err := NewMemClusterNode(dir) if err != nil { panic(err) } defer cn.Close() dir2, err := ioutil.TempDir("", "mailbox") if err != nil { panic(err) } defer os.RemoveAll(dir2) cn2, err := NewMemClusterNode(dir2) if err != nil { panic(err) } defer cn2.Close() // Setup 2 service objects s1, err := vega.NewService(cPort, cn) if err != nil { panic(err) } defer s1.Close() go s1.Accept() s2, err := vega.NewService(cPort2, cn2) if err != nil { panic(err) } defer s2.Close() go s2.Accept() // Wire up a client going to s1 toS1, err := vega.NewClient(cPort) if err != nil { panic(err) } toS1.Declare("a") cn2.AddRoute("a", toS1) // Push data into cn2 and see it show up in cn toS2, err := vega.NewClient(cPort2) if err != nil { panic(err) } msg := vega.Msg([]byte("between nodes")) err = toS2.Push("a", msg) if err != nil { panic(err) } // debugf("polling\n") ret, err := toS1.Poll("a") if err != nil { panic(err) } assert.True(t, msg.Equal(ret.Message), "message did not route properly") }