sniffer.asciidoc 6.0 KB

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