初始化开发工具包

云眼About 2 min

初始化开发工具包

本主题介绍如何在应用程序中初始化云眼灰度实验 Go SDK。

要初始化公开 API 方法(如 Decide)的 EyeofcloudClient,需要 SDK 密钥或硬编码的 JSON 数据文件。

使用开发工具包密钥进行基本初始化

Go

import eoc "github.com/eyeofcloud/go-sdk" // Instantiates a client that syncs the datafile in the background eocClient, er := eoc.Client("SDK_KEY_HERE")

高级配置

静态客户端实例

如果不希望客户端在后台定期同步数据文件,则可以实例化静态客户端。这对于在短期环境(例如 Lambda 函数)中使用开发工具包非常有用。可以将 EyeofcloudFactory 用于以下更高级的用例:

Go

import "github.com/eyeofcloud/go-sdk/pkg/client" eyeofcloudFactory := &client.EyeofcloudFactory{ Datafile: []byte("DATAFILE_JSON_STRING_HERE"), } // Instantiate a static client (no datafile polling) staticEocClient, err := eyeofcloudFactory.StaticClient()

如果希望 SDK 初始化并仅执行一次性远程数据文件提取,则可以传入 SDK 密钥并实例化静态客户端:

Go

import "github.com/eyeofcloud/go-sdk/pkg/client" eyeofcloudFactory := &client.EyeofcloudFactory{ SDKKey: "[SDK_KEY_HERE]", } // Instantiate a static client that will pull down the datafile one time staticEocClient, err := eyeofcloudFactory.StaticClient()

自定义初始化

可以使用 EyeofcloudFactory 进一步自定义 SDK 的顶级组件:事件处理器、配置管理器、默认决策选项和决策服务:

Go

import ( "time" "github.com/eyeofcloud/go-sdk/pkg/client" ) eyeofcloudFactory := &client.EyeofcloudFactory{ SDKKey: "[SDK_KEY_HERE]", } datafilePollingInterval := 2 * time.Second eventBatchSize := 20 eventQueueSize := 1500 eventFlushInterval := 10 * time.Second defaultDecideOptions := decide.EyeofcloudDecideOptions{ IgnoreUserProfileService: true, } // Instantiate a client with custom configuration eyeofcloudClient, _ := eyeofcloudFactory.Client( client.WithPollingConfigManager(datafilePollingInterval, nil), client.WithBatchEventProcessor( eventBatchSize, eventQueueSize, eventFlushInterval, ), client.WithDefaultDecideOptions(defaultDecideOptions), )

For more details on configuring the event batching please see Event Batching. For more details on configuring default decide options, see Decide methods.

Use authenticated datafile in secure environment

📘

Note

Authenticated datafiles are in beta. Contact your Customer Success Manager if you are interested in becoming an early user of authenticated datafiles as part of the beta secure environment feature.

可以使用服务器端(仅限)云眼灰度实验 SDK 从经过身份验证的终结点提取云眼灰度实验数据文件。要使用经过身份验证的数据文件,请从设置>环境中Eyeofcloud 应用程序open in new window下载环境的访问令牌。选择安全环境,然后复制_数据文件访问令牌_。下面的示例显示了如何使用访问令牌和sdk_key初始化 Eyeofcloud 客户端,使客户端能够获取经过身份验证的数据文件并完成初始化。

Go

// fetch the datafile from an authenticated endpoint accessToken := `<YOUR_DATAFILE_ACCESS_TOKEN>` sdkKey := `<YOUR_SKD_KEY>` factory := client.EyeofcloudFactory{SDKKey: sdkKey} eyeofcloudClient, err := factory.Client(client.WithDatafileAccessToken(accessToken))

Last update:
Contributors: “zhangweixue”