sniffer.asciidoc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. The low-level REST client is subject to the same release cycle as
  10. elasticsearch. Replace the version with the desired sniffer version, first
  11. released with `5.0.0-alpha4`. There is no relation between the sniffer version
  12. and the elasticsearch version that the client can communicate with. Sniffer
  13. supports fetching the nodes list from elasticsearch 2.x and onwards.
  14. ==== Maven configuration
  15. Here is how you can configure the dependency using maven as a dependency manager.
  16. Add the following to your `pom.xml` file:
  17. ["source","xml",subs="attributes"]
  18. --------------------------------------------------
  19. <dependency>
  20. <groupId>org.elasticsearch.client</groupId>
  21. <artifactId>sniffer</artifactId>
  22. <version>{version}</version>
  23. </dependency>
  24. --------------------------------------------------
  25. ==== Gradle configuration
  26. Here is how you can configure the dependency using gradle as a dependency manager.
  27. Add the following to your `build.gradle` file:
  28. ["source","groovy",subs="attributes"]
  29. --------------------------------------------------
  30. dependencies {
  31. compile 'org.elasticsearch.client:sniffer:{version}'
  32. }
  33. --------------------------------------------------
  34. === Usage
  35. Once a `RestClient` instance has been created as shown in <<java-rest-low-usage-initialization>>,
  36. a `Sniffer` can be associated to it. The `Sniffer` will make use of the provided `RestClient`
  37. to periodically (every 5 minutes by default) fetch the list of current nodes from the cluster
  38. and update them by calling `RestClient#setHosts`.
  39. ["source","java",subs="attributes,callouts,macros"]
  40. --------------------------------------------------
  41. include-tagged::{doc-tests}/SnifferDocumentation.java[sniffer-init]
  42. --------------------------------------------------
  43. It is important to close the `Sniffer` so that its background thread gets
  44. properly shutdown and all of its resources are released. The `Sniffer`
  45. object should have the same lifecycle as the `RestClient` and get closed
  46. right before the client:
  47. ["source","java",subs="attributes,callouts,macros"]
  48. --------------------------------------------------
  49. include-tagged::{doc-tests}/SnifferDocumentation.java[sniffer-close]
  50. --------------------------------------------------
  51. The `Sniffer` updates the nodes by default every 5 minutes. This interval can
  52. be customized by providing it (in milliseconds) as follows:
  53. ["source","java",subs="attributes,callouts,macros"]
  54. --------------------------------------------------
  55. include-tagged::{doc-tests}/SnifferDocumentation.java[sniffer-interval]
  56. --------------------------------------------------
  57. It is also possible to enable sniffing on failure, meaning that after each
  58. failure the nodes list gets updated straightaway rather than at the following
  59. ordinary sniffing round. In this case a `SniffOnFailureListener` needs to
  60. be created at first and provided at `RestClient` creation. Also once the
  61. `Sniffer` is later created, it needs to be associated with that same
  62. `SniffOnFailureListener` instance, which will be notified at each failure
  63. and use the `Sniffer` to perform the additional sniffing round as described.
  64. ["source","java",subs="attributes,callouts,macros"]
  65. --------------------------------------------------
  66. include-tagged::{doc-tests}/SnifferDocumentation.java[sniff-on-failure]
  67. --------------------------------------------------
  68. <1> Set the failure listener to the `RestClient` instance
  69. <2> When sniffing on failure, not only do the nodes get updated after each
  70. failure, but an additional sniffing round is also scheduled sooner than usual,
  71. by default one minute after the failure, assuming that things will go back to
  72. normal and we want to detect that as soon as possible. Said interval can be
  73. customized at `Sniffer` creation time through the `setSniffAfterFailureDelayMillis`
  74. method. Note that this last configuration parameter has no effect in case sniffing
  75. on failure is not enabled like explained above.
  76. <3> Set the `Sniffer` instance to the failure listener
  77. The Elasticsearch Nodes Info api doesn't return the protocol to use when
  78. connecting to the nodes but only their `host:port` key-pair, hence `http`
  79. is used by default. In case `https` should be used instead, the
  80. `ElasticsearchHostsSniffer` instance has to be manually created and provided
  81. as follows:
  82. ["source","java",subs="attributes,callouts,macros"]
  83. --------------------------------------------------
  84. include-tagged::{doc-tests}/SnifferDocumentation.java[sniffer-https]
  85. --------------------------------------------------
  86. In the same way it is also possible to customize the `sniffRequestTimeout`,
  87. which defaults to one second. That is the `timeout` parameter provided as a
  88. querystring parameter when calling the Nodes Info api, so that when the
  89. timeout expires on the server side, a valid response is still returned
  90. although it may contain only a subset of the nodes that are part of the
  91. cluster, the ones that have responded until then.
  92. ["source","java",subs="attributes,callouts,macros"]
  93. --------------------------------------------------
  94. include-tagged::{doc-tests}/SnifferDocumentation.java[sniff-request-timeout]
  95. --------------------------------------------------
  96. Also, a custom `HostsSniffer` implementation can be provided for advanced
  97. use-cases that may require fetching the hosts from external sources rather
  98. than from Elasticsearch:
  99. ["source","java",subs="attributes,callouts,macros"]
  100. --------------------------------------------------
  101. include-tagged::{doc-tests}/SnifferDocumentation.java[custom-hosts-sniffer]
  102. --------------------------------------------------
  103. <1> Fetch the hosts from the external source