diff --git a/api/config.go b/api/config.go index be5f384c..66a52d09 100644 --- a/api/config.go +++ b/api/config.go @@ -27,7 +27,6 @@ import ( "sync/atomic" "time" - "github.com/google/uuid" "github.com/hashicorp/go-multierror" "github.com/modern-go/reflect2" "gopkg.in/yaml.v2" @@ -261,20 +260,21 @@ func InitContextByConfig(cfg config.Configuration) (SDKContext, error) { if err := cfg.Verify(); err != nil { return nil, model.NewSDKError(model.ErrCodeAPIInvalidConfig, err, "fail to verify input config") } - getSelfIP(cfg) - token := model.SDKToken{ + initSelfIP(cfg) + token := &model.SDKToken{ IP: cfg.GetGlobal().GetAPI().GetBindIP(), PID: int32(os.Getpid()), - UID: strings.ToUpper(uuid.New().String()), Client: version.ClientType, Version: version.Version, PodName: getPodName(), HostName: getHostName(), } + token.InitUID() + initSelfLabels(cfg, token) log.GetBaseLogger().Infof("\n-------Start to init SDKContext of version %s, IP: %s, PID: %d, UID: %s, CONTAINER: "+"%s, HOSTNAME:%s-------", version.Version, token.IP, token.PID, token.UID, token.PodName, token.HostName) - globalCtx.SetValue(model.ContextKeyToken, token) + globalCtx.SetValue(model.ContextKeyToken, *token) plugManager := plugin.NewPluginManager() globalCtx.SetValue(model.ContextKeyPlugins, plugManager) connManager, err := network.NewConnectionManager(cfg, globalCtx) @@ -321,8 +321,8 @@ func InitContextByConfig(cfg config.Configuration) (SDKContext, error) { return ctx, nil } -// getSelfIP 获取SDK自身的IP -func getSelfIP(cfg config.Configuration) { +// initSelfIP 获取SDK自身的IP +func initSelfIP(cfg config.Configuration) { bindIP := cfg.GetGlobal().GetAPI().GetBindIP() bindIntf := cfg.GetGlobal().GetAPI().GetBindIntf() if len(bindIP) != 0 || len(bindIntf) != 0 { @@ -346,6 +346,19 @@ func getSelfIP(cfg config.Configuration) { } } +func initSelfLabels(cfg config.Configuration, sdkToken *model.SDKToken) { + clientCfg := cfg.GetGlobal().GetClient().(*config.ClientConfigImpl) + if clientCfg.GetId() == "" { + clientCfg.SetId(sdkToken.UID) + } + clientCfg.AddLabels(map[string]string{ + "CLIENT_IP": cfg.GetGlobal().GetAPI().GetBindIP(), + "CLIENT_ID": clientCfg.GetId(), + "CLIENT_VERSION": sdkToken.Version, + "CLIENT_LANGUAGE": sdkToken.Client, + }) +} + // onContextInitialized 在全局上下文初始化完成后,触发事件回调,可针对不同插件做一些阻塞等待某个事件完成的操作 func onContextInitialized(ctx SDKContext) error { eventHandlers := ctx.GetPlugins().GetEventSubscribers(common.OnContextStarted) diff --git a/examples/configuration/crud/go.sum b/examples/configuration/crud/go.sum index 55fed020..ef33672a 100644 --- a/examples/configuration/crud/go.sum +++ b/examples/configuration/crud/go.sum @@ -171,7 +171,6 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= -github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw= github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -320,7 +319,6 @@ github.com/googleapis/gax-go/v2 v2.3.0/go.mod h1:b8LNqSzNabLiUpXKkY7HAR5jr6bIT99 github.com/googleapis/gax-go/v2 v2.4.0/go.mod h1:XOTVJ59hdnfJLIP/dh8n5CGryZR2LxK9wbMD5+iXC6c= github.com/googleapis/gax-go/v2 v2.5.1/go.mod h1:h6B0KMMFNtI2ddbGJn3T3ZbwkeT6yqEF02fYlzkUCyo= github.com/googleapis/go-type-adapters v1.0.0/go.mod h1:zHW75FOG2aur7gAO2B+MLby+cLsWGBF62rFAi7WjWO4= -github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= @@ -338,7 +336,6 @@ github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= -github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= @@ -401,9 +398,7 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= -github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= -github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= diff --git a/examples/configuration/normal/go.mod b/examples/configuration/normal/go.mod index ba846ada..1f119279 100644 --- a/examples/configuration/normal/go.mod +++ b/examples/configuration/normal/go.mod @@ -17,7 +17,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/natefinch/lumberjack v2.0.0+incompatible // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/polarismesh/specification v1.3.2-alpha.2 // indirect + github.com/polarismesh/specification v1.4.1 // indirect github.com/prometheus/client_golang v1.12.2 // indirect github.com/prometheus/client_model v0.2.0 // indirect github.com/prometheus/common v0.32.1 // indirect diff --git a/examples/configuration/normal/go.sum b/examples/configuration/normal/go.sum index 093a5191..d93306f5 100644 --- a/examples/configuration/normal/go.sum +++ b/examples/configuration/normal/go.sum @@ -171,7 +171,6 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= -github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw= github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -321,7 +320,6 @@ github.com/googleapis/gax-go/v2 v2.3.0/go.mod h1:b8LNqSzNabLiUpXKkY7HAR5jr6bIT99 github.com/googleapis/gax-go/v2 v2.4.0/go.mod h1:XOTVJ59hdnfJLIP/dh8n5CGryZR2LxK9wbMD5+iXC6c= github.com/googleapis/gax-go/v2 v2.5.1/go.mod h1:h6B0KMMFNtI2ddbGJn3T3ZbwkeT6yqEF02fYlzkUCyo= github.com/googleapis/go-type-adapters v1.0.0/go.mod h1:zHW75FOG2aur7gAO2B+MLby+cLsWGBF62rFAi7WjWO4= -github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= @@ -340,7 +338,6 @@ github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= -github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= @@ -375,8 +372,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/polarismesh/specification v1.3.2-alpha.2 h1:cMghyvCnRVM5ca2kYCGHOgIIxVnokiMvw0720q8a8RA= -github.com/polarismesh/specification v1.3.2-alpha.2/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= +github.com/polarismesh/specification v1.4.1 h1:lTZqeyUhhWuKyr6NDKBwmUrNfcUDvKLxWT/uOq71T5A= +github.com/polarismesh/specification v1.4.1/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M= @@ -404,9 +401,7 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= -github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= -github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= diff --git a/examples/configuration/normal/polaris.yaml b/examples/configuration/normal/polaris.yaml index fdee6156..2e43bfef 100644 --- a/examples/configuration/normal/polaris.yaml +++ b/examples/configuration/normal/polaris.yaml @@ -2,6 +2,9 @@ global: serverConnector: addresses: - 127.0.0.1:8091 + client: + labels: + env: pre config: configConnector: addresses: diff --git a/pkg/config/api.go b/pkg/config/api.go index 8ad0cf53..f0de217c 100644 --- a/pkg/config/api.go +++ b/pkg/config/api.go @@ -44,6 +44,8 @@ type GlobalConfig interface { GetStatReporter() StatReporterConfig // GetLocation global.location前缀开头的所有配置项 GetLocation() LocationConfig + // GetClient global.client前缀开头的所有配置项 + GetClient() ClientConfig } // ConsumerConfig consumer config object. @@ -213,10 +215,18 @@ type LocationConfig interface { BaseConfig // GetProvider 获取地理位置的提供者插件名称 GetProviders() []*LocationProviderConfigImpl - + // GetProvider 根据类型名称获取对应插件的配置内容信息 GetProvider(typ string) *LocationProviderConfigImpl } +type ClientConfig interface { + BaseConfig + // GetId 获取客户端ID + GetId() string + // GetLabels 获取客户端标签 + GetLabels() map[string]string +} + // ServerConnectorConfig 与名字服务服务端的连接配置. type ServerConnectorConfig interface { BaseConfig diff --git a/pkg/config/default.go b/pkg/config/default.go index aa4cb24a..b7b71de1 100644 --- a/pkg/config/default.go +++ b/pkg/config/default.go @@ -461,6 +461,8 @@ func (g *GlobalConfigImpl) Init() { g.StatReporter.Init() g.Location = &LocationConfigImpl{} g.Location.Init() + g.Client = &ClientConfigImpl{} + g.Client.Init() } // Init 初始化ConsumerConfigImpl. @@ -616,3 +618,51 @@ func (s *ServerClusterConfigImpl) Verify() error { } return errs } + +type ClientConfigImpl struct { + ID string `yaml:"id" json:"id"` + Labels map[string]string `yaml:"labels" json:"labels"` +} + +// Init 初始化 +func (c *ClientConfigImpl) Init() { +} + +func (c *ClientConfigImpl) SetId(id string) { + c.ID = id +} + +func (c *ClientConfigImpl) GetId() string { + return c.ID +} + +func (c *ClientConfigImpl) SetLabels(m map[string]string) { + c.Labels = m +} + +func (c *ClientConfigImpl) AddLabels(m map[string]string) { + if len(c.Labels) == 0 { + c.Labels = map[string]string{} + } + for k, v := range m { + c.Labels[k] = v + } +} + +func (c *ClientConfigImpl) GetLabels() map[string]string { + copyM := map[string]string{} + for k, v := range c.Labels { + copyM[k] = v + } + return copyM +} + +func (c *ClientConfigImpl) SetDefault() { + if len(c.Labels) == 0 { + c.Labels = map[string]string{} + } +} + +func (c *ClientConfigImpl) Verify() error { + return nil +} diff --git a/pkg/config/impl.go b/pkg/config/impl.go index 2f876e3e..5afaca0b 100644 --- a/pkg/config/impl.go +++ b/pkg/config/impl.go @@ -64,6 +64,7 @@ type GlobalConfigImpl struct { ServerConnector *ServerConnectorConfigImpl `yaml:"serverConnector" json:"serverConnector"` StatReporter *StatReporterConfigImpl `yaml:"statReporter" json:"statReporter"` Location *LocationConfigImpl `yaml:"location" json:"location"` + Client *ClientConfigImpl `yaml:"client" json:"client"` } // GetSystem 获取系统配置. @@ -91,6 +92,11 @@ func (g *GlobalConfigImpl) GetLocation() LocationConfig { return g.Location } +// GetClient global.client前缀开头的所有配置项. +func (g *GlobalConfigImpl) GetClient() ClientConfig { + return g.Client +} + // ConsumerConfigImpl 消费者配置. type ConsumerConfigImpl struct { LocalCache *LocalCacheConfigImpl `yaml:"localCache" json:"localCache"` diff --git a/pkg/flow/configuration/config_flow.go b/pkg/flow/configuration/config_flow.go index c9aec99e..4a0079bf 100644 --- a/pkg/flow/configuration/config_flow.go +++ b/pkg/flow/configuration/config_flow.go @@ -43,9 +43,9 @@ type ConfigFileFlow struct { configFilePool map[string]*ConfigFileRepo notifiedVersion map[string]uint64 - connector configconnector.ConfigConnector - chain configfilter.Chain - configuration config.Configuration + connector configconnector.ConfigConnector + chain configfilter.Chain + conf config.Configuration persistHandler *CachePersistHandler @@ -54,12 +54,12 @@ type ConfigFileFlow struct { // NewConfigFileFlow 创建配置中心服务 func NewConfigFileFlow(connector configconnector.ConfigConnector, chain configfilter.Chain, - configuration config.Configuration) (*ConfigFileFlow, error) { + conf config.Configuration) (*ConfigFileFlow, error) { persistHandler, err := NewCachePersistHandler( - configuration.GetConfigFile().GetLocalCache().GetPersistDir(), - configuration.GetConfigFile().GetLocalCache().GetPersistMaxWriteRetry(), - configuration.GetConfigFile().GetLocalCache().GetPersistMaxReadRetry(), - configuration.GetConfigFile().GetLocalCache().GetPersistRetryInterval(), + conf.GetConfigFile().GetLocalCache().GetPersistDir(), + conf.GetConfigFile().GetLocalCache().GetPersistMaxWriteRetry(), + conf.GetConfigFile().GetLocalCache().GetPersistMaxReadRetry(), + conf.GetConfigFile().GetLocalCache().GetPersistRetryInterval(), ) if err != nil { return nil, err @@ -68,7 +68,7 @@ func NewConfigFileFlow(connector configconnector.ConfigConnector, chain configfi configFileService := &ConfigFileFlow{ connector: connector, chain: chain, - configuration: configuration, + conf: conf, repos: make([]*ConfigFileRepo, 0, 8), configFileCache: map[string]model.ConfigFile{}, configFilePool: map[string]*ConfigFileRepo{}, @@ -112,7 +112,7 @@ func (c *ConfigFileFlow) GetConfigFile(req *model.GetConfigFileRequest) (model.C return configFile, nil } - fileRepo, err := newConfigFileRepo(configFileMetadata, c.connector, c.chain, c.configuration, c.persistHandler) + fileRepo, err := newConfigFileRepo(configFileMetadata, c.connector, c.chain, c.conf, c.persistHandler) if err != nil { return nil, err } diff --git a/pkg/flow/configuration/file_repo.go b/pkg/flow/configuration/file_repo.go index 402f03b5..08940462 100644 --- a/pkg/flow/configuration/file_repo.go +++ b/pkg/flow/configuration/file_repo.go @@ -47,9 +47,9 @@ var ( // ConfigFileRepo 服务端配置文件代理类,从服务端拉取配置并同步数据 type ConfigFileRepo struct { - connector configconnector.ConfigConnector - chain configfilter.Chain - configuration config.Configuration + connector configconnector.ConfigConnector + chain configfilter.Chain + conf config.Configuration configFileMetadata model.ConfigFileMetadata // 长轮询通知的版本号 @@ -71,12 +71,12 @@ type ConfigFileRepoChangeListener func(configFileMetadata model.ConfigFileMetada func newConfigFileRepo(metadata model.ConfigFileMetadata, connector configconnector.ConfigConnector, chain configfilter.Chain, - configuration config.Configuration, + conf config.Configuration, persistHandler *CachePersistHandler) (*ConfigFileRepo, error) { repo := &ConfigFileRepo{ connector: connector, chain: chain, - configuration: configuration, + conf: conf, configFileMetadata: metadata, notifiedVersion: initVersion, retryPolicy: retryPolicy{ @@ -85,7 +85,7 @@ func newConfigFileRepo(metadata model.ConfigFileMetadata, }, remoteConfigFileRef: &atomic.Value{}, persistHandler: persistHandler, - fallbackToLocalCache: configuration.GetConfigFile().GetLocalCache().IsFallbackToLocalCache(), + fallbackToLocalCache: conf.GetConfigFile().GetLocalCache().IsFallbackToLocalCache(), } repo.remoteConfigFileRef.Store(&configconnector.ConfigFile{ Namespace: metadata.GetNamespace(), @@ -143,6 +143,13 @@ func (r *ConfigFileRepo) pull() error { FileGroup: r.configFileMetadata.GetFileGroup(), FileName: r.configFileMetadata.GetFileName(), Version: r.notifiedVersion, + Tags: make([]*configconnector.ConfigFileTag, 0, len(r.conf.GetGlobal().GetClient().GetLabels())), + } + for k, v := range r.conf.GetGlobal().GetClient().GetLabels() { + pullConfigFileReq.Tags = append(pullConfigFileReq.Tags, &configconnector.ConfigFileTag{ + Key: k, + Value: v, + }) } log.GetBaseLogger().Infof("[Config] start pull config file. config file = %+v, version = %d", diff --git a/pkg/model/context.go b/pkg/model/context.go index 5684c970..1e7ea649 100644 --- a/pkg/model/context.go +++ b/pkg/model/context.go @@ -19,10 +19,13 @@ package model import ( "context" + "strconv" + "strings" "sync" "sync/atomic" "time" + "github.com/google/uuid" "github.com/polarismesh/polaris-go/pkg/clock" ) @@ -53,6 +56,22 @@ type SDKToken struct { HostName string } +func (sdkToken *SDKToken) InitUID() { + if sdkToken.PodName != "" { + sdkToken.UID = sdkToken.PodName + return + } + if sdkToken.HostName != "" { + sdkToken.UID = sdkToken.HostName + "-" + strconv.Itoa(int(sdkToken.PID)) + return + } + if sdkToken.IP != "" { + sdkToken.UID = sdkToken.IP + "-" + strconv.Itoa(int(sdkToken.PID)) + return + } + sdkToken.UID = strings.ToUpper(uuid.New().String()) +} + // LocationInfo 地域信息 type LocationInfo interface { // GetLocation 获取地域明细 diff --git a/pkg/version/version.go b/pkg/version/version.go index de31f009..b2320ce7 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -20,7 +20,7 @@ package version var ( // Version current version number - Version = "v1.0.0" + Version = "v1.6.0" // ClientType client type ClientType = "polaris-go" ) diff --git a/plugin/configconnector/polaris/config_connector.go b/plugin/configconnector/polaris/config_connector.go index 876821c3..8f7272d0 100644 --- a/plugin/configconnector/polaris/config_connector.go +++ b/plugin/configconnector/polaris/config_connector.go @@ -385,12 +385,20 @@ func (c *Connector) handleResponse(request string, reqID string, opKey string, r } func transferToClientConfigFileInfo(configFile *configconnector.ConfigFile) *config_manage.ClientConfigFileInfo { + tags := make([]*config_manage.ConfigFileTag, 0, len(configFile.GetLabels())) + for key, val := range configFile.GetLabels() { + tags = append(tags, &config_manage.ConfigFileTag{ + Key: wrapperspb.String(key), + Value: wrapperspb.String(val), + }) + } return &config_manage.ClientConfigFileInfo{ Namespace: wrapperspb.String(configFile.Namespace), Group: wrapperspb.String(configFile.GetFileGroup()), FileName: wrapperspb.String(configFile.GetFileName()), Version: wrapperspb.UInt64(configFile.GetVersion()), PublicKey: wrapperspb.String(configFile.GetPublicKey()), + Tags: tags, } }