六十.项目引入ElasticSearch

新建一个模块webshop-elasticsearch,专门用来做ElasticSearch搜索服务:
六十.项目引入ElasticSearch_第1张图片
在模块webshop-elasticsearch的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.jiejie.webshop</groupId>
    <artifactId>webshop-elasticsearch</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>webshop-elasticsearch</name>
    <description>ElasticSearch搜索引擎服务</description>

    <properties>
        <java.version>1.8</java.version>
        <elasticsearch.version>7.4.2</elasticsearch.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.jiejie.webshop</groupId>
            <artifactId>webshop-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.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

再把该模块纳入管理,修改总的pom.xml,如下:
六十.项目引入ElasticSearch_第2张图片
由于引入了公共的模块webshop-common,该模块有数据源的依赖,所以在webshop-elasticsearch模块中需要排除该自动装配操作,同时加入服务注册发现需要的注解,修改启动类WebshopElasticsearchApplication,如下:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
public class WebshopElasticsearchApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebshopElasticsearchApplication.class, args);
    }
}

在application.yml加入如下注解,定义上注册中心地址等,如下:

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  application:
    name: webshop-elasticsearch
#ES搜索服务端口号
server:
  port: 12000

使用测试类测试下,如下:

package com.jiejie.webshop.elasticsearch;


import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class WebshopElasticsearchApplicationTests {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Test
    public void contextLoads() {
        System.out.println(restHighLevelClient);
    }

}

输出结果如下:
在这里插入图片描述

说明ElasticSearch引入成功,也有能够操作API的客户端工具类了。

你可能感兴趣的:(从零开始搭建一个电商系统)