package main
import (
"context"
"encoding/json"
"fmt"
"time"
//etcd_client "github.com/coreos/etcd/clientv3"
etcd_client "go.etcd.io/etcd/clientv3"
)
type SecInfoConf struct {
ProductId int
StartTime int
EndTime int
Status int
Total int
Left int
}
const (
//EtcdKey = "/pjy/seckill/product"
EtcdKey = "E:\monster.properties"
)
func SetLogConfToEtcd() {
cli, err := etcd_client.New(etcd_client.Config{
Endpoints: []string{"localhost:2379", "localhost:22379", "localhost:32379"},
DialTimeout: 5 * time.Second,
})
if err != nil {
fmt.Println("in the SetLogConfToEtcd connect is failed err:", err)
return
}
fmt.Println("connect is success")
defer cli.Close()
var secInfoConf []SecInfoConf
secInfoConf = append(
secInfoConf,
SecInfoConf{
ProductId: 1028,
StartTime: 1569686400,
EndTime: 1569772800,
Status: 0,
Total: 1000,
Left: 1000,
},
)
secInfoConf = append(
secInfoConf,
SecInfoConf{
ProductId: 1029,
StartTime: 1569686400,
EndTime: 1569772800,
Status: 0,
Total: 2000,
Left: 2000,
},
)
data, err := json.Marshal(secInfoConf)
if err != nil {
fmt.Println("json Marshal is err: ", err)
return
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
_, err = cli.Put(ctx, EtcdKey, string(data))
cancel()
if err != nil {
fmt.Println("put failed err:", err)
return
}
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
resp, err := cli.Get(ctx, EtcdKey)
cancel()
if err != nil {
fmt.Println("get failed err:", err)
return
}
for _, ev := range resp.Kvs {
fmt.Printf("%v : %v\n", ev.Key, ev.Value)
}
}
func main() {
SetLogConfToEtcd()
}