Skip to content
This repository was archived by the owner on Nov 18, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 9 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ Dropwizard Elasticsearch
[![Coverage Status](https://img.shields.io/coveralls/dropwizard/dropwizard-elasticsearch.svg)](https://coveralls.io/r/dropwizard/dropwizard-elasticsearch)
[![Maven Central](https://img.shields.io/maven-central/v/io.dropwizard.modules/dropwizard-elasticsearch.svg)](http://mvnrepository.com/artifact/io.dropwizard.modules/dropwizard-elasticsearch)

A set of classes for using [Elasticsearch][1] (version 2.3.0 and higher) in a [Dropwizard][2] application.
A set of classes for using [Elasticsearch][1] (version 7.1.0 and higher) in a [Dropwizard][2] application.

The package provides a [lifecycle-managed][3] client class (`ManagedEsClient`), a configuration class with the most
common options (`EsConfiguration`), and some [health checks][4] which can instantly be used in any Dropwizard application.

[1]: http://www.elasticsearch.org/
[2]: http://dropwizard.io/1.2.0/docs
[3]: http://dropwizard.io/1.2.0/docs/manual/core.html#managed-objects
[4]: http://dropwizard.io/1.2.0/docs/manual/core.html#health-checks
[2]: http://dropwizard.io/1.3.0/docs
[3]: http://dropwizard.io/1.3.0/docs/manual/core.html#managed-objects
[4]: http://dropwizard.io/1.3.0/docs/manual/core.html#health-checks


Usage
Expand Down Expand Up @@ -43,33 +43,14 @@ Configuration

The following configuration settings are supported by `EsConfiguration`:

* `nodeClient`: When `true`, `ManagedEsClient` will create a `NodeClient`, otherwise a `TransportClient`; default: `true`
* `servers`: A list of servers for usage with the created TransportClient if `nodeClient` is `false`
* `clusterName`: The name of the Elasticsearch cluster; default: "elasticsearch"
* `settings`: Any additional settings for Elasticsearch, see [Configuration](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/setup-configuration.html)
* `settingsFile`: Any additional settings file for Elasticsearch, see [Configuration](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/setup-configuration.html)

An example configuration file for creating a Node Client could like this:
An example configuration file for creating a High Level Rest Client could like this:

clusterName: MyClusterName
settings:
node.name: MyCustomNodeName

The order of precedence is: `nodeClient`/`servers`/`clusterName` > `settings` > `settingsFile`, meaning that
any setting in `settingsFile` can be overwritten with `settings` which in turn get overwritten by the specific settings
like `clusterName`.

Maven Artifacts
---------------

This project is available on Maven Central. To add it to your project simply add the following dependencies to your
`pom.xml`:

<dependency>
<groupId>io.dropwizard.modules</groupId>
<artifactId>dropwizard-elasticsearch</artifactId>
<version>1.2.0-1</version>
</dependency>
servers:
- http://127.0.0.1:9200
- http://127.0.0.1:9201
- http://127.0.0.1:9202


Support
Expand Down
13 changes: 9 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<groupId>io.dropwizard.modules</groupId>
<artifactId>dropwizard-elasticsearch</artifactId>
<version>1.2.0-2-SNAPSHOT</version>
<version>1.3.0-1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Dropwizard Elasticsearch Bundle</name>
Expand Down Expand Up @@ -69,7 +69,7 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<dropwizard.version>1.3.12</dropwizard.version>
<elasticsearch.version>2.4.6</elasticsearch.version>
<elasticsearch.version>7.1.0</elasticsearch.version>
</properties>

<dependencyManagement>
Expand All @@ -90,8 +90,13 @@
<artifactId>dropwizard-core</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client-sniffer</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.dropwizard.elasticsearch.config;

import com.fasterxml.jackson.annotation.JsonProperty;

import javax.validation.constraints.NotNull;

public class BasicAuthenticationConfiguration {
@JsonProperty
@NotNull
private String user="";

@JsonProperty
@NotNull
private String password="";

public String getUser() {
return user;
}

public String getPassword() {
return password;
}
}
Original file line number Diff line number Diff line change
@@ -1,61 +1,111 @@
package io.dropwizard.elasticsearch.config;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.net.HostAndPort;
import io.dropwizard.validation.ValidationMethod;
import org.hibernate.validator.constraints.NotEmpty;

import javax.validation.constraints.NotNull;
import org.apache.http.HttpHost;
import org.apache.http.message.BasicHeader;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.NotNull;

/**
* Configuration class for Elasticsearch related settings.
*/
public class EsConfiguration {
@JsonProperty
@NotNull
private List<HostAndPort> servers = Collections.emptyList();
private List<String> servers = Collections.emptyList();

@JsonProperty
@NotEmpty
private String clusterName = "elasticsearch";
@NotNull
private int connectTimeOut = 1000;

@JsonProperty
private boolean nodeClient = true;
@NotNull
private int socketTimeOut = 30000;

@JsonProperty
private int numberOfThreads = 0;

@JsonProperty
private String node = "";

@JsonProperty
private BasicAuthenticationConfiguration basicAuthentication = null;

@JsonProperty
private KeyStoreConfiguration keystore = null;

@JsonProperty
private SnifferConfiguration sniffer = null;

@JsonProperty
@NotNull
private Map<String, String> settings = Collections.emptyMap();

@JsonProperty
private String settingsFile = null;
private Map<String, String> headers = Collections.emptyMap();

public List<HostAndPort> getServers() {
public List<String> getServers() {
return servers;
}

public String getClusterName() {
return clusterName;
public List<HttpHost> getServersAsHttpHosts() {
ArrayList<HttpHost> httpHosts=new ArrayList<>();
getServers().forEach(hostAndPort -> {
HttpHost httpHost = HttpHost.create(hostAndPort);
if (httpHost.getPort() < 0) {
httpHost = new HttpHost(httpHost.getHostName(), 9200);
}
httpHosts.add(httpHost);
});
return httpHosts;
}

public boolean isNodeClient() {
return nodeClient;
public Map<String, String> getHeaders() {
return headers;
}

public Map<String, String> getSettings() {
return settings;
public List<BasicHeader> getHeadersAsHeaders() {
ArrayList<BasicHeader> basicHeaders = new ArrayList<>();
for (Map.Entry<String, String> entry: getHeaders().entrySet()) {
basicHeaders.add(new BasicHeader(entry.getKey(), entry.getValue()));
}
return basicHeaders;
}

public int getConnectTimeOut() {
return connectTimeOut;
}

public int getSocketTimeOut() {
return socketTimeOut;
}

public int getNumberOfThreads() {
return numberOfThreads;
}

public String getSettingsFile() {
return settingsFile;
public String getNode() {
return node;
}

@ValidationMethod
@JsonIgnore
public boolean isValidConfig() {
return nodeClient || !servers.isEmpty();
public BasicAuthenticationConfiguration getBasicAuthentication() {
return basicAuthentication;
}

public KeyStoreConfiguration getKeystore() {
return keystore;
}

public SnifferConfiguration getSniffer() {
return sniffer;
}

public Map<String, String> getSettings() {
return settings;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.dropwizard.elasticsearch.config;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.nio.file.Path;
import java.nio.file.Paths;

import javax.validation.constraints.NotNull;

public class KeyStoreConfiguration {
@JsonProperty
private String type = "jks";

@JsonProperty
@NotNull
private String keyStorePath="";

@JsonProperty
@NotNull
private String keyStorePass="";

public String getType() {
return type;
}

public Path getKeyStorePath() {
return Paths.get(keyStorePath);
}

public String getKeyStorePass() {
return keyStorePass;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.dropwizard.elasticsearch.config;

import com.fasterxml.jackson.annotation.JsonProperty;

public class SnifferConfiguration {

@JsonProperty
private Boolean sniffOnFailure = true;

@JsonProperty
private int sniffIntervalMillis = 5000;

@JsonProperty
private int sniffAfterFailureDelayMillis = 30000;

public Boolean getSniffOnFailure() {
return sniffOnFailure;
}

public int getSniffIntervalMillis() {
return sniffIntervalMillis;
}

public int getSniffAfterFailureDelayMillis() {
return sniffAfterFailureDelayMillis;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package io.dropwizard.elasticsearch.health;

import com.codahale.metrics.health.HealthCheck;

import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.cluster.health.ClusterHealthStatus;

import static com.google.common.base.Preconditions.checkNotNull;
Expand All @@ -12,7 +17,7 @@
* @see <a href="http://www.elasticsearch.org/guide/reference/api/admin-cluster-health/">Admin Cluster Health</a>
*/
public class EsClusterHealthCheck extends HealthCheck {
private final Client client;
private final RestHighLevelClient client;
private final boolean failOnYellow;

/**
Expand All @@ -21,7 +26,7 @@ public class EsClusterHealthCheck extends HealthCheck {
* @param client an Elasticsearch {@link Client} instance connected to the cluster
* @param failOnYellow whether the health check should fail if the cluster health state is yellow
*/
public EsClusterHealthCheck(Client client, boolean failOnYellow) {
public EsClusterHealthCheck(RestHighLevelClient client, boolean failOnYellow) {
this.client = checkNotNull(client);
this.failOnYellow = failOnYellow;
}
Expand All @@ -30,9 +35,9 @@ public EsClusterHealthCheck(Client client, boolean failOnYellow) {
* Construct a new Elasticsearch cluster health check which will fail if the cluster health state is
* {@link ClusterHealthStatus#RED}.
*
* @param client an Elasticsearch {@link Client} instance connected to the cluster
* @param client an Elasticsearch {@link RestHighLevelClient} instance connected to the cluster
*/
public EsClusterHealthCheck(Client client) {
public EsClusterHealthCheck(RestHighLevelClient client) {
this(client, false);
}

Expand All @@ -47,7 +52,9 @@ public EsClusterHealthCheck(Client client) {
*/
@Override
protected Result check() throws Exception {
final ClusterHealthStatus status = client.admin().cluster().prepareHealth().get().getStatus();
ClusterHealthRequest request = new ClusterHealthRequest();
ClusterHealthResponse response = client.cluster().health(request, RequestOptions.DEFAULT);
final ClusterHealthStatus status = response.getStatus();

if (status == ClusterHealthStatus.RED || (failOnYellow && status == ClusterHealthStatus.YELLOW)) {
return Result.unhealthy("Last status: %s", status.name());
Expand Down
Loading