springboot 集成 elasticsearch
-
版本要求
-
背景
- 公司使用的开源框架 jeecg 需要使用es ,jeecgboot 是 springboot 项目,所以我这边进行了下集成
-
服务器安装elasticsearch
-
docker 启动
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -v /home/software/elasticsearch/conf:/usr/share/elasticsearch/config -v /home/software/elasticsearch/data:/usr/share/elasticsearch/data -v /home/software/elasticsearch/plugins:/usr/share/elasticsearch/plugins --restart=always elasticsearch
-
修改 elasticsearch 配置文件 使其可以对外访问
http.host: 0.0.0.0 # Uncomment the following lines for a production cluster deployment transport.host: 0.0.0.0 #discovery.zen.minimum_master_nodes: 1
-
修改服务器默认内存分配数量
cat /proc/sys/vm/max_map_count 65530 设置max_map_count: sysctl -w vm.max_map_count=262144
-
-
elasticsearch 中安装 ik 分词器
-
在上面映射的plugins目录建立 ik 文件 ,然后将下载下来的和es 对应的ik 的zip 包进行解压
-
重启 es
- 本地请求测试端口是否开启
-
pom 引入 elasticsearch依赖
<!-- elasticsearch --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
-
yml 写入配置
spring: data: elasticsearch: cluster-name: elasticsearch cluster-nodes: 192.168.5.9:9300 elasticsearch: rest: uris: http://192.168.5.9:9200
- 这里的spring.elasticsearch.rest.uris 是配置es 健康检测地址
-
代码
-
在项目的启动Application文件中,添加如下代码
/** * 取消客户端初始化设置处理器数量 * https://blog.csdn.net/qq_37495786/article/details/96873800 * @param application * @return */ static{ System.setProperty("es.set.netty.runtime.available.processors", "false"); }
-
Document业务代码
import lombok.Data; 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 java.io.Serializable; /** * @program: jeecg-boot-parent * @description: * @author: fq * @create: 2020-11-19 16:53 **/ @Data @Document(indexName = "goods",type = "detail",shards = 3,replicas = 1) public class EsGoods implements Serializable { /** * @Document 注解说明 * index –> DB * type –> Table * Document –> row */ private static final long serialVersionUID = 1L; @Id Long id; @Field(type = FieldType.Text,analyzer = "ik_max_word") String title; //标题 @Field(type = FieldType.Keyword) String category;// 分类 @Field(type = FieldType.Keyword) String brand; // 品牌 @Field(type = FieldType.Double) Double price; // 价格 @Field(index = false, type = FieldType.Keyword) String images; // 图片地址 }
-
Repository 集成抽象类方法
import org.jeecg.elasticsearch.entity.EsGoods; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; /** * @program: jeecg-boot-parent * @description: * @author: fq * @create: 2020-11-19 16:59 **/ public interface EsGoodsRepository extends ElasticsearchRepository<EsGoods,Long> { }
-
Controller 测试方法
import org.checkerframework.checker.units.qual.A; import org.jeecg.elasticsearch.entity.EsGoods; import org.jeecg.elasticsearch.service.EsGoodsRepository; import org.jeecg.common.api.vo.Result; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; /** * @program: jeecg-boot-parent * @description: * @author: fq * @create: 2020-11-19 17:09 **/ @RequestMapping("/els") @RestController public class EsGoodsController { @Autowired public EsGoodsRepository esGoodsRepository; /** * 添加 * @param esGoods * @return */ @PostMapping("/add") private Result<?> add(@RequestBody EsGoods esGoods){ return Result.ok(esGoodsRepository.save(esGoods)); } /** * 删除 */ @DeleteMapping("/del/{id}") private Result<?> del(@PathVariable Long id){ esGoodsRepository.deleteById(id); return Result.ok(); } /** * 修改 */ @PutMapping("/update") private Result<?> update(@RequestBody EsGoods esGoods){ return Result.ok(esGoodsRepository.save(esGoods)); } /** * 查询 */ @GetMapping("/get") private Result<?> getEsGoods(){ Iterable<EsGoods> esGoodsIterable = esGoodsRepository.findAll(); List<EsGoods> esGoodsList = new ArrayList<>(); esGoodsIterable.forEach(x-> esGoodsList.add(x)); return Result.ok(esGoodsList); } }
-
-
结构如下
-
运行错误
-
取消客户端初始化设置处理器数量,如上所示,在启动类里面添加静态代码块
-
jeecg 我本来自己建立了自己的包名,但是在启动的时候,我发现 我建立的es 代码放在自己的包内,无法扫描到, 所以我只能迁移到 jeecg 的目录下,发现它可以正常扫码到。暂时明白为什么
这里已经找到了原因,springboot 在 2.0 以后针对es 的 Repositories 文件扫描,专门给与了注解 @EnableElasticsearchRepositories(basePackages = "com.xxxx.**") 来进行扫描, 加在Application 启动文件那里就行 规则:如果你的 Repositories 和 Application 在同一个包下,他会自动扫描到,如果不是的话,就需要上面的注解,进行手动指定
-
-
测试