Spring Boot 使用docker整合ElasticSearch

ElasticSearch是咱們常常用到的搜索引擎之一,本篇博客從零開始使用docker安裝elasticsearch,elasticsearch-head而後整合Spring Boot對數據進行新增和查詢。因爲篇幅緣由,後面會分兩篇blog實戰使用分詞器以及拼音搜索功能。html

準備工做

新建Spring Boot項目

添加咱們須要使用的依賴,除了elasticsearch其餘兩個非必要,web是爲了方便調試,若是沒有使用過lombok的須要安裝IDEA/Eclipse插件。java

<!-- elasticsearch -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- web方便調試 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
複製代碼

安裝ElasticSearch

由於個人電腦是Windows10,因此這裏咱們選擇使用docker來安裝elasticsearch,操做簡單。沒有安裝docker的能夠試一下。docker win10安裝教學node

  1. 首先咱們訪問dockerhub找到elasticsearch,下載本身須要的版本,我這邊就下載最新版。
    dockerhub-elasticsearch
  2. 打開shell命令,拉取鏡像
    拉取鏡像
  3. 啓動elasticsearch,咱們按照dockerhub description啓動開發配置的elasticsearch。
// shell命令
docker network create somenetwork

docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:tag
複製代碼

How to use image
灰色有些命令看不清楚,因此換個背景顏色,啓動完成以後 docker ps看一下容器是否啓動成功。
啓動完成
瀏覽器訪問 127.0.0.1:9200,查看是否啓動成功。
127.0.0.1:9200

安裝elasticsearch-head

  1. 一樣使用docker安裝,可是訪問dockerhub的時候發現elasticsearch-head404了,執行docker pull mobz/elasticsearch-head也不能夠,可是好在docker pull mobz/elasticsearch-head:5是能夠拉取的,安裝並啓動。
// shell命令
docker pull mobz/elasticsearch-head:5

docker run -d --name es_admin -p 9100:9100 mobz/elasticsearch-head:5
複製代碼

安裝elasticsearch head

  1. 訪問http://127.0.0.1:9100/,檢查head是否啓動成功,發現head是啓動成功了,可是沒有鏈接上elasticsearch,由於咱們自定義了網絡,因此須要配置一下elasticsearch支持跨域訪問。
    elasticsearch head
  2. 配置elasticsearch.yml支持跨域。
// shell命令
// 進入容器
docker exec -it elasticsearch /bin/bash

// 修改配置文件
vi ./config/elasticsearch.yml

// 添加如下配置
http.cors.enabled: true
http.cors.allow-origin: "*"

// ctrl + c,:wq!保存配置
// 退出容器
exit

// 重啓elasticsearch
docker restart elasticsearch
複製代碼

再從新訪問一下http://127.0.0.1:9100/,鏈接成功了。 nginx

在這裏插入圖片描述

使用Spring Boot訪問ElasticSearch

添加application.yml配置

# application.yml
server:
 port: 8080

spring:
 application:
 name: elasticsearch-demo
 elasticsearch:
 rest:
 uris: http://127.0.0.1:9200
 connection-timeout: 1s
 read-timeout: 30s
複製代碼

啓動Spring Boot project

啓動完成

使用ElasticSearchRepository存取數據

項目結構

// UserEntity 
package com.example.elasticsearch.entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Document(indexName = "user")
public class UserEntity {

    @Id
    private Long userId;

    private String userName;

    private Integer age;

    private Integer sex;

}

// UserRepository 
package com.example.elasticsearch.repository;

import com.example.elasticsearch.entity.UserEntity;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface UserRepository extends ElasticsearchRepository<UserEntity, Long> {

}

// ElasticSearchService 
package com.example.elasticsearch.service;

import com.example.elasticsearch.entity.UserEntity;

import java.util.List;

public interface ElasticSearchService {

    void saveUser( UserEntity userEntity );

    void saveUser( List<UserEntity> userEntity );

    UserEntity findById(Long id);

}

// ElasticSearchServiceImpl 
package com.example.elasticsearch.service.impl;

import com.example.elasticsearch.entity.UserEntity;
import com.example.elasticsearch.repository.UserRepository;
import com.example.elasticsearch.service.ElasticSearchService;

import org.springframework.stereotype.Service;

import java.util.List;

import javax.annotation.Resource;

@Service
public class ElasticSearchServiceImpl implements ElasticSearchService {

    @Resource
    private UserRepository userRepository;

    @Override
    public void saveUser( UserEntity userEntity ) {
        userRepository.save(userEntity);
    }

    @Override
    public void saveUser( List<UserEntity> userEntity ) {
        userEntity.containsAll(userEntity);
    }

    @Override
    public UserEntity findById( Long id ) {
        return userRepository.findById(id).get();
    }
}

// Test
package com.example.elasticsearch;

import com.example.elasticsearch.entity.UserEntity;
import com.example.elasticsearch.service.ElasticSearchService;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

@SpringBootTest
class ElasticsearchApplicationTests {

    @Resource
    private ElasticSearchService elasticSearchService;

    @Test
    void save() {
        elasticSearchService.saveUser(UserEntity.builder()
                .userId(1L).userName("David").age(18).sex(1)
                .build());
    }

    @Test
    void findById() {
        UserEntity byId = elasticSearchService.findById(1L);
        System.out.println(byId);
    }

}
複製代碼

數據插入成功後,head沒法查看,由於咱們使用的head是5,可是es是7,解析格式不一樣,因此咱們須要修改vendor.js文件。web

// 將vendor.js拷貝至本地
docker cp es_admin:/usr/src/app/_site/vendor.js ./

// 修改6886和7573行 application/x-www-form-urlencoded修改成application/json;charset=UTF-8
// 將vendor.js拷貝到es_admines_admin原處
docker cp vendor.js es_admin:/usr/src/app/_site
複製代碼

操做完成後直接刷新頁面便可。 spring

es-head

總結

ElasticSearch是咱們常常用到的搜索引擎之一,本篇博客從零開始使用docker安裝elasticsearchelasticsearch-head而後整合Spring Boot對數據進行新增和查詢。後面會分兩篇blog實戰使用分詞器以及拼音搜索功能。使用docker能夠爲咱們很大的便利,docker對容器和鏡像的管理使用起來很是簡單,後面我還會使用docker來進行MongoDB實戰,以及Linux的學習,你們能夠到docker菜鳥教程學習一下,玩一玩兒就會了。docker

相關文章
相關標籤/搜索