引言:
最近在做一个搜索引擎相关的功能接口,在自己对技术的追求与热爱以及了解下,决定利用elasticSearch去实现该接口搜索功能,由于资源有限,又想学集群,就直接通过端口号不同来做个伪集群了。

ElasticSearch 简介
ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便

安装部署
步骤一:下载安装 打开网站 https://www.elastic.co/cn/downloads/elasticsearch 选择Not the version you're looking for? View past releases.然后进入过去版本页面选择自己需要的搜索引擎版本并下载 步骤二:安装
下载的安装包放到E盘新建的elasticsearch目录下,分别解压三份文件,并将文件名称分别修改为elasticsearch-5.6.1-node-1,elasticsearch-5.6.1-node-2,elasticsearch-5.6.1-node-3
es下载解压后,配置文件主要在config目录下,包含的文件:elasticsearch.yml,jvm.options,log4j2.properties。这三个文件分别对应ES配置,JVM配置,ES日志配置。我们这里只讨论elasticsearch.yml的配置,其他的暂时不论。
步骤三:单机多节点(伪集群)配置

在elasticsearch-5.6.1-node-1,elasticsearch-5.6.1-node-2,elasticsearch-5.6.1-node-3这三个目录的config下分别对elasticsearch.yml文件做配置,这里我们把elasticsearch-5.6.1-node-1当做主节点elasticsearch-5.6.1-node-2,elasticsearch-5.6.1-node-3当做从节点
 master(elasticsearch-5.6.1-node-1) 的 elasticsearch.yml:
 #指定集群的名称
 cluster.name: elasticsearch
 #节点名称
 node.name: node-1
 #是不是主节点
 node.master: true
 node.attr.rack: r1
 #最大集群节点数
 node.max_local_storage_nodes: 3
 #网关地址
 network.host: 127.0.0.1
 #端口
 http.port: 9200
 #内部节点之间沟通端口
 transport.tcp.port: 9300
 # 开启安全防护(启用跨域访问)
 http.cors.enabled: true
 http.cors.allow-origin: "*"
 #时间放长,防止脑裂
 discovery.zen.ping_timeout: 120s
 client.transport.ping_timeout: 60s
 http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
 #配置有机会参与选举为master的节点
 discovery.zen.ping.unicast.hosts: ["127.0.0.1:9300","127.0.0.1:9301","127.0.0.1:9302"]
 #elasticSearch服务器的数据目录和日志目录,可以自行新建后配置路径使elasticSearch服务启动后将运行数据和日志生成到指定目录下
 path.data: D:\elasticsearch\node-1\data
 path.logs: D:\elasticsearch\node-1\logsslave1(elasticsearch-5.6.1-node-2) 的 elasticsearch.yml:
 #指定集群的名称
 cluster.name: elasticsearch
 #节点名称
 node.name: node-2
 #是不是主节点
 node.master: false
 node.attr.rack: r1
 #最大集群节点数
 node.max_local_storage_nodes: 3
 #网关地址
 network.host: 127.0.0.1
 #端口
 http.port: 9201
 #内部节点之间沟通端口
 transport.tcp.port: 9301
 # 开启安全防护(启用跨域访问)
 http.cors.enabled: true
 http.cors.allow-origin: "*"
 #时间放长,防止脑裂
 discovery.zen.ping_timeout: 120s
 client.transport.ping_timeout: 60s
 http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
 #配置有机会参与选举为master的节点
 discovery.zen.ping.unicast.hosts: ["127.0.0.1:9300","127.0.0.1:9301","127.0.0.1:9302"]
 #elasticSearch服务器的数据目录和日志目录,可以自行新建后配置路径使elasticSearch服务启动后将运行数据和日志生成到指定目录下
 path.data: D:\elasticsearch\node-2\data
 path.logs: D:\elasticsearch\node-2\logsslave2(elasticsearch-5.6.1-node-3) 的 elasticsearch.yml:
 #指定集群的名称
 cluster.name: elasticsearch
 #节点名称
 node.name: node-3
 #是不是主节点
 node.master: false
 node.attr.rack: r1
 #最大集群节点数
 node.max_local_storage_nodes: 3
 #网关地址
 network.host: 127.0.0.1
 #端口
 http.port: 9202
 #内部节点之间沟通端口
 transport.tcp.port: 9302
 # 开启安全防护(启用跨域访问)
 http.cors.enabled: true
 http.cors.allow-origin: "*"
 #时间放长,防止脑裂
 discovery.zen.ping_timeout: 120s
 client.transport.ping_timeout: 60s
 http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
 #配置有机会参与选举为master的节点
 discovery.zen.ping.unicast.hosts: ["127.0.0.1:9300","127.0.0.1:9301","127.0.0.1:9302"]
 #elasticSearch服务器的数据目录和日志目录,可以自行新建后配置路径使elasticSearch服务启动后将运行数据和日志生成到指定目录下
 path.data: D:\elasticsearch\node-3\data
 path.logs: D:\elasticsearch\node-3\logs

