ES集群需要配置几个地方,最简单的是配置集群名称和Node名称,其他的基本使用默认值就可以了,下面是elasticsearch.yml配置文件部分属性的用法,可以参考使用
|
|
elasticsearch.yml源文件链接:
Dockerfile文件
|
|
Dockerfile源文件链接:
https://github.com/birdben/birdDocker/blob/master/escluster/Dockerfile
supervisor配置文件内容
|
|
控制台终端
|
|
访问ElasticSearch集群的插件测试
|
|
ES集群的访问方式
Java API
Elasticsearch为Java用户提供了两种内置客户端:
需要在项目中引入elasticsearch.jar,这个jar中有两种ES提供的Client
节点客户端 (Node Client)
Instantiating a node based client is the simplest way to get a Client that can execute operations against elasticsearch.
Embedding a node client into your application is the easiest way to connect to an Elasticsearch cluster
Node Client加入ES集群是一个没有数据的Node,就是这个Node不存储任何数据,但是这个Node知道数据存储在ES集群的哪个Node,可以把请求转发到正确的Node上去
注意:
实际上Node Client是在我们的系统,单元测试中启动了一个Embedded ElasticSearch,这个Embedded ElasticSearch是我们不存储任何数据的Node加入ES集群,我们的系统通过它来和ES集群进行通信,最好关闭Embedded Node的http端口(9200端口)防止http请求直接访问,因为Node Client与ES集群Node的通信都是使用的9300端口
|
|
传输客户端 (Transport Client)
The TransportClient connects remotely to an Elasticsearch cluster using the transport module. It does not join the cluster, but simply gets one or more initial transport addresses and communicates with them in round robin fashion on each action (though most actions will probably be “two hop” operations).
Transport Client不会加入集群,只是负责把请求转发给ES集群的Node
Java Client和ES集群通信通过9300端口,使用native ElasticSearch transport protocol, ES集群的Node也使用9300端口进行通信。(如果ES集群的9300端口没有开通,你的Nodes无法构建成一个集群)
注意:Java Client必须和ES集群的Node版本一致(也就是jar包的版本要和ES集群的版本对应一致),否则无法通信
|
|
注意:addTransportAddress方法
Adds a transport address that will be used to connect to.The Node this transport address represents will be used if its possible to connect to it. If it is unavailable, it will be automatically connected to once it is up.In order to get the list of all the current connected nodes, please see connectedNodes().
当一个ES Node(对应一个transportAddress)不可用时,client会自动发现当前可用的nodes(the current connected nodes),从以下这段代码可知:
TransportClientNodesService
|
|
RESTFUL API
其他语言都可以使用RESTFUL API通过9200端口和ES进行通信,甚至可以通过命令行使用curl命令
HTTP Request格式
|
|
参考文章:
- http://my.oschina.net/xiaohui249/blog/228748
- http://blog.csdn.net/geloin/article/details/8444972
- https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/client.html
- https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/node-client.html
- https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html
- http://www.cnblogs.com/huangfox/p/3543134.html
- http://es.xiaoleilu.com/010_Intro/15_API.html