1. 程式人生 > >SpringBoot -- 整合Elasticsearch

SpringBoot -- 整合Elasticsearch

前置工作

  • 當前伺服器為CentOS6.5+ 64bit
  • 新建 elasticsearch使用者,ES無法用root啟動

  • useradd -d /usr/elasticsearch -g elastic -m elasticsearch

獲取elasticsearch,本文用的v2.3.4 找到要獲取的版本

瞭解ES與mysql的對應關係 
  • index –> DB 
  • type –> Table 
  • Document –> row

安裝Elasticsearch

用wget命令 獲取elasticsearch後,直接解壓到對應的目錄中

修改 config下的elasticsearch.yml 
配置 cluster.namenetwork.host

cluster.name:nini
network.host 0.0.0.0 #為任意都可連線
  • 1
  • 2

啟動ES,bin目錄下

sh elasticsearch
  • 1

Springboot整合ES

新建ES module,引入 spring-boot-starter-data-elasticsearch、spring-data-elasticsearch

build.gradle

apply plugin: 'org.springframework.boot'
dependencyManagement{ imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:" + springCloudVersion mavenBom "org.springframework.boot:spring-boot-starter:"+ springBootVersion } } repositories { mavenCentral() } dependencies { compile ('org.springframework.data:spring-data-redis'
) compile ('org.springframework.boot:spring-boot-starter-data-mongodb:'+springBootVersion) compile ('org.springframework.boot:spring-boot-starter-web:'+springBootVersion) compile('org.springframework.cloud:spring-cloud-starter-eureka') compile ('mysql:mysql-connector-java:'+mysqlVersion) compile ('com.alibaba:druid:'+druidVersion) compile ('org.mybatis:mybatis-spring:'+mybatisSpringBootVersion) compile ('org.mybatis:mybatis:'+mybatisVersion) compile('org.springframework.boot:spring-boot-starter-log4j2') compile ('org.springframework.boot:spring-boot-starter-thymeleaf') compile ('net.sourceforge.nekohtml:nekohtml:'+nekoHtmlVersion) compile('org.apache.logging.log4j:log4j-1.2-api:'+ log4jAPIVersion) compile('org.springframework.boot:spring-boot-starter-jdbc') compile('org.springframework.boot:spring-boot-starter-aop') compile ('com.alibaba:fastjson:'+fastjsonVersion) compile ('redis.clients:jedis') compile ('org.springframework.boot:spring-boot-starter-data-elasticsearch:'+springBootVersion) compile ('org.springframework.data:spring-data-elasticsearch') testCompile ('org.springframework.boot:spring-boot-starter-test') testCompile group: 'junit', name: 'junit', version: '4.11' } configurations { all*.exclude module: 'spring-boot-starter-logging' all*.exclude module: 'logback-classic' all*.exclude module: 'log4j-over-slf4j' all*.exclude module: 'snappy-java' } jar { baseName = 'es-server-bootcwenao' }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

在application.yml中配置ES資訊

application.yml

spring:
    data:
        elasticsearch:
            cluster-name: nini
            cluster-nodes: 192.168.21.1:9300
            local: false
            repositories:
                enable: true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

建立 ES bean

用來查詢獲取物件,與mybatis差不多,唯獨多了@Document與@Id 註解

AccountInfo.java

/**
 * @author cwenao
 * @version $Id AccountInfo.java, v 0.1 2017-02-06 10:28 cwenao Exp $$
 */
@Document(indexName = "cwenao",type = "accountinfo", shards = 1,replicas = 0, refreshInterval = "-1")
public class AccountInfo {

    @Id
    private String id;
    @Field
    private String accountName;
    @Field
    private String nickName;

    //getter setter ...
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

建立 Repository

用於資料查詢,需要extends ElasticsearchRepository

ElasticAccountInfoRepository.java

/**
 * @author cwenao
 * @version $Id ElasticAccountInfoRepository.java, v 0.1 2017-02-06 10:26 cwenao Exp $$
 */
@Component("elasticAccountInfoRepository")
public interface ElasticAccountInfoRepository extends ElasticsearchRepository<AccountInfo,String> {
    //TODO define the search
    AccountInfo findByAccountName(String accountName);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

建立 service

ESAccountInfoService.java

/**
 * @author cwenao
 * @version $Id ESAccountInfoService.java, v 0.1 2017-02-06 10:36 cwenao Exp $$
 */
public interface ESAccountInfoService {

    AccountInfo queryAccountInfoById(String id);

    AccountInfo queryAccountInfoByName(String accountName);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

ESAccountInfoServiceImpl.java

/**
 * @author cwenao
 * @version $Id ESAccountInfoServiceImpl.java, v 0.1 2017-02-06 10:38 cwenao Exp $$
 */
@Service("esAccountInfoServiceImpl")
public class ESAccountInfoServiceImpl implements ESAccountInfoService {

    @Autowired
    private ElasticAccountInfoRepository elasticAccountInfoRepository;

    public AccountInfo queryAccountInfoById(String id) {
        return elasticAccountInfoRepository.findOne(id);
    }

    @Override
    public AccountInfo queryAccountInfoByName(String accountName) {
        return elasticAccountInfoRepository.findByAccountName(accountName);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

建立controller

ESController.java

/**
 * @author cwenao
 * @version $Id ESController.java, v 0.1 2017-02-06 10:44 cwenao Exp $$
 */
@Controller
public class ESController {

    @Autowired
    private ESAccountInfoService esAccountInfoServiceImpl;

    @RequestMapping("/esAccountInfo")
    public String queryAccountInfo(String id, ModelMap modelMap){

        AccountInfo accountInfo = esAccountInfoServiceImpl.queryAccountInfoById(id);
        modelMap.addAttribute("esAccountInfo",accountInfo);
        modelMap.addAttribute("test_elastic","Test the elasticsearch");

        return "accountInfo";
    }

    @RequestMapping("/esAccountInfoName")
    public String queryAccountInfoByAccountName(String accountName, ModelMap modelMap){

        AccountInfo accountInfo = esAccountInfoServiceImpl.queryAccountInfoByName(accountName);
        modelMap.addAttribute("esAccountInfo",accountInfo);
        modelMap.addAttribute("test_elastic","Test the elasticsearch");

        return "accountInfo";
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

在apigateway工程配置path

bootstrap.yml

zuul:
  routes:
    esserver:
      path: /esserver/**
      serviceId: ESSERVER
  • 1
  • 2
  • 3
  • 4
  • 5

測試

這裡寫圖片描述

更多的查詢方式

  • ElasticsearchRepository提供了很有用的查詢構造器
  • 比如查詢 去重distinct : findDistinctAccountInfoById(String id)
  • 比如條件組合 **AND/OR : findAccountInfoByIdAndAccountname(String id,accountname)
  • 查詢構造器會忽略 find..By 等字首

程式碼

如有疑問請加公眾號(K171),如果覺得對您有幫助請 github start 
公眾號_k171