示例#1
0
func (es *ESServer) Start() error {
	api.SetHosts(es.hosts)

	// Add the client as a subscriber
	r := make(chan *buffer.Event, esRecvBuffer)
	es.b.AddSubscriber(es.host, r)
	defer es.b.DelSubscriber(es.host)

	// Create indexer
	idx := &Indexer{0, bytes.NewBuffer(nil)}

	// Loop events and publish to elasticsearch
	tick := time.NewTicker(time.Duration(esFlushInterval) * time.Second)

	for {
		select {
		case ev := <-r:
			idx.index(ev)
		case <-tick.C:
			idx.flush()
		case <-es.term:
			tick.Stop()
			log.Println("Elasticsearch received term signal")
			break
		}
	}

	return nil
}
func loadEndpointConfig() {
	log.Info("Loading ElasticSearch config")

	port := config.AtPath("hailo", "service", "elasticsearch", "port").AsInt(9200)
	hosts := config.AtPath("hailo", "service", "elasticsearch", "hosts").AsHostnameArray(port)

	if len(hosts) == 0 {
		hosts = append(hosts, "localhost:19200")
	}

	// Set these hosts in the Elasticsearch library
	// This will initialise a host pool which uses an Epsilon Greedy algorithm to find healthy hosts
	// and send to requests to them, and not unhealthy or slow hosts
	eapi.Port = strconv.Itoa(port)
	if port == 443 {
		eapi.Protocol = "https"
	}
	eapi.SetHosts(hosts)

	log.Infof("ElasticSearch hosts loaded: %v", eapi.Hosts)
}