去oracle官网下载jdk, 上传到服务器,执行下面命令安装:html
rpm -ivh jdk-8u231-linux-x64.rpm复制代码
oracle jdk1.8下载页面: www.oracle.com/technetwork…
java
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 #设置开机启动
复制代码
mv /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.baknode
vim /etc/elasticsearch/elasticsearch.yml 建立新配置文件linux
除node.name 须要更改外,其余同样git
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"]
cluster.initial_master_nodes: ["node1"]复制代码
配置解释:github
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节点复制代码
执行启动命令:golang
systemctl start elasticsearch复制代码
查看日志信息:json
tail -f /var/log/elasticsearch/elasticsearch.log复制代码
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("没有查询到数据")
}
}
复制代码