欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

elasticsearch7.6.2集成springboot2.2.6---2

程序员文章站 2022-03-31 16:13:51
...
在本文集成之前可以看下官网的地址

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html
官网说的很明白了
1,依赖关系:
org.elasticsearch.client:elasticsearch-rest-client
org.elasticsearch:elasticsearch
2,还有就是初始化这个是官网给的demo:
RestHighLevelClient client = new RestHighLevelClient(
        RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http")));



好了,上面两个简单看下,下面就可以开始以idea、gradle、jdk1.8为例:
1,建立项目一个es为例的项目
打开idea-点击File-New-Project-Spring Initializr-根据自己的包名和项目名字下一步--Devloper Tools 全部勾上--选择Nosql勾上Spring Data Elasticsearch -next一直完成即可,项目建立后大家一定注意,es的jar默认是6.8.X,工具导入的是这个
  implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'
所以根本不是我们想要的7.6.2,也就是说ieda导入的不是我们想要的jar,我们要手动处理下,加入
/es相关的jar
    implementation 'org.elasticsearch.client:elasticsearch-rest-high-level-client:7.6.2'
    implementation 'org.elasticsearch:elasticsearch:7.6.2'即可。

2,项目添加相对应的jar如下,这个是我添加的jar,默认是mavenCentral(),这个巨慢无比,我就换了阿里的,各位如果慢可以看我的其他文章地址已经列举,不在这里说了:
dependencies {
//es相关的jar
  implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'
    implementation 'org.elasticsearch.client:elasticsearch-rest-high-level-client:7.6.2'
    implementation 'org.elasticsearch:elasticsearch:7.6.2'
    implementation 'org.elasticsearch.client:elasticsearch-rest-client:7.6.2'

    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'com.alibaba:fastjson:1.2.29'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}
3,如果jar下载完整后,初始化开始,如下我的配置类:

package com.zys.zysnbaesapi.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;

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

}
这些配置好,启动项目如果正常说明环境搞定,大功基本搞成,下面就可以继续玩语法了。