You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ti...@apache.org on 2021/04/12 06:23:50 UTC

[servicecomb-service-center] branch master updated: SCB-2176 Decompose the plugin and server pkg (#937)

This is an automated email from the ASF dual-hosted git repository.

tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git


The following commit(s) were added to refs/heads/master by this push:
     new c8860df  SCB-2176 Decompose the plugin and server pkg (#937)
c8860df is described below

commit c8860dfcf389d594eacc6f790a2bf7ea76e13a87
Author: little-cui <su...@qq.com>
AuthorDate: Mon Apr 12 14:23:02 2021 +0800

    SCB-2176 Decompose the plugin and server pkg (#937)
---
 datasource/mongo/mongo.go                          |  8 ++--
 server/plugin/uuid/uuid.go => pkg/plugin/common.go | 28 ++++++------
 server/plugin/auth/auth.go => pkg/plugin/config.go | 23 +++-------
 pkg/plugin/loader.go                               |  7 +--
 pkg/plugin/loader_test.go                          | 20 ++++++---
 {server => pkg}/plugin/plugin.go                   | 24 ++++++++---
 .../tlsconf.go => pkg/plugin/plugin_test.go        | 41 ++++++++----------
 pkg/plugin/types.go                                | 50 ++++++++++++++++++++++
 server/config/config.go                            |  2 +-
 server/config/types.go                             | 10 +++++
 server/metrics/sys.go                              |  6 +++
 server/plugin/auditlog/auditlog.go                 |  2 +-
 server/plugin/auth/auth.go                         |  2 +-
 server/plugin/auth/buildin/buildin.go              |  6 +--
 server/plugin/common.go                            | 42 ------------------
 server/plugin/plugin_test.go                       | 16 +++----
 server/plugin/quota/buildin/buildin.go             |  8 ++--
 server/plugin/quota/quota.go                       |  8 ++--
 server/plugin/security/cipher/buildin/buildin.go   | 10 ++---
 server/plugin/security/cipher/cipher.go            |  2 +-
 server/plugin/security/tlsconf/buildin/buildin.go  | 10 ++---
 server/plugin/security/tlsconf/tlsconf.go          |  2 +-
 server/plugin/tracing/pzipkin/buildin.go           |  6 +--
 server/plugin/tracing/tracing.go                   |  2 +-
 server/plugin/types.go                             | 41 ------------------
 server/plugin/uuid/buildin/buildin.go              | 10 ++---
 server/plugin/uuid/context/context.go              |  6 +--
 server/plugin/uuid/uuid.go                         |  2 +-
 server/server.go                                   |  2 +-
 29 files changed, 193 insertions(+), 203 deletions(-)

diff --git a/datasource/mongo/mongo.go b/datasource/mongo/mongo.go
index 2d8ee89..8addf8f 100644
--- a/datasource/mongo/mongo.go
+++ b/datasource/mongo/mongo.go
@@ -19,6 +19,8 @@ package mongo
 
 import (
 	"context"
+	"fmt"
+	"github.com/apache/servicecomb-service-center/pkg/util"
 
 	"github.com/go-chassis/go-chassis/v2/storage"
 	"go.mongodb.org/mongo-driver/mongo"
@@ -91,7 +93,7 @@ func (ds *DataSource) initPlugins() error {
 }
 
 func (ds *DataSource) initClient() error {
-	uri := config.GetString("registry.mongo.cluster.uri", "mongodb://localhost:27017")
+	uri := config.GetString("registry.mongo.cluster.uri", "mongodb://localhost:27017", config.WithStandby("manager_cluster"))
 	sslEnable := config.GetBool("registry.mongo.cluster.sslEnabled", false)
 	rootCA := config.GetString("registry.mongo.cluster.rootCAFile", "/opt/ssl/ca.crt")
 	verifyPeer := config.GetBool("registry.mongo.cluster.verifyPeer", false)
@@ -213,7 +215,7 @@ func wrapCreateCollectionError(err error) {
 		if ok && cmdErr.Code == client.CollectionsExists {
 			return
 		}
-		log.Fatal("failed to create collection with validation", err)
+		log.Fatal(fmt.Sprintf("failed to create collection with validation, err type: %s", util.Reflect(err).FullName), err)
 	}
 }
 
@@ -224,7 +226,7 @@ func wrapCreateIndexesError(err error) {
 		if ok && cmdErr.Code == client.DuplicateKey {
 			return
 		}
-		log.Fatal("failed to create indexes ", err)
+		log.Fatal(fmt.Sprintf("failed to create indexes, err type: %s", util.Reflect(err).FullName), err)
 	}
 }
 
diff --git a/server/plugin/uuid/uuid.go b/pkg/plugin/common.go
similarity index 63%
copy from server/plugin/uuid/uuid.go
copy to pkg/plugin/common.go
index 9723011..1317baa 100644
--- a/server/plugin/uuid/uuid.go
+++ b/pkg/plugin/common.go
@@ -15,25 +15,29 @@
  * limitations under the License.
  */
 
-package uuid
+package plugin
 
 import (
-	"context"
+	pg "plugin"
 
-	"github.com/apache/servicecomb-service-center/pkg/util"
-	"github.com/apache/servicecomb-service-center/server/plugin"
+	"github.com/apache/servicecomb-service-center/pkg/log"
 )
 
 const (
-	UUID       plugin.Kind = "uuid"
-	ContextKey util.CtxKey = "_uuid_key"
+	Buildin = "buildin"
+	Static  = "static"
+	Dynamic = "dynamic"
 )
 
-type IDGenerator interface {
-	GetServiceID(ctx context.Context) string
-	GetInstanceID(ctx context.Context) string
-}
+// DynamicPluginFunc should be called in buildin implement
+func DynamicPluginFunc(pn Kind, funcName string) pg.Symbol {
+	if !Plugins().IsDynamicPlugin(pn) {
+		return nil
+	}
 
-func Generator() IDGenerator {
-	return plugin.Plugins().Instance(UUID).(IDGenerator)
+	f, err := FindFunc(pn.String(), funcName)
+	if err != nil {
+		log.Errorf(err, "plugin '%s': not implemented function '%s'", pn, funcName)
+	}
+	return f
 }
diff --git a/server/plugin/auth/auth.go b/pkg/plugin/config.go
similarity index 70%
copy from server/plugin/auth/auth.go
copy to pkg/plugin/config.go
index 57a371d..7a9f982 100644
--- a/server/plugin/auth/auth.go
+++ b/pkg/plugin/config.go
@@ -15,24 +15,15 @@
  * limitations under the License.
  */
 
-package auth
+package plugin
 
-import (
-	"net/http"
-
-	"github.com/apache/servicecomb-service-center/server/plugin"
-)
-
-const AUTH plugin.Kind = "auth"
-
-type Authenticate interface {
-	Identify(r *http.Request) error
+// DefaultConfigurator is the default impl of Configurator
+type DefaultConfigurator struct {
 }
 
-func Auth() Authenticate {
-	return plugin.Plugins().Instance(AUTH).(Authenticate)
+func (c *DefaultConfigurator) GetImplName(_ Kind) string {
+	return Buildin
 }
-
-func Identify(r *http.Request) error {
-	return Auth().Identify(r)
+func (c *DefaultConfigurator) GetPluginDir() string {
+	return ""
 }
diff --git a/pkg/plugin/loader.go b/pkg/plugin/loader.go
index e7dacb1..63b3b8f 100644
--- a/pkg/plugin/loader.go
+++ b/pkg/plugin/loader.go
@@ -41,7 +41,6 @@ type wrapPlugin struct {
 }
 
 type Loader struct {
-	Dir     string
 	Plugins map[string]*wrapPlugin
 	mux     sync.RWMutex
 }
@@ -56,7 +55,7 @@ func (pm *Loader) Init() {
 }
 
 func (pm *Loader) ReloadPlugins() error {
-	dir := os.ExpandEnv(pm.Dir)
+	dir := os.ExpandEnv(GetConfigurator().GetPluginDir())
 	if len(dir) == 0 {
 		dir, _ = os.Getwd()
 	}
@@ -128,10 +127,6 @@ func (pm *Loader) Exist(pluginName string) bool {
 	return ok
 }
 
-func SetPluginDir(dir string) {
-	loader.Dir = dir
-}
-
 func GetLoader() *Loader {
 	once.Do(loader.Init)
 	return &loader
diff --git a/pkg/plugin/loader_test.go b/pkg/plugin/loader_test.go
index 5fca70c..a19ef0a 100644
--- a/pkg/plugin/loader_test.go
+++ b/pkg/plugin/loader_test.go
@@ -31,6 +31,16 @@ func TestLoader_Init(t *testing.T) {
 	loader.Init()
 }
 
+type testLoaderConfigurator struct {
+}
+
+func (c *testLoaderConfigurator) GetImplName(_ Kind) string {
+	return "test"
+}
+func (c *testLoaderConfigurator) GetPluginDir() string {
+	return "dir"
+}
+
 func TestLoader_ReloadPlugins(t *testing.T) {
 	loader := Loader{}
 	loader.Init()
@@ -39,11 +49,15 @@ func TestLoader_ReloadPlugins(t *testing.T) {
 		t.Fatalf(`TestLoader_ReloadPlugins failed, %s`, err.Error())
 	}
 
-	loader.Dir = "xxx"
+	old := GetConfigurator()
+	RegisterConfigurator(&testLoaderConfigurator{})
+
 	err = loader.ReloadPlugins()
 	if err == nil {
 		t.Fatalf(`TestLoader_ReloadPlugins failed`)
 	}
+
+	RegisterConfigurator(old)
 }
 
 func TestLoader_Exist(t *testing.T) {
@@ -70,10 +84,6 @@ func TestLoader_Find(t *testing.T) {
 	}
 }
 
-func TestSetPluginDir(t *testing.T) {
-	SetPluginDir("")
-}
-
 func TestPluginLoader(t *testing.T) {
 	loader := GetLoader()
 	if loader == nil {
diff --git a/server/plugin/plugin.go b/pkg/plugin/plugin.go
similarity index 90%
rename from server/plugin/plugin.go
rename to pkg/plugin/plugin.go
index ec862f7..95dee41 100644
--- a/server/plugin/plugin.go
+++ b/pkg/plugin/plugin.go
@@ -20,10 +20,7 @@ package plugin
 import (
 	"sync"
 
-	"github.com/apache/servicecomb-service-center/server/config"
-
 	"github.com/apache/servicecomb-service-center/pkg/log"
-	"github.com/apache/servicecomb-service-center/pkg/plugin"
 	"github.com/apache/servicecomb-service-center/pkg/util"
 )
 
@@ -32,7 +29,10 @@ const (
 	defaultPluginImplSize = 5
 )
 
-var pluginMgr = &Manager{}
+var (
+	globalConfigurator Configurator = &DefaultConfigurator{}
+	pluginMgr                       = &Manager{}
+)
 
 func init() {
 	pluginMgr.Initialize()
@@ -145,7 +145,10 @@ func (pm *Manager) New(pn Kind) {
 			return
 		}
 
-		name := config.GetString(pn.String()+".kind", Buildin, config.WithStandby(pn.String()+"_plugin"))
+		name := GetConfigurator().GetImplName(pn)
+		if len(name) == 0 {
+			log.Warn("configurator return plugin implement name is empty")
+		}
 		p, ok = m[ImplName(name)]
 		if !ok {
 			return
@@ -173,7 +176,7 @@ func (pm *Manager) existDynamicPlugin(pn Kind) *Plugin {
 		return nil
 	}
 	// 'buildin' implement of all plugins should call DynamicPluginFunc()
-	if plugin.GetLoader().Exist(pn.String()) {
+	if GetLoader().Exist(pn.String()) {
 		return m[Buildin]
 	}
 	return nil
@@ -200,3 +203,12 @@ func LoadPlugins() {
 		pluginMgr.Instance(p)
 	}
 }
+
+func GetConfigurator() Configurator {
+	return globalConfigurator
+}
+
+// RegisterConfigurator registers the customize Configurator impl
+func RegisterConfigurator(cfg Configurator) {
+	globalConfigurator = cfg
+}
diff --git a/server/plugin/security/tlsconf/tlsconf.go b/pkg/plugin/plugin_test.go
similarity index 56%
copy from server/plugin/security/tlsconf/tlsconf.go
copy to pkg/plugin/plugin_test.go
index 8d3814b..0d7532f 100644
--- a/server/plugin/security/tlsconf/tlsconf.go
+++ b/pkg/plugin/plugin_test.go
@@ -14,37 +14,30 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
-package tlsconf
+package plugin
 
 import (
-	"crypto/tls"
-
-	"github.com/apache/servicecomb-service-center/server/plugin"
+	"github.com/stretchr/testify/assert"
+	"testing"
 )
 
-const TLS plugin.Kind = "ssl"
-
-var options Options
-
-type TLSConfig interface {
-	ClientConfig() (*tls.Config, error)
-	ServerConfig() (*tls.Config, error)
+type testPluginConfigurator struct {
 }
 
-func ClientConfig() (*tls.Config, error) {
-	return plugin.Plugins().Instance(TLS).(TLSConfig).ClientConfig()
+func (c *testPluginConfigurator) GetImplName(_ Kind) string {
+	return "test"
 }
-
-func ServerConfig() (*tls.Config, error) {
-	return plugin.Plugins().Instance(TLS).(TLSConfig).ServerConfig()
-}
-
-func Init(opts Options) error {
-	options = opts
-	return nil
+func (c *testPluginConfigurator) GetPluginDir() string {
+	return "dir"
 }
 
-func GetOptions() Options {
-	return options
+func TestRegisterConfigurator(t *testing.T) {
+	t.Run("default configurator should not be nil", func(t *testing.T) {
+		assert.NotNil(t, GetConfigurator())
+	})
+	t.Run("register a customize configurator", func(t *testing.T) {
+		RegisterConfigurator(&testPluginConfigurator{})
+		assert.Equal(t, "test", GetConfigurator().GetImplName(""))
+		assert.Equal(t, "dir", GetConfigurator().GetPluginDir())
+	})
 }
diff --git a/pkg/plugin/types.go b/pkg/plugin/types.go
new file mode 100644
index 0000000..43ee517
--- /dev/null
+++ b/pkg/plugin/types.go
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package plugin
+
+// Kind is an alias, it represents a plugin interface.
+type Kind string
+
+// ImplName is an alias,it represents a plugin interface implementation.
+type ImplName string
+
+// Instance is an instance of a plugin interface which is represented by
+// Kind.
+type Instance interface{}
+
+// String implements fmt.Stringer.
+func (pn Kind) String() string {
+	return string(pn)
+}
+
+// Plugin generates a plugin instance
+// Plugin holds the 'Kind' and 'ImplName'
+// to manage the plugin instance generation.
+type Plugin struct {
+	Kind Kind
+	Name ImplName
+	// New news an instance of 'Kind' represented plugin interface
+	New func() Instance
+}
+
+// Configurator provides the interfaces for obtaining configs in
+// plugin mgr's operations, user can customize the impl
+type Configurator interface {
+	GetImplName(kind Kind) string
+	GetPluginDir() string
+}
diff --git a/server/config/config.go b/server/config/config.go
index a4573f3..9a08fd3 100644
--- a/server/config/config.go
+++ b/server/config/config.go
@@ -104,7 +104,7 @@ func Init() {
 	Configurations.Server = ServerInfo
 	*ServerInfo = newInfo()
 
-	plugin.SetPluginDir(GetPlugin().PluginsDir)
+	plugin.RegisterConfigurator(Configurations)
 
 	version.Ver().Log()
 }
diff --git a/server/config/types.go b/server/config/types.go
index 3ca396e..29f60c3 100644
--- a/server/config/types.go
+++ b/server/config/types.go
@@ -17,6 +17,8 @@
 
 package config
 
+import "github.com/apache/servicecomb-service-center/pkg/plugin"
+
 //Config is yaml file struct
 type Config struct {
 	Gov    *Gov               `yaml:"gov"`
@@ -30,3 +32,11 @@ type DistributorOptions struct {
 	Type     string `yaml:"type"`
 	Endpoint string `yaml:"endpoint"`
 }
+
+// GetImplName return the impl name
+func (c *Config) GetImplName(kind plugin.Kind) string {
+	return GetString(kind.String()+".kind", plugin.Buildin, WithStandby(kind.String()+"_plugin"))
+}
+func (c *Config) GetPluginDir() string {
+	return c.Server.Config.PluginsDir
+}
diff --git a/server/metrics/sys.go b/server/metrics/sys.go
index 526bd85..73c9768 100644
--- a/server/metrics/sys.go
+++ b/server/metrics/sys.go
@@ -19,6 +19,7 @@ package metrics
 
 import (
 	"context"
+	"fmt"
 	"runtime"
 	"time"
 
@@ -58,6 +59,11 @@ func AutoReportCPUUsage(ctx context.Context) {
 			return
 		case <-time.After(durationReportCPUUsage):
 			pt, ct := util.GetProcCPUUsage()
+			if pt <= 0 && ct <= 0 {
+				log.Warn(fmt.Sprintf("can not get proc cpu usage of current os %s/%s", runtime.GOOS, runtime.GOARCH))
+				return
+			}
+
 			diff := ct - cpuTotal
 			if diff <= 0 {
 				log.Warnf("the current cpu usage is the same as the previous period")
diff --git a/server/plugin/auditlog/auditlog.go b/server/plugin/auditlog/auditlog.go
index b54b471..d6033ca 100644
--- a/server/plugin/auditlog/auditlog.go
+++ b/server/plugin/auditlog/auditlog.go
@@ -20,7 +20,7 @@ package auditlog
 import (
 	"net/http"
 
-	"github.com/apache/servicecomb-service-center/server/plugin"
+	"github.com/apache/servicecomb-service-center/pkg/plugin"
 )
 
 const AUDITLOG plugin.Kind = "auditlog"
diff --git a/server/plugin/auth/auth.go b/server/plugin/auth/auth.go
index 57a371d..0cad5b3 100644
--- a/server/plugin/auth/auth.go
+++ b/server/plugin/auth/auth.go
@@ -20,7 +20,7 @@ package auth
 import (
 	"net/http"
 
-	"github.com/apache/servicecomb-service-center/server/plugin"
+	"github.com/apache/servicecomb-service-center/pkg/plugin"
 )
 
 const AUTH plugin.Kind = "auth"
diff --git a/server/plugin/auth/buildin/buildin.go b/server/plugin/auth/buildin/buildin.go
index ea178a2..0e94862 100644
--- a/server/plugin/auth/buildin/buildin.go
+++ b/server/plugin/auth/buildin/buildin.go
@@ -20,6 +20,7 @@ package buildin
 import (
 	"context"
 	"errors"
+	"github.com/apache/servicecomb-service-center/pkg/plugin"
 	"net/http"
 	"strings"
 
@@ -27,7 +28,6 @@ import (
 	"github.com/apache/servicecomb-service-center/pkg/log"
 	"github.com/apache/servicecomb-service-center/pkg/rbacframe"
 	"github.com/apache/servicecomb-service-center/pkg/rest"
-	mgr "github.com/apache/servicecomb-service-center/server/plugin"
 	"github.com/apache/servicecomb-service-center/server/plugin/auth"
 	"github.com/apache/servicecomb-service-center/server/service/rbac"
 	"github.com/go-chassis/go-chassis/v2/security/authr"
@@ -35,10 +35,10 @@ import (
 )
 
 func init() {
-	mgr.RegisterPlugin(mgr.Plugin{Kind: auth.AUTH, Name: "buildin", New: New})
+	plugin.RegisterPlugin(plugin.Plugin{Kind: auth.AUTH, Name: "buildin", New: New})
 }
 
-func New() mgr.Instance {
+func New() plugin.Instance {
 	return &TokenAuthenticator{}
 }
 
diff --git a/server/plugin/common.go b/server/plugin/common.go
deleted file mode 100644
index 47282e3..0000000
--- a/server/plugin/common.go
+++ /dev/null
@@ -1,42 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one or more
-// contributor license agreements.  See the NOTICE file distributed with
-// this work for additional information regarding copyright ownership.
-// The ASF licenses this file to You under the Apache License, Version 2.0
-// (the "License"); you may not use this file except in compliance with
-// the License.  You may obtain a copy of the License at
-//
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package plugin
-
-import (
-	pg "plugin"
-
-	"github.com/apache/servicecomb-service-center/pkg/log"
-	"github.com/apache/servicecomb-service-center/pkg/plugin"
-)
-
-const (
-	Buildin = "buildin"
-	Static  = "static"
-	Dynamic = "dynamic"
-)
-
-// DynamicPluginFunc should be called in buildin implement
-func DynamicPluginFunc(pn Kind, funcName string) pg.Symbol {
-	if !Plugins().IsDynamicPlugin(pn) {
-		return nil
-	}
-
-	f, err := plugin.FindFunc(pn.String(), funcName)
-	if err != nil {
-		log.Errorf(err, "plugin '%s': not implemented function '%s'", pn, funcName)
-	}
-	return f
-}
diff --git a/server/plugin/plugin_test.go b/server/plugin/plugin_test.go
index 2cbd619..1a87338 100644
--- a/server/plugin/plugin_test.go
+++ b/server/plugin/plugin_test.go
@@ -14,14 +14,14 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package plugin_test
+package plugin
 
 import (
+	"github.com/apache/servicecomb-service-center/pkg/plugin"
 	"net/http"
 	"testing"
 
 	"github.com/apache/servicecomb-service-center/server/config"
-	. "github.com/apache/servicecomb-service-center/server/plugin"
 	"github.com/apache/servicecomb-service-center/server/plugin/auth"
 )
 
@@ -38,7 +38,7 @@ func (*mockAuthPlugin) Identify(r *http.Request) error {
 }
 
 func TestPluginManager_New(t *testing.T) {
-	pm := &Manager{}
+	pm := &plugin.Manager{}
 	pm.Initialize()
 
 	p := pm.Get(auth.AUTH, "buildin")
@@ -47,11 +47,11 @@ func TestPluginManager_New(t *testing.T) {
 	}
 
 	times := 0
-	fn := func() Instance {
+	fn := func() plugin.Instance {
 		times++
 		return &mockAuthPlugin{times}
 	}
-	pm.Register(Plugin{auth.AUTH, "buildin", fn})
+	pm.Register(plugin.Plugin{auth.AUTH, "buildin", fn})
 
 	i := pm.Instance(auth.AUTH)
 	if i != pm.Instance(auth.AUTH) {
@@ -69,8 +69,8 @@ func TestPluginManager_New(t *testing.T) {
 			t.Fatalf("TestPluginManager_New failed")
 		}
 	}()
-	RegisterPlugin(Plugin{Kind(999), "999", nil})
-	DynamicPluginFunc(Kind(999), "999")
+	plugin.RegisterPlugin(plugin.Plugin{plugin.Kind(999), "999", nil})
+	plugin.DynamicPluginFunc(plugin.Kind(999), "999")
 
-	LoadPlugins()
+	plugin.LoadPlugins()
 }
diff --git a/server/plugin/quota/buildin/buildin.go b/server/plugin/quota/buildin/buildin.go
index 8e08be4..407d1e3 100644
--- a/server/plugin/quota/buildin/buildin.go
+++ b/server/plugin/quota/buildin/buildin.go
@@ -20,15 +20,15 @@ package buildin
 import (
 	"context"
 	"github.com/apache/servicecomb-service-center/pkg/log"
-	mgr "github.com/apache/servicecomb-service-center/server/plugin"
+	"github.com/apache/servicecomb-service-center/pkg/plugin"
 	"github.com/apache/servicecomb-service-center/server/plugin/quota"
 )
 
 func init() {
-	mgr.RegisterPlugin(mgr.Plugin{Kind: quota.QUOTA, Name: "buildin", New: New})
+	plugin.RegisterPlugin(plugin.Plugin{Kind: quota.QUOTA, Name: "buildin", New: New})
 }
 
-func New() mgr.Instance {
+func New() plugin.Instance {
 	quota.Init()
 	log.Infof("quota init, service: %d, instance: %d, schema: %d/service, tag: %d/service, rule: %d/service",
 		quota.DefaultServiceQuota, quota.DefaultInstanceQuota,
@@ -58,7 +58,7 @@ func (q *Quota) GetQuota(ctx context.Context, t quota.ResourceType) int64 {
 
 //向配额中心上报配额使用量
 func (q *Quota) RemandQuotas(ctx context.Context, quotaType quota.ResourceType) {
-	df, ok := mgr.DynamicPluginFunc(quota.QUOTA, "RemandQuotas").(func(context.Context, quota.ResourceType))
+	df, ok := plugin.DynamicPluginFunc(quota.QUOTA, "RemandQuotas").(func(context.Context, quota.ResourceType))
 	if ok {
 		df(ctx, quotaType)
 		return
diff --git a/server/plugin/quota/quota.go b/server/plugin/quota/quota.go
index 971e0ca..1c7d2b8 100644
--- a/server/plugin/quota/quota.go
+++ b/server/plugin/quota/quota.go
@@ -21,14 +21,14 @@ import (
 	"context"
 	"errors"
 	"fmt"
+	"strconv"
+
 	"github.com/apache/servicecomb-service-center/datasource"
 	"github.com/apache/servicecomb-service-center/pkg/log"
+	"github.com/apache/servicecomb-service-center/pkg/plugin"
 	"github.com/apache/servicecomb-service-center/pkg/util"
-	"github.com/apache/servicecomb-service-center/server/metrics"
-	"strconv"
-
 	"github.com/apache/servicecomb-service-center/server/config"
-	"github.com/apache/servicecomb-service-center/server/plugin"
+	"github.com/apache/servicecomb-service-center/server/metrics"
 	pb "github.com/go-chassis/cari/discovery"
 )
 
diff --git a/server/plugin/security/cipher/buildin/buildin.go b/server/plugin/security/cipher/buildin/buildin.go
index 2eafb2d..77d53e4 100644
--- a/server/plugin/security/cipher/buildin/buildin.go
+++ b/server/plugin/security/cipher/buildin/buildin.go
@@ -18,15 +18,15 @@
 package plain
 
 import (
-	mgr "github.com/apache/servicecomb-service-center/server/plugin"
+	"github.com/apache/servicecomb-service-center/pkg/plugin"
 	"github.com/apache/servicecomb-service-center/server/plugin/security/cipher"
 )
 
 func init() {
-	mgr.RegisterPlugin(mgr.Plugin{Kind: cipher.CIPHER, Name: "buildin", New: New})
+	plugin.RegisterPlugin(plugin.Plugin{Kind: cipher.CIPHER, Name: "buildin", New: New})
 }
 
-func New() mgr.Instance {
+func New() plugin.Instance {
 	return &DefaultCipher{}
 }
 
@@ -34,7 +34,7 @@ type DefaultCipher struct {
 }
 
 func (c *DefaultCipher) Encrypt(src string) (string, error) {
-	df, ok := mgr.DynamicPluginFunc(cipher.CIPHER, "Encrypt").(func(src string) (string, error))
+	df, ok := plugin.DynamicPluginFunc(cipher.CIPHER, "Encrypt").(func(src string) (string, error))
 	if ok {
 		return df(src)
 	}
@@ -42,7 +42,7 @@ func (c *DefaultCipher) Encrypt(src string) (string, error) {
 }
 
 func (c *DefaultCipher) Decrypt(src string) (string, error) {
-	df, ok := mgr.DynamicPluginFunc(cipher.CIPHER, "Decrypt").(func(src string) (string, error))
+	df, ok := plugin.DynamicPluginFunc(cipher.CIPHER, "Decrypt").(func(src string) (string, error))
 	if ok {
 		return df(src)
 	}
diff --git a/server/plugin/security/cipher/cipher.go b/server/plugin/security/cipher/cipher.go
index 23d9295..1dc11ca 100644
--- a/server/plugin/security/cipher/cipher.go
+++ b/server/plugin/security/cipher/cipher.go
@@ -18,7 +18,7 @@
 package cipher
 
 import (
-	"github.com/apache/servicecomb-service-center/server/plugin"
+	"github.com/apache/servicecomb-service-center/pkg/plugin"
 	"github.com/go-chassis/foundation/security"
 )
 
diff --git a/server/plugin/security/tlsconf/buildin/buildin.go b/server/plugin/security/tlsconf/buildin/buildin.go
index 059473a..e31aeba 100644
--- a/server/plugin/security/tlsconf/buildin/buildin.go
+++ b/server/plugin/security/tlsconf/buildin/buildin.go
@@ -19,16 +19,16 @@ package buildin
 
 import (
 	"crypto/tls"
+	"github.com/apache/servicecomb-service-center/pkg/plugin"
 
-	mgr "github.com/apache/servicecomb-service-center/server/plugin"
 	"github.com/apache/servicecomb-service-center/server/plugin/security/tlsconf"
 )
 
 func init() {
-	mgr.RegisterPlugin(mgr.Plugin{Kind: tlsconf.TLS, Name: "buildin", New: New})
+	plugin.RegisterPlugin(plugin.Plugin{Kind: tlsconf.TLS, Name: "buildin", New: New})
 }
 
-func New() mgr.Instance {
+func New() plugin.Instance {
 	return &DefaultTLS{}
 }
 
@@ -37,7 +37,7 @@ type DefaultTLS struct {
 }
 
 func (c *DefaultTLS) ClientConfig() (*tls.Config, error) {
-	df, ok := mgr.DynamicPluginFunc(tlsconf.TLS, "ClientConfig").(func() (*tls.Config, error))
+	df, ok := plugin.DynamicPluginFunc(tlsconf.TLS, "ClientConfig").(func() (*tls.Config, error))
 	if ok {
 		return df()
 	}
@@ -45,7 +45,7 @@ func (c *DefaultTLS) ClientConfig() (*tls.Config, error) {
 }
 
 func (c *DefaultTLS) ServerConfig() (*tls.Config, error) {
-	df, ok := mgr.DynamicPluginFunc(tlsconf.TLS, "ServerConfig").(func() (*tls.Config, error))
+	df, ok := plugin.DynamicPluginFunc(tlsconf.TLS, "ServerConfig").(func() (*tls.Config, error))
 	if ok {
 		return df()
 	}
diff --git a/server/plugin/security/tlsconf/tlsconf.go b/server/plugin/security/tlsconf/tlsconf.go
index 8d3814b..8162486 100644
--- a/server/plugin/security/tlsconf/tlsconf.go
+++ b/server/plugin/security/tlsconf/tlsconf.go
@@ -20,7 +20,7 @@ package tlsconf
 import (
 	"crypto/tls"
 
-	"github.com/apache/servicecomb-service-center/server/plugin"
+	"github.com/apache/servicecomb-service-center/pkg/plugin"
 )
 
 const TLS plugin.Kind = "ssl"
diff --git a/server/plugin/tracing/pzipkin/buildin.go b/server/plugin/tracing/pzipkin/buildin.go
index 2c7ed41..abdba48 100644
--- a/server/plugin/tracing/pzipkin/buildin.go
+++ b/server/plugin/tracing/pzipkin/buildin.go
@@ -19,13 +19,13 @@ package pzipkin
 
 import (
 	"context"
+	"github.com/apache/servicecomb-service-center/pkg/plugin"
 	"net/http"
 	"net/url"
 	"sync"
 
 	"github.com/apache/servicecomb-service-center/pkg/log"
 	"github.com/apache/servicecomb-service-center/pkg/util"
-	mgr "github.com/apache/servicecomb-service-center/server/plugin"
 	"github.com/apache/servicecomb-service-center/server/plugin/tracing"
 	"github.com/opentracing/opentracing-go"
 	"github.com/opentracing/opentracing-go/ext"
@@ -35,10 +35,10 @@ import (
 var once sync.Once
 
 func init() {
-	mgr.RegisterPlugin(mgr.Plugin{Kind: tracing.TRACING, Name: "buildin", New: New})
+	plugin.RegisterPlugin(plugin.Plugin{Kind: tracing.TRACING, Name: "buildin", New: New})
 }
 
-func New() mgr.Instance {
+func New() plugin.Instance {
 	return &Zipkin{}
 }
 
diff --git a/server/plugin/tracing/tracing.go b/server/plugin/tracing/tracing.go
index f54ca45..8271063 100644
--- a/server/plugin/tracing/tracing.go
+++ b/server/plugin/tracing/tracing.go
@@ -20,8 +20,8 @@ package tracing
 import (
 	"context"
 
+	"github.com/apache/servicecomb-service-center/pkg/plugin"
 	"github.com/apache/servicecomb-service-center/pkg/util"
-	"github.com/apache/servicecomb-service-center/server/plugin"
 )
 
 const (
diff --git a/server/plugin/types.go b/server/plugin/types.go
deleted file mode 100644
index 6ad544d..0000000
--- a/server/plugin/types.go
+++ /dev/null
@@ -1,41 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one or more
-// contributor license agreements.  See the NOTICE file distributed with
-// this work for additional information regarding copyright ownership.
-// The ASF licenses this file to You under the Apache License, Version 2.0
-// (the "License"); you may not use this file except in compliance with
-// the License.  You may obtain a copy of the License at
-//
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package plugin
-
-// Kind is an alias, it represents a plugin interface.
-type Kind string
-
-// ImplName is an alias,it represents a plugin interface implementation.
-type ImplName string
-
-// Instance is an instance of a plugin interface which is represented by
-// Kind.
-type Instance interface{}
-
-// String implements fmt.Stringer.
-func (pn Kind) String() string {
-	return string(pn)
-}
-
-// Plugin generates a plugin instance
-// Plugin holds the 'Kind' and 'ImplName'
-// to manage the plugin instance generation.
-type Plugin struct {
-	Kind Kind
-	Name ImplName
-	// New news an instance of 'Kind' represented plugin interface
-	New func() Instance
-}
diff --git a/server/plugin/uuid/buildin/buildin.go b/server/plugin/uuid/buildin/buildin.go
index 2f7964e..88c5b89 100644
--- a/server/plugin/uuid/buildin/buildin.go
+++ b/server/plugin/uuid/buildin/buildin.go
@@ -19,17 +19,17 @@ package buildin
 
 import (
 	"context"
+	"github.com/apache/servicecomb-service-center/pkg/plugin"
 
 	"github.com/apache/servicecomb-service-center/pkg/util"
-	mgr "github.com/apache/servicecomb-service-center/server/plugin"
 	"github.com/apache/servicecomb-service-center/server/plugin/uuid"
 )
 
 func init() {
-	mgr.RegisterPlugin(mgr.Plugin{Kind: uuid.UUID, Name: "buildin", New: New})
+	plugin.RegisterPlugin(plugin.Plugin{Kind: uuid.UUID, Name: "buildin", New: New})
 }
 
-func New() mgr.Instance {
+func New() plugin.Instance {
 	return &UUID{}
 }
 
@@ -37,7 +37,7 @@ type UUID struct {
 }
 
 func (du *UUID) GetServiceID(_ context.Context) string {
-	df, ok := mgr.DynamicPluginFunc(uuid.UUID, "GetServiceID").(func() string)
+	df, ok := plugin.DynamicPluginFunc(uuid.UUID, "GetServiceID").(func() string)
 	if ok {
 		return df()
 	}
@@ -45,7 +45,7 @@ func (du *UUID) GetServiceID(_ context.Context) string {
 }
 
 func (du *UUID) GetInstanceID(_ context.Context) string {
-	df, ok := mgr.DynamicPluginFunc(uuid.UUID, "GetInstanceID").(func() string)
+	df, ok := plugin.DynamicPluginFunc(uuid.UUID, "GetInstanceID").(func() string)
 	if ok {
 		return df()
 	}
diff --git a/server/plugin/uuid/context/context.go b/server/plugin/uuid/context/context.go
index 9f9dde9..0860545 100644
--- a/server/plugin/uuid/context/context.go
+++ b/server/plugin/uuid/context/context.go
@@ -21,18 +21,18 @@ import (
 	"context"
 	"crypto/sha1"
 	"fmt"
+	"github.com/apache/servicecomb-service-center/pkg/plugin"
 
 	"github.com/apache/servicecomb-service-center/pkg/util"
-	mgr "github.com/apache/servicecomb-service-center/server/plugin"
 	"github.com/apache/servicecomb-service-center/server/plugin/uuid"
 	"github.com/apache/servicecomb-service-center/server/plugin/uuid/buildin"
 )
 
 func init() {
-	mgr.RegisterPlugin(mgr.Plugin{Kind: uuid.UUID, Name: "context", New: New})
+	plugin.RegisterPlugin(plugin.Plugin{Kind: uuid.UUID, Name: "context", New: New})
 }
 
-func New() mgr.Instance {
+func New() plugin.Instance {
 	return &UUID{}
 }
 
diff --git a/server/plugin/uuid/uuid.go b/server/plugin/uuid/uuid.go
index 9723011..63d0836 100644
--- a/server/plugin/uuid/uuid.go
+++ b/server/plugin/uuid/uuid.go
@@ -20,8 +20,8 @@ package uuid
 import (
 	"context"
 
+	"github.com/apache/servicecomb-service-center/pkg/plugin"
 	"github.com/apache/servicecomb-service-center/pkg/util"
-	"github.com/apache/servicecomb-service-center/server/plugin"
 )
 
 const (
diff --git a/server/server.go b/server/server.go
index ba69652..13d9546 100644
--- a/server/server.go
+++ b/server/server.go
@@ -28,13 +28,13 @@ import (
 	"github.com/apache/servicecomb-service-center/pkg/log"
 	"github.com/apache/servicecomb-service-center/pkg/metrics"
 	nf "github.com/apache/servicecomb-service-center/pkg/notify"
+	"github.com/apache/servicecomb-service-center/pkg/plugin"
 	"github.com/apache/servicecomb-service-center/pkg/signal"
 	"github.com/apache/servicecomb-service-center/pkg/util"
 	"github.com/apache/servicecomb-service-center/server/command"
 	"github.com/apache/servicecomb-service-center/server/config"
 	"github.com/apache/servicecomb-service-center/server/core"
 	"github.com/apache/servicecomb-service-center/server/notify"
-	"github.com/apache/servicecomb-service-center/server/plugin"
 	"github.com/apache/servicecomb-service-center/server/plugin/security/tlsconf"
 	"github.com/apache/servicecomb-service-center/server/service/gov"
 	"github.com/apache/servicecomb-service-center/server/service/rbac"