首页 > 技术文章 > SpringBoot 集成 ES

flower-dance 2020-09-11 14:29 原文

第一步创建一个SpringBoot项目

 

 

 

 点击Next

 

 修改为自己想要的名字之后点击下一步

 

 选择自己需要的依赖关系,点击Next

 

 配置好路径后点击 Finish

等待项目构建完成后,记得看一下,ES 的版本

 

 如果是低版本的springBoot 可能ES的版本比较低

建议修改为和自己使用的ES的版本一致

 

修改后点击Maven的刷新

 

 

 刷新后发现,版本已经修改完毕

 

 编写配置类

package com.dance.danceesapi.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;

/**
 * ElasticSearch配置类
 * @author ZYGisComputer
 */
@Configuration
public class ElasticSearchClientConfig {

    @Bean
    public RestHighLevelClient restHighLevelClient(){
        return new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("127.0.0.1",9200,"http")
                )
        );
    }

}

在用的时候注入就可以了

package com.flower.floweres;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class FlowerEsApplication {
    
    @Autowired
    @Qualifier("restHighLevelClient")
    public RestHighLevelClient restHighLevelClient;

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

}

到此集成完毕

作者:彼岸舞

时间:2020\09\11

内容关于:ElasticSearch

本文来源于网络,只做技术分享,一概不负任何责任

 

推荐阅读