Selaa lähdekoodia

增加用户名密码

wangchuanjin 4 vuotta sitten
vanhempi
commit
18b0165f5f
1 muutettua tiedostoa jossa 25 lisäystä ja 9 poistoa
  1. 25 9
      esv7/elasticutil.go

+ 25 - 9
esv7/elasticutil.go

@@ -16,9 +16,13 @@ import (
 )
 
 //检索库服务地址
-var addrs []string
-var LocCity = map[string]string{}
-var SIZE = 30
+var (
+	addrs    []string
+	LocCity  = map[string]string{}
+	SIZE     = 30
+	username string
+	password string
+)
 
 const (
 	QStr = `{"query":{"bool":{"must":[$and],"must_not":[],
@@ -57,16 +61,30 @@ var poolsize = int32(20)
 
 //n倍的池
 func InitElasticSize(addr string, size int) {
+	InitElasticSizeByAuth(addr, size, "", "")
+}
+
+//初始化es,带有用户名密码认证
+func InitElasticSizeByAuth(addr string, size int, u, p string) {
 	poolsize = int32(3 * size)
 	pool = make(chan *es.Client, poolsize)
 	for _, s := range strings.Split(addr, ",") {
 		addrs = append(addrs, s)
 	}
+	username = u
+	password = p
 	for i := 0; i < size; i++ {
-		client, _ := es.NewClient(es.SetURL(addrs...), es.SetMaxRetries(2), es.SetSniff(false))
+		client, _ := newClient()
 		pool <- client
 	}
 }
+func newClient() (*es.Client, error) {
+	opt := []es.ClientOptionFunc{es.SetURL(addrs...), es.SetMaxRetries(2), es.SetSniff(false)}
+	if username != "" && password != "" {
+		opt = append(opt, es.SetBasicAuth(username, password))
+	}
+	return es.NewClient(opt...)
+}
 
 //关闭连接
 func DestoryEsConn(client *es.Client) {
@@ -87,14 +105,12 @@ var (
 )
 
 //获取连接
-
 func GetEsConn() *es.Client {
 	select {
 	case c := <-pool:
 		if c == nil || !c.IsRunning() {
 			log.Println("new esclient.", len(pool))
-			client, err := es.NewClient(es.SetURL(addrs...),
-				es.SetMaxRetries(2), es.SetSniff(false))
+			client, err := newClient()
 			if err == nil && client.IsRunning() {
 				return client
 			}
@@ -110,10 +126,10 @@ func GetEsConn() *es.Client {
 		if c > 12 {
 			lastTime = time.Now().Unix()
 			log.Println("add client..", len(pool))
-			c, _ := es.NewClient(es.SetURL(addrs...), es.SetMaxRetries(2), es.SetSniff(false))
+			c, _ := newClient()
 			go func() {
 				for i := 0; i < 2; i++ {
-					client, _ := es.NewClient(es.SetURL(addrs...), es.SetMaxRetries(2), es.SetSniff(false))
+					client, _ := newClient()
 					pool <- client
 				}
 			}()