全部產品
Search
文件中心

ApsaraDB for OceanBase (Deprecated):通過 Go 用戶端串連 OceanBase 資料庫

更新時間:Jul 01, 2024

為您介紹使用 Go 用戶端通過代理模式串連資料庫的操作。

前提條件

  • 在使用 GO 用戶端時,盡量使用最新版本的用戶端,最新版本用戶端請詳見GitHub go 用戶端倉庫

  • 引入 obkv-table-client-go 依賴,在本地 GO 倉庫上執行以下命令,擷取最新obkv-table-client-go依賴,也可以擷取指定的版本。

    go get github.com/oceanbase/obkv-table-client-go

串連方法

OceanBase 支援通過代理模式串連資料庫,您可使用直接調用函數串連資料庫和使用 toml 檔案串連資料庫兩種方式。

方法一:直接調用函數

在您的 Go 代碼中調用函數,並參考表格中的內容修改參數,進行資料庫連接。

func NewOdpClient(
	fullUserName string,
	passWord string,
	odpIP string,
	odpRpcPort int,
	database string,
	cliConfig *config.ClientConfig) (Client, error)
  • param 參數說明

    參數

    說明

    fullUserName

    格式為 <帳號名稱>@<租戶名稱>#<叢集名稱>。

    passWord

    fullUserName 中的帳號密碼。

    odpIP

    OceanBase 資料庫連接的網域名稱。

    odpRpcPort

    提供 OceanBase 資料庫寬表引擎(或 KV 模式)服務連接埠,預設是 3307。

    database

    需要串連的資料庫名稱。

    cliConfig

    用戶端配置,具體見 ClientConfig

方法二:使用 toml 檔案

  1. 參考模板檔案configurations/obkv-table-default.toml制定您的設定檔,選擇 Mode 模式為 proxy,修改OdpClientConfig內容,其他內容根據實際業務需求修改,具體內容參考ClientConfig

  2. 在您的 Go 代碼中調用函數, 輸入 toml檔案地址/位置。

    func NewClientWithTomlConfig(configFilePath string) (Client, error)
    • param 參數說明

      參數

      說明

      configFilePath

      toml檔案路徑:configurations/obkv-table-default.toml

    • return 參數說明

      參數

      說明

      Client

      用戶端介面, 介面包含 insert、get、update 等方法,更多內容參見通過 Go 用戶端串連資料庫使用樣本

      error

      錯誤資訊。

ClientConfig

程式碼範例:

type ClientConfig struct {
        ConnPoolMaxConnSize int
        ConnConnectTimeOut  time.Duration
        ConnLoginTimeout    time.Duration
        OperationTimeOut    time.Duration
        LogLevel            log.Level

        TableEntryRefreshLockTimeout     time.Duration
        TableEntryRefreshTryTimes        int
        TableEntryRefreshIntervalBase    time.Duration
        TableEntryRefreshIntervalCeiling time.Duration

        MetadataRefreshInterval    time.Duration
        MetadataRefreshLockTimeout time.Duration

        RsListLocalFileLocation    string
        RsListHttpGetTimeout       time.Duration
        RsListHttpGetRetryTimes    int
        RsListHttpGetRetryInterval time.Duration

        EnableRerouting bool
}

重要配置項說明:

配置項

說明

ConnPoolMaxConnSize

串連池串連數量, 預設值 1。

ConnConnectTimeOut

tcp 連線逾時時間, 預設 1s。

ConnLoginTimeout

鑒權操作逾時時間, 預設 1s。

OperationTimeOut

單個請求逾時時間, 預設 10s。

EnableRerouting

是否開啟二次路由功能, 預設關閉。