Skip to content

Commit

Permalink
feature: support multi nodes config for es
Browse files Browse the repository at this point in the history
  • Loading branch information
hyphennn committed Jul 17, 2024
1 parent 256737f commit aad94de
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
26 changes: 19 additions & 7 deletions cmd/internal/storage/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"flag"
"fmt"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -125,28 +126,39 @@ func newStorage(
elasticHost string,
enableSniffer bool,
) (storage.StorageDriver, error) {
// Remove all spaces to help user to configure
elasticHost = strings.ReplaceAll(elasticHost, " ", "")
hosts := strings.Split(elasticHost, ",")

// Obtain a client and connect to the default Elasticsearch installation
// on 127.0.0.1:9200. Of course you can configure your client to connect
// to other hosts and configure it in various other ways.
client, err := elastic.NewClient(
elastic.SetHealthcheck(true),
elastic.SetSniff(enableSniffer),
elastic.SetHealthcheckInterval(30*time.Second),
elastic.SetURL(elasticHost),
elastic.SetURL(hosts...),
)
if err != nil {
// Handle error
return nil, fmt.Errorf("failed to create the elasticsearch client - %s", err)
}

// Ping the Elasticsearch server to get e.g. the version number
info, code, err := client.Ping().URL(elasticHost).Do()
if err != nil {
// Handle error
return nil, fmt.Errorf("failed to ping the elasticsearch - %s", err)

// Just ping anyone of hosts successfully will be ok
var res *elastic.PingResult
var code int
for _, host := range hosts {
res, code, err = client.Ping().URL(host).Do()
if err == nil {
break
}
fmt.Printf("ping host %s failed, code: %d, err: %s", host, code, err)
}
if res == nil {
return nil, fmt.Errorf("failed to ping any host of the elasticsearch")
}
fmt.Printf("Elasticsearch returned with code %d and version %s", code, info.Version.Number)
fmt.Printf("Elasticsearch returned with code %d and version %s", code, res.Version.Number)

ret := &elasticStorage{
client: client,
Expand Down
2 changes: 2 additions & 0 deletions docs/storage/elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Specify ES host address:

```
-storage_driver_es_host="http://elasticsearch:9200"
# If you has several hosts, just use comma to separate it.
-storage_driver_es_host="http://elasticsearch1:9200,http://elasticsearch2:9200,http://elasticsearch3:9200"
```

There are also optional flags:
Expand Down

0 comments on commit aad94de

Please sign in to comment.