Revert "Change: Remove the microserivce auto clear mechanism (#1074)"

This reverts commit 54fd94abb1b7e6db732f6e4eb8f836dc640db9e3.
diff --git a/server/core/config.go b/server/core/config.go
index a3457bb..2e14a33 100644
--- a/server/core/config.go
+++ b/server/core/config.go
@@ -34,7 +34,17 @@
 
 const (
 	InitVersion = "0"
-	minCacheTTL = 5 * time.Minute
+
+	defaultServiceClearInterval = 12 * time.Hour //0.5 day
+	defaultServiceTTL           = 24 * time.Hour //1 day
+
+	minServiceClearInterval = 30 * time.Second
+	minServiceTTL           = 30 * time.Second
+	minCacheTTL             = 5 * time.Minute
+
+	maxServiceClearInterval = 24 * time.Hour       //1 day
+	maxServiceTTL           = 24 * 365 * time.Hour //1 year
+
 )
 
 var ServerInfo = proto.NewServerInformation()
@@ -67,6 +77,16 @@
 		maxLogBackupCount = 50
 	}
 
+	serviceClearInterval, err := time.ParseDuration(os.Getenv("SERVICE_CLEAR_INTERVAL"))
+	if err != nil || serviceClearInterval < minServiceClearInterval || serviceClearInterval > maxServiceClearInterval {
+		serviceClearInterval = defaultServiceClearInterval
+	}
+
+	serviceTTL, err := time.ParseDuration(os.Getenv("SERVICE_TTL"))
+	if err != nil || serviceTTL < minServiceTTL || serviceTTL > maxServiceTTL {
+		serviceTTL = defaultServiceTTL
+	}
+
 	cacheTTL, err := time.ParseDuration(
 		util.GetEnvString("CACHE_TTL", beego.AppConfig.DefaultString("cache_ttl", "")))
 	if err == nil && cacheTTL < minCacheTTL {
@@ -117,6 +137,10 @@
 			CacheTTL:     cacheTTL,
 			SelfRegister: beego.AppConfig.DefaultInt("self_register", 1) != 0,
 
+			ServiceClearEnabled:  os.Getenv("SERVICE_CLEAR_ENABLED") == "true",
+			ServiceClearInterval: serviceClearInterval,
+			ServiceTTL:           serviceTTL,
+
 			SchemaDisable: os.Getenv("SCHEMA_DISABLE") == "true",
 		},
 	}
diff --git a/server/core/proto/types.go b/server/core/proto/types.go
index 1c722b0..3ea9a55 100644
--- a/server/core/proto/types.go
+++ b/server/core/proto/types.go
@@ -62,6 +62,11 @@
 
 	SelfRegister bool `json:"selfRegister"`
 
+	//clear no-instance services
+	ServiceClearEnabled  bool          `json:"serviceClearEnabled"`
+	ServiceClearInterval time.Duration `json:"serviceClearInterval"`
+	//if a service's existence time reaches this value, it can be cleared
+	ServiceTTL time.Duration `json:"serviceTTL"`
 	//CacheTTL is the ttl of cache
 	CacheTTL time.Duration `json:"cacheTTL"`
 
diff --git a/server/server.go b/server/server.go
index 2e257f5..e0609a3 100644
--- a/server/server.go
+++ b/server/server.go
@@ -36,6 +36,7 @@
 	"github.com/apache/servicecomb-service-center/server/plugin"
 	"github.com/apache/servicecomb-service-center/server/service/rbac"
 	serviceUtil "github.com/apache/servicecomb-service-center/server/service/util"
+	"github.com/apache/servicecomb-service-center/server/task"
 	"github.com/apache/servicecomb-service-center/version"
 	"github.com/astaxie/beego"
 )
@@ -148,6 +149,40 @@
 	})
 }
 
+// clear services who have no instance
+func (s *ServiceCenterServer) clearNoInstanceServices() {
+	if !core.ServerInfo.Config.ServiceClearEnabled {
+		return
+	}
+	log.Infof("service clear enabled, interval: %s, service TTL: %s",
+		core.ServerInfo.Config.ServiceClearInterval,
+		core.ServerInfo.Config.ServiceTTL)
+
+	s.goroutine.Do(func(ctx context.Context) {
+		for {
+			select {
+			case <-ctx.Done():
+				return
+			case <-time.After(core.ServerInfo.Config.ServiceClearInterval):
+				lock, err := mux.Try(mux.ServiceClearLock)
+				if err != nil {
+					log.Errorf(err, "can not clear no instance services by this service center instance now")
+					continue
+				}
+				err = task.ClearNoInstanceServices(core.ServerInfo.Config.ServiceTTL)
+				if err := lock.Unlock(); err != nil {
+					log.Error("", err)
+				}
+				if err != nil {
+					log.Errorf(err, "no-instance services cleanup failed")
+					continue
+				}
+				log.Info("no-instance services cleanup succeed")
+			}
+		}
+	})
+}
+
 func (s *ServiceCenterServer) initialize() {
 	s.cacheService = backend.Store()
 	s.apiService = GetAPIServer()
@@ -174,6 +209,8 @@
 	if buildin != beego.AppConfig.DefaultString("registry_plugin", buildin) {
 		// compact backend automatically
 		s.compactBackendService()
+		// clean no-instance services automatically
+		s.clearNoInstanceServices()
 	}
 
 	// api service