更新时间:2024-04-18 GMT+08:00
分享

通过Go接入集群

本文介绍通过Go语言访问CSS集群的配置说明。

准备工作

  • CSS集群处于可用状态。
  • 确保运行Go代码的服务器与CSS集群的网络是互通的。
  • 确认服务器已安装Go,Go官网下载地址:https://go.dev/dl/。

连接非安全集群

连接非安全集群,示例代码如下:

package main

import (
	"github.com/elastic/go-elasticsearch/v7"
	"log"
)

func main() {
	cfg := elasticsearch.Config{
		Addresses: []string{
			"http://HOST:9200/",
		},
	}

	es, _ := elasticsearch.NewClient(cfg)
	log.Println(es.Info())
}

连接安全集群

  • 连接未开启https的安全集群,示例如下:
    package main
    
    import (
    	"github.com/elastic/go-elasticsearch/v7"
    	"log"
    )
    
    func main() {
    	cfg := elasticsearch.Config{
    		Addresses: []string{
    			"http://HOST:9200/",
    		},
    		Username: "USERNAME",
    		Password: "PASSWORD",
    	}
    
    	es, _ := elasticsearch.NewClient(cfg)
    	log.Println(es.Info())
    }

  • 连接开启https的安全集群,不使用证书,示例代码如下:
    package main
    
    import (
    	"crypto/tls"
    	"github.com/elastic/go-elasticsearch/v7"
    	"log"
    	"net/http"
    )
    
    func main() {
    	cfg := elasticsearch.Config{
    		Addresses: []string{
    			"https://HOST:9200/",
    		},
    		Username: "USERNAME",
    		Password: "PASSWORD",
    		Transport: &http.Transport{
    			TLSClientConfig: &tls.Config{
    				InsecureSkipVerify: true,
    			},
    		},
    	}
    
    	es, _ := elasticsearch.NewClient(cfg)
    	log.Println(es.Info())
    }

  • 连接开启https的安全集群,使用证书,示例代码如下:
    package main
    
    import (
    	"crypto/tls"
    	"crypto/x509"
    	"flag"
    	"github.com/elastic/go-elasticsearch/v7"
    	"io/ioutil"
    	"log"
    	"net"
    	"net/http"
    	"time"
    )
    
    func main() {
    	insecure := flag.Bool("insecure-ssl", false, "Accept/Ignore all server SSL certificates")
    	flag.Parse()
    
    	// Get the SystemCertPool, continue with an empty pool on error
    	rootCAs, _ := x509.SystemCertPool()
    	if rootCAs == nil {
    		rootCAs = x509.NewCertPool()
    	}
    
    	// Read in the cert file
    	certs, err := ioutil.ReadFile("/tmp/CloudSearchService.cer")
    	if err != nil {
    		log.Fatalf("Failed to append %q to RootCAs: %v", "xxx", err)
    	}
    
    	// Append our cert to the system pool
    	if ok := rootCAs.AppendCertsFromPEM(certs); !ok {
    		log.Println("No certs appended, using system certs only")
    	}
    
    	config := elasticsearch.Config{
    		Addresses: []string{
    			"https://HOST:9200/",
    		},
    		Username: "USERNAME",
    		Password: "PASSWORD",
    		Transport: &http.Transport{
    			MaxIdleConnsPerHost:   10,
    			ResponseHeaderTimeout: time.Second,
    			DialContext: (&net.Dialer{
    				Timeout:   30 * time.Second,
    				KeepAlive: 30 * time.Second,
    			}).DialContext,
    			TLSClientConfig: &tls.Config{
    				InsecureSkipVerify: *insecure,
    				RootCAs:            rootCAs,
    			},
    		},
    	}
    	es, _ := elasticsearch.NewClient(config)
    	log.Println(elasticsearch.Version)
    	log.Println(es.Info())
    }

运行代码

根据集群类型将以上适配的代码写入到EsTest.gc文件并存放到一个单独的目录,在该目录执行以下命令:

go env -w GO111MODULE=on
go env -w GOPROXY=https://repo.huaweicloud.com/repository/goproxy/ 
go env -w GONOSUMDB=*

go mod init test
go mod tidy
go run EsTest.go
分享:

    相关文档

    相关产品