「Elasticsearch」SpringBoot快速整合ES
阿新 • • 發佈:2020-12-07
Elastic Search 的底層是開源庫 Lucene。但是Lucene的使用門檻比較高,必須自己寫程式碼去呼叫它的介面。而Elastic Search的出現正是為了解決了這個問題,它是 Lucene 的封裝,提供了 REST API 的操作介面,我們可以開箱即用。
環境
- JDK版本:8
- SpringBoot:2.x
- ES版本:7.1.1
依賴
<!-- 整合es client,並排除低版本依賴(5.6) --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.1.1</version> <exclusions> <exclusion> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> </exclusion> <exclusion> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>7.1.1</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.1.1</version> </dependency>
配置
使用配置類配置,可以參考如下所示:
先建立一個builder,用於初始化ES client
public class EsClientBuilder { private int connectTimeoutMillis = 1000; private int socketTimeoutMillis = 30000; private int connectionRequestTimeoutMillis = 500; private int maxConnectPerRoute = 10; private int maxConnectTotal = 30; private final List<HttpHost> httpHosts; private EsClientBuilder(List<HttpHost> httpHosts) { this.httpHosts = httpHosts; } public EsClientBuilder setConnectTimeoutMillis(int connectTimeoutMillis) { this.connectTimeoutMillis = connectTimeoutMillis; return this; } public EsClientBuilder setSocketTimeoutMillis(int socketTimeoutMillis) { this.socketTimeoutMillis = socketTimeoutMillis; return this; } public EsClientBuilder setConnectionRequestTimeoutMillis(int connectionRequestTimeoutMillis) { this.connectionRequestTimeoutMillis = connectionRequestTimeoutMillis; return this; } public EsClientBuilder setMaxConnectPerRoute(int maxConnectPerRoute) { this.maxConnectPerRoute = maxConnectPerRoute; return this; } public EsClientBuilder setMaxConnectTotal(int maxConnectTotal) { this.maxConnectTotal = maxConnectTotal; return this; } public static EsClientBuilder build(List<HttpHost> httpHosts) { return new EsClientBuilder(httpHosts); } public RestHighLevelClient create() { HttpHost[] httpHostArr = httpHosts.toArray(new HttpHost[0]); RestClientBuilder builder = RestClient.builder(httpHostArr); builder.setRequestConfigCallback(requestConfigBuilder -> { requestConfigBuilder.setConnectTimeout(connectTimeoutMillis); requestConfigBuilder.setSocketTimeout(socketTimeoutMillis); requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeoutMillis); return requestConfigBuilder; }); builder.setHttpClientConfigCallback(httpClientBuilder -> { httpClientBuilder.setMaxConnTotal(maxConnectTotal); httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute); return httpClientBuilder; }); return new RestHighLevelClient(builder); } }
再交給spring容器管理:
@Configuration public class ESConfig { @Value("${elasticsearch.nodes}") private List<String> nodes; @Value("${elasticsearch.schema}") private String schema; @Value("${elasticsearch.max-connect-total}") private Integer maxConnectTotal; @Value("${elasticsearch.max-connect-per-route}") private Integer maxConnectPerRoute; @Value("${elasticsearch.connection-request-timeout-millis}") private Integer connectionRequestTimeoutMillis; @Value("${elasticsearch.socket-timeout-millis}") private Integer socketTimeoutMillis; @Value("${elasticsearch.connect-timeout-millis}") private Integer connectTimeoutMillis; @Bean public RestHighLevelClient getRestHighLevelClient() { List<HttpHost> httpHosts = new ArrayList<>(); for (String node : nodes) { try { String[] parts = StringUtils.split(node, ":"); Assert.notNull(parts,"Must defined"); Assert.state(parts.length == 2, "Must be defined as 'host:port'"); httpHosts.add(new HttpHost(parts[0], Integer.parseInt(parts[1]), schema)); } catch (RuntimeException ex) { throw new IllegalStateException( "Invalid ES nodes " + "property '" + node + "'", ex); } } return EsClientBuilder.build(httpHosts) .setConnectionRequestTimeoutMillis(connectionRequestTimeoutMillis) .setConnectTimeoutMillis(connectTimeoutMillis) .setSocketTimeoutMillis(socketTimeoutMillis) .setMaxConnectTotal(maxConnectTotal) .setMaxConnectPerRoute(maxConnectPerRoute) .create(); } }
再配置一下常用的配置:
#配置es
elasticsearch:
# 如果是cluster,application.yml的nodes設定多個ip:host逗號隔開即可。
nodes: 127.0.0.1:9200
schema: http
max-connect-total: 50
max-connect-per-route: 10
connection-request-timeout-millis: 500
socket-timeout-millis: 30000
connect-timeout-millis: 1000
使用
接下來我們測試一下配置是否生效,一個簡單的驗證送給大家,如下所示:
@Autowired
private RestHighLevelClient client;
@Test
public void test() throws IOException {
MainResponse info = client.info(RequestOptions.DEFAULT);
System.out.println(info.toString());
}
配置生效的話,就會返回Elastic Search的配置資訊:包含當前節點、叢集、版本等資訊。
SpringBoot整合ES至此結束。
本文可轉載,但需宣告原文出處。 程式設計師小明,一個很少加班的程式設計師。歡迎關注微信公眾號,獲取更多優質文章。