本章內容html
ES 及 x-pack 下載安裝java
Kibana 及 x-pack 下載安裝node
Spring Boot 整合 ESweb
Spring Boot 操做 ESspring
1. ES 及 x-pack 下載安裝數據庫
spring-data-elasticsearch 之 ElasticSearch 架構初探,詳細看下我另一篇文章《深刻淺出 spring-data-elasticsearch 之 ElasticSearch 架構初探(一)》 http://www.spring4all.com/article/330apache
ES 三大要素:編程
文檔(Document) 文檔,在面向對象觀念就是一個對象。在 ES 裏面,是一個大 JSON 對象,是指定了惟一 ID 的最底層或者根對象。文檔的位置由 index、type 和 _id 惟一標識json
索引(Index) 索引,用於區分文檔成組,即分到一組的文檔集合。索引,用於存儲文檔和使文檔可被搜索。好比項目存索引 project 裏面,交易存索引 sales 等。vim
類型(Type) 類型,用於區分索引中的文檔,即在索引中對數據邏輯分區。好比索引 project 的項目數據,根據項目類型 ui 項目、插畫項目等進行區分。
和關係型數據庫 MySQL 作個類比:
Document 相似於 Record
Type 相似於 Table
Index 相似於 Database
安裝步驟以下:
1. 下載 ES 5.5.3
下載地址: https://www.elastic.co/downloads/past-releases/elasticsearch-5-5-3
2. 安裝 ES 插件 x-pack
解壓 - 而後安裝 插件 x-pack
tar -xzf elasticsearch-5.3.0.tar.gz cd elasticsearch-5.3.0/ // 安裝 X-Pack bin/elasticsearch-plugin install x-pack
3. 配置 ES 並啓動
設置 Xpack 安全驗證爲 false:
vim config/elasticsearch.yml
並添加下面配置:
xpack.security.enabled: false
並啓動 ES:
./bin/elasticsearch 或者後臺啓動 ./bin/elasticsearch -d
2. Kibana 及 x-pack 下載安裝
1. 下載 Kibana 5.5.3
下載地址: https://www.elastic.co/downloads/past-releases/kibana-5-5-3
2. 安裝 Kibana 插件 x-pack
解壓 - 而後安裝 插件 x-pack
tar -zxvf kibana-5.5.3-darwin-x86_64.tar.gz cd kibana-5.5.3-darwin-x86_64/ // 安裝 X-Pack bin/kibana-plugin install x-pack
3. 配置 Kibana 並啓動
設置 Xpack 安全驗證爲 false:
vim config/kibana.yml
並添加下面配置:
xpack.security.enabled: false
並啓動 Kibana:
./bin/kibanah 或者後臺啓動 nohup ./bin/kibana &
必須注意:先啓動 ES,再啓動 Kibana。
若是後臺啓動注意,關閉命令以下:
ps aux | grep 'elastic' kill -9 pid
啓動成功後,打開網頁訪問 127.0.0.1:5601 , 默認帳號爲:elasti,密碼爲 changeme。
3. Spring Boot 整合 ES
1. pom.xml 依賴
代碼以下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.spring4all</groupId> <artifactId>bysocket</artifactId> <version>1.0.0</version> <description>bysocket :: AI For All</description> <properties> <lombok.version>1.16.18</lombok.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.M7</version> </parent> <dependencies> <!-- web 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- 日誌 log4j2 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <!-- 容器 undertow --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency> <!-- 簡化 lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> <!-- ES --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.7.RELEASE</version> <configuration> <fork>true</fork> <addResources>true</addResources> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
Spring Data
要了解 spring-data-elasticsearch 是什麼,首先了解什麼是 Spring Data。 Spring Data 基於 Spring 爲數據訪問提供一種類似且一致性的編程模型,並保存底層數據存儲的。
Spring Data Elasticsearch
spring-data-elasticsearch 是 Spring Data 的 Community modules 之一,是 Spring Data 對 Elasticsearch 引擎的實現。 Elasticsearch 默認提供輕量級的 HTTP Restful 接口形式的訪問。相對來講,使用 HTTP Client 調用也很簡單。但 spring-data-elasticsearch 能夠更快的支持構建在 Spring 應用上,好比在 application.properties 配置 ES 節點信息和 spring-boot-starter-data-elasticsearch 依賴,直接在 Spring Boot 應用上使用。
這裏依賴的 spring-boot-starter-data-elasticsearch 版本是 2.0,對應的 spring-data-elasticsearch 版本是 5.5.3.RELEASE。 對應 ES 儘可能安裝成版本一致 5.5.3。
2. application.properties 配置 ES 地址
application.properties 配置以下:
# ES spring: data: elasticsearch: repositories: enabled: true cluster-name: elasticsearch cluster-nodes: 120.132.29.37:9300
默認 9300 是 Java 客戶端的端口。9200 是支持 Restful HTTP 的接口。 更多配置:
spring.data.elasticsearch.cluster-name Elasticsearch 集羣名。(默認值: elasticsearch)
spring.data.elasticsearch.cluster-nodes 集羣節點地址列表,用逗號分隔。若是沒有指定,就啓動一個客戶端節點。
spring.data.elasticsearch.propertie 用來配置客戶端的額外屬性。
spring.data.elasticsearch.repositories.enabled 開啓 Elasticsearch 倉庫。(默認值:true。)
3. ES 數據操做層
文章實體類代碼以下:
/** * 文章 */ @Document(indexName = "elasticsearch", type = "article") @Data public class ArticleEntity implements Serializable { // 做者信息 private String writer; // 文章信息 @Id private Long id; private String title; private String content; // 歸屬信息 private Long admin; }
City 屬性名不支持駝峯式。
indexName 配置必須是所有小寫,否則會出異常。 org.elasticsearch.indices.InvalidIndexNameException: Invalid index name [provinceIndex], must be lowercase
ES 數據操做層實現代碼以下:
@Repository public interface ArticleRepository extends ElasticsearchRepository<ArticleEntity, Long> { }
接口只要繼承 ElasticsearchRepository 接口類便可,具體使用的是該接口的方法:
<S extends T> S save(S var1); <S extends T> Iterable<S> saveAll(Iterable<S> var1); Optional<T> findById(ID var1); boolean existsById(ID var1); Iterable<T> findAll(); Iterable<T> findAllById(Iterable<ID> var1); long count(); void deleteById(ID var1); void delete(T var1); void deleteAll(Iterable<? extends T> var1); void deleteAll(); <S extends T> S index(S var1); Iterable<T> search(QueryBuilder var1); Page<T> search(QueryBuilder var1, Pageable var2); Page<T> search(SearchQuery var1); Page<T> searchSimilar(T var1, String[] var2, Pageable var3); void refresh(); Class<T> getEntityClass();
增刪改查、搜索都有了,不須要咱們具體實現,只需咱們傳入對應的參數便可。
4. 文章搜索 ES 業務邏輯實現類
實現了批量保存到 ES 的接口,代碼以下:
@Service @Primary @AllArgsConstructor @Log4j2 public class ArticleServiceImpl implements ArticleService { private final ArticleRepository articleRepository; @Override public boolean saveArticles(List<ArticleBean> articleBeanList) { List articleEntityList = transferToArticleEntityList(articleBeanList); articleRepository.saveAll(articleEntityList); return true; } ... 省略 }
4. Spring Boot 操做 ES
用 Postman 工具新增文章:
POST /api/articles HTTP/1.1 Host: 127.0.0.1:8080 Content-Type: application/json Cache-Control: no-cache Postman-Token: d6288a82-b98f-1c1e-6dad-15b0223102ab [ { "id":2, "title":"文章題目", "writer":"溫嶺", "content":"溫嶺是個沿海城市", "admin":1 }, { "id":3, "title":"文章題目文章題目文章題目", "writer":"溫1嶺", "content":"溫2嶺是個沿海城市", "admin":1 } ]
而後在 Kibana 設置 indexName 爲 "elasticsearch",並打開 Discover tab,能夠看到數據,並能夠搜索查詢,如圖:
文章來源:http://mp.weixin.qq.com/s/t82YJgMmFoBeQaWzPJmRIQ
springboot視頻教程:http://www.roncoo.com/course/list.html?courseName=spring+boot