memberlist demo

package main

import (
    "flag"
    "fmt"
    "github.com/hashicorp/memberlist"
    "os"
    "strconv"
    "time"
)

var bindPort = flag.Int("port", 8001, "gossip port")

func main()  {
    flag.Parse()
    hostname, _ := os.Hostname()
    config := memberlist.DefaultLocalConfig()  // 返回一个 struct
    fmt.Println(config)
    config.Name = hostname + "-" + strconv.Itoa(*bindPort)
    config.BindPort = *bindPort
    config.AdvertisePort = *bindPort

    fmt.Println("config.DisableTcpPings", config.DisableTcpPings)
    fmt.Println("config.IndirectChecks", config.IndirectChecks)
    fmt.Println("config.RetransmitMult", config.RetransmitMult)
    fmt.Println("config.PushPullInterval", config.PushPullInterval)
    fmt.Println("config.ProbeInterval", config.ProbeInterval)
    fmt.Println("config.GossipInterval", config.GossipInterval)
    fmt.Println("config.GossipNodes", config.GossipNodes)
    fmt.Println("config.BindPort", config.BindPort)

    list, _ := memberlist.Create(config)
    list.Join([]string{"127.0.0.1:8001", "127.0.0.1:8002"})
    fmt.Println(list.Members())

    for {
        fmt.Println("---------------start----------------")
        for _, member := range list.Members() {
            fmt.Printf("Member: %s %s\n", member.Name, member.Addr)
        }
        fmt.Println("---------------end----------------")
        time.Sleep(time.Second * 3)
    }

}

你可能感兴趣的:(memberlist demo)