sniffer.asciidoc 5.6 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. === 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, a `Sniffer` can be associated
  36. to it. The `Sniffer` will make use of the provided `RestClient` to periodically
  37. (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]
  40. --------------------------------------------------
  41. Sniffer sniffer = Sniffer.builder(restClient).build();
  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]
  48. --------------------------------------------------
  49. sniffer.close();
  50. restClient.close();
  51. --------------------------------------------------
  52. The Elasticsearch Nodes Info api doesn't return the protocol to use when
  53. connecting to the nodes but only their `host:port` key-pair, hence `http`
  54. is used by default. In case `https` should be used instead, the
  55. `ElasticsearchHostsSniffer` object has to be manually created and provided
  56. as follows:
  57. [source,java]
  58. --------------------------------------------------
  59. HostsSniffer hostsSniffer = new ElasticsearchHostsSniffer(restClient,
  60. ElasticsearchHostsSniffer.DEFAULT_SNIFF_REQUEST_TIMEOUT,
  61. ElasticsearchHostsSniffer.Scheme.HTTPS);
  62. Sniffer sniffer = Sniffer.builder(restClient)
  63. .setHostsSniffer(hostsSniffer).build();
  64. --------------------------------------------------
  65. In the same way it is also possible to customize the `sniffRequestTimeout`,
  66. which defaults to one second. That is the `timeout` parameter provided as a
  67. querystring parameter when calling the Nodes Info api, so that when the
  68. timeout expires on the server side, a valid response is still returned
  69. although it may contain only a subset of the nodes that are part of the
  70. cluster, the ones that have responsed until then.
  71. Also, a custom `HostsSniffer` implementation can be provided for advanced
  72. use-cases that may require fetching the hosts from external sources.
  73. The `Sniffer` updates the nodes by default every 5 minutes. This interval can
  74. be customized by providing it (in milliseconds) as follows:
  75. [source,java]
  76. --------------------------------------------------
  77. Sniffer sniffer = Sniffer.builder(restClient)
  78. .setSniffIntervalMillis(60000).build();
  79. --------------------------------------------------
  80. It is also possible to enable sniffing on failure, meaning that after each
  81. failure the nodes list gets updated straightaway rather than at the following
  82. ordinary sniffing round. In this case a `SniffOnFailureListener` needs to
  83. be created at first and provided at `RestClient` creation. Also once the
  84. `Sniffer` is later created, it needs to be associated with that same
  85. `SniffOnFailureListener` instance, which will be notified at each failure
  86. and use the `Sniffer` to perform the additional sniffing round as described.
  87. [source,java]
  88. --------------------------------------------------
  89. SniffOnFailureListener sniffOnFailureListener = new SniffOnFailureListener();
  90. RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200))
  91. .setFailureListener(sniffOnFailureListener).build();
  92. Sniffer sniffer = Sniffer.builder(restClient).build();
  93. sniffOnFailureListener.setSniffer(sniffer);
  94. --------------------------------------------------
  95. When using sniffing on failure, not only do the nodes get updated after each
  96. failure, but an additional sniffing round is also scheduled sooner than usual,
  97. by default one minute after the failure, assuming that things will go back to
  98. normal and we want to detect that as soon as possible. Said interval can be
  99. customized at `Sniffer` creation time as follows:
  100. [source,java]
  101. --------------------------------------------------
  102. Sniffer sniffer = Sniffer.builder(restClient)
  103. .setSniffAfterFailureDelayMillis(30000).build();
  104. --------------------------------------------------
  105. Note that this last configuration parameter has no effect in case sniffing
  106. on failure is not enabled like explained above.