Java

SpringBoot整合ElasticSearch(五)

2019-05-06  本文已影响0人  Invi

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

<?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.invi</groupId>
    <artifactId>springboot-02-elastic</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-02-elastic</name>
    <description>Demo project for Spring Boot</description>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>


        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>5.3.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.22</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
上一篇下一篇

猜你喜欢

热点阅读