前言:
最近想着体验一下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);
}
}