本文整理匯總了Golang中github.com/wfxiang08/rpc_proxy/zk.Topology.ProductServicePath方法的典型用法代碼示例。如果您正苦於以下問題:Golang Topology.ProductServicePath方法的具體用法?Golang Topology.ProductServicePath怎麽用?Golang Topology.ProductServicePath使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/wfxiang08/rpc_proxy/zk.Topology
的用法示例。
在下文中一共展示了Topology.ProductServicePath方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: NewBackService
// 創建一個BackService
func NewBackService(serviceName string, poller *zmq.Poller, topo *zk.Topology) *BackService {
backSockets := NewBackSockets(poller)
service := &BackService{
ServiceName: serviceName,
backend: backSockets,
poller: poller,
topo: topo,
}
var evtbus chan interface{} = make(chan interface{}, 2)
servicePath := topo.ProductServicePath(serviceName)
endpoints, err := topo.WatchChildren(servicePath, evtbus)
if err != nil {
log.Println("Error: ", err)
panic("Reading Service List Failed: ")
}
go func() {
for true {
// 如何監聽endpoints的變化呢?
addrSet := make(map[string]bool)
nowStr := time.Now().Format("@2006-01-02 15:04:05")
for _, endpoint := range endpoints {
// 這些endpoint變化該如何處理呢?
log.Println(utils.Green("---->Find Endpoint: "), endpoint, "For Service: ", serviceName)
endpointInfo, _ := topo.GetServiceEndPoint(serviceName, endpoint)
addr, ok := endpointInfo["frontend"]
if ok {
addrStr := addr.(string)
log.Println(utils.Green("---->Add endpoint to backend: "), addrStr, nowStr, "For Service: ", serviceName)
addrSet[addrStr] = true
}
}
service.backend.UpdateEndpointAddrs(addrSet)
// 等待事件
<-evtbus
// 讀取數據,繼續監聽
endpoints, err = topo.WatchChildren(servicePath, evtbus)
}
}()
ticker := time.NewTicker(time.Millisecond * 1000)
go func() {
for _ = range ticker.C {
service.backend.PurgeEndpoints()
}
}()
return service
}
示例2: mainBody
func mainBody(zkAddr string, productName string, serviceName string, frontendAddr string, backendAddr string) {
// 1. 創建到zk的連接
var topo *zk.Topology
topo = zk.NewTopology(productName, zkAddr)
// 2. 啟動服務
frontend, _ := zmq.NewSocket(zmq.ROUTER)
backend, _ := zmq.NewSocket(zmq.ROUTER)
defer frontend.Close()
defer backend.Close()
// ROUTER/ROUTER綁定到指定的端口
// tcp://127.0.0.1:5555 --> tcp://127_0_0_1:5555
lbServiceName := GetServiceIdentity(frontendAddr)
frontend.SetIdentity(lbServiceName)
frontend.Bind(frontendAddr) // For clients "tcp://*:5555"
backend.Bind(backendAddr) // For workers "tcp://*:5556"
log.Printf("FrontAddr: %s, BackendAddr: %s\n", magenta(frontendAddr), magenta(backendAddr))
// 後端的workers queue
workersQueue := queue.NewPPQueue()
// 心跳間隔1s
heartbeat_at := time.Tick(HEARTBEAT_INTERVAL)
poller1 := zmq.NewPoller()
poller1.Add(backend, zmq.POLLIN)
poller2 := zmq.NewPoller()
// 前提:
// 1. 當zeromq通知消息可讀時,那麽整個Message(所有的msg parts)都可讀
// 2. 往zeromq寫數據時,是異步的,因此也不存在block(除非數據量巨大)
//
poller2.Add(backend, zmq.POLLIN)
poller2.Add(frontend, zmq.POLLIN)
// 3. 注冊zk
var endpointInfo map[string]interface{} = make(map[string]interface{})
endpointInfo["frontend"] = frontendAddr
endpointInfo["backend"] = backendAddr
topo.AddServiceEndPoint(serviceName, lbServiceName, endpointInfo)
isAlive := true
isAliveLock := &sync.RWMutex{}
go func() {
servicePath := topo.ProductServicePath(serviceName)
evtbus := make(chan interface{})
for true {
// 隻是為了監控狀態
_, err := topo.WatchNode(servicePath, evtbus)
if err == nil {
// 等待事件
e := (<-evtbus).(topozk.Event)
if e.State == topozk.StateExpired || e.Type == topozk.EventNotWatching {
// Session過期了,則需要刪除之前的數據,因為這個數據的Owner不是當前的Session
topo.DeleteServiceEndPoint(serviceName, lbServiceName)
topo.AddServiceEndPoint(serviceName, lbServiceName, endpointInfo)
}
} else {
time.Sleep(time.Second)
}
isAliveLock.RLock()
isAlive1 := isAlive
isAliveLock.RUnlock()
if !isAlive1 {
break
}
}
}()
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
// syscall.SIGKILL
// kill -9 pid
// kill -s SIGKILL pid 還是留給運維吧
//
// 自動退出條件:
//
var suideTime time.Time
for {
var sockets []zmq.Polled
var err error
sockets, err = poller2.Poll(HEARTBEAT_INTERVAL)
if err != nil {
// break // Interrupted
log.Errorf("Error When Pollling: %v\n", err)
continue
//.........這裏部分代碼省略.........