Skip to content

Commit 691a8c5

Browse files
DATAES-488 - Add reactive Elasticsearch client support.
Initial implementation of a ReactiveElasticsearchClient using WebClient to connect to cluster nodes. ReactiveElasticsearchClient client = ElasticsearchClients.createClient() .connectedTo("http://localhost:9200/", "http://localhost:9201/") .reactive(); A HostProvider selects active nodes and routes requests. client.index(request -> request.index("spring-data") .type("elasticsearch") .id(randomUUID().toString()) .source(singletonMap("feature", "reactive-client")) .setRefreshPolicy(IMMEDIATE); ); This implementation provides the first building block for reactive Template and Repository support to be added subsequently. Along the lines we upgraded to Elasticsearch 6.5. Original Pull Request: spring-projects#226
1 parent 25b02f2 commit 691a8c5

File tree

59 files changed

+5566
-35
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+5566
-35
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
.DS_Store
2+
*.graphml
3+
.springBeans
4+
15
atlassian-ide-plugin.xml
26

37
## Ignore svn files

pom.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<properties>
2020
<commonscollections>3.2.1</commonscollections>
2121
<commonslang>2.6</commonslang>
22-
<elasticsearch>6.3.0</elasticsearch>
22+
<elasticsearch>6.5.0</elasticsearch>
2323
<log4j>2.9.1</log4j>
2424
<springdata.commons>2.2.0.BUILD-SNAPSHOT</springdata.commons>
2525
<java-module-name>spring.data.elasticsearch</java-module-name>
@@ -51,6 +51,24 @@
5151
<version>${springdata.commons}</version>
5252
</dependency>
5353

