func getService(w http.ResponseWriter, r *http.Request) { r.ParseForm() service := r.Form.Get("service") if len(service) == 0 { http.Error(w, "Require service", 400) return } s, err := registry.GetService(service) if err != nil { http.Error(w, err.Error(), 500) return } if s == nil || len(s.Name) == 0 { http.Error(w, "Service not found", 404) return } b, err := json.Marshal(s) if err != nil { http.Error(w, err.Error(), 500) return } w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Length", strconv.Itoa(len(b))) w.Write(b) }
func registryCommands() []cli.Command { return []cli.Command{ { Name: "list", Usage: "List items in registry", Subcommands: []cli.Command{ { Name: "services", Usage: "List services in registry", Action: func(c *cli.Context) { rsp, err := registry.ListServices() if err != nil { fmt.Println(err.Error()) return } for _, service := range rsp { fmt.Println(service.Name) } }, }, }, }, { Name: "get", Usage: "Get item from registry", Subcommands: []cli.Command{ { Name: "service", Usage: "Get service from registry", Action: func(c *cli.Context) { if !c.Args().Present() { fmt.Println("Service required") return } service, err := registry.GetService(c.Args().First()) if err != nil { fmt.Println(err.Error()) return } fmt.Printf("%s\n\n", service.Name) fmt.Println("Id\tAddress\tPort\tMetadata") for _, node := range service.Nodes { var meta []string for k, v := range node.Metadata { meta = append(meta, k+"="+v) } fmt.Printf("%s\t%s\t%d\t%s\n", node.Id, node.Address, node.Port, strings.Join(meta, ",")) } }, }, }, }, } }
func (s *Sidecar) serve() { http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { if c, err := s.hc(); err != nil { http.Error(w, err.Error(), c) return } }) http.HandleFunc("/registry", func(w http.ResponseWriter, r *http.Request) { r.ParseForm() service := r.Form.Get("service") if len(service) == 0 { http.Error(w, "Require service", 400) return } s, err := registry.GetService(service) if err != nil { http.Error(w, err.Error(), 500) return } if s == nil { http.Error(w, "Service not found", 404) return } b, err := json.Marshal(s) if err != nil { http.Error(w, err.Error(), 500) return } w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Length", strconv.Itoa(len(b))) w.Write(b) }) http.HandleFunc("/rpc", rpcHandler) http.HandleFunc("/broker", brokerHandler) if err := http.ListenAndServe(Address, nil); err != nil { log.Fatal(err) os.Exit(1) } }
func registryHandler(w http.ResponseWriter, r *http.Request) { r.ParseForm() svc := r.Form.Get("service") if len(svc) > 0 { s, err := registry.GetService(svc) if err != nil { http.Error(w, "Error occurred:"+err.Error(), 500) return } t, err := template.New("service").Funcs(template.FuncMap{ "format": format, }).Parse(serviceTemplate) if err != nil { http.Error(w, "Error occurred:"+err.Error(), 500) return } if err := t.ExecuteTemplate(w, "T", s); err != nil { http.Error(w, "Error occurred:"+err.Error(), 500) return } return } services, err := registry.ListServices() if err != nil { http.Error(w, "Error occurred:"+err.Error(), 500) return } t, err := template.New("registry").Parse(registryTemplate) if err != nil { http.Error(w, "Error occurred:"+err.Error(), 500) return } if err := t.ExecuteTemplate(w, "T", services); err != nil { http.Error(w, "Error occurred:"+err.Error(), 500) return } }
func (r *rpcClient) Stream(ctx context.Context, request Request, responseChan interface{}) (Streamer, error) { service, err := registry.GetService(request.Service()) if err != nil { return nil, errors.InternalServerError("go.micro.client", err.Error()) } if len(service.Nodes) == 0 { return nil, errors.NotFound("go.micro.client", "Service not found") } n := rand.Int() % len(service.Nodes) node := service.Nodes[n] address := node.Address if node.Port > 0 { address = fmt.Sprintf("%s:%d", address, node.Port) } return r.stream(ctx, address, request, responseChan) }
func (h *httpBroker) Publish(topic string, msg *Message) error { s, err := registry.GetService("topic:" + topic) if err != nil { return err } msg.Header[":topic"] = topic b, err := json.Marshal(msg) if err != nil { return err } for _, node := range s.Nodes { r, err := http.Post(fmt.Sprintf("http://%s:%d%s", node.Address, node.Port, DefaultSubPath), "application/json", bytes.NewBuffer(b)) if err == nil { r.Body.Close() } } return nil }
func registryCommands() []cli.Command { return []cli.Command{ { Name: "list", Usage: "List items in registry", Subcommands: []cli.Command{ { Name: "services", Usage: "List services in registry", Action: func(c *cli.Context) { rsp, err := registry.ListServices() if err != nil { fmt.Println(err.Error()) return } for _, service := range rsp { fmt.Println(service.Name) } }, }, }, }, { Name: "get", Usage: "Get item from registry", Subcommands: []cli.Command{ { Name: "service", Usage: "Get service from registry", Action: func(c *cli.Context) { if !c.Args().Present() { fmt.Println("Service required") return } service, err := registry.GetService(c.Args().First()) if err != nil { fmt.Println(err.Error()) return } fmt.Printf("%s\n\n", service.Name) fmt.Println("Id\tAddress\tPort\tMetadata") for _, node := range service.Nodes { var meta []string for k, v := range node.Metadata { meta = append(meta, k+"="+v) } fmt.Printf("%s\t%s\t%d\t%s\n", node.Id, node.Address, node.Port, strings.Join(meta, ",")) } for _, e := range service.Endpoints { var request, response string var meta []string for k, v := range e.Metadata { meta = append(meta, k+"="+v) } if e.Request != nil && len(e.Request.Values) > 0 { request = formatEndpoint(e.Request.Values[0], 0) } else { request = " {}" } if e.Response != nil && len(e.Response.Values) > 0 { response = formatEndpoint(e.Response.Values[0], 0) } else { response = " {}" } fmt.Printf("\nEndpoint: %s\nMetadata: %s\n", e.Name, strings.Join(meta, ",")) fmt.Printf("Request:%s\nResponse:%s\n", request, response) } }, }, }, }, } }
func Commands() []cli.Command { commands := []cli.Command{ { Name: "registry", Usage: "Query registry", Subcommands: registryCommands(), }, { Name: "query", Usage: "Query a service method using rpc", Action: func(c *cli.Context) { if len(c.Args()) < 2 { fmt.Println("require service and method") return } service := c.Args()[0] method := c.Args()[1] var request map[string]interface{} var response map[string]interface{} json.Unmarshal([]byte(strings.Join(c.Args()[2:], " ")), &request) req := client.NewJsonRequest(service, method, request) err := client.Call(context.Background(), req, &response) if err != nil { fmt.Printf("error calling %s.%s: %v\n", service, method, err) return } b, _ := json.MarshalIndent(response, "", "\t") fmt.Println(string(b)) }, }, { Name: "health", Usage: "Query the health of a service", Action: func(c *cli.Context) { if !c.Args().Present() { fmt.Println("require service name") return } service, err := registry.GetService(c.Args().First()) if err != nil { fmt.Printf("error querying registry: %v", err) return } fmt.Println("node\t\taddress:port\t\tstatus") req := client.NewRequest(service.Name, "Debug.Health", &health.Request{}) for _, node := range service.Nodes { address := node.Address if node.Port > 0 { address = fmt.Sprintf("%s:%d", address, node.Port) } rsp := &health.Response{} err := client.CallRemote(context.Background(), address, req, rsp) var status string if err != nil { status = err.Error() } else { status = rsp.Status } fmt.Printf("%s\t\t%s:%d\t\t%s\n", node.Id, node.Address, node.Port, status) } }, }, } return append(commands, registryCommands()...) }