sunty
2019-10-30 a564ed60bc1a6824bcfa504432e2b6bfb0992d4a
add es cluster manger
2个文件已修改
72 ■■■■■ 已修改文件
controllers/es.go 57 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
extend/esutil/EsClient.go 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/es.go
@@ -195,7 +195,7 @@
        nodeInfo["ip"] = nodeIp
        nodeInfo["nodeType"] = nodeType
        url := "http://"+nodeIp+":9200"
        buf := esutil.httpGet(url)
        buf := esutil.HttpGet(url)
        var info interface{}
        json.Unmarshal(buf,&info)
        tmpInfo := info.(map[string]interface{})
@@ -209,3 +209,58 @@
    return nodeInfos,nil
}
func AddEsCluster(hosts []string) (string){
    msg := "加入失败"
    for i,val := range hosts{
        val =val+":9300"
        hosts[i] = val
    }
    verificationHosts := "[\""+strings.Replace(strings.Trim(fmt.Sprint(hosts), "[]"), " ", "\",\"", -1)+"\"]"
    for i,val := range hosts{
        val ="\\\""+val+"\\\""
        hosts[i] = val
    }
    oldUnicastHost := "\\[\\\"0.0.0.0:9300\\\"\\]"
    newUnicastHost := strings.Replace(strings.Trim(fmt.Sprint(hosts), ""), " ", ",", -1)
    str := "sed -ie 's/discovery.zen.ping.unicast.hosts: "+oldUnicastHost+"/discovery.zen.ping.unicast.hosts: "+newUnicastHost+"/g' /opt/elasticsearch/config/elasticsearch.yml"
    fmt.Println(str)
    cmd := exec.Command("sh","-c",str)
    var out bytes.Buffer
    cmd.Stdout = &out
    err := cmd.Run()
    if err != nil {
    }
    res := getUnicastHosts()
    fmt.Println("res:          ",res)
    res1 := "discovery.zen.ping.unicast.hosts: "+verificationHosts+""
    fmt.Println("res1:         ",res1)
    if res == res1{
        msg = "加入成功"
    }
    str2 := "echo \"node.master: true\" >> /opt/elasticsearch/config/elasticsearch.yml"
    cmd2 := exec.Command("sh","-c",str2)
    var out2 bytes.Buffer
    cmd2.Stdout = &out2
    err2 := cmd2.Run()
    if err2 != nil {
        msg = "加入失败"
    }
    return msg
}
func getUnicastHosts() (string){
    str := "cat /opt/elasticsearch/config/elasticsearch.yml | grep discovery.zen.ping.unicast.hosts:"
    cmd := exec.Command("sh","-c",str)
    var out bytes.Buffer
    cmd.Stdout = &out
    err := cmd.Run()
    if err != nil {
    }
    infos := strings.Split(string(out.String()),"\n")[0]
    return infos
}
extend/esutil/EsClient.go
@@ -303,3 +303,18 @@
    }
    return sources, nil
}
func HttpGet(str string) ([]byte){
    resp, err :=   http.Get(str)
    if err != nil {
        // handle error
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        // handle error
    }
    return body
}