54+
<dependency>
55+
<groupId>org.springframework</groupId>
56+
<artifactId>spring-webflux</artifactId>
57+
<version>5.1.0.RELEASE</version>
58+
</dependency>
59+
60+
<dependency>
61+
<groupId>io.projectreactor.netty</groupId>
62+
<artifactId>reactor-netty</artifactId>
63+
<version>0.8.0.RELEASE</version>
64+
</dependency>
65+
66+
<dependency>
67+
<groupId>io.projectreactor</groupId>
68+
<artifactId>reactor-test</artifactId>
69+
<version>3.2.0.RELEASE</version>
70+
</dependency>
71+
5472
<!-- APACHE -->
5573
<dependency>
5674
<groupId>commons-lang</groupId>
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.elasticsearch.client;
18+
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.List;
22+
import java.util.stream.Collectors;
23+
24+
import org.apache.http.Header;
25+
import org.apache.http.HttpHost;
26+
import org.apache.http.message.BasicHeader;
27+
import org.elasticsearch.client.RestClient;
28+
import org.elasticsearch.client.RestClientBuilder;
29+
import org.elasticsearch.client.RestHighLevelClient;
30+
import org.springframework.data.elasticsearch.client.reactive.DefaultReactiveElasticsearchClient;
31+
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;
32+
import org.springframework.http.HttpHeaders;
33+
import org.springframework.util.Assert;
34+
35+
/**
36+
* Utility class for common access to Elasticsearch clients. {@link ElasticsearchClients} consolidates set up routines
37+
* for the various drivers into a single place.
38+
*
39+
* @author Christoph Strobl
40+
* @since 4.0
41+
*/
42+
public final class ElasticsearchClients {
43+
44+
private ElasticsearchClients() {}
45+
46+
/**
47+
* Start here to create a new client tailored to your needs.
48+
*
49+
* @return new instance of {@link ClientBuilderWithRequiredHost}.
50+
*/
51+
public static ClientBuilderWithRequiredHost createClient() {
52+
return new ElasticsearchClientBuilderImpl();
53+
}
54+
55+
/**
56+
* @author Christoph Strobl
57+
*/
58+
public interface ElasticsearchClientBuilder {
59+
60+
/**
61+
* Apply the configuration to create a {@link ReactiveElasticsearchClient}.
62+
*
63+
* @return new instance of {@link ReactiveElasticsearchClient}.
64+
*/
65+
ReactiveElasticsearchClient reactive();
66+
67+
/**
68+
* Apply the configuration to create a {@link RestHighLevelClient}.
69+
*
70+
* @return new instance of {@link RestHighLevelClient}.
71+
*/
72+
RestHighLevelClient rest();
73+
74+
/**
75+
* Apply the configuration to create a {@link RestClient}.
76+
*
77+
* @return new instance of {@link RestClient}.
78+
*/
79+
default RestClient lowLevelRest() {
80+
return rest().getLowLevelClient();
81+
}
82+
}
83+
84+
/**
85+
* @author Christoph Strobl
86+
*/
87+
public interface ClientBuilderWithRequiredHost {
88+
89+
/**
90+
* @param host the {@literal host} and {@literal port} formatted as String {@literal host:port}. You may leave out
91+
* {@literal http / https} and use {@link MaybeSecureClientBuilder#viaSsl() viaSsl}.
92+
* @return the {@link MaybeSecureClientBuilder}.
93+
*/
94+
default MaybeSecureClientBuilder connectedTo(String host) {
95+
return connectedTo(new String[] { host });
96+
}
97+
98+
/**
99+
* @param hosts the list of {@literal host} and {@literal port} combinations formatted as String
100+
* {@literal host:port}. You may leave out {@literal http / https} and use
101+
* {@link MaybeSecureClientBuilder#viaSsl() viaSsl}.
102+
* @return the {@link MaybeSecureClientBuilder}.
103+
*/
104+
MaybeSecureClientBuilder connectedTo(String... hosts);
105+
106+
/**
107+
* Obviously for testing.
108+
*
109+
* @return the {@link MaybeSecureClientBuilder}.
110+
*/
111+
default MaybeSecureClientBuilder connectedToLocalhost() {
112+
return connectedTo("localhost:9200");
113+
}
114+
}
115+
116+
/**
117+
* @author Christoph Strobl
118+
*/
119+
public interface MaybeSecureClientBuilder extends ClientBuilderWithOptionalDefaultHeaders {
120+
121+
/**
122+
* Connect via {@literal https} <br />
123+
* <strong>NOTE</strong> You need to leave out the protocol in
124+
* {@link ClientBuilderWithRequiredHost#connectedTo(String)}.
125+
*
126+
* @return the {@link ClientBuilderWithOptionalDefaultHeaders}.
127+
*/
128+
ClientBuilderWithOptionalDefaultHeaders viaSsl();
129+
}
130+
131+
/**
132+
* @author Christoph Strobl
133+
*/
134+
public interface ClientBuilderWithOptionalDefaultHeaders extends ElasticsearchClientBuilder {
135+
136+
/**
137+
* @param defaultHeaders
138+
* @return the {@link ElasticsearchClientBuilder}
139+
*/
140+
ElasticsearchClientBuilder withDefaultHeaders(HttpHeaders defaultHeaders);
141+
}
142+
143+
private static class ElasticsearchClientBuilderImpl
144+
implements ElasticsearchClientBuilder, ClientBuilderWithRequiredHost, MaybeSecureClientBuilder {
145+
146+
private List<String> hosts = new ArrayList<>();
147+
private HttpHeaders headers = HttpHeaders.EMPTY;
148+
private String protocoll = "http";
149+
150+
@Override
151+
public ReactiveElasticsearchClient reactive() {
152+
return DefaultReactiveElasticsearchClient.create(headers, formattedHosts().toArray(new String[0]));
153+
}
154+
155+
@Override
156+
public RestHighLevelClient rest() {
157+
158+
HttpHost[] httpHosts = formattedHosts().stream().map(HttpHost::create).toArray(HttpHost[]::new);
159+
RestClientBuilder builder = RestClient.builder(httpHosts);
160+
161+
if (!headers.isEmpty()) {
162+
163+
Header[] httpHeaders = headers.toSingleValueMap().entrySet().stream()
164+
.map(it -> new BasicHeader(it.getKey(), it.getValue())).toArray(Header[]::new);
165+
builder = builder.setDefaultHeaders(httpHeaders);
166+
}
167+
168+
return new RestHighLevelClient(builder);
169+
}
170+
171+
@Override
172+
public MaybeSecureClientBuilder connectedTo(String... hosts) {
173+
174+
Assert.notEmpty(hosts, "At least one host is required.");
175+
this.hosts.addAll(Arrays.asList(hosts));
176+
return this;
177+
}
178+
179+
@Override
180+
public ClientBuilderWithOptionalDefaultHeaders withDefaultHeaders(HttpHeaders defaultHeaders) {
181+
182+
Assert.notNull(defaultHeaders, "DefaultHeaders must not be null!");
183+
this.headers = defaultHeaders;
184+
return this;
185+
}
186+
187+
List<String> formattedHosts() {
188+
return hosts.stream().map(it -> it.startsWith("http") ? it : protocoll + "://" + it).collect(Collectors.toList());
189+
}
190+
191+
@Override
192+
public ClientBuilderWithOptionalDefaultHeaders viaSsl() {
193+
this.protocoll = "https";
194+
return this;
195+
}
196+
}
197+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.elasticsearch.client;
18+
19+
import java.time.Instant;
20+
21+
/**
22+
* Value Object containing information about Elasticsearch cluster nodes.
23+
*
24+
* @author Christoph Strobl
25+
* @since 4.0
26+
*/
27+
public class ElasticsearchHost {
28+
29+
private final String host;
30+
private final State state;
31+
private final Instant timestamp;
32+
33+
public ElasticsearchHost(String host, State state) {
34+
35+
this.host = host;
36+
this.state = state;
37+
this.timestamp = Instant.now();
38+
}
39+
40+
/**
41+
* @param host must not be {@literal null}.
42+
* @return new instance of {@link ElasticsearchHost}.
43+
*/
44+
public static ElasticsearchHost online(String host) {
45+
return new ElasticsearchHost(host, State.ONLINE);
46+
}
47+
48+
/**
49+
* @param host must not be {@literal null}.
50+
* @return new instance of {@link ElasticsearchHost}.
51+
*/
52+
public static ElasticsearchHost offline(String host) {
53+
return new ElasticsearchHost(host, State.OFFLINE);
54+
}
55+
56+
/**
57+
* @return {@literal true} if the last known {@link State} was {@link State#ONLINE}
58+
*/
59+
public boolean isOnline() {
60+
return State.ONLINE.equals(state);
61+
}
62+
63+
/**
64+
* @return never {@literal null}.
65+
*/
66+
public String getHost() {
67+
return host;
68+
}
69+
70+
/**
71+
* @return the last known {@link State}.
72+
*/
73+
public State getState() {
74+
return state;
75+
}
76+
77+
/**
78+
* @return the {@link Instant} the information was captured.
79+
*/
80+
public Instant getTimestamp() {
81+
return timestamp;
82+
}
83+
84+
@Override
85+
public String toString() {
86+
return "ElasticsearchHost(" + host + ", " + state.name() + ")";
87+
}
88+
89+
public enum State {
90+
ONLINE, OFFLINE, UNKNOWN
91+
}
92+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.elasticsearch.client;
18+
19+
import java.util.Set;
20+
21+
/**
22+
* {@link RuntimeException} to be emitted / thrown when the cluster is down (aka none of the known nodes is reachable).
23+
*
24+
* @author Christoph Strobl
25+
* @since 4.0
26+
*/
27+
public class NoReachableHostException extends RuntimeException {
28+
29+
public NoReachableHostException(Set<ElasticsearchHost> hosts) {
30+
super(createMessage(hosts));
31+
}
32+
33+
public NoReachableHostException(Set<ElasticsearchHost> hosts, Throwable cause) {
34+
super(createMessage(hosts), cause);
35+
}
36+
37+
private static String createMessage(Set<ElasticsearchHost> hosts) {
38+
39+
if (hosts.size() == 1) {
40+
return String.format("Host '%s' not reachable. Cluster state is offline.", hosts.iterator().next().getHost());
41+
}
42+
43+
return String.format("No active host found in cluster. (%s) of (%s) nodes offline.", hosts.size(), hosts.size());
44+
}
45+
}

0 commit comments

Comments
 (0)