问题描述:
使用etcd报如下错误:
invalid operation: ev.Type == "go.etcd.io/etcd/mvcc/mvccpb".DELETE
(mismatched types "github.com/coreos/etcd/mvcc/mvccpb".Event_EventType
and "go.etcd.io/etcd/mvcc/mvccpb".Event_EventType)
invalid operation: ev.Type == "go.etcd.io/etcd/mvcc/mvccpb".PUT
(mismatched types "github.com/coreos/etcd/mvcc/mvccpb".Event_EventType
and "go.etcd.io/etcd/mvcc/mvccpb".Event_EventType)
我的代码:
/**
*@Time :2019/11/2022:58
*@Author :Shuai Jhou
*@File :config
*@Software:GoLand
*/
package common
import (
"context"
"encoding/json"
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
"go.etcd.io/etcd/mvcc/mvccpb"
"sync"
"time"
)
var (
AppConfig = &Config{
ProductConfigMap: make(map[int]*SeckillProductConfig, 1024),
}
)
type Config struct {
RunMode string
Log_level string
Log_path string
RedisAddr string
EtcdAddr string
EtcdSeckillKey string
ProductConfigMap map[int]*SeckillProductConfig
ProductConfigRwLock sync.RWMutex
}
type SeckillProductConfig struct {
ProductId int `json:"product_id"`
StartTime int `json:"start_time"`
EndTime int `json:"end_time"`
Status int `json:"status"`
Total int `json:"total"`
Free int `json:"free"`
Used int `json:"used"`
}
// 初始化系统配置
func InitConfig() (err error) {
runmode := beego.AppConfig.String("runmode")
if len(runmode) == 0 {
err = fmt.Errorf("[InitConfig]RunMode config[%v] err ", runmode)
return
}
AppConfig.RunMode = runmode
AppConfig.Log_level = beego.AppConfig.String("log_level")
if len(AppConfig.Log_level) == 0 {
AppConfig.Log_level = "debug"
}
AppConfig.Log_path = beego.AppConfig.String("log_path")
if len(AppConfig.Log_path) == 0 {
AppConfig.Log_path = "./logs/system.log"
}
redisAddr := beego.AppConfig.String("redisAddr")
etcdAddr := beego.AppConfig.String("etcdAddr")
etcdSeckillKey := beego.AppConfig.String("etcdSeckillKey")
if len(redisAddr) == 0 || len(etcdAddr) == 0 || len(etcdSeckillKey) == 0 {
err = fmt.Errorf("[InitConfig]redis config[%v] err or etcd config[%v] err or etcdSeckillKey config[%v]err",
redisAddr, etcdAddr, etcdSeckillKey)
return
}
AppConfig.EtcdAddr = etcdAddr
AppConfig.RedisAddr = redisAddr
AppConfig.EtcdSeckillKey = etcdSeckillKey
logs.Debug("Redis:%v,Etcd:%v,EtcdSeckillKey:%v", redisAddr, etcdAddr, etcdSeckillKey)
return
}
// 加载商品秒杀活动配置
func LoadSeckillConfig() (err error) {
key := fmt.Sprintf("%s/Product", AppConfig.EtcdSeckillKey)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
resp, err := EtcdClient.Get(ctx, key)
cancel()
if err != nil {
logs.Error("Get[%s] from etcd failed, err:%v", key, err)
return
}
var seckillProductConfig []SeckillProductConfig
for _, ev := range resp.Kvs {
logs.Debug("Key[%s] Value:%s", ev.Key, ev.Value)
err = json.Unmarshal(ev.Value, &seckillProductConfig)
if err != nil {
logs.Error("json.Unmarshal failed,Key[%s] Value:%s err:%v", ev.Key, ev.Value, err)
return
}
logs.Debug("seckillInfoConfig:%v", seckillProductConfig)
}
// 更新配置
UpdateSeckillProductConfig(seckillProductConfig)
return
}
//初始化一个监控商品配置的线程。配置发送变化,实时通知,修改配置
func InitSeckillConfigWatch() {
go watchSeckillConfig()
}
// 监控 配置文件是否变化,实时更新配置
func watchSeckillConfig() {
var flag = true
key := fmt.Sprintf("%s/Product", AppConfig.EtcdSeckillKey)
var seckillProductConfig []SeckillProductConfig
rch := EtcdClient.Watch(context.Background(), key)
for wresp := range rch {
for _, ev := range wresp.Events {
logs.Warn("Type: %s Key:%s Value:%v", ev.Type, ev.Kv.Key, ev.Kv.Value)
if ev.Type == mvccpb.DELETE {
logs.Warn("key[%s] config delete ", key)
continue
}
if ev.Type == mvccpb.PUT && string(ev.Kv.Key) == key {
err := json.Unmarshal(ev.Kv.Value, &seckillProductConfig)
if err != nil {
logs.Error("json.Unmarshal key[%s] err:%v", key, ev.Kv.Value)
flag = false
continue
}
}
}
if flag {
// 更新配置
UpdateSeckillProductConfig(seckillProductConfig)
}
}
}
// 更新配置
func UpdateSeckillProductConfig(seckillProductConfig []SeckillProductConfig) {
// 1、先用临时变量存储
var tmp map[int]*SeckillProductConfig = make(map[int]*SeckillProductConfig, 1024)
for _, v := range seckillProductConfig {
tmp[v.ProductId] = &v
}
// 2、再加锁 更新配置
AppConfig.ProductConfigRwLock.Lock()
AppConfig.ProductConfigMap = tmp
AppConfig.ProductConfigRwLock.Unlock()
}
问题解决方法:
import 的 go.etcd.io/etcd/clientv3 引用的是 go.etcd.io/etcd/mvcc/mvccpb,
但是在
(if ev.Type == mvccpb.DELETE) 和
(if ev.Type == mvccpb.PUT && string(ev.Kv.Key) == key)
应用了 github.com/coreos/etcd/mvcc/mvccpb
代码如下:
/**
*@Time :2019/11/2022:58
*@Author :Shuai Jhou
*@File :config
*@Software:GoLand
*/
package common
import (
"context"
"encoding/json"
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
"github.com/coreos/etcd/mvcc/mvccpb"
"sync"
"time"
)
var (
AppConfig = &Config{
ProductConfigMap: make(map[int]*SeckillProductConfig, 1024),
}
)
type Config struct {
RunMode string
Log_level string
Log_path string
RedisAddr string
EtcdAddr string
EtcdSeckillKey string
ProductConfigMap map[int]*SeckillProductConfig
ProductConfigRwLock sync.RWMutex
}
type SeckillProductConfig struct {
ProductId int `json:"product_id"`
StartTime int `json:"start_time"`
EndTime int `json:"end_time"`
Status int `json:"status"`
Total int `json:"total"`
Free int `json:"free"`
Used int `json:"used"`
}
// 初始化系统配置
func InitConfig() (err error) {
runmode := beego.AppConfig.String("runmode")
if len(runmode) == 0 {
err = fmt.Errorf("[InitConfig]RunMode config[%v] err ", runmode)
return
}
AppConfig.RunMode = runmode
AppConfig.Log_level = beego.AppConfig.String("log_level")
if len(AppConfig.Log_level) == 0 {
AppConfig.Log_level = "debug"
}
AppConfig.Log_path = beego.AppConfig.String("log_path")
if len(AppConfig.Log_path) == 0 {
AppConfig.Log_path = "./logs/system.log"
}
redisAddr := beego.AppConfig.String("redisAddr")
etcdAddr := beego.AppConfig.String("etcdAddr")
etcdSeckillKey := beego.AppConfig.String("etcdSeckillKey")
if len(redisAddr) == 0 || len(etcdAddr) == 0 || len(etcdSeckillKey) == 0 {
err = fmt.Errorf("[InitConfig]redis config[%v] err or etcd config[%v] err or etcdSeckillKey config[%v]err",
redisAddr, etcdAddr, etcdSeckillKey)
return
}
AppConfig.EtcdAddr = etcdAddr
AppConfig.RedisAddr = redisAddr
AppConfig.EtcdSeckillKey = etcdSeckillKey
logs.Debug("Redis:%v,Etcd:%v,EtcdSeckillKey:%v", redisAddr, etcdAddr, etcdSeckillKey)
return
}
// 加载商品秒杀活动配置
func LoadSeckillConfig() (err error) {
key := fmt.Sprintf("%s/Product", AppConfig.EtcdSeckillKey)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
resp, err := EtcdClient.Get(ctx, key)
cancel()
if err != nil {
logs.Error("Get[%s] from etcd failed, err:%v", key, err)
return
}
var seckillProductConfig []SeckillProductConfig
for _, ev := range resp.Kvs {
logs.Debug("Key[%s] Value:%s", ev.Key, ev.Value)
err = json.Unmarshal(ev.Value, &seckillProductConfig)
if err != nil {
logs.Error("json.Unmarshal failed,Key[%s] Value:%s err:%v", ev.Key, ev.Value, err)
return
}
logs.Debug("seckillInfoConfig:%v", seckillProductConfig)
}
// 更新配置
UpdateSeckillProductConfig(seckillProductConfig)
return
}
//初始化一个监控商品配置的线程。配置发送变化,实时通知,修改配置
func InitSeckillConfigWatch() {
go watchSeckillConfig()
}
// 监控 配置文件是否变化,实时更新配置
func watchSeckillConfig() {
var flag = true
key := fmt.Sprintf("%s/Product", AppConfig.EtcdSeckillKey)
var seckillProductConfig []SeckillProductConfig
rch := EtcdClient.Watch(context.Background(), key)
for wresp := range rch {
for _, ev := range wresp.Events {
logs.Warn("Type: %s Key:%s Value:%v", ev.Type, ev.Kv.Key, ev.Kv.Value)
if ev.Type == mvccpb.DELETE {
logs.Warn("key[%s] config delete ", key)
continue
}
if ev.Type == mvccpb.PUT && string(ev.Kv.Key) == key {
err := json.Unmarshal(ev.Kv.Value, &seckillProductConfig)
if err != nil {
logs.Error("json.Unmarshal key[%s] err:%v", key, ev.Kv.Value)
flag = false
continue
}
}
}
if flag {
// 更新配置
UpdateSeckillProductConfig(seckillProductConfig)
}
}
}
// 更新配置
func UpdateSeckillProductConfig(seckillProductConfig []SeckillProductConfig) {
// 1、先用临时变量存储
var tmp map[int]*SeckillProductConfig = make(map[int]*SeckillProductConfig, 1024)
for _, v := range seckillProductConfig {
tmp[v.ProductId] = &v
}
// 2、再加锁 更新配置
AppConfig.ProductConfigRwLock.Lock()
AppConfig.ProductConfigMap = tmp
AppConfig.ProductConfigRwLock.Unlock()
}