feat: support multi endpoints(format: scheme://user:passwd@ip1:port1,ip2:port2...)
This commit is contained in:
parent
ebb4365135
commit
dde92f2e59
@ -7,9 +7,11 @@ import (
|
|||||||
elastic "github.com/elastic/go-elasticsearch/v7"
|
elastic "github.com/elastic/go-elasticsearch/v7"
|
||||||
"github.com/elastic/go-elasticsearch/v7/esapi"
|
"github.com/elastic/go-elasticsearch/v7/esapi"
|
||||||
"github.com/loveuer/esgo2dump/internal/util"
|
"github.com/loveuer/esgo2dump/internal/util"
|
||||||
|
"github.com/samber/lo"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -21,7 +23,12 @@ func NewClient(ctx context.Context, url *url.URL) (*elastic.Client, error) {
|
|||||||
client *elastic.Client
|
client *elastic.Client
|
||||||
errCh = make(chan error)
|
errCh = make(chan error)
|
||||||
cliCh = make(chan *elastic.Client)
|
cliCh = make(chan *elastic.Client)
|
||||||
address = fmt.Sprintf("%s://%s", url.Scheme, url.Host)
|
endpoints = lo.Map(
|
||||||
|
strings.Split(url.Host, ","),
|
||||||
|
func(item string, index int) string {
|
||||||
|
return fmt.Sprintf("%s://%s", url.Scheme, item)
|
||||||
|
},
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if url.User != nil {
|
if url.User != nil {
|
||||||
@ -71,12 +78,12 @@ func NewClient(ctx context.Context, url *url.URL) (*elastic.Client, error) {
|
|||||||
cliCh <- cli
|
cliCh <- cli
|
||||||
}
|
}
|
||||||
|
|
||||||
go ncFunc([]string{address}, urlUsername, urlPassword)
|
go ncFunc(endpoints, urlUsername, urlPassword)
|
||||||
timeout := util.TimeoutCtx(ctx, 10)
|
timeout := util.TimeoutCtx(ctx, 10)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-timeout.Done():
|
case <-timeout.Done():
|
||||||
return nil, fmt.Errorf("dial es=%s err=%v", address, context.DeadlineExceeded)
|
return nil, fmt.Errorf("dial es=%v err=%v", endpoints, context.DeadlineExceeded)
|
||||||
case client = <-cliCh:
|
case client = <-cliCh:
|
||||||
return client, nil
|
return client, nil
|
||||||
case err = <-errCh:
|
case err = <-errCh:
|
||||||
|
20
xes/es7/client_test.go
Normal file
20
xes/es7/client_test.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package es7
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/loveuer/esgo2dump/internal/util"
|
||||||
|
"net/url"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewClient(t *testing.T) {
|
||||||
|
uri := "http://es1.dev:9200,es2.dev:9200"
|
||||||
|
ins, _ := url.Parse(uri)
|
||||||
|
|
||||||
|
c, err := NewClient(util.Timeout(5), ins)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log("success!!!")
|
||||||
|
_ = c
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user