sniffer.asciidoc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. [[sniffer]]
  2. == Sniffer
  3. Minimal library that allows to automatically discover nodes from a running
  4. Elasticsearch cluster and set them to an existing `RestClient` instance.
  5. It retrieves by default the nodes that belong to the cluster using the
  6. Nodes Info api and uses jackson to parse the obtained json response.
  7. Compatible with Elasticsearch 2.x and onwards.
  8. === Maven Repository
  9. Here is how you can configure the dependency using maven as a dependency manager.
  10. Add the following to your `pom.xml` file:
  11. ["source","xml",subs="attributes"]
  12. --------------------------------------------------
  13. <dependency>
  14. <groupId>org.elasticsearch.client</groupId>
  15. <artifactId>sniffer</artifactId>
  16. <version>{version}</version>
  17. </dependency>
  18. --------------------------------------------------
  19. The low-level REST client is subject to the same release cycle as
  20. elasticsearch. Replace `${es.version}` with the desired sniffer version, first
  21. released with `5.0.0-alpha4`. There is no relation between the sniffer version
  22. and the elasticsearch version that the client can communicate with. Sniffer
  23. supports fetching the nodes list from elasticsearch 2.x and onwards.
  24. === Usage
  25. Once a `RestClient` instance has been created, a `Sniffer` can be associated
  26. to it. The `Sniffer` will make use of the provided `RestClient` to periodically
  27. (every 5 minutes by default) fetch the list of current nodes from the cluster
  28. and update them by calling `RestClient#setHosts`.
  29. [source,java]
  30. --------------------------------------------------
  31. Sniffer sniffer = Sniffer.builder(restClient).build();
  32. --------------------------------------------------
  33. It is important to close the `Sniffer` so that its background thread gets
  34. properly shutdown and all of its resources are released. The `Sniffer`
  35. object should have the same lifecycle as the `RestClient` and get closed
  36. right before the client:
  37. [source,java]
  38. --------------------------------------------------
  39. sniffer.close();
  40. restClient.close();
  41. --------------------------------------------------
  42. The Elasticsearch Nodes Info api doesn't return the protocol to use when
  43. connecting to the nodes but only their `host:port` key-pair, hence `http`
  44. is used by default. In case `https` should be used instead, the
  45. `ElasticsearchHostsSniffer` object has to be manually created and provided
  46. as follows:
  47. [source,java]
  48. --------------------------------------------------
  49. HostsSniffer hostsSniffer = new ElasticsearchHostsSniffer(restClient,
  50. ElasticsearchHostsSniffer.DEFAULT_SNIFF_REQUEST_TIMEOUT,
  51. ElasticsearchHostsSniffer.Scheme.HTTPS);
  52. Sniffer sniffer = Sniffer.builder(restClient)
  53. .setHostsSniffer(hostsSniffer).build();
  54. --------------------------------------------------
  55. In the same way it is also possible to customize the `sniffRequestTimeout`,
  56. which defaults to one second. That is the `timeout` parameter provided as a
  57. querystring parameter when calling the Nodes Info api, so that when the
  58. timeout expires on the server side, a valid response is still returned
  59. although it may contain only a subset of the nodes that are part of the
  60. cluster, the ones that have responsed until then.
  61. Also, a custom `HostsSniffer` implementation can be provided for advanced
  62. use-cases that may require fetching the hosts from external sources.
  63. The `Sniffer` updates the nodes by default every 5 minutes. This interval can
  64. be customized by providing it (in milliseconds) as follows:
  65. [source,java]
  66. --------------------------------------------------
  67. Sniffer sniffer = Sniffer.builder(restClient)
  68. .setSniffIntervalMillis(60000).build();
  69. --------------------------------------------------
  70. It is also possible to enable sniffing on failure, meaning that after each
  71. failure the nodes list gets updated straightaway rather than at the following
  72. ordinary sniffing round. In this case a `SniffOnFailureListener` needs to
  73. be created at first and provided at `RestClient` creation. Also once the
  74. `Sniffer` is later created, it needs to be associated with that same
  75. `SniffOnFailureListener` instance, which will be notified at each failure
  76. and use the `Sniffer` to perform the additional sniffing round as described.
  77. [source,java]
  78. --------------------------------------------------
  79. SniffOnFailureListener sniffOnFailureListener = new SniffOnFailureListener();
  80. RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200))
  81. .setFailureListener(sniffOnFailureListener).build();
  82. Sniffer sniffer = Sniffer.builder(restClient).build();
  83. sniffOnFailureListener.setSniffer(sniffer);
  84. --------------------------------------------------
  85. When using sniffing on failure, not only do the nodes get updated after each
  86. failure, but an additional sniffing round is also scheduled sooner than usual,
  87. by default one minute after the failure, assuming that things will go back to
  88. normal and we want to detect that as soon as possible. Said interval can be
  89. customized at `Sniffer` creation time as follows:
  90. [source,java]
  91. --------------------------------------------------
  92. Sniffer sniffer = Sniffer.builder(restClient)
  93. .setSniffAfterFailureDelayMillis(30000).build();
  94. --------------------------------------------------
  95. Note that this last configuration parameter has no effect in case sniffing
  96. on failure is not enabled like explained above.
  97. === License
  98. Copyright 2013-2016 Elasticsearch
  99. Licensed under the Apache License, Version 2.0 (the "License");
  100. you may not use this file except in compliance with the License.
  101. You may obtain a copy of the License at
  102. http://www.apache.org/licenses/LICENSE-2.0
  103. Unless required by applicable law or agreed to in writing, software
  104. distributed under the License is distributed on an "AS IS" BASIS,
  105. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  106. See the License for the specific language governing permissions and
  107. limitations under the License.