| | |
| | | package cache |
| | | |
| | | import ( |
| | | "basic.com/dbapi.git" |
| | | "basic.com/pubsub/protomsg.git" |
| | | "basic.com/valib/gopherdiscovery.git" |
| | | "basic.com/pubsub/cache.git/shardmap" |
| | | "basic.com/valib/logger.git" |
| | | "errors" |
| | | "github.com/gogo/protobuf/proto" |
| | | "fmt" |
| | | "github.com/satori/go.uuid" |
| | | "strconv" |
| | | ) |
| | | |
| | | const ( |
| | | SERVER_KEY = "SERVERINFO" |
| | | ) |
| | | |
| | | var cMap *shardmap.ShardMap |
| | | |
| | | |
| | | func Init(initChan chan bool,dbIp string,surveyPort int,pubSubPort int){ |
| | | cMap = shardmap.New(uint8(32)) |
| | | urlSurvey := "tcp://" + dbIp + ":" + strconv.Itoa(surveyPort) |
| | | urlPubSub := "tcp://" + dbIp + ":" + strconv.Itoa(pubSubPort) |
| | | client, _ := gopherdiscovery.ClientWithSub(urlSurvey, urlPubSub, "webServerProc_"+uuid.NewV4().String()) |
| | | recvMsg := client.HeartBeatMsg() |
| | | fmt.Println(<-recvMsg) |
| | | |
| | | initCacheData(initChan) |
| | | |
| | | peers, _ := client.Peers() |
| | | for b := range peers{ |
| | | updateData(b) |
| | | } |
| | | } |
| | | |
| | | func initCacheData(initChan chan bool) { |
| | | initServerInfo()//初始化服务器配置信息 |
| | | |
| | | initChan <- true |
| | | } |
| | | |
| | | var newUpdateMsg = &protomsg.DbChangeMessage{} |
| | | |
| | | func updateData(b []byte){ |
| | | if err :=proto.Unmarshal(b,newUpdateMsg);err !=nil{ |
| | | logger.Debug("dbChangeMsg unmarshal err:",err) |
| | | return |
| | | } |
| | | switch newUpdateMsg.Table { |
| | | case protomsg.TableChanged_T_Server: |
| | | initServerInfo() |
| | | default: |
| | | logger.Debug("other updateData operation") |
| | | |
| | | } |
| | | } |
| | | |
| | | func initServerInfo() { |
| | | var api dbapi.SysSetApi |
| | | b, s := api.GetServerInfo() |
| | | if b{ |
| | | cMap.Set(SERVER_KEY,s) |
| | | } |
| | | } |
| | | |
| | | func GetServerInfo() (conf protomsg.LocalConfig,err error) { |
| | | config, b := cMap.Get(SERVER_KEY) |
| | | if b { |
| | | return config.(protomsg.LocalConfig),nil |
| | | } else { |
| | | return conf,errors.New("conf not found") |
| | | } |
| | | } |