启动Elasticsearch:bin/elasticsearch.bat

所有配置文件都配置好之后我们挨个启动一下服务,之后使用Head连接,便可轻松对搜索引擎服务器进行管理以及操作

补充:Head插件安装
下载elasticsearch-head的zip包,github网址如下:https://github.com/mobz/elasticsearch-head 检查node.js,这个版本要求node要9.0.0以上。很坑,之前更新了node没更npm,装了半天没反应。详细请自行百度。
安装grunt
grunt是一个很方便的构建工具,可以进行打包压缩、测试、执行等等的工作,5.0里的head插件就是通过grunt启动的。因此需要安装grunt:
注意:路径切到D:/node-v9.2.1-win-x64下。

npm install grunt
 npm install -g grunt-cli npm install grunt-contrib-clean
npm install grunt-contrib-concat
npm install grunt-contrib-watch
npm install grunt-contrib-connect
npm install grunt-contrib-copy
npm install grunt-contrib-jasmine

-g代表全局安装。安装路径为C:/Users/yourname/AppData/Roaming/npm,并且自动加入PATH变量。安装完成后检查一下:
修改head源码 
由于head的代码还是2.6版本的,直接执行有很多限制,比如无法跨机器访问。因此需要用户修改两个地方:

目录:head/Gruntfile.js:

复制代码
connect: { 
 server: { 
 options: { 
 port: 9100, 
 hostname: ‘*’, 
 base: ‘.’, 
 keepalive: true 
 } 
 } }复制代码 
 增加hostname属性,设置为*修改连接地址: 
 目录:head/_site/app.js修改head的连接【我的就是本地环境因此不需要修改】:
this.base_uri = this.config.base_uri || this.prefs.get(“app-base_uri”) 
 || “http://localhost:9200“;把localhost修改成你es的服务器地址,如:
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://111.111.111.111:9200";
 运行head 
 修改elasticsearch的参数 
 修改一下es使用的参数。编辑config/elasticsearch.yml:复制代码
#换个集群的名字,免得跟别人的集群混在一起 
 #cluster.name: es-5.0-test/ # 换个节点名字 
 /node.name: node-101# 修改一下ES的监听地址,这样别的机器也可以访问 
 network.host: 0.0.0.0# 默认的就好 
 #http.port: 9200# 增加新的参数,这样head插件可以访问es 
 http.cors.enabled: true 
 http.cors.allow-origin: “*”复制代码 
 注意,设置参数的时候:后面要有空格!开启elasticsearch-5.5.1:
D:/elasticsearch-5.5.1/bin/elasticsearch.bat
然后在head源码目录中,执行npm install 下载的包: 
 npm install 
 初次运行安装可能会报警告或错误。可以重新运行一次npm install。最后,在head源代码目录下启动nodejs: 
 grunt server

访问:target:9100 
这个时候,访问http://localhost:9100就可以访问head插件了.

参考文档:https://www.aliyun.com/jiaocheng/871767.htmlhttp://chenzhijun.me/2017/12/01/elasticsearch-install/