配置network.bind_host和publish_host(为公网地址)能够穿透局域网.java
-----------------------------------------------------------------------------------------------------------------------nicenode
全文搜索服务,基于Lucene构建的开源,分布式,RESTful搜索引擎。
官方网站: http://www.elasticsearch.org/
最好的学习资料:https://www.elastic.co/cn/git
表明一个集群,集群中有多个节点,其中有一个为主节点,这个主节点是能够经过选举产生的,主从节点是对于集群内部来讲的。es的一个概念就是去中心化,字面上理解就是无中心节点,这是对于集群外部来讲的,由于从外部来看es集群,在逻辑上是个总体,你与任何一个节点的通讯和与整个es集群通讯是等价的。github
表明索引分片,es能够把一个完整的索引分红多个分片,这样的好处是能够把一个大的索引拆分红多个,分布到不一样的节点上。构成分布式搜索。分片的数量只能在索引建立前指定,而且索引建立后不能更改。数据库
表明索引副本,es能够设置多个索引的副本,副本的做用一是提升系统的容错性,当个某个节点某个分片损坏或丢失时能够从副本中恢复。二是提升es的查询效率,es会自动对搜索请求进行负载均衡。npm
表明数据恢复或叫数据从新分布,es在有节点加入或退出时会根据机器的负载对索引分片进行从新分配,挂掉的节点从新启动时也会进行数据恢复。json
表明es的一个数据源,也是其它存储方式(如:数据库)同步数据到es的一个方法。它是以插件方式存在的一个es服务,经过读取river中的数据并把它索引到es中,官方的river有couchDB的,RabbitMQ的,Twitter的,Wikipedia的。
gateway
表明es索引快照的存储方式,es默认是先把索引存放到内存中,当内存满了时再持久化到本地硬盘。gateway对索引快照进行存储,当这个es集群关闭再从新启动时就会从gateway中读取索引备份数据。
es支持多种类型的gateway,有本地文件系统(默认),分布式文件系统,Hadoop的HDFS和amazon的s3云存储服务。bootstrap
表明es的自动发现节点机制,es是一个基于p2p的系统,它先经过广播寻找存在的节点,再经过多播协议来进行节点之间的通讯,同时也支持点对点的交互。跨域
表明es内部节点或集群与客户端的交互方式,默认内部是使用tcp协议进行交互,同时它支持http协议(json格式)、thrift、servlet、memcached、zeroMQ等的传输协议(经过插件方式集成)。浏览器
解压上传好的jdk包,编辑/etc/profile,配置好路径就能够了
我是解压到/opt下
vi /etc/profile
最后面追加:
export JAVA_HOME=/opt/jdk1.8.0_101 export PATH=.:$JAVA_HOME/bin:PATH
运行命令source /etc/profile
,使环境变量生效
检查java -version:
出现:
root@iZwz9dzr0kho293sd4lp0sZ:/opt# java -version java version "1.8.0_101" Java(TM) SE Runtime Environment (build 1.8.0_101-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
则代表jdk已经安装好
请不要用root帐号操做,用小号操做,解压到/opt下
hadoop@iZwz9dzr0kho293sd4lp0sZ:/opt/elasticsearch-5.4.0$ tree -L 2
.
├── bin
│ ├── elasticsearch 启动脚本
│ ├── elasticsearch.bat 启动脚本
│ ├── elasticsearch.in.bat 环境变量配置
│ ├── elasticsearch.in.sh 环境变量配置
│ ├── elasticsearch-keystore
│ ├── elasticsearch-keystore.bat
│ ├── elasticsearch-plugin 插件安装器
│ ├── elasticsearch-plugin.bat
│ ├── elasticsearch-service.bat
│ ├── elasticsearch-service-mgr.exe
│ ├── elasticsearch-service-x64.exe
│ ├── elasticsearch-service-x86.exe
│ ├── elasticsearch-systemd-pre-exec
│ ├── elasticsearch-translog
│ └── elasticsearch-translog.bat
├── config
│ ├── elasticsearch.yml 参数配置,集群,节点,网络,端口,并发等
│ ├── jvm.options
│ └── log4j2.properties
├── NOTICE.txt
├── lib jar库
├── plugins 插件安装后的目录
└── README.textile
./elasticsearch -d
验证,安装成功则有以下提示:
hadoop@iZwz9dzr0kho293sd4lp0sZ:/opt/elasticsearch-5.4.0/bin$ curl http://127.0.0.1:9200 { "name" : "d6tyDQa", "cluster_name" : "elasticsearch", "cluster_uuid" : "9uaoOC9mT3qiCdPViQ7vMg", "version" : { "number" : "5.4.0", "build_hash" : "780f8c4", "build_date" : "2017-04-28T17:43:27.229Z", "build_snapshot" : false, "lucene_version" : "6.5.0" }, "tagline" : "You Know, for Search" }
集群名称,默认为elasticsearch:
cluster.name: elasticsearch
设置一个节点的并发数量,有两种状况,一种是在初始复苏过程当中:
cluster.routing.allocation.node_initial_primaries_recoveries: 4
另外一种是在添加、删除节点及调整时:
cluster.routing.allocation.node_concurrent_recoveries: 2
es启动时会自动建立随机字符串做为节点名称而且每次不同,推荐进行配置:
node.name: elasticsearch-01
是否容许做为主节点,默认值为true:
node.master: true
是否存储数据,即存储索引片断,默认值为true:
node.data: true
注意:
master和data同时配置会产生一些奇异的效果:
当master为false,而data为true时,会对该节点产生严重负荷;
当master为true,而data为false时,该节点做为一个协调者(推荐);
当master为false,data也为false时,该节点就变成了一个负载均衡器。
你能够经过链接http://localhost:9200/_cluster/health或者http://localhost:9200/_cluster/nodes,或者使用插件http://github.com/lukas-vlcek/bigdesk或http://mobz.github.com/elasticsearch-head来查看集群状态。
节点自定义属性,可用于后期集群进行分片分配时的过滤:
node.rack: rack-01
设置一个索引的分片数量,默认值为5:
index.number_of_shards: 5
设置一个索引可被复制的数量,默认值为1:
index.number_of_replicas: 1
当你不须要分布式时,可进行以下设置:
index.number_of_shards: 1
index.number_of_replicas: 0
这两个属性的设置直接影响集群中索引和搜索操做的执行,假设有足够的机器来持有分片和副本,那么能够按以下规则设置这两个值:
- 拥有更多的分片能够提高索引执行能力,并容许经过机器分发一个大型的索引;
- 拥有更多的副本可以提高搜索执行能力以及集群能力,但副本增多会下降插入和删除操做的效率。
- 对于一个索引来讲,number_of_shards一旦设置将不能修改,而number_of_replicas可使用索引更新设置API在任什么时候候被增长或者减小;ElasticSearch关注加载均衡、迁移、从节点汇集结果等等,能够尝试多种设计来完成这些功能,能够链接http://localhost:9200/A/_status来检测索引的状态。
JVM开始交换时,ElasticSearch表现并很差:你须要保障JVM不进行交换,能够将bootstrap.mlockall设置为true禁止交换:
bootstrap.mlockall: true
能够编辑 elasticsearch-5.4.0/config/jvm.options
设置
-Xms10g
-Xmx10g
或者启动时 设置 ./bin/elasticsearch -Xmx10g -Xms10g
默认状况下,ElasticSearch使用0.0.0.0地址,并为http传输开启9200-9300端口,为节点到节点的通讯开启9300-9400端口,也能够自行设置IP地址:
network.bind_host: 192.168.0.1 #设置为0.0.0.0 可让其余机器访问
publish_host设置其余节点链接此节点的地址,若是不设置的话,则自动获取,publish_host的地址必须为真实地址:
network.publish_host: 192.168.0.1
bind_host和publish_host能够一块儿设置:
network.host: 192.168.0.1
能够定制该节点与其余节点交互的端口:
transport.tcp.port: 9300 (TCP)
节点间交互时,能够设置是否压缩,转为为不压缩:
transport.tcp.compress: true
能够为Http传输监听定制端口:
http.port: 9200 (HTTP)
设置内容的最大长度:
http.max_content_length: 100mb
禁止HTTP:
http.enabled: false
设置一个集群中主节点的数量,当多于三个节点时,该值可在2-4之间:
discovery.zen.minimum_master_nodes: 1
设置ping其余节点时的超时时间,网络比较慢时可将该值设大:
discovery.zen.ping.timeout: 3s
上有更多关于discovery的设置。
是否容许当前节点发现多个集群节点,默认值为true:
discovery.zen.ping.multicast.enabled: false
设置新节点被启动时可以发现的主节点列表(主要用于不一样网段机器链接,可带端口,可省略端口9300):
discovery.zen.ping.unicast.hosts: ["host1", "host2:port", "host3[portX-portY]"]
要修改 elasticsearch-5.4.0\config\elasticsearch.yml
追加配置,不然由于跨域问题,es不让elastic-head访问.
http.cors.enabled: true http.cors.allow-origin: "*"
而后在根目录下$elasticsearch-head下运行
npm run start
便可启动 elasticsearch-head,浏览器打开
http://localhost:9100/ ,elasticsearch-head便可自动链接本地的9200 elasticsearch端口
两种安装方式,在线安装和下载安装包离线安装,以head插件为例:
./elasticsearch-plugin install mobz/elasticsearch-head -verbose
安装完成,在浏览器打开http://127.0.0.1:9200/_plugin/head/ 便可
cd /opt git clone https://github.com/mobz/elasticsearch-head cd elasticsearch-head npm install npm run start
浏览器打开 http://localhost:9100/ (设置好elasticsearch的跨域)
在线上es的config目录的elasticsearch.yml增长如下配置,而后重启es
indices.query.bool.max_clause_count: 10240
hadoop@iZwz9dzr0kho293sd4lp0sZ:/opt/elasticsearch-5.4.0/bin$ Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error='Cannot allocate memory' (errno=12) # # There is insufficient memory for the Java Runtime Environment to continue. # Native memory allocation (mmap) failed to map 2060255232 bytes for committing reserved memory. # An error report file with more information is saved as: # /opt/elasticsearch-5.4.0/bin/hs_err_pid1679.log
那是由于内存分配不够,这里我作测试,因此改成1g内存
################################################################ # Xms represents the initial size of total heap space # Xmx represents the maximum size of total heap space -Xms1g -Xmx1g ################################################################
查看 logs/集群名.log日志,发现:
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:127) ~[elasticsearch-5.4.0.jar:5.4.0]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:114) ~[elasticsearch-5.4.0.jar:5.4.0]
max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
[2017-06-07T22:03:54,832][INFO ][o.e.n.Node ] [node-1] stopping ...
[2017-06-07T22:03:54,865][INFO ][o.e.n.Node ] [node-1] stopped
[2017-06-07T22:03:54,865][INFO ][o.e.n.Node ] [node-1] closing ...
[2017-06-07T22:03:54,885][INFO ][o.e.n.Node ] [node-1] closed
vi /etc/security/limits.conf
添加
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
NoNodeAvailableException[None of the configured nodes are available: [{#tran.......
请将节点设置:
network.host: 0.0.0.0
network.host:这个参数是用来同时设置bind_host和publish_host,这个值有时候设置为0.0.0.0也不行,由于机器有多张网卡(特别是自行安装了虚拟机的),绑定时会随机绑定网卡,当网络发生变化时,节点会被踢出去,因此最好的解决方法就是须要强制指定ip为具体的ip
network.host: 192.168.0.1
network.bind_host:设置绑定的ip地址,能够是ipv4或ipv6的,默认为0.0.0.0。
network.bind_host: 192.168.0.1
network.publish_host:设置其它节点和该节点交互的ip地址,若是不设置它会自动判断,值必须是个真实的ip地址。
network.publish_host: 192.168.0.1
某些时候连不上集群,集群以前报错,多是netty版本默认为4设置都比较高,能够设置为3
transport.type: netty3
若是是代码里抛这个错,多是没有初始化这个client变量,必定要初始化:
private static Client client; private static final Logger logger = LoggerFactory.getLogger(ESHelper.class); public ESHelper() { initClient(); } private static void initClient() { WTOIPProperties wtoipProperties=WTOIPProperties.getByConfType(""); String esNodes = wtoipProperties.getProperty("es.nodes", "127.0.0.1:9300"); String clusterName=wtoipProperties.getProperty("es.clusterName", "easin-tech"); Settings settings = Settings.builder() .put("cluster.name", clusterName)// 集群名 .put("client.transport.sniff", true)// 自动把集群下的机器添加到列表中 .build(); TransportClient newClient = new PreBuiltTransportClient(settings); for(String node:esNodes.split(",")) { String[] ipAndPort=node.split(":"); if(ipAndPort.length==1) { newClient.addTransportAddress(new InetSocketTransportAddress(InetAddresses.forString(ipAndPort[0]),9300));} else { newClient.addTransportAddresses(new InetSocketTransportAddress(InetAddresses.forString(ipAndPort[0]),Integer.valueOf(ipAndPort[1]))); } } ESHelper.client=newClient; logger.info("eshelper init ok!!!!"); } public static Client getClient() { if(ESHelper.client==null) { initClient(); } return ESHelper.client; }
而后任何地方要用 getClient(),而不能直接使用this.client成员变量