Untitled
unknown
golang
3 years ago
1.8 kB
4
Indexable
package main import ( "fmt" "log" "net/http" "os" "strconv" consulapi "github.com/hashicorp/consul/api" ) func main() { serviceRegistryWithConsul() log.Println("Starting Hello World Server...") http.HandleFunc("/helloworld", helloworld) http.HandleFunc("/check", check) http.ListenAndServe(getPort(), nil) } func serviceRegistryWithConsul() { config := consulapi.DefaultConfig() consul, err := consulapi.NewClient(config) if err != nil { log.Println(err) } serviceID := "helloworld-server" port, _ := strconv.Atoi(getPort()[1:len(getPort())]) address := "192.168.0.102" registration := &consulapi.AgentServiceRegistration{ ID: serviceID, Name: "helloworld-server", Port: port, Address: "192.168.0.102", Check: &consulapi.AgentServiceCheck{ HTTP: fmt.Sprintf("http://%s:%v/check", address, port), Interval: "10s", Timeout: "30s", }, } regiErr := consul.Agent().ServiceRegister(registration) if regiErr != nil { log.Printf("Failed to register service: %s:%v ", address, port) } else { log.Printf("successfully register service: %s:%v", address, port) } } func helloworld(w http.ResponseWriter, r *http.Request) { log.Println("helloworld service is called.") w.WriteHeader(http.StatusOK) fmt.Fprintf(w, "Hello world.") } func check(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) fmt.Fprintf(w, "{\r\n \"status\" : \"UP\",\r\n \"groups\" : [ \"liveness\", \"readiness\" ]\r\n}") } func getPort() (port string) { port = os.Getenv("PORT") if len(port) == 0 { port = "8123" } port = ":" + port return } func getHostname() (hostname string) { hostname, _ = os.Hostname() return }
Editor is loading...