用docker部署es及springboot整合es

閱讀目錄

  1. es是什麼?用來幹什麼?
  2. docker部署es
  3. docker部署es-head
  4. springboot整合es

一、es是什麼?用來幹什麼?

Elasticsearch也使用Java開發並使用Lucene作爲其核心來實現所有索引和搜索的功能,但是它的目的是通過簡單的RESTful API來隱藏Lucene的複雜性,從而讓全文搜索變得簡單。
不過,Elasticsearch不僅僅是Lucene和全文搜索,我們還能這樣去描述它:

  • 分佈式的實時文件存儲,每個字段都被索引並可被搜索
  • 分佈式的實時分析搜索引擎
  • 可以擴展到上百臺服務器,處理PB級結構化或非結構化數據

二、docker部署es

2.1 拉取鏡像

docker pull docker.elastic.co/elasticsearch/elasticsearch:6.3.2

注意:如果結合springboot中的spring-boot-starter-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
Spring Boot Version (x) Spring Data ESearch Version (y) ESearch Version (z)
x <= 1.3.5              y <= 1.3.4                      z <= 1.7.2*
x >= 1.4.x              2.0.0 <=y < 5.0.0**             2.0.0 <= z < 5.0.0**

否則可能會出現的錯誤有:

1、NoNodeAvailableException None of the configured nodes are available

2、nested exception is java.lang.NoClassDefFoundError: org/elasticsearch/script/ScriptEngine

2.2 運行容器

ElasticSearch的默認端口是9200,我們把宿主環境9200端口映射到Docker容器中的9200端口,就可以訪問到Docker容器中的ElasticSearch服務了,同時我們把這個容器命名爲es632

docker run -d --name es632 -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.3.2

2.3 配置跨域

2.3.1 進入容器

docker exec -it es632 /bin/bash

2.3.2 進行配置

# 顯示文件
ls
結果如下:
LICENSE.txt  README.textile  config  lib   modules
NOTICE.txt   bin             data    logs  plugins

# 進入配置文件夾
cd config

# 顯示文件
ls
結果如下:
elasticsearch.keystore  ingest-geoip  log4j2.properties  roles.yml  users_roles
elasticsearch.yml       jvm.options   role_mapping.yml   users

# 修改配置文件
vi elasticsearch.yml

# 加入跨域配置
http:
  cors:
    enabled : true
    allow-origin : "*"

2.4 重啓容器

docker restart es632

2.5 查看運行情況

http://localhost:9200/

三、docker部署es-head

ElasticSearch-Head是一個管理查看ElasticSearch相關信息的界面。

3.1 拉取鏡像

docker pull mobz/elasticsearch-head:5

3.2 運行容器

docker run -d --name es_head5 -p 9100:9100 mobz/elasticsearch-head:5

3.3 查看運行情況

http://localhost:9100/

四、springboot整合es

4.1 添加pom依賴

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

4.2 修改application.properties

#Cluster node port configuration
spring.data.elasticsearch.cluster-name=docker-cluster
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
spring.data.elasticsearch.repositories.enabled=true

4.3 創建Document

@Document(indexName = "eslog")
public class MoEsLog {
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    private String dateTime;

    public String getDateTime() {
        return dateTime;
    }

    public void setDateTime(String dateTime) {
        this.dateTime = dateTime;
    }

    @Id
    private String _id;
}

4.4 創建Repository

@Repository
public interface IEsRepository extends ElasticsearchRepository<MoEsLog, String> {
}

4.5 創建EsLogServiceImpl

@Service
public class EsLogServiceImpl {
    @Autowired
    private IEsRepository esRepository;

    public void addEsLog(String message) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        MoEsLog esLog = new MoEsLog();
        esLog.setMessage(message);
        esLog.setDateTime(simpleDateFormat.format(new Date()));
        MoEsLog save = esRepository.save(esLog);
    }
}

4.5 創建Test

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class Test {

    @Autowired
    private EsLogServiceImpl esLogService;

    @Test
    public void esLogService() {
        esLogService.addEsLog("aaa");
    }
}

保存之後,es-head控制檯效果如下:

現在,已經可以簡單使用es了。

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章