SpringBoot整合Elasticsearch7.5


由于项目要重构成微服务架构,闲来无事做下技术调研,看了网上好多资料头都晕,各种试还是没偷懒成功,所以潜下心来自已搞,不废话一起来玩。

版本信息

  • SpringBoot: 2.2.3.BUILD-SNAPSHOT
  • Elasticsearch: 7.5.1
  • JDK: 1.8

一、安装Elasticsearch服务以及新建SpringBoot项目

安装Elasticsearch

  • 下载地址: https://www.elastic.co/downloads/elasticsearch

  • 选择版本7.5.1, 我选择在windows版,方便用先装本地,你可选择其他环境版本

  • 解压elasticsearch-7.5.1-windows-x86_64.zip文件

  • 启动服务:运行bin\elasticsearch.bat文件即可

  • 检验:访问http://localhost:9200看到ES的服务信息,安装成功

    ps: 也可再安装个kibana工具玩,这个可以直接对ES上的数据进行CRUD,有界面的哦。这个后面再介绍

新建SpringBoot项目

网上一大堆,这里就不说了,新建时注意选择版本为 2.2.3.BUILD-SNAPSHOT

二、集成Elasticsearch

1、设置Elasticsearch版本

<elasticsearch.version>7.5.1</elasticsearch.version>

2、添加Elasticsearch包依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.2</version>
</dependency>
<!-- elasticsearch配置 -->
<dependency>
	<groupId>org.elasticsearch.client</groupId>
	<artifactId>elasticsearch-rest-high-level-client</artifactId>
	<version>${elasticsearch.version}</version>
</dependency>
<dependency>
	<groupId>org.elasticsearch</groupId>
	<artifactId>elasticsearch</artifactId>
	<version>${elasticsearch.version}</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>

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
	<groupId>com.google.code.gson</groupId>
	<artifactId>gson</artifactId>
	<version>2.8.6</version>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-configuration-processor</artifactId>
	<optional>true</optional>
</dependency>

三、配置并连接Elasticsearch服务器

1、修改application.yml文件,添加如下配置

server:
  port: 8082
elasticsearch:
  host: localhost
  port: 9200

这个就不作解决了,大家都能看懂

2、新建Elasticsearch配置类EsConfig,并生成RestHighLevelClient类

RestHighLevelClient类此类用于连接和操作Elasticsearch服务端数据

ps: 此处配置依赖以下配置, 并且类上的注解一定要再添加个@Component注解

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-configuration-processor</artifactId>
   <optional>true</optional>
</dependency>

EsConfig类:

@Component
@ConfigurationProperties(prefix = "elasticsearch")
public class EsConfig {
    private String host;
    private Integer port;

    @Bean(destroyMethod = "close")
    public RestHighLevelClient client() {
        return new RestHighLevelClient(RestClient.builder(
                new HttpHost(host, port, "http")
        ));
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }
}

四、测试看效果

1、创建HelloWorldController类

@RestController
public class HelloWorldController {
    @Resource
    private RestHighLevelClient restHighLevelClient;
    private String indexName = "indexname";
    
    @RequestMapping(value = "hello")
    public String hello() {
        
        Map<String, Object> query = new HashMap<>();

        try {
            Goods goods = new Goods(1L,"Gary1", "gggggggggggggggggggggggggg", "one");
            // 注意indexId是唯一的
            String indexId = "test001";
            add(goods, indexId);

            goods = new Goods(2L,"Gary2", "kkkkkkkkkkkkkkkkkkkk", "two");
            // 注意indexId是唯一的
            indexId = "test002";
            add(goods, indexId);
            
            query = this.query("test002");

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "Hello World!" + query.toString();
    }
    
   private boolean add(Goods goods, String indexId) throws IOException {
        String json = new Gson().toJson(goods);
        IndexRequest request = new IndexRequest(indexName).id(indexId).source(json, XContentType.JSON);
        restHighLevelClient.index(request, RequestOptions.DEFAULT);

        return true;
    }
    
    private Map<String, Object> query(String indexId) throws IOException {
        GetRequest request = new GetRequest(indexName, indexId);
        GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
        
        return response.getSource();
    }
}

2、添加Goods类

public class Goods {
    public Goods(Long id, String name, String description){
        this.id = id;
        this.name = name;
        this.description = description;
    }

    private Long id;

    private String name;

    private String description;
    ……
get and set此处略,自己添加
}

3、启动服务查看结果

地址:http://localhost:8082/hello


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM