ElasticSearch8.x+SpringBoot3.X联调踩坑指南

前言:

最近想着体验一下ES8,于是在服务器上使用docker镜像进行了搭建。在正确配置服务器入站规则+本地浏览器可以访问URL的情况下,使用RestHighLevelClient的API始终无法正确连接,提示"找不到主机名"。

经过长达两个小时的折腾,我归纳出一些你可能会踩下的坑。

在此之前:请跟着我回顾我的操作

1.ElasticSearch的部署

1. 拉取镜像

bash 复制代码
docker pull swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/library/elasticsearch:8.6.2

2. 创建挂载目录

bash 复制代码
mkdir -p ~/es_data/{data,logs,config}
chmod -R 777 ~/es_data

3. 启动容器

ruby 复制代码
docker run -d 
  --name elasticsearch 
  -p 9200:9200 
  -p 9300:9300 
  -e "discovery.type=single-node" 
  -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" 
  -e "xpack.security.enabled=false" 
  -v ~/es_data/data:/usr/share/elasticsearch/data 
  -v ~/es_data/logs:/usr/share/elasticsearch/logs 
  --restart=unless-stopped 
  swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/library/elasticsearch:8.6.2

请关注 -e "xpack.security.enabled=false" 这一行:指的是在测试环境中不必开启密钥校验,你一定要加上这一行,这样可以确保你在本地可以顺利连接。从而规避SSL对你的约束。

4. 验证安装

sql 复制代码
curl -X GET "localhost:9200"

2.入站处理和出站处理

注意要对我们在docker启动命令中使用的端口,进行入站的运行放行。我们的出站安全组是All,因此不做多的阐述

3.最关键的部分------SDK的选取

如果你希望使用ElasticSearch8.x的版本,请你务必使用 JDK17 JDK17 JDK17.

同时将你的SpringBoot版本升级到 3.2.x

因为Spring2.x的版本 是不支持 Es8的连接的

你需要在pom部分配置以下内容

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

<!-- Elasticsearch Java Client -->
<dependency>
    <groupId>co.elastic.clients</groupId>
    <artifactId>elasticsearch-java</artifactId>
    <version>8.12.0</version>
</dependency>

'''

同时在Application.yml部分配置以下内容:

yaml 复制代码
spring:
 
  elasticsearch:
    uris: http://x.x.x.x:9200

这里我会给出我自己的测试用例代码:

less 复制代码
package com.xiaoyongcai.io.pikapika.ElasticSearchTest.Controller;
​
import com.xiaoyongcai.io.pikapika.ElasticSearchTest.Model.Product;
import com.xiaoyongcai.io.pikapika.ElasticSearchTest.Service.ProductService;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import java.util.List;
​
@RestController
@RequestMapping("/api/products")
@RequiredArgsConstructor
public class ProductController {
    private final ProductService productService;
​
    @PostMapping
    public Product createProduct(@RequestBody Product product) {
        return productService.saveProduct(product);
    }
​
    @GetMapping("/{id}")
    public Product getProduct(@PathVariable String id) {
        return productService.findById(id);
    }
​
    @GetMapping("/search")
    public List<Product> searchProducts(@RequestParam("name") String name) {
        return productService.searchByName(name);
    }
​
    @DeleteMapping("/{id}")
    public void deleteProduct(@PathVariable String id) {
        productService.deleteProduct(id);
    }
}
kotlin 复制代码
package com.xiaoyongcai.io.pikapika.ElasticSearchTest.Model;
​
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import lombok.Data;
​
@Data
@Document(indexName = "products")  // 索引名
public class Product {
    @Id
    private String id;
​
    @Field(type = FieldType.Text, analyzer = "standard")  // 文本类型,使用标准分词器
    private String name;
​
    @Field(type = FieldType.Double)
    private Double price;
​
    @Field(type = FieldType.Keyword)  // 不分词的关键字类型
    private String category;
}
java 复制代码
package com.xiaoyongcai.io.pikapika.ElasticSearchTest.Repository;
​
import com.xiaoyongcai.io.pikapika.ElasticSearchTest.Model.Product;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
​
public interface ProductRepository extends ElasticsearchRepository<Product, String> {
    // 自定义查询方法(根据字段名自动推导)
    List<Product> findByName(String name);
    List<Product> findByPriceBetween(Double minPrice, Double maxPrice);
}
typescript 复制代码
package com.xiaoyongcai.io.pikapika.ElasticSearchTest.Service;
​
import com.xiaoyongcai.io.pikapika.ElasticSearchTest.Model.Product;
import com.xiaoyongcai.io.pikapika.ElasticSearchTest.Repository.ProductRepository;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import java.util.List;
​
@Service
@RequiredArgsConstructor
public class ProductService {
    private final ProductRepository productRepository;
​
    public Product saveProduct(Product product) {
        return productRepository.save(product);
    }
​
    public Product findById(String id) {
        return productRepository.findById(id).orElse(null);
    }
​
    public List<Product> searchByName(String name) {
        return productRepository.findByName(name);
    }
​
    public void deleteProduct(String id) {
        productRepository.deleteById(id);
    }
}
相关推荐
小蒜学长3 小时前
springboot多功能智能手机阅读APP设计与实现(代码+数据库+LW)
java·spring boot·后端·智能手机
追逐时光者4 小时前
精选 4 款开源免费、美观实用的 MAUI UI 组件库,助力轻松构建美观且功能丰富的应用程序!
后端·.net
你的人类朋友5 小时前
【Docker】说说卷挂载与绑定挂载
后端·docker·容器
间彧5 小时前
在高并发场景下,如何平衡QPS和TPS的监控资源消耗?
后端
间彧5 小时前
QPS和TPS的区别,在实际项目中,如何准确测量和监控QPS和TPS?
后端
间彧5 小时前
消息队列(RocketMQ、RabbitMQ、Kafka、ActiveMQ)对比与选型指南
后端·消息队列
brzhang7 小时前
AI Agent 干不好活,不是它笨,告诉你一个残忍的现实,是你给他的工具太难用了
前端·后端·架构
brzhang7 小时前
一文说明白为什么现在 AI Agent 都把重点放在上下文工程(context engineering)上?
前端·后端·架构
Roye_ack7 小时前
【项目实战 Day9】springboot + vue 苍穹外卖系统(用户端订单模块 + 商家端订单管理模块 完结)
java·vue.js·spring boot·后端·mybatis
AAA修煤气灶刘哥8 小时前
面试必问的CAS和ConcurrentHashMap,你搞懂了吗?
后端·面试