點擊上方【JAVA樂園】,選擇「置頂公衆號」,有內涵有價值的文章第一時間送達!html
首先明確一點,SpringBoot自帶的ES模板,不建議使用,建議使用Rest Client。若是業務簡單,且無特殊要求,可使用SpringBoot的模板ElasticsearchRepository來搞定。這個很是簡單,這裏不做介紹,有須要看最底下的鏈接java
ElasticsearchRepositorynginx
優勢: 簡單,SpringBoot無縫對接,配置簡單web
缺點: 基於即將廢棄的TransportClient, 不能支持複雜的業務數據庫
建立SpringBoot項目
IDEA 建立SpringBoot工程若是不會建立,能夠參考:express
https://www.jianshu.com/p/2101d176555bapache
maven 和 gradle均可以,建議使用 JDK使用1.8 由於Elasticsearch 的Java High Level REST Client 對java的版本要求是8。大家能夠去官網查證。數組
【參考:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started-maven.html】微信
添加Rest Client依賴,增長配置
maven工程:併發
<!-- Java Low Level REST Client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.3.2</version>
</dependency>
<!-- Java High Level REST Client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.3.2</version>
</dependency>
gradle 工程
dependencies {
compile 'org.elasticsearch.client:elasticsearch-rest-client:6.3.2'
compile 'org.elasticsearch.client:elasticsearch-rest-high-level-client:6.3.2'
}
編寫單例Rest Low Level Client 和Rest High Level Client的bean
想用Spring 的IOC管理ES的鏈接客戶端,可分下面幾步
配置ES節點
配置Rest Client
配置Rest High Level Client
使用IOC注入
根據我從其餘網站上查詢的資料,Rest Client是長鏈接,並且內部有默認的線程池管理,所以通常無需自定義線程池管理鏈接。若是不對請指正
基於以上結論。先把鏈接點所有配置到配置文件中.(爲了省事,直接一個數組搞定,有心的朋友能夠注入成list+對象)
配置節點
elasticsearch.ip=127.0.0.1:9200,127.0.0.2:9200,127.0.0.3:9200,127.0.0.4:9200,127.0.0.5:9200,127.0.0.6:9200
編寫Config類,配置Rest Client和Rest High Level Client
請找一個pack,建立ElasticsearchRestClient 類
/**
* this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
* Create By @author zing @date 2018/7/18 17:20
*/
@Slf4j@Configurationpublic class ElasticsearchRestClient {
/**
* 使用冒號隔開ip和端口
*/
@Value("${elasticsearch.ip}")
String[] ipAddress;
private static final int ADDRESS_LENGTH = 2;
private static final String HTTP_SCHEME = "http";
@Bean
public RestClient restClient() {
HttpHost[] hosts = Arrays.stream(ipAddress)
.map(this::makeHttpHost)
.filter(Objects::nonNull)
.toArray(HttpHost[]::new);
log.debug("hosts:{}", Arrays.toString(hosts));
return RestClient
.builder(hosts)
.build();
}
@Bean(name = "highLevelClient")
public RestHighLevelClient highLevelClient(@Autowired RestClient restClient) {
return new RestHighLevelClient(restClient);
}
private HttpHost makeHttpHost(String s) {
assert StringUtils.isNotEmpty(s);
String[] address = s.split(":");
if (address.length == ADDRESS_LENGTH) {
String ip = address[0];
int port = Integer.parseInt(address[1]);
return new HttpHost(ip, port, HTTP_SCHEME);
} else {
return null;
}
}
}
注:@Slf4j註解是lombok的日誌註解,能夠自行刪除,切換成其餘日誌方式;Stream不會的朋友能夠寫成for循環,速度大約能快些
業務使用
強烈建議從TransportClient遷移到RestClient,由於業務壓測發現TransportClient存在併發瓶頸。
在service裏而後使用以前建立的highLevelClient呢?
demo以下
@Servicepublic
class XXXServiceImpl implements XXXService {
@Autowired
RestHighLevelClient highLevelClient;
@Override
public boolean testEsRestClient(){
SearchRequest searchRequest = new SearchRequest("gdp_tops*");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termQuery("city", "北京市"));
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
searchRequest.source(sourceBuilder); try {
SearchResponse response = highLevelClient.search(searchRequest);
Arrays.stream(response.getHits().getHits())
.forEach(i -> {
System.out.println(i.getIndex());
System.out.println(i.getSource());
System.out.println(i.getType());
});
System.out.println(response.getHits().totalHits);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
若是你沒有Query DSL的基礎,建議先學習ES的基本查詢方法,若是有時間我可能會寫一些,可是網上已經有N多入門教程。
ps:
ES 沒有事務概念,ES存儲暫時能夠理解成每次請求是原子性的,若是涉及多種數據庫操做,如:存完ES再存MySQL,且要保證一致性的話,能夠考慮使用消息隊列作失敗補償,若是須要及時返回的存儲,最好不要同時操做兩種數據庫。不然則須要手動控制事務失敗後的恢復。相對麻煩不少
RestClient其餘詳細使用方法參考我前面的文章:
Elasticsearch Java Rest Client 上手指南(上)
Elasticsearch Java Rest Client 上手指南(下)
想用自帶SpringBoot光環的ElasticsearchRepository的可參考:https://juejin.im/post/5aec0b386fb9a07abb23784d
做者:MaxZing
原文連接:
https://www.jianshu.com/p/0b4f5e41405e
舒適提示
若是你喜歡本文,請分享到朋友圈,想要得到更多信息,請關注我。
本文分享自微信公衆號 - JAVA樂園(happyhuangjinjin88)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。