From 261dccfa25a783c891172d371eae231f5b330585 Mon Sep 17 00:00:00 2001 From: "jeff.wang" Date: Tue, 31 Jan 2023 18:42:56 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E8=BF=9C=E7=A8=8Bnacos=E6=95=85=E9=9A=9C?= =?UTF-8?q?=EF=BC=8C=E9=99=8D=E7=BA=A7=E6=9C=AC=E5=9C=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clients/config_client/config_client.go | 11 +++ clients/config_client/config_proxy.go | 15 +++- common/constant/client_config_options.go | 7 ++ common/constant/config.go | 1 + common/constant/const.go | 1 + common/nacos_server/nacos_server.go | 2 +- common/security/security_proxy.go | 13 ++- example/config/demo/main.go | 109 +++++++++++++++++++++++ 8 files changed, 153 insertions(+), 6 deletions(-) create mode 100644 example/config/demo/main.go diff --git a/clients/config_client/config_client.go b/clients/config_client/config_client.go index 81267c9f..434c5f15 100644 --- a/clients/config_client/config_client.go +++ b/clients/config_client/config_client.go @@ -212,6 +212,12 @@ func (client *ConfigClient) getConfigInner(param vo.ConfigParam) (content string clientConfig.TimeoutMs, false, client) if err != nil { logger.Infof("get config from server error:%+v ", err) + if clientConfig, err := client.GetClientConfig(); err == nil { + if clientConfig.DisableUseSnapShot { + logger.Errorf("get config from cache error:%+v ", err) + return "", errors.New("get config from remote nacos server fail, and is not allowed to read local file") + } + } content, err = cache.ReadConfigFromFile(cacheKey, client.configCacheDir) if err != nil { logger.Errorf("get config from cache error:%+v ", err) @@ -423,6 +429,11 @@ func (client *ConfigClient) executeConfigListen() { logger.Warnf("ConfigBatchListenRequest failure,err:%+v", err) continue } + if !iResponse.IsSuccess() { + logger.Warnf("ConfigBatchListenRequest failure, error code:%+v", iResponse.GetErrorCode()) + continue + } + if iResponse == nil && !iResponse.IsSuccess() { continue } diff --git a/clients/config_client/config_proxy.go b/clients/config_client/config_proxy.go index 7dbe5e54..8036c2bf 100644 --- a/clients/config_client/config_proxy.go +++ b/clients/config_client/config_proxy.go @@ -152,11 +152,20 @@ func (cp *ConfigProxy) queryConfig(dataId, group, tenant string, timeout uint64, return response, nil } +func appName(client *ConfigClient) string { + if clientConfig, err := client.GetClientConfig(); err == nil { + appName := clientConfig.AppName + return appName + } + return "unknown" +} + func (cp *ConfigProxy) createRpcClient(ctx context.Context, taskId string, client *ConfigClient) *rpc.RpcClient { labels := map[string]string{ - constant.LABEL_SOURCE: constant.LABEL_SOURCE_SDK, - constant.LABEL_MODULE: constant.LABEL_MODULE_CONFIG, - "taskId": taskId, + constant.LABEL_SOURCE: constant.LABEL_SOURCE_SDK, + constant.LABEL_MODULE: constant.LABEL_MODULE_CONFIG, + constant.APPNAME_HEADER: appName(client), + "taskId": taskId, } iRpcClient, _ := rpc.CreateClient(ctx, "config-"+taskId+"-"+client.uid, rpc.GRPC, labels, cp.nacosServer) diff --git a/common/constant/client_config_options.go b/common/constant/client_config_options.go index 94089057..82b8b8d3 100644 --- a/common/constant/client_config_options.go +++ b/common/constant/client_config_options.go @@ -116,6 +116,13 @@ func WithCacheDir(cacheDir string) ClientOption { } } +// WithDisableUseSnapShot ... +func WithDisableUseSnapShot(disableUseSnapShot bool) ClientOption { + return func(config *ClientConfig) { + config.DisableUseSnapShot = disableUseSnapShot + } +} + // WithUpdateThreadNum ... func WithUpdateThreadNum(updateThreadNum int) ClientOption { return func(config *ClientConfig) { diff --git a/common/constant/config.go b/common/constant/config.go index 23768477..39460cee 100644 --- a/common/constant/config.go +++ b/common/constant/config.go @@ -39,6 +39,7 @@ type ClientConfig struct { SecretKey string // the SecretKey for kms OpenKMS bool // it's to open kms,default is false. https://help.aliyun.com/product/28933.html CacheDir string // the directory for persist nacos service info,default value is current path + DisableUseSnapShot bool // It's a switch, default is false, means that when get remote config fail, use local cache file instead UpdateThreadNum int // the number of goroutine for update nacos service info,default value is 20 NotLoadCacheAtStart bool // not to load persistent nacos service info in CacheDir at start time UpdateCacheWhenEmpty bool // update cache when get empty service instance from server diff --git a/common/constant/const.go b/common/constant/const.go index bd6bfa93..f5e92fcc 100644 --- a/common/constant/const.go +++ b/common/constant/const.go @@ -89,6 +89,7 @@ const ( DEFAULT_TIMEOUT_MILLS = 3000 ALL_SYNC_INTERNAL = 5 * time.Minute CLIENT_APPNAME_HEADER = "Client-AppName" + APPNAME_HEADER = "AppName" CLIENT_REQUEST_TS_HEADER = "Client-RequestTS" CLIENT_REQUEST_TOKEN_HEADER = "Client-RequestToken" EX_CONFIG_INFO = "exConfigInfo" diff --git a/common/nacos_server/nacos_server.go b/common/nacos_server/nacos_server.go index 1ef35288..6ea8393e 100644 --- a/common/nacos_server/nacos_server.go +++ b/common/nacos_server/nacos_server.go @@ -87,7 +87,7 @@ func NewNacosServer(ctx context.Context, serverList []constant.ServerConfig, cli _, err := securityLogin.Login() if err != nil { - return &ns, err + logger.Errorf("login in err:%v", err) } securityLogin.AutoRefresh(ctx) diff --git a/common/security/security_proxy.go b/common/security/security_proxy.go index dee22c39..c6ad272a 100644 --- a/common/security/security_proxy.go +++ b/common/security/security_proxy.go @@ -75,7 +75,12 @@ func (ac *AuthClient) AutoRefresh(ctx context.Context) { } go func() { - timer := time.NewTimer(time.Second * time.Duration(ac.tokenTtl-ac.tokenRefreshWindow)) + var timer *time.Timer + if lastLoginSuccess := ac.lastRefreshTime > 0 && ac.tokenTtl > 0 && ac.tokenRefreshWindow > 0; lastLoginSuccess { + timer = time.NewTimer(time.Second * time.Duration(ac.tokenTtl-ac.tokenRefreshWindow)) + } else { + timer = time.NewTimer(time.Second * time.Duration(5)) + } defer timer.Stop() for { select { @@ -83,8 +88,11 @@ func (ac *AuthClient) AutoRefresh(ctx context.Context) { _, err := ac.Login() if err != nil { logger.Errorf("login has error %+v", err) + timer.Reset(time.Second * time.Duration(5)) + } else { + logger.Infof("login success, tokenTtl: %+v seconds, tokenRefreshWindow: %+v seconds", ac.tokenTtl, ac.tokenRefreshWindow) + timer.Reset(time.Second * time.Duration(ac.tokenTtl-ac.tokenRefreshWindow)) } - timer.Reset(time.Second * time.Duration(ac.tokenTtl-ac.tokenRefreshWindow)) case <-ctx.Done(): return } @@ -156,6 +164,7 @@ func (ac *AuthClient) login(server constant.ServerConfig) (bool, error) { if val, ok := result[constant.KEY_ACCESS_TOKEN]; ok { ac.accessToken.Store(val) + ac.lastRefreshTime = time.Now().UnixMilli() ac.tokenTtl = int64(result[constant.KEY_TOKEN_TTL].(float64)) ac.tokenRefreshWindow = ac.tokenTtl / 10 } diff --git a/example/config/demo/main.go b/example/config/demo/main.go new file mode 100644 index 00000000..252c6505 --- /dev/null +++ b/example/config/demo/main.go @@ -0,0 +1,109 @@ +package main + +import ( + "fmt" + "github.com/nacos-group/nacos-sdk-go/v2/common/logger" + "time" + + "github.com/nacos-group/nacos-sdk-go/v2/clients" + "github.com/nacos-group/nacos-sdk-go/v2/common/constant" + "github.com/nacos-group/nacos-sdk-go/v2/vo" +) + +func main() { + //os.Setenv("nacos.remote.client.grpc.timeout", "1000") + sc := []constant.ServerConfig{ + *constant.NewServerConfig("nacos.test.infra.ww5sawfyut0k.bitsvc.io", 8848), + //*constant.NewServerConfig("10.18.1.4", 8848), + } + + //create ClientConfig + cc := constant.NewClientConfig( + constant.WithNamespaceId(""), + constant.WithTimeoutMs(1000), + constant.WithNotLoadCacheAtStart(true), + constant.WithLogDir("/tmp/nacos/log"), + constant.WithUsername("nacos"), + constant.WithPassword("nacos"), + constant.WithCacheDir("/tmp/nacos/cache"), + constant.WithLogLevel("debug"), + constant.WithAppName("local-golang-idea"), + constant.WithDisableUseSnapShot(false), + ) + + // create config client + s := time.Now() + client, err := clients.NewConfigClient( + vo.NacosClientParam{ + ClientConfig: cc, + ServerConfigs: sc, + }, + ) + fmt.Println("建立client耗时: ", time.Since(s)) + + if err != nil { + panic(err) + } + + //fmt.Println(os.Getenv("xxx") == "true") + + var content string + s = time.Now() + + content, err = client.GetConfig(vo.ConfigParam{ + DataId: "option-zone.yaml", + Group: "ROUTE_STRATEGY", + }) + + logger.Info(content) + fmt.Println("配置内容->" + content) + + fmt.Println("第一次获取配置耗时: ", time.Since(s)) + //fmt.Println(cost, "=====>>>>>>>>>") + + if err != nil { + println("获取配置失败", err) + return + } + + s = time.Now() + + content, err = client.GetConfig(vo.ConfigParam{ + DataId: "option-zone.yaml", + Group: "ROUTE_STRATEGY", + }) + + fmt.Println("第二次次获取配置耗时: ", time.Since(s)) + //fmt.Println("config") + //fmt.Println(content) + //fmt.Println("获取配置:", float64(time.Now().UnixMilli()-s.UnixMilli()), " ms") + // + //time.Sleep(100 * time.Millisecond) + + s = time.Now() + err = client.ListenConfig(vo.ConfigParam{ + DataId: "option-zone.yaml", + Group: "ROUTE_STRATEGY", + OnChange: func(namespace, group, dataId, data string) { + fmt.Println("收到内容变更推送事件", data) + }, + }) + if err != nil { + fmt.Println("监听error", err) + return + } + + fmt.Println("监控耗时:", time.Since(s)) + for { + time.Sleep(10 * time.Second) + s = time.Now() + + content, err = client.GetConfig(vo.ConfigParam{ + DataId: "option-zone.yaml", + Group: "ROUTE_STRATEGY", + }) + + fmt.Println(content) + fmt.Println("获取配置耗时: ", time.Since(s)) + } +} From c9fc5bd37861e8618b9805a426e8a4e7bdee1e23 Mon Sep 17 00:00:00 2001 From: "jeff.wang" Date: Tue, 31 Jan 2023 19:10:29 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=88=A0=E9=99=A4=20=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/config/demo/main.go | 109 ------------------------------------ 1 file changed, 109 deletions(-) delete mode 100644 example/config/demo/main.go diff --git a/example/config/demo/main.go b/example/config/demo/main.go deleted file mode 100644 index 252c6505..00000000 --- a/example/config/demo/main.go +++ /dev/null @@ -1,109 +0,0 @@ -package main - -import ( - "fmt" - "github.com/nacos-group/nacos-sdk-go/v2/common/logger" - "time" - - "github.com/nacos-group/nacos-sdk-go/v2/clients" - "github.com/nacos-group/nacos-sdk-go/v2/common/constant" - "github.com/nacos-group/nacos-sdk-go/v2/vo" -) - -func main() { - //os.Setenv("nacos.remote.client.grpc.timeout", "1000") - sc := []constant.ServerConfig{ - *constant.NewServerConfig("nacos.test.infra.ww5sawfyut0k.bitsvc.io", 8848), - //*constant.NewServerConfig("10.18.1.4", 8848), - } - - //create ClientConfig - cc := constant.NewClientConfig( - constant.WithNamespaceId(""), - constant.WithTimeoutMs(1000), - constant.WithNotLoadCacheAtStart(true), - constant.WithLogDir("/tmp/nacos/log"), - constant.WithUsername("nacos"), - constant.WithPassword("nacos"), - constant.WithCacheDir("/tmp/nacos/cache"), - constant.WithLogLevel("debug"), - constant.WithAppName("local-golang-idea"), - constant.WithDisableUseSnapShot(false), - ) - - // create config client - s := time.Now() - client, err := clients.NewConfigClient( - vo.NacosClientParam{ - ClientConfig: cc, - ServerConfigs: sc, - }, - ) - fmt.Println("建立client耗时: ", time.Since(s)) - - if err != nil { - panic(err) - } - - //fmt.Println(os.Getenv("xxx") == "true") - - var content string - s = time.Now() - - content, err = client.GetConfig(vo.ConfigParam{ - DataId: "option-zone.yaml", - Group: "ROUTE_STRATEGY", - }) - - logger.Info(content) - fmt.Println("配置内容->" + content) - - fmt.Println("第一次获取配置耗时: ", time.Since(s)) - //fmt.Println(cost, "=====>>>>>>>>>") - - if err != nil { - println("获取配置失败", err) - return - } - - s = time.Now() - - content, err = client.GetConfig(vo.ConfigParam{ - DataId: "option-zone.yaml", - Group: "ROUTE_STRATEGY", - }) - - fmt.Println("第二次次获取配置耗时: ", time.Since(s)) - //fmt.Println("config") - //fmt.Println(content) - //fmt.Println("获取配置:", float64(time.Now().UnixMilli()-s.UnixMilli()), " ms") - // - //time.Sleep(100 * time.Millisecond) - - s = time.Now() - err = client.ListenConfig(vo.ConfigParam{ - DataId: "option-zone.yaml", - Group: "ROUTE_STRATEGY", - OnChange: func(namespace, group, dataId, data string) { - fmt.Println("收到内容变更推送事件", data) - }, - }) - if err != nil { - fmt.Println("监听error", err) - return - } - - fmt.Println("监控耗时:", time.Since(s)) - for { - time.Sleep(10 * time.Second) - s = time.Now() - - content, err = client.GetConfig(vo.ConfigParam{ - DataId: "option-zone.yaml", - Group: "ROUTE_STRATEGY", - }) - - fmt.Println(content) - fmt.Println("获取配置耗时: ", time.Since(s)) - } -} From 77ed42b3ad4214a1ac5936136d09a1c48d70acb0 Mon Sep 17 00:00:00 2001 From: "jeff.wang" Date: Wed, 1 Feb 2023 21:28:58 +0800 Subject: [PATCH 3/3] make code better and make ci pass --- clients/config_client/config_client.go | 16 +++++++--------- common/security/security_proxy.go | 2 +- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/clients/config_client/config_client.go b/clients/config_client/config_client.go index 434c5f15..c514989b 100644 --- a/clients/config_client/config_client.go +++ b/clients/config_client/config_client.go @@ -212,11 +212,9 @@ func (client *ConfigClient) getConfigInner(param vo.ConfigParam) (content string clientConfig.TimeoutMs, false, client) if err != nil { logger.Infof("get config from server error:%+v ", err) - if clientConfig, err := client.GetClientConfig(); err == nil { - if clientConfig.DisableUseSnapShot { - logger.Errorf("get config from cache error:%+v ", err) - return "", errors.New("get config from remote nacos server fail, and is not allowed to read local file") - } + if clientConfig, err := client.GetClientConfig(); err == nil && clientConfig.DisableUseSnapShot { + logger.Errorf("get config from cache error:%+v ", err) + return "", errors.New("get config from remote nacos server fail, and is not allowed to read local file") } content, err = cache.ReadConfigFromFile(cacheKey, client.configCacheDir) if err != nil { @@ -429,12 +427,12 @@ func (client *ConfigClient) executeConfigListen() { logger.Warnf("ConfigBatchListenRequest failure,err:%+v", err) continue } - if !iResponse.IsSuccess() { - logger.Warnf("ConfigBatchListenRequest failure, error code:%+v", iResponse.GetErrorCode()) + if iResponse == nil { + logger.Warnf("ConfigBatchListenRequest failure, response is nil") continue } - - if iResponse == nil && !iResponse.IsSuccess() { + if !iResponse.IsSuccess() { + logger.Warnf("ConfigBatchListenRequest failure, error code:%+v", iResponse.GetErrorCode()) continue } changeKeys := make(map[string]struct{}) diff --git a/common/security/security_proxy.go b/common/security/security_proxy.go index c6ad272a..39cb807a 100644 --- a/common/security/security_proxy.go +++ b/common/security/security_proxy.go @@ -164,7 +164,7 @@ func (ac *AuthClient) login(server constant.ServerConfig) (bool, error) { if val, ok := result[constant.KEY_ACCESS_TOKEN]; ok { ac.accessToken.Store(val) - ac.lastRefreshTime = time.Now().UnixMilli() + ac.lastRefreshTime = time.Now().Unix() ac.tokenTtl = int64(result[constant.KEY_TOKEN_TTL].(float64)) ac.tokenRefreshWindow = ac.tokenTtl / 10 }