func main() { // Create a TChannel and listen for inbound connections. ch, err := tchannel.NewChannel("keyvalue", nil) if err != nil { log.Fatalf("Failed to create tchannel: %v", err) } ip, err := tchannel.ListenIP() if err != nil { log.Fatalf("Failed to find IP to Listen on: %v", err) } ch.ListenAndServe(ip.String() + ":12345") // Register both the KeyValue and Admin services. h := newKVHandler() server := thrift.NewServer(ch) server.Register(keyvalue.NewTChanKeyValueServer(h)) server.Register(keyvalue.NewTChanAdminServer(h)) config := hyperbahn.Configuration{InitialNodes: os.Args[1:]} if len(config.InitialNodes) == 0 { log.Fatalf("No Autobahn nodes to advertise with") } client := hyperbahn.NewClient(ch, config, nil) if err := client.Advertise(); err != nil { log.Fatalf("Hyperbahn advertise failed: %v", err) } // The service is now started up, run it till we receive a ctrl-c. log.Printf("KeyValue service has started on %v", ch.PeerInfo().HostPort) select {} }
func setupServer(h *mocks.TChanTCollector) (*tchannel.Channel, net.Listener, error) { tchan, err := tchannel.NewChannel(tcollectorServiceName, nil) if err != nil { return nil, nil, err } listener, err := net.Listen("tcp", ":0") if err != nil { return nil, nil, err } server := thrift.NewServer(tchan) server.Register(gen.NewTChanTCollectorServer(h)) tchan.Serve(listener) return tchan, listener, nil }
func setupServer() (net.Listener, error) { tchan, err := tchannel.NewChannel("server", optsFor("server")) if err != nil { return nil, err } listener, err := net.Listen("tcp", ":0") if err != nil { return nil, err } server := thrift.NewServer(tchan) server.Register(gen.NewTChanFirstServer(&firstHandler{})) server.Register(gen.NewTChanSecondServer(&secondHandler{})) // Serve will set the local peer info, and start accepting sockets in a separate goroutine. tchan.Serve(listener) return listener, nil }
func main() { // Create a TChannel and register the Thrift handlers. ch, err := tchannel.NewChannel("keyvalue", nil) if err != nil { log.Fatalf("Failed to create tchannel: %v", err) } // Register both the KeyValue and Admin services. // We can register multiple Thrift services on a single Hyperbahn service. h := newKVHandler() server := thrift.NewServer(ch) server.Register(keyvalue.NewTChanKeyValueServer(h)) server.Register(keyvalue.NewTChanAdminServer(h)) // Listen for connections on the external interface so we can receive connections. ip, err := tchannel.ListenIP() if err != nil { log.Fatalf("Failed to find IP to Listen on: %v", err) } // We use port 0 which asks the OS to assign any available port. // Static port allocations are not necessary for services on Hyperbahn. ch.ListenAndServe(fmt.Sprintf("%v:%v", ip, 0)) // Advertising registers this service instance with Hyperbahn so // that Hyperbahn can route requests for "keyvalue" to us. config := hyperbahn.Configuration{InitialNodes: os.Args[1:]} if len(config.InitialNodes) == 0 { log.Fatalf("No Autobahn nodes to advertise with") } client, err := hyperbahn.NewClient(ch, config, nil) if err != nil { log.Fatalf("hyperbahn.NewClient failed: %v", err) } if err := client.Advertise(); err != nil { log.Fatalf("Hyperbahn advertise failed: %v", err) } // The service is now started up, run it till we receive a ctrl-c. log.Printf("KeyValue service has started on %v", ch.PeerInfo().HostPort) select {} }