554325746@qq.com
2020-06-02 abf8ec9aabce434794c9b001e37c3e1966f8e889
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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")
    }
}