github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/prepare/23_proto_actor/cluster-broadcast/node2/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "github.com/AsynkronIT/goconsole" 6 "github.com/AsynkronIT/protoactor-go/actor" 7 "github.com/AsynkronIT/protoactor-go/cluster" 8 "github.com/AsynkronIT/protoactor-go/cluster/consul" 9 "github.com/AsynkronIT/protoactor-go/examples/cluster-broadcast/shared" 10 "github.com/AsynkronIT/protoactor-go/remote" 11 "log" 12 "time" 13 ) 14 15 func main() { 16 startNode(8081) 17 18 fmt.Print("\nBoot other nodes and press Enter\n") 19 console.ReadLine() 20 21 calcAdd("Eggs", 1) 22 calcAdd("Eggs", 10) 23 24 calcAdd("Bananas", 1000) 25 26 calcAdd("Meat", 3) 27 calcAdd("Meat", 9000) 28 29 getAll() 30 31 console.ReadLine() 32 33 cluster.Shutdown(true) 34 } 35 36 func startNode(port int64) { 37 // how long before the grain poisons itself 38 timeout := 10 * time.Minute 39 40 // this node knows about Hello kind 41 remote.Register("Calculator", actor.PropsFromProducer(func() actor.Actor { 42 return &shared.CalculatorActor{ 43 Timeout: &timeout, 44 } 45 })) 46 47 // this node knows about Hello kind 48 remote.Register("Tracker", actor.PropsFromProducer(func() actor.Actor { 49 return &shared.TrackerActor{ 50 Timeout: &timeout, 51 } 52 })) 53 54 shared.CalculatorFactory(func() shared.Calculator { 55 return &shared.CalcGrain{} 56 }) 57 58 shared.TrackerFactory(func() shared.Tracker { 59 return &shared.TrackGrain{} 60 }) 61 62 cp, err := consul.New() 63 if err != nil { 64 log.Fatal(err) 65 } 66 cluster.Start("mycluster", fmt.Sprintf("127.0.0.1:%v", port), cp) 67 } 68 69 func calcAdd(grainId string, addNumber int64) { 70 calcGrain := shared.GetCalculatorGrain(grainId) 71 total1, err := calcGrain.Add(&shared.NumberRequest{Number: addNumber}) 72 if err != nil { 73 panic(err) 74 } 75 76 fmt.Printf("Grain: %v - Total: %v \n", calcGrain.ID, total1.Number) 77 } 78 79 func getAll() { 80 trackerGrain := shared.GetTrackerGrain("singleTrackerGrain") 81 totals, err := trackerGrain.BroadcastGetCounts(&shared.Noop{}) 82 if err != nil { 83 panic(err) 84 } 85 86 fmt.Println("--- Totals ---") 87 for grainId, count := range totals.Totals { 88 fmt.Printf("%v : %v\n", grainId, count) 89 } 90 }