ElasticSearch(7)springbooot整合ES

ElasticSearch(7)springbooot整合ES,第1张

ElasticSearch(7)springbooot整合ES 使用springBoot整合ES 1 搭建SpringBoot工程 2 引入Elastiesearch相关坐标

引用

        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            7.4.0
        
        
            org.elasticsearch.client
            elasticsearch-rest-client
            7.4.0
        
        
            org.elasticsearch
            elasticsearch
            7.4.0
        
3 创建编辑配置类

application.yml

elasticsearch:
  host: 192.168.156.131
  port: 9200

ElasticSearchConfig

package com.yy.esdemo.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticSearchConfig {
    private String host;
    private int port;

    public String getHost() {
        return host;
    }

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

    public int getPort() {
        return port;
    }

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

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

4 测试

EsDemoApplicationTests

package com.yy.esdemo;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class EsDemoApplicationTests {
    @Autowired
    private RestHighLevelClient client;

    @Test
    void contextLoads() {
        //创建es客户端对象
//        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
//                new HttpHost(
//                        "192.168.156.131",
//                        9200,
//                        "http"
//                )
//        ));
        System.out.println(client);

    }

}

运行结果截图如下,证明整合成功

项目结构:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5681180.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存