SpringBoot集成Elasticsearch

1. 准备工作

  需要提前安装好Elasticsearch,访问地址:http://127.0.0.1:9200/ 得到以下结果,得到cluster_name,下面配置使用。

{
  "name" : "O8GslS3",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "pviTqfXtR3GtnxF-Po-_aA",
  "version" : {
    "number" : "6.5.0",
    ......
  },
  "tagline" : "You Know, for Search"
}

2. 使用Maven创建SpringBoot工程

  配置Maven的pom.xml文件

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

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

  注意:spring-boot-starter-data-elasticsearch包,引用的是spring-data-elasticsearch包,而spring-data-elasticsearch包的版本与elasticsearch服务版本是有兼容性问题的。

  目前并不支持elasticsearch7.x,参考:https://github.com/spring-projects/spring-data-elasticsearch

  配置application.yml文件

spring:
  data:
    elasticsearch:
      cluster-name: docker-cluster
      cluster-nodes: 127.0.0.1:9300
      repositories:
        enabled: true

3. 代码

  实体类。使用@Document注解,参数indexName是索引名称,type是type名称。

// 声明索引名称,type名称@Document(indexName = "houseindex", type = "house")
public class HouseIndexTemplate {

    @Id
    private Long id;
    private String name;   ......
}

  访问接口。使用@Repository注解,并继承ElasticsearchRepository接口,就可以直接访问的。

  有两个参数:1.返回的对象,2.ID参数数据类型

@Repository
public interface HouseRepository extends ElasticsearchRepository<HouseIndexTemplate, Long> {

}

  测试用例

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class UserServiceTest {

    @Autowired
    private HouseRepository houseRepository;

    @Test
    public void selectUser(){        HouseIndexTemplate template = new HouseIndexTemplate();        template.setId(1);        template.setName("Tom");
        houseRepository.save(template);
    }

}

4. 异常解释

  问题1: NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{IVH9QII0QrOU9GkXdsJPiA}{127.0.0.1}{127.0.0.1:9300}]]

  原因:这是说配置的节点不可用,原因答题有3种可能:(1)IP地址或端口填写有误;(2)cluster_name填写有误;(3)Elasticsearch服务已关闭

原文地址:https://www.cnblogs.com/huanshilang/p/12622356.html

时间: 2024-08-30 18:29:55

SpringBoot集成Elasticsearch的相关文章

springboot 集成elasticsearch

In this article, we will discuss about “How to create a Spring Boot + Spring Data + Elasticsearch Example”. Tools used in this article : Spring Boot 1.5.1.RELEASE Spring Boot Starter Data Elasticsearch 1.5.1.RELEASE Spring Data Elasticsearch 2.10.REL

springboot集成elk 一: springboot + Elasticsearch

1.ELK介绍 1> Elasticsearch是实时全文搜索和分析引擎, 提供搜集.分析.存储数据三大功能: 是一套开放REST和JAVA API等结构提供高效搜索功能,可扩展的分布式系统. 2> Logstash是一个用来搜集.分析.过滤日志的工具. 它支持几乎任何类型的日志,包括系统日志.错误日志和自定义应用程序日志. 它可以从许多来源接收日志,这些来源包括 syslog.消息传递(例如 RabbitMQ)和JMX,它能够以多种方式输出数据,包括电子邮件.websockets和Elast

Spring Boot集成ElasticSearch实现简单的增删查改接口

SpringBoot集成ElasticSearch pom.xml文件中,依赖的各jar包版本如下: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> &l

使用Logstash同步数据至Elasticsearch,Spring Boot中集成Elasticsearch实现搜索

安装logstash.同步数据至ElasticSearch 为什么使用logstash来同步,CSDN上有一篇文章简要的分析了以下几种同步工具的优缺点:https://blog.csdn.net/laoyang360/article/details/51694519. 下面开始实践: 1. 下载Logstash安装包,需要注意版本与elasticsearch保持一致,windows系统下直接解压即可. 2.添加同步mysql数据库的配置,并将mysql连接驱动jar包放在指定的配置目录 注: 目

SpringBoot集成Zipkin实现分布式全链路监控

目录 Zipkin 简介 Springboot 集成 Zipkin 安装启动 zipkin 版本说明 项目结构 工程端口分配 引入 Maven 依赖 配置文件.收集器的设置 编写 Controller 发送请求进行测试 Springboot 启动类 运行分析 核心概念 Zipkin 简介 Zipkin is a distributed tracing system. It helps gather timing data needed to troubleshoot latency proble

SpringBoot集成MyBatis的分页插件PageHelper

俗话说:好??不吃回头草,但是在这里我建议不管你是好马还是不好马,都来吃吃,带你复习一下分页插件PageHelper. 昨天给各位总结了本人学习springboot整合mybatis第一阶段的一些学习心得和源码,主要就算是敲了一下SpringBoot的门儿,希望能给各位的入门带给一点儿捷径,今天给各位温习一下MyBatis的分页插件PageHelper和SpringBoot的集成,它的使用也非常简单,开发更为高效.因为PageHelper插件是属于MyBatis框架的,所以相信很多哥们儿都已经用

springboot集成swagger2构建RESTful API文档

在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可以在访问接口上,直接添加注释 先介绍一下开发环境: jdk版本是1.8 springboot的版本是1.4.1 开发工具为 intellij idea 我们先引入swagger2的jar包,pom文件引入依赖如下: <dependency> <groupId>io.springfox&

spring-boot集成Springfox-Swagger2

import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documenta

【spring-boot】spring-boot集成ehcache实现缓存机制

EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. ehcache提供了多种缓存策略,主要分为内存和磁盘两级,所以无需担心容量问题. spring-boot是一个快速的集成框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. 由于spring-boot无需任何样板化的配置文件,所以spring-boot集成一些其他框架时会有略微的