前言:現在公司有一個項目要用到檢索功能,檢索上面現在最常用的是Solr/ES,最后經過對比選擇了ElasticSearch開源組件包,因為這個是公司的一個產品項目,技術版本當然要用最新的啦,最后完全確定的技術是SpringBoot2.2.2+es7.5.1.好了廢話不多說;上硬菜.
材料:
1: SpringBoot 2.2.2快速腳手架
2: ElasticSearch7.5.1 for Linux
1 安裝ElasticSearch
安裝就不說了,安裝文檔一大堆,網上百度去吧。
2 項目的pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> <exclusions> <exclusion> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> </exclusion> <exclusion> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> </exclusion> <exclusion> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.5.1</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.5.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.21</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> <scope>runtime</scope> </dependency>
3: 集成開始
ElasticSearch的發展歷史,簡單的說就是漸漸的淘汰了以前類似於redis-client那樣類似的客戶端API模式,現在基本上都是基於Restful規范的APIS,根據官方文檔說明的推薦就是這樣的Http請求方式,它分為兩個版本的rest-client,第一個是高級版本、第二個是普通版本,對於我們程序員來說,毋庸置疑高級版本啦。
3.1 集成第一篇
集成之前確保ElasticSearch的服務是啟動的,不然會發生鏈接超時保存.
第一種方式:
使用工具類的方式集成:
新建一個ElasticServerUtils類,代碼如下:
@Component public class ElasticServiceUtils { /** * <li>logger :SLF4J日志 </li> */ private final static Logger logger = LoggerFactory.getLogger(ElasticServiceUtils.class); private RestHighLevelClient restHighLevelClient; /** * <li>Description: 在Servlet容器初始化前執行 </li> */ @PostConstruct private void init() { try { if (restHighLevelClient != null) { restHighLevelClient.close(); } //節點1和2 HttpHost node1 = new HttpHost("192.168.10.40", 9200, "http"); HttpHost node2 = new HttpHost("192.168.10.95", 9200, "http"); RestClientBuilder builder = RestClient.builder(node1,node2); restHighLevelClient = new RestHighLevelClient(builder); } catch (IOException e) { e.printStackTrace(); logger.error(e.getMessage()); } } //省略創建索引更新索引等代碼,官網有具體的例子. //官網地址: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-index.html }
第二種方式:
這個方式就是和Spring框架深度集成,類似將RestHighLevelClient類的生命周期交給Spring區管理, 優點是使用方便直接使用@Autowired注解就可以獲取到對象.缺點是相對的復雜.
第一步: 需要編寫Bean的托管和創建配置.
代碼:
/** * <li>hosts :配置的值 </li> */ @Value("${elasticsearch.hosts}") private String[] hosts; /** * <li>restHighLevelClient :restHighLevel客戶端 </li> */ private RestHighLevelClient restHighLevelClient; /** * 返回實例 * @return RestHighLevelClient * @throws Exception 異常信息 */ @Override public RestHighLevelClient getObject() throws Exception { return this.restHighLevelClient; } /** * 反射 * * @return RestHighLevelClient.class */ @Override public Class<?> getObjectType() { return RestHighLevelClient.class; } /** * 客戶端是否單例 * @return true */ @Override public boolean isSingleton() { return true; } /** * 客戶端實例的銷毀 * @throws Exception 異常信息 */ @Override public void destroy() throws Exception { if (restHighLevelClient != null) { restHighLevelClient.close(); } } /** * 注入參數 * @throws Exception 異常信息 */ @Override public void afterPropertiesSet() throws Exception { restHighLevelClient = buildClient(); } /** * <li>Description: 自定義的構造方法 </li> * * @return RestHighLevelClient */ private RestHighLevelClient buildClient() { try { //這里的builder方法有兩個方式,第一個是傳入Node(包含了多個節點,需要密碼這些,我們沒有配置,就暫時不需要),第二個就是傳入HttpHost restHighLevelClient = new RestHighLevelClient(RestClient.builder(HttpHost.create(hosts[0]), HttpHost.create(hosts[1]))); } catch (Exception e) { logger.error(e.getMessage()); } return restHighLevelClient; }
配置代碼:
elasticsearch: hosts: 192.168.10.40:9200,192.168.10.95:9200
這樣就算是集成完了.