安裝
- 下載源碼包
go get github.com/aliyun/aliyun-tablestore-go-sdk/tunnel
-
安裝依賴
-
可以在tunnel目錄下使用dep安裝依賴
- 安裝dep
- dep ensure -v
- 也可以直接使用go get安裝依賴包:
-
go get -u go.uber.org/zap
go get github.com/cenkalti/backoff
go get github.com/golang/protobuf/proto
go get github.com/satori/go.uuid
go get github.com/stretchr/testify/assert
go get github.com/smartystreets/goconvey/convey
go get github.com/golang/mock/gomock
go get gopkg.in/natefinch/lumberjack.v2
[]()快速開始
- 初始化Tunnel client:
// endpoint是表格存儲實例endpoint,如https://instance.cn-hangzhou.ots.aliyun.com
// instance爲實例名稱
// accessKeyId和accessKeySecret分別爲訪問表格存儲服務的AccessKey的Id和Secret
tunnelClient := tunnel.NewTunnelClient(endpoint, instance,
accessKeyId, accessKeySecret)
- 創建新Tunnel:
req := &tunnel.CreateTunnelRequest{
TableName: "testTable",
TunnelName: "testTunnel",
Type: tunnel.TunnelTypeBaseStream, //全量加增量類型Tunnel
}
resp, err := tunnelClient.CreateTunnel(req)
if err != nil {
log.Fatal("create test tunnel failed", err)
}
log.Println("tunnel id is", resp.TunnelId)
- 獲取已有Tunnel信息:
req := &tunnel.DescribeTunnelRequest{
TableName: "testTable",
TunnelName: "testTunnel",
}
resp, err := tunnelClient.DescribeTunnel(req)
if err != nil {
log.Fatal("create test tunnel failed", err)
}
log.Println("tunnel id is", resp.Tunnel.TunnelId)
- 註冊callback,開始數據消費:
//用戶定義消費callback函數
func exampleConsumeFunction(ctx *tunnel.ChannelContext, records []*tunnel.Record) error {
fmt.Println("user-defined information", ctx.CustomValue)
for _, rec := range records {
fmt.Println("tunnel record detail:", rec.String())
}
fmt.Println("a round of records consumption finished")
return nil
}
//配置callback到SimpleProcessFactory,配置消費端TunnelWorkerConfig
workConfig := &tunnel.TunnelWorkerConfig{
ProcessorFactory: &tunnel.SimpleProcessFactory{
CustomValue: "user custom interface{} value",
ProcessFunc: exampleConsumeFunction,
},
}
//使用TunnelDaemon持續消費指定tunnel
daemon := tunnel.NewTunnelDaemon(tunnelClient, tunnelId, workConfig)
log.Fatal(daemon.Run())
- 刪除Tunnel
req := &tunnel.DeleteTunnelRequest {
TableName: "testTable",
TunnelName: "testTunnel",
}
_, err := tunnelClient.DeleteTunnel(req)
if err != nil {
log.Fatal("delete test tunnel failed", err)
}
[]()配置項
- tunnel client配置
初始化tunnel client時可以通過NewTunnelClientWithConfig接口自定義客戶端配置,使用不指定config初始化接口或者config爲nil時會使用DefaultTunnelConfig:
var DefaultTunnelConfig = &TunnelConfig{
//最大指數退避重試時間
MaxRetryElapsedTime: 45 * time.Second,
//HTTP請求超時時間
RequestTimeout: 30 * time.Second,
//http.DefaultTransport
Transport: http.DefaultTransport,
}
- 數據消費worker配置
TunnelWorkerConfig中包含了數據消費worker需要的配置,其中ProcessorFactory爲必填項,其餘字段不填將使用默認值,通常使用默認值即可:
type TunnelWorkerConfig struct {
//worker同Tunnel服務的心跳超時時間,通常使用默認值即可
HeartbeatTimeout time.Duration
//worker發送心跳的頻率,通常使用默認值即可
HeartbeatInterval time.Duration
//tunnel下消費連接建立接口,通常使用默認值即可
ChannelDialer ChannelDialer
//消費連接上具體處理器產生接口,通常使用callback函數初始化SimpleProcessFactory即可
ProcessorFactory ChannelProcessorFactory
//zap日誌配置,默認值爲DefaultLogConfig
LogConfig *zap.Config
//zap日誌輪轉配置,默認值爲DefaultSyncer
LogWriteSyncer zapcore.WriteSyncer
}
其中的ProcessorFactory爲用戶註冊消費callback函數以及其他信息的接口,建議使用SDK中自帶SimpleProcessorFactory實現:
type SimpleProcessFactory struct {
//用戶自定義信息,會傳遞到ProcessFunc和ShutdownFunc中的ChannelContext參數中
CustomValue interface{}
//Worker記錄checkpoint的間隔,CpInterval<=0時會使用DefaultCheckpointInterval
CpInterval time.Duration
//worker數據處理的同步調用callback,ProcessFunc返回error時worker會用本批數據退避重試ProcessFunc
ProcessFunc func(channelCtx *ChannelContext, records []*Record) error
//worker退出時的同步調用callback
ShutdownFunc func(channelCtx *ChannelContext)
//日誌配置,Logger爲nil時會使用DefaultLogConfig初始化logger
Logger *zap.Logger
}
- 日誌配置
默認日誌配置:
//DefaultLogConfig是TunnelWorkerConfig和SimpleProcessFactory使用的默認日誌配置
var DefaultLogConfig = zap.Config{
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
Development: false,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
Encoding: "json",
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
}
日誌輪轉配置:
//DefaultSyncer是TunnelWorkerConfig和SimpleProcessFactory使用的默認日誌輪轉配置
var DefaultSyncer = zapcore.AddSync(&lumberjack.Logger{
//日誌文件路徑
Filename: "tunnelClient.log",
//最大日誌文件大小
MaxSize: 512, //MB
//壓縮輪轉的日誌文件數
MaxBackups: 5,
//輪轉日誌文件保留的最大天數
MaxAge: 30, //days
//是否壓縮輪轉日誌文件
Compress: true,
})