springboot 集成 elasticsearch jeecg

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 在同一個包下,他會自動掃描到,如果不是的話,就需要上面的註解,進行手動指定

  • 測試

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章