SpringBoot整合ElasticSearch(五)

spring

spring-data-elasticsearch 与安装的elasticsearch 版本不合适会引起很多问题。

编写案例的时候也很遇到了。

下面整理下:

https://docs.spring.io/spring-data/elasticsearch/docs/3.0.6.RELEASE/reference/html/

https://github.com/spring-projects/spring-data-elasticsearch

官方给出的对应版本信息如下:

spring data elasticsearch elasticsearch
3.2.x 6.5.0
3.1.x 6.2.2
3.0.x 5.5.0
2.1.x 2.4.0
2.0.x 2.2.0
1.3.x 1.5.2

1.安装与spring-data-elasticsearch版本匹配的elasticsearch:

  1. 确定自己 spring-data-elasticsearch 依赖的elasticsearch版本

    External liberies中找到自己依赖的elasticsearch版本

    • Maven: org.apache.tomcat:tomcat-annotations-api:8.5.29

    • Maven: org.assertj:assertj-core:2.6.0

    • Maven: org.elasticsearch:elasticsearch:2.4.6

    • Maven: org.elasticsearch:securesm:1.0

    • Maven: org.hamcrest:hamcrest-core:1.3

    • Maven: org.hamcrest:hamcrest-library:1.3

    • Maven: org.hdrhistogram:HdrHistogram:2.1.6

      看到自己使用的2.4.6版本的elasticsearch

  2. 安装对应版本的镜像:docker pull registry.docker-cn.com/library/elasticsearch:2.4.6

    下载镜像

     ❯ docker pull registry.docker-cn.com/library/elasticsearch:2.4.6
    2.4.6: Pulling from library/elasticsearch
    05d1a5232b46: Already exists
    5cee356eda6b: Already exists
    89d3385f0fd3: Already exists
    65dd87f6620b: Already exists
    78a183a01190: Already exists
    1a4499c85f97: Already exists
    2c9d39b4bfc1: Already exists
    1b1cec2222c9: Already exists
    59ff4ce9df68: Already exists
    1976bc3ee432: Already exists
    a27899b7a5b5: Pull complete
    b0fc7d2c927a: Pull complete
    6d94b96bbcd0: Pull complete
    6f5bf40725fd: Pull complete
    2bf2a528ae9a: Pull complete
    Digest: sha256:41ed3a1a16b63de740767944d5405843db00e55058626c22838f23b413aa4a39
    Status: Downloaded newer image for registry.docker-cn.com/library/elasticsearch:2.4.6
    

    查看镜像id

    ❯ docker  images
    REPOSITORY                                     TAG                 IMAGE ID            CREATED             SIZE
    registry.docker-cn.com/library/elasticsearch   latest              5acf0e8da90b        5 months ago        486MB
    registry.docker-cn.com/library/elasticsearch   2.4.6               5e9d896dc62c        6 months ago        479MB
    registry.docker-cn.com/library/elasticsearch   5.5.0               519c56205eb0        19 months ago       315MB
    

    查看已占用端口

     ❯ docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                                            NAMES
    bb8353eecba5        5e9d896dc62c        "/docker-entrypoint.…"   About a minute ago   Up About a minute   0.0.0.0:9201->9200/tcp, 0.0.0.0:9301->9300/tcp   ES02
    4a89939026d2        519c56205eb0        "/docker-entrypoint.…"   2 hours ago          Up About an hour    0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp   ES01
    

    安装镜像 将本地9201端口映射到 docker 9200; 将本地9301端口映射到 docker 9300

    ❯ docker  run  -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9201:9200 -p 9301:9300 --name ES02  5e9d896dc62c
    bb8353eecba58935e20f7bcf9c8111bc6e2a4af85f02604179ad1ff4a0adbc67
    

    检查安装情况

    访问:http://localhost:9201/

    {
      "name" : "Odin",
      "cluster_name" : "elasticsearch",
      "cluster_uuid" : "QrpLxdn8Swmy637ffUfKIg",
      "version" : {
        "number" : "2.4.6",
        "build_hash" : "5376dca9f70f3abef96a77f4bb22720ace8240fd",
        "build_timestamp" : "2017-07-18T12:17:44Z",
        "build_snapshot" : false,
        "lucene_version" : "5.5.4"
      },
      "tagline" : "You Know, for Search"
    }
    

    "number" : "2.4.6",安装成功!版本已经更换为2.4.6版。

  1. 修改项目中的配置信息

    将使用http协议的jest 端口修改为:9201

    spring.elasticsearch.jest.uris=http://localhost:9201
    

    将spring.data.elasticsearch模块的nodes端口修改为:9301

    spring.data.elasticsearch.cluster-name=elasticsearch
    spring.data.elasticsearch.cluster-nodes=localhost:9301
    

    重启项目,控制台打印:

    adding transport node : localhost:9301

    Started Springboot02ElasticApplication in 4.68 seconds (JVM running for 5.678)

    无异常,启动成功!

spring data elasticsearch. Version: 3.0.10.RELEASE

整个pom.xml



    4.0.0

    com.invi
    springboot-02-elastic
    0.0.1-SNAPSHOT
    springboot-02-elastic
    Demo project for Spring Boot


    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.12.RELEASE
         
    


    
        UTF-8
        UTF-8
        1.8
    


    


        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        


        
            io.searchbox
            jest
            5.3.3
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        


        
            org.projectlombok
            lombok
            1.16.22
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



你可能感兴趣的:(SpringBoot整合ElasticSearch(五))