简单介绍
Elasticsearch 是一个分布式、高扩展、高实时的搜索与数据分析引擎。它能很方便的使大量数据具有搜索、分析和探索的能力。
ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana , 它们都是开源软件。新增了一个FileBeat,它是一个轻量级的日志收集处理工具(Agent),Filebeat占用资源少,适合于在各个服务器上搜集日志后传输给Logstash,官方也推荐此工具。Elasticsearch是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
Logstash
主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。Kibana 也是一个开源和免费的工具,Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web
界面,可以帮助汇总、分析和搜索重要数据日志
我这边主要使用在储存日志中,本文主要是一些简单使用
1准备工作
elasticsearch-6.4.3.tar.gz
kibana-6.4.3-linux-x86_64.tar.gz
jdk1.8
本次使用的都是6.4.3版本 在配置中尽可能保存版本一致
2 单机搭建
Elasticsearch 在启动使用的时候是不能使用root 命令来进行的 所以我们在解压后应该新建立用户组
2.1 : 建立用户组并授予权限
useradd jiang
passwd jiang
chown -R jiang:jiang elasticsearch-6.4.3
cd /elasticsearch-6.4.3/config 修改配置文件
vi config/elasticsearch.yml
#在末尾添加配置
cluster.name: elk #集群名称
node.name: node-1 #节点名称
network.host: 0.0.0.0 #访问本地ip
http.port: 9200 #端口
启动之前先需要修改计算机的一些配置
1: vim /etc/security/limits.conf
末尾加上
# End of file
jiang soft nofile 65536 #jiang 你的用户组名称
jiang hard nofile 65536 #jiang 你的用户组名称
2: vim /etc/security/limits.d/90-nproc.conf
* soft nproc 4096
root soft nproc unlimited
jiang soft nofile 65536
jiang hard nofile 65536
3: vim /etc/sysctl.conf
#es 在末尾加上
vm.max_map_count = 655360
wq保存推出
sysctl -p 刷新生效
配置好之后启动
注意切换到新建组用户启动 root用户是无法启动的
进入bin目录 /opt/elasticsearch-6.4.3/bin
./elasticsearch 非后台启动
./elasticsearch & 后台启动
启动踩过的坑
1,使用root命令启动
2, 没有配置计算机参数: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
修改见 配置 3
3,max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
修改见 配置 1和2
4,启动后产生的日志 /opt/elasticsearch-6.4.3/logs 里产生了一些非新建组用户权限的目录 重新加权限为目录组即可
例如:chown -R jiang:jiang elasticsearch_access.log注意检查下 当时好几个目录里都产生了root用户组的文件 !!!
单机启动完成测试
浏览器 输入 http://192.168.2.131:9200
返回结果
{
"name" : "node-1",
"cluster_name" : "elk",
"cluster_uuid" : "kjfgT0BqSuSqwjdMUtlzhQ",
"version" : {
"number" : "6.4.3",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "fe40335",
"build_date" : "2018-10-30T23:17:19.084789Z",
"build_snapshot" : false,
"lucene_version" : "7.4.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
看到这个结果 恭喜成功
3 集群搭建
我这边是使用一台集器做了个伪集群 通过修改端口号就行了
3.1 复制三份 elasticsearch
同样的道理 都是不能已 root 命令启动的
同时建立data 文件和logs 文件
mkdir data
mkdir logs
修改三个elasticsearch配置文件
配置文件简单介绍
集器名称
cluster.name: elk
节点名称
node.name: node-1
最大集器节点数
node.max_local_storage_nodes: 3
是否有资格主节点
node.master: true
是否储存数据
node.data: true
ip地址
network.host: 0.0.0.0
端口
http.port: 9200
集器直接通信端口
transport.tcp.port: 9700
es7之后新增的配置 发现节点
discovery.seed_hosts: ["localhost:9700","localhost:9800","localhost:9900"]
es7 之后新增的配置 用来节点选举
cluster.initial_master_nodes: ["node-1","node-2","node-3"]
储存日志和文件
path.data: /opt/data
path.logs: /opt/logs
开始elasticsearch1的配置
在结尾添加配置
cluster.name: elk
node.name: node-1
node.master: true
node.data: true
node.max_local_storage_nodes: 3
network.host: 0.0.0.0
http.port: 9201
transport.tcp.port: 9700
#因为我们是用的 7之前的配置 所以不用添加这个 加上会报错
#discovery.seed_hosts["localhost:9700","localhost:9800","localhost:9900"]
discovery.zen.ping.unicast.hosts: ["localhost:9700","localhost:9800","localhost:9900"]
discovery.zen.minimum_master_nodes: 2
#因为我们是用的 7之前的配置 所以不用添加这个 加上会报错
#cluster.initial_master_nodes: ["node-1","node-2","node-3"]
path.data: /opt/data
path.logs: /opt/logs
开始elasticsearch2的配置
#添加配置
cluster.name: elk
node.name: node-2
node.master: true
node.data: true
node.max_local_storage_nodes: 3
network.host: 0.0.0.0
http.port: 9202
transport.tcp.port: 9800
#discovery.seed_hosts: ["localhost:9700","localhost:9800","localhost:9900"]
#cluster.initial_master_nodes: ["node-1","node-2","node-3"]
discovery.zen.ping.unicast.hosts: ["localhost:9700","localhost:9800","localhost:9900"]
discovery.zen.minimum_master_nodes: 2
path.data: /opt/data
path.logs: /opt/logs
开始elasticsearch3的配置
#添加配置
cluster.name: elk
node.name: node-3
node.master: true
node.data: true
node.max_local_storage_nodes: 3
network.host: 0.0.0.0
http.port: 9203
transport.tcp.port: 9900
#discovery.seed_hosts: ["localhost:9700","localhost:9800","localhost:9900"]
#cluster.initial_master_nodes: ["node-1","node-2","node-3"]
discovery.zen.ping.unicast.hosts: ["localhost:9700","localhost:9800","localhost:9900"]
discovery.zen.minimum_master_nodes: 2
path.data: /opt/data
path.logs: /opt/logs
切记一定要授权到新建用户组哦!!!!!
设置es 的jvm占用内存参数 我这边演示修改一台
vim /opt/elasticsearch1/config/bin/elasticsearch
修改 vim /opt/elasticsearch1/config/jvm.options
es 启动默认最小1G 最大1G 我们修改为 256m
切换用户 依次启动 bin目录下 ./elasticsearch
http://192.168.2.131:9201/_cat/health?v
看到这 恭喜你 ok 拉
这样看起来非常不简明
引入 kibana
安装包如上
直接进入修改配置文件
vim /opt/kibana-6.4.3-linux-x86_64/config/kibana.yml
#访问端口
server.port: 5601
#ip地址 0.0.0.0 表示可远程访问
server.host: "0.0.0.0"
#服务名
server.name: kibana-elk
#elasticsearch 地址
#高版本的话 es7之后新增的配置 配置如下
#elasticsearch.hosts:["http://localhost:9201","http://localhost:9202","http://localhost:9203"]
elasticsearch.url: "http://localhost:9201"
# 请求elasticsearch 超时时间 默认30000
elasticsearch.requestTimeout: 99999
启动 可以root下启动 高版本是不建议的 我这个版本非root 是启动不了的
bin目录下 ./kibana
访问 192.168.2.131:5601 进入图形化操作界面
点击 Dev Tools 进行 elasticSearch的一些简单操作
#索引就类似我们的数据库
1 #新增索引
PUT /test_index
#查询索引
GET /test_index
#返回结果
{
"test_index": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1605141744625",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "E37aELtMRB27S8tdy-1Jbw",
"version": {
"created": "6040399"
},
"provided_name": "test_index"
}
}
}
}
2 #添加数据
PUT /test_index/user/1
{
"name":"test",
"sex":"1",
"age":"12"
}
3 #获取数据
GET /test_index/user/1
#返回结果
{
"_index": "test_index",
"_type": "user",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"name": "test",
"sex": "1",
"age": "12"
}
}
4 #修改数据和添加是一样的操作
5 #删除数据
DELETE /test_index/user/1
6 #关闭索引
POST /test_index/_close
7 #打开索引
POST /test_index/_open
#....等等就不列举了
至此一个简单的存储和获取就搭建成功了
4 springBoot整合 Elasticsearch
版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 加入es核心jar包 -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
之前springBoot采用了高版本一直找不到jar包 切换成2.2.2.RELEASE这个可以正常使用
yml配置
spring:
data:
elasticsearch:
cluster-name: elk #elasticsearch.yml 配置的
cluster-nodes: 192.168.2.131:9700 # 外部连接节点
代码测试:
1 新建实体类
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "estest", type = "user")
@Data
public class User implements Serializable {
private String id;
private String name;
private int sex;
private int age;
}
2 接口层(dao) 简单一个查询
@Component
public interface UserReposiory extends ElasticsearchRepository<User ,String> {
User queryEmployeeById(String id);
}
3 controller层
@RestController
@RequestMapping("es")
public class ElasticSearchController {
@Autowired
private UserReposiory userReposiory;
//增加
@RequestMapping("/add/{id}")
public String add(@PathVariable("id")String id){
User user =new User ();
user.setId(id);
user.setName("哈哈");
user.setAge(26);
user.setSex(1);
# save (elasticsearch内置自带方法,可以直接使用)
userReposiory.save(user);
log.info("😁添加成功了》》》》");
return "添加成功了";
}
//删除
@RequestMapping("/delete")
public String delete(){
User user =new User ();
user.setId("1");
# delete(elasticsearch内置自带方法,可以直接使用)
userReposiory.delete(user);
log.info("😁删除成功了》》》》");
return "删除成功了";
}
//局部更新
@RequestMapping("/update")
public String update(){
User user = userReposiory.queryEmployeeById("1");
user.setName("我来更新了");
userReposiory.save(employee);
log.info("😁更新成功了》》》》");
return "更新成功";
}
}
添加后进入控制台查看 发现数据和我们添加的一致
这些就是关于es的一些简单操作 后续有更新 请继续关注