第一章第十七节:Elasticsearch之商城项目整合elasticsearch

1、在onlinemall下创建子项目onlinemall-search项目


2、引入elasticsearch依赖以及更新默认版本的配置

springboot2.1.8默认引入的是elasticsearch6.4.3版本

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/>
    </parent>
    <groupId>com.applesnt.onlinemall</groupId>
    <artifactId>onlinemall-search</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>onlinemall-search</name>
    <description>elasticsearch全文检索服务</description>
    <properties>
        <java.version>1.8</java.version>
        <!--替换springboot默认的es版本为当前使用版本-->
        <elasticsearch.version>7.4.2</elasticsearch.version>
    </properties>
    <dependencies>

        <!--引入elasticsearch客户端依赖-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.4.2</version>
        </dependency>
        
        <!--引入web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--引入公共项目依赖-->
        <dependency>
            <groupId>com.applesnt.onlinemall</groupId>
            <artifactId>onlinemall-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

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

</project>

2、配合onlinemall-search项目

application.yml

#应用服务端口
server:
  port: 20000

spring:
  #nacos注册中心
  cloud:
    nacos:
      discovery:
        server-addr: 116.196.121.63:8848
  #应用名称
  application:
    name: onlinemall-search

logging:
  level:
    com.applesnt.onlinemall.product: debug

3、开启注册中心以及排除数据加载

com.applesnt.onlinemall.search.OnlinemallSearchApplication

package com.applesnt.onlinemall.search;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/*开启注册中心*/
@EnableDiscoveryClient
/*因为引入了mybatis,没有配置数据源相关信息 如果不排除数据源,启动会报错*/
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class OnlinemallSearchApplication {

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

}

4、编写elasticsearch配置类

com.applesnt.onlinemall.search.config.ElasticSearchConfig

package com.applesnt.onlinemall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author menghaibin
 * @project onlinemall
 * @date 2021/7/3-22:01
 * @describe:elasticsearch配置类
 */
@Configuration
public class ElasticSearchConfig {

    /*可以设置builder的信息,暂时不知道有啥用*/
    public static final RequestOptions COMMON_OPTIONS;
    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
        COMMON_OPTIONS = builder.build();
    }

    @Bean
    public RestHighLevelClient esHighLevelClient(){
        RestClientBuilder builder = null;
        builder = RestClient.builder(new HttpHost("116.196.121.63",9200,"http"));
        RestHighLevelClient client = new RestHighLevelClient(builder);
        return client;
    }
}

5、测试打印

com.applesnt.onlinemall.search.OnlinemallSearchApplicationTests

package com.applesnt.onlinemall.search;

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 OnlinemallSearchApplicationTests {

    @Autowired
    private RestHighLevelClient client;
    @Test
    public void contextLoads() {
        System.out.println(client);
    }
}

posted @   努力的校长  阅读(117)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
点击右上角即可分享
微信分享提示