1. elasticsearch集群搭建
1.1 下载安装jdk1.8(es需要jre运行环境)
去oracle官网下载jdk, 上传到服务器,执行下面命令安装:
rpm -ivh jdk-8u231-linux-x64.rpm
oracle jdk1.8下载页面: www.oracle.com/technetwork…
1.2 下载安装安装elasticsearch
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.4.2-x86_64.rpm
rpm -ivh elasticsearch-7.4.2-x86_64.rpm
systemctl daemon-reload #reload配置
systemctl enable elasticsearch.service #设置开机启动
1.3 配置elasticsearch(以3个节点为例)
mv /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak
vim /etc/elasticsearch/elasticsearch.yml 创建新配置文件
除node.name 需要更改,discovery.seed_hosts需要更改
cluster.name: elasticsearch
node.name: node1 #随机器变化。node2 node3 以此推
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
transport.tcp.compress: true
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
action.auto_create_index: true
discovery.zen.minimum_master_nodes: 1
node.max_local_storage_nodes: 3
discovery.seed_hosts: ["ip1","ip2","ip3"] #三台机器ip
cluster.initial_master_nodes: ["node1"]
配置解释:
cluster.name: 集群名称
node.name: node1 节点名称
network.host: 0.0.0.0 监控地址
http.port: 9200 数据端口
transport.tcp.port: 9300 java api端口
transport.tcp.compress: true
path.data: /var/lib/elasticsearch 数据目录
path.logs: /var/log/elasticsearch 日志目录
action.auto_create_index: true 自动创建索引
discovery.zen.minimum_master_nodes: 最小通信点数,(3节点先1,避免脑裂)
node.max_local_storage_nodes: 单机多进程数
discovery.seed_hosts: 集群节点ip
cluster.initial_master_nodes: 初始化指定的master节点
1.4 启动elasticsearch
执行启动命令:
systemctl start elasticsearch
查看日志信息:
tail -f /var/log/elasticsearch/elasticsearch.log
2. golang demo(查看某个索引内容)
2.1 输出该索引总条数,以及显示最近10条
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/olivere/elastic/v7"
"log"
)
type Subject struct {
id string
name string
age int
region string
}
func main() {
var client, _ = elastic.NewSimpleClient(elastic.SetURL("http://127.0.0.1:9200/"))
ctx := context.Background()
boolQuery := elastic.NewBoolQuery()
searchResult,err := client.Search().Index("index_name").
Query(boolQuery).From(0).Size(10).Do(ctx)
if err != nil {
log.Println("err:", err)
}
if searchResult.TotalHits() > 0 {
log.Printf("Found a total of %d indice\n", searchResult.TotalHits())
for _, hit := range searchResult.Hits.Hits {
var t Subject
err := json.Unmarshal(hit.Source, &t)
if err != nil {
log.Printf("%v", err)
}
fmt.Printf("name: %v,age: %v, region: %v\n",t.name,t.age,t.region)
}
} else {
log.Println("没有查询到数据")
}
}