Example #1
0
//continue to register a service preferably ran in a go routine.
func registerService(name string, port int, ttl int) {

	reportInterval := make(chan bool, 1)
	go func() {
		for {
			time.Sleep(time.Duration(ttl) / 2 * time.Second)
			reportInterval <- true
		}
	}()

	client, err := consul.NewClient(consul.DefaultConfig())
	if nil != err {
		log.Error("Failed to get consul client")
	}
	agent := client.Agent()

	serviceRegister(name, port, ttl, agent)
	for {
		select {
		case <-reportInterval: //report registration
			{
				servicePassing(name, agent)

			}
		}
	}

}
Example #2
0
func getConsulMasterIP() *consul.Config {
	e := os.Getenv(consulEnv)
	if e == "" {
		panic("consul not found!")
	}
	serverIP := fmt.Sprintf("%s:9200", e)
	config := consul.DefaultConfig()
	config.Address = serverIP
	fmt.Println(serverIP)
	return config
}