背景
最近業(yè)務(wù)上要做一個(gè)頻繁查詢通話次數(shù)的結(jié)果,但是通話記錄表特別大,有幾千萬(wàn)數(shù)據(jù)求摇,在mysql上面,我們都知道m(xù)ysql在這樣大的數(shù)據(jù)下性能出現(xiàn)了問(wèn)題殊者,而且mysql面向的業(yè)務(wù)一般都是事務(wù)型的与境,為了解決這個(gè)問(wèn)題,我們最終選擇了elasticsearch猖吴,es是基于apache lucene 去實(shí)現(xiàn)的摔刁,lucene是一個(gè)流行開(kāi)源的檢索服務(wù),性能在開(kāi)源項(xiàng)目中算是數(shù)一數(shù)二的了海蔽,關(guān)于對(duì)apache lucene的分析以后會(huì)陸續(xù)出文章共屈,感興趣的小伙伴可以交流交流。
本項(xiàng)目是基于spring data 整合 elasticsearch党窜,這個(gè)過(guò)程中遇到很多坑拗引,在引入es的依賴的時(shí)候,會(huì)出現(xiàn)問(wèn)題幌衣,最后發(fā)現(xiàn)是版本的問(wèn)題矾削,然后把springboot web 版本升級(jí)到最高就得以解決壤玫,注意下面項(xiàng)目依賴
ES 對(duì)于版本的兼容性要求很高。大版本之間是無(wú)法兼容的哼凯。具體可以見(jiàn)
- 項(xiàng)目結(jié)構(gòu)
- 項(xiàng)目依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.0.M3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.0.M3</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.0.0.RC2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.0.0.M3</version>
<scope>test</scope>
</dependency>
</dependencies>
- es 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd">
<elasticsearch:transport-client id="client" cluster-nodes="${elasticsearch.cluster-nodes}" cluster-name="${elasticsearch.cluster-name}" />
<bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client" />
</bean >
<elasticsearch:repositories base-package="com.example.demo.repository.UserRepository"
elasticsearch-template-ref="elasticsearchTemplate" />
</beans>
- repository配置
public interface UserRepository extends ElasticsearchRepository<User,String> {
User findByName(String name);
}
完整項(xiàng)目github鏈接:
https://github.com/myyan/ElasticsearchDemo