分布式系列教程(30) -SpringBoot整合ElasticSearch

简介: 分布式系列教程(30) -SpringBoot整合ElasticSearch

1. 引言

代码已提交至Github,有兴趣的同学可以下载来看看:https://github.com/ylw-github/SpringBoot-ElasticSearch-Demo

2. SpringBoot整合ElasticSearch

1.新建Maven项目Spring-ElasticSearch-Demo

2.添加maven依赖:

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.0.RELEASE</version>
  <relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  </dependency>
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
  </dependency>
</dependencies>

3.application.yml

spring:
  data:
    elasticsearch:
    ####集群名称
     cluster-name: myes
    ####地址 
     cluster-nodes: 192.168.162.131:9300

4.实体类层

@Document(indexName = "user_dao", type = "user")
public class UserEntity {
  private String id;
  private String name;
  private int sex;
  private int age;
  //getter/setter......
}

5.Dao类层

public interface UserReposiory extends CrudRepository<UserEntity, String> {
}

6.控制器层:

@RestController
public class EsController {
  @Autowired
  private UserReposiory userReposiory;
  @RequestMapping("/addUser")
  public UserEntity addUser(@RequestBody UserEntity user) {
    return userReposiory.save(user);
  }
  @RequestMapping("/findUser")
  public Optional<UserEntity> findUser(String id) {
    return userReposiory.findById(id);
  }
}

7.启动项目:

@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = "com.ylw.springboot.es.repository")
public class AppEs {
  public static void main(String[] args) {
    SpringApplication.run(AppEs.class, args);
  }
}

如果报以下异常:

None of the configured nodes are available:

说明没有配置cluster节点名,那么进入ElasticSearch配置文件目录配置:

[ylw@localhost root]$ cd /usr/local/elasticsearch-6.4.3/config/
[ylw@localhost config]$ vi elasticsearch.yml

配置内容:

cluster.name: myes

8.PostMan请求添加索引:http://127.0.0.1:8080/addUser

9.PostMan请求查询:http://127.0.0.1:8080/findUser?id=1,查询成功。

3. 9300与9200的区别

  • 「9300端口」: ES节点之间通讯使用。
  • 「9200端口」: ES节点 和 外部 通讯使用。
  • 「9300端口」:是TCP协议端口号,ES集群之间通讯端口号。
  • 「9200端口」:暴露ES RESTful接口端口号。

举个例子:调用RESTful创建文档(/索引/类型/id

1.创建文档,发送请求POST请求:http://192.168.162.131:9200/user_dao1/user/1

{
  "name":"ylw",
   "age":18,
   "sex":0
}

2.查询文档,发送请求GET请求:http://192.168.162.131:9200/user_dao1/user/1

相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。 &nbsp;
目录
相关文章
|
9月前
|
存储 Linux iOS开发
Elasticsearch Enterprise 9.1.5 发布 - 分布式搜索和分析引擎
Elasticsearch Enterprise 9.1.5 (macOS, Linux, Windows) - 分布式搜索和分析引擎
590 0
|
10月前
|
JSON 监控 Java
Elasticsearch 分布式搜索与分析引擎技术详解与实践指南
本文档全面介绍 Elasticsearch 分布式搜索与分析引擎的核心概念、架构设计和实践应用。作为基于 Lucene 的分布式搜索引擎,Elasticsearch 提供了近实时的搜索能力、强大的数据分析功能和可扩展的分布式架构。本文将深入探讨其索引机制、查询 DSL、集群管理、性能优化以及与各种应用场景的集成,帮助开发者构建高性能的搜索和分析系统。
624 0
|
10月前
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
1763 5
存储 JSON Java
960 0
|
存储 安全 Linux
Elasticsearch Enterprise 9.0 发布 - 分布式搜索和分析引擎
Elasticsearch Enterprise 9.0 (macOS, Linux, Windows) - 分布式搜索和分析引擎
538 0
|
存储 Linux iOS开发
Elasticsearch Enterprise 8.18 发布 - 分布式搜索和分析引擎
Elasticsearch Enterprise 8.18 (macOS, Linux, Windows) - 分布式搜索和分析引擎
469 0
|
安全 Java Linux
Linux安装Elasticsearch详细教程
Linux安装Elasticsearch详细教程
2362 64
|
缓存 NoSQL Java
基于SpringBoot的Redis开发实战教程
Redis在Spring Boot中的应用非常广泛,其高性能和灵活性使其成为构建高效分布式系统的理想选择。通过深入理解本文的内容,您可以更好地利用Redis的特性,为应用程序提供高效的缓存和消息处理能力。
1542 79
|
存储 索引
Elasticsearch分布式架构
【11月更文挑战第2天】
380 1

热门文章

最新文章