一、在ecs-common工程下添加依赖包

  1. <!--spring整合elasticsearch包-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  5. </dependency>
  6. <!--QueryDSL查询框架,通过API方式来构建查询-->
  7. <dependency>
  8. <groupId>com.querydsl</groupId>
  9. <artifactId>querydsl-jpa</artifactId>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.querydsl</groupId>
  13. <artifactId>querydsl-apt</artifactId>
  14. </dependency>

二、在ec-impl-goods工程ApplicationGoods启动类加上如下方法

  1. /**
  2. * 初始化es客户端组件RestHighLevelClient,其封装了操作es的crud方法
  3. * @return
  4. * @throws IOException
  5. */
  6. @Bean
  7. public RestHighLevelClient getRestHighLevelClient() throws IOException {
  8. RestHighLevelClient client = null;
  9. try {
  10. client = new RestHighLevelClient(
  11. RestClient.builder(
  12. new HttpHost(InetAddress.getByName("192.168.217.110"),9200,"http"),
  13. new HttpHost(InetAddress.getByName("192.168.217.110"),9201,"http")));
  14. log.info("ElasticSearch初始化完成。。");
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. log.error("ElasticSearch初始化失败:" + e.getMessage(),e);
  18. client.close();
  19. }
  20. return client;
  21. }