func createCluster() cluster.Cluster { config := &cluster.Config{ BackendType: cluster.MemoryBackend, } cl, _ := cluster.New(config) return cl }
func TestStartup(t *testing.T) { if testing.Short() { t.Skip() } port1 := nextPort() // Configure and create the cluster module cfConfig := &cluster.Config{ BackendType: cluster.MemoryBackend, } cl, err := cluster.New(cfConfig) assert.NoError(t, err) assert.NotNil(t, cl) rep1, server1 := setupServer(t, port1, port1+1000, cl) defer func() { server1.Stop() rep1.Stop() }() register(t, port1) assert.EqualValues(t, len(instances), list(t, port1)) for _, inst := range instances { assert.EqualValues(t, 1, lookup(t, port1, inst.ServiceName)) } }
func TestSynchronization(t *testing.T) { if testing.Short() { t.Skip() } // Configure and create the cluster module cfConfig := &cluster.Config{ BackendType: cluster.MemoryBackend, } cl, err := cluster.New(cfConfig) assert.NoError(t, err) assert.NotNil(t, cl) port1 := nextPort() port2 := nextPort() rep1, server1 := setupServer(t, port1, port1+1000, cl) defer func() { server1.Stop() rep1.Stop() }() ids := register(t, port1) assert.EqualValues(t, len(instances), list(t, port1)) for _, inst := range instances { assert.EqualValues(t, 1, lookup(t, port1, inst.ServiceName)) } rep2, server2 := setupServer(t, port2, port2+1000, cl) defer func() { server2.Stop() rep2.Stop() }() // Let a few milliseconds for the replication time.Sleep(time.Duration(1) * time.Second) assert.EqualValues(t, len(instances), list(t, port2)) for _, inst := range instances { assert.EqualValues(t, 1, lookup(t, port2, inst.ServiceName)) } for _, id := range ids { renew(t, port2, id) } }
// registryMain is the logical entry point for the service registry. func registryMain(conf *config.Values) error { // Configure logging parsedLogLevel, err := logrus.ParseLevel(conf.LogLevel) if err != nil { return err } logrus.SetLevel(parsedLogLevel) formatter, err := logging.GetLogFormatter(conf.LogFormat) if err != nil { return err } logrus.SetFormatter(formatter) // Configure locales and translations err = i18n.LoadLocales("./locales") if err != nil { return err } var rep replication.Replication if conf.Replication { // Wait for private network to become available // In some cloud environments, that may take several seconds networkAvailable := network.WaitForPrivateNetwork() if !networkAvailable { return fmt.Errorf("No private network is available within defined timeout") } // Configure and create the cluster module clConfig := &cluster.Config{ BackendType: cluster.FilesystemBackend, Directory: conf.ClusterDirectory, Size: conf.ClusterSize, } cl, err := cluster.New(clConfig) if err != nil { return fmt.Errorf("Failed to create the cluster module: %s", err) } // Configure and create the replication module self := cluster.NewMember(network.GetPrivateIP(), conf.ReplicationPort) repConfig := &replication.Config{ Membership: cl.Membership(), Registrator: cl.Registrator(self), } rep, err = replication.New(repConfig) if err != nil { return fmt.Errorf("Failed to create the replication module: %s", err) } } var authenticator auth.Authenticator if len(conf.AuthModes) > 0 { auths := make([]auth.Authenticator, len(conf.AuthModes)) for i, mode := range conf.AuthModes { switch mode { case "trusted": auths[i] = auth.NewTrustedAuthenticator() case "jwt": jwtAuth, err := auth.NewJWTAuthenticator([]byte(conf.JWTSecret)) if err != nil { return fmt.Errorf("Failed to create the authentication module: %s", err) } auths[i] = jwtAuth default: return fmt.Errorf("Failed to create the authentication module: unrecognized authentication mode '%s'", err) } } authenticator, err = auth.NewChainAuthenticator(auths) if err != nil { return err } } else { authenticator = auth.DefaultAuthenticator() } regConfig := &store.Config{ DefaultTTL: conf.DefaultTTL, MinimumTTL: conf.MinTTL, MaximumTTL: conf.MaxTTL, SyncWaitTime: conf.SyncTimeout, NamespaceCapacity: conf.NamespaceCapacity, } reg := store.New(regConfig, rep) serverConfig := &api.Config{ HTTPAddressSpec: fmt.Sprintf(":%d", conf.APIPort), Registry: reg, Authenticator: authenticator, RequireHTTPS: conf.RequireHTTPS, } server, err := api.NewServer(serverConfig) if err != nil { return err } go metrics.DumpPeriodically() return server.Start() }