1,创建gulimall-search,导入es的高阶依赖 等mvn依赖
<properties> <java.version>1.8</java.version> <!--默认org.elasticsearch是6.4.3版本,不匹配,如下图--> <elasticsearch.version>7.4.2</elasticsearch.version> </properties> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.4.2</version> </dependency>
2,编写es自定义配置
package com.atguigu.gulimall.search.config; import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 1、导入依赖 * 2、编写配置,给容器中注入一个RestHighLevelClient * 3、参照API https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.4/index.html */ @Configuration public class GulimallElasticSearchConfig { @Bean public RestHighLevelClient esRestClient(){ RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.56.10", 9200, "http"))); return client; } }
3, 编写测试
package com.atguigu.gulimall.search; import org.elasticsearch.client.RestHighLevelClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; @RunWith(SpringRunner.class) @SpringBootTest public class GulimallSearchApplicationTests { @Resource private RestHighLevelClient client; @Test public void contextLoads() { System.out.println(client); } }
其他:
gulimall-search pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.atguigu.gulimall</groupId> <artifactId>gulimall-search</artifactId> <version>0.0.1-SNAPSHOT</version> <name>gulimall-search</name> <description>ElasticSearch检索服务</description> <properties> <java.version>1.8</java.version> <!--默认org.elasticsearch是6.4.3版本,不匹配--> <elasticsearch.version>7.4.2</elasticsearch.version> </properties> <dependencies> <dependency> <groupId>com.atguigu.gulimall</groupId> <artifactId>gulimall-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.4.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
gulimall-search application.properties
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.application.name=gulimall-search