ELK docker elasticsearch7 設置xpack賬號密碼---整合springboot


之前配置了es7的xpack賬號密碼權限后,代碼系統工程也需要同步修改。

這里走了個誤區,因為在網上搜索了一大堆資料后,發現很多人走的都是引入x-pack-transport 包后,進行配置。搞了好久,就是搞不定這個包下載的問題。糾結郁悶了很久,求教了華哥(玉華哥是個技術大牛,啥都懂)。昨天他就立馬上網查詢,幾分鍾就給我一個連接文檔,我還是很愚鈍。糾結在簡單配置就好,不用自己寫配置java類。最后搞不定了,細品華哥給的掛網文檔。最終解決問題。

es沒加入賬號密碼配置之前,也就是xpack配置前。只需要簡單配置即可使用。

application.yml中加上

spring:
  data:
    elasticsearch:
     cluster-name: mses-cluster
     cluster-nodes: 115.28.136.252:9300

后面就不用做什么配置。就能接上了。pom.xml配置。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

啟動類入口。

@SpringBootApplication
@MapperScan({"com.ms.cloud.**.mapper"})
@EnableSwagger2
public class ESApp {
  public static void main(String[] args) {
    SpringApplication.run(ESApp.class, args);
  }
}

 

加入了xpack做了權限驗證。同時還做了tsl驗證,就是上篇寫的文章后。原有的配置不行了,需要做修改。

ELK docker elasticsearch7 設置xpack賬號密碼

誤區也寫出來,防日后自己再跌倒。就是網上很多資料配置的,官網也給的配置如下。但是我無論如何也搞不定。。可能還是道行太淺。

https://www.elastic.co/guide/en/elasticsearch/reference/7.1/setup-xpack-client.html

我先是在pom.xml中加上maven配置依賴,跟官網說的一樣,但是就是提示:Missing artifact org.elasticsearch.client:x-pack-transport:jar:7.1.1

私有倉庫也加上了代理到官網庫

 

 

 

不行,然后到官網將jar下載下來,手動加入到buildpath中。包是下載下來了,也加入工程了,然后運行不是少這個就是少那個。classnodfind異常。。

https://artifacts.elastic.co/maven/org/elasticsearch/client/x-pack-transport/7.1.1/x-pack-transport-7.1.1.jar

因為x-pack-transport 這個包中只有一個類,他依賴於x-pack-api。。然而api我也下載不下來,找了很久。。然后放棄了。。

上面是elastic官網。。。

 

再到springboot  spring-boot-starter-data-elasticsearch 官網,看華哥給我介紹的文檔。。

https://docs.spring.io/spring-data/elasticsearch/docs/4.0.0.RELEASE/reference/html/#elasticsearch.clients.transport

 

 

就這樣簡單的解決了。

重點==============================

自定義配置application.yml,將原有配置去掉。

增加自定義配置信息。這里使用的是9200,網絡http接口。

elasticsearch: 
    urls: 192.168.89.138:9204,192.168.89.138:9205,192.168.89.138:9206
    username: elastic
    password: elastic

 

 

寫一個配置文件RestClientConfig.java

package com.my.cloud.common.config;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;

@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {

    @Value("${elasticsearch.username}")
    private String USERNAME;
    @Value("${elasticsearch.password}")
    private String PASSWORD;
    @Value("${elasticsearch.urls}")
    private String URLS;// 都好分割 

    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {
        if (StringUtils.isEmpty(URLS)) {
            throw new RuntimeException("配置有問題,elasticsearch.urls為空");
        }
        String[] urls = URLS.split(",");
        HttpHost[] httpHostArr = new HttpHost[urls.length];
        for (int i=0; i<urls.length; i++) {
            String urlStr = urls[i];
            if(StringUtils.isEmpty(urlStr)) {
                continue;
            }
            httpHostArr[i] = HttpHost.create(urlStr);
        }
        
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USERNAME, PASSWORD));  //es賬號密碼(默認用戶名為elastic)
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(httpHostArr)
                        .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                                httpClientBuilder.disableAuthCaching();
                                return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                            }
                        }));
        return client;
    }
}

 

測試

 用之前的es測試類測試一下。。

連接成功,能寫,能查,能刪除。

@Autowired
    private HouseDao repository;
    
    /**
     * @throws ParseException 
     * @desc 插入單條信息
     * @date 2020年5月9日 下午2:05:49
     */
    @Test
    public void insert() throws ParseException {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("insert");
        String pkId = "5000005";
        HouseUser houseUser = new HouseUser("李無", 25L, simpleDateFormat.parse("2010-05-08 15:23:00"));
        List<HousePhoto> photos = new ArrayList<HousePhoto>();
        photos.add(new HousePhoto("10000", "相片01", "五菱最終到es", simpleDateFormat.parse("2020-05-11 5:23:00")));
        photos.add(new HousePhoto("10002", "相片02", "五菱里面會存儲一條數據33333", simpleDateFormat.parse("2020-05-12 7:50:31")));
        photos.add(new HousePhoto("10003", "相片03", "五菱在第二種類型里面", simpleDateFormat.parse("2020-05-13 9:20:35")));
        photos.add(new HousePhoto("10004", "相片04", "五菱而如果聲明了car類型是nested", simpleDateFormat.parse("2020-05-14 13:40:30")));
        
        HouseVo houseVo = new HouseVo(pkId, "10000", "eclipse中格式化代碼快捷鍵Ctrl+Shift+F失效,很長一段時間我的eclie都有個毛病,就是當我要格式化代碼的時候,右鍵", photos, houseUser, new BigDecimal("5089.32"));
        houseVo.setCopyrightId("112");
        Map<String, String> signMap = new HashMap<>();
        signMap.put("car", "五菱");
        signMap.put("house", "好房");
        houseVo.setSignMap2(signMap);
        String dateStr = "2020-05-10 14:20:30";
        Date mydate = simpleDateFormat.parse(dateStr);
        houseVo.setCreateTime(mydate);
        List<String> fkIdDicTypes = new ArrayList<>();
        fkIdDicTypes.add("556");
        houseVo.setFkIdDicTypes(fkIdDicTypes);
        repository.save(houseVo);
        
        Optional<HouseVo> optional = repository.findById(pkId);
        System.out.println(JSON.toJSON(optional));
        System.out.println(MyDateUtil.dateToDateStr(MyDateUtil.DATE_TIME_FMT, houseVo.getCreateTime()));
        System.out.println(MyDateUtil.dateToDateStr(MyDateUtil.DATE_TIME_FMT, houseVo.getUser().getBirthday()));
    }
}

 

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import com.ms.cloud.business.bean.HouseVo;

public interface HouseDao extends ElasticsearchRepository<HouseVo, String> {

}

 

其他實體類信息

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import lombok.Data;

@Data // 加上這個聲明,就自動帶get、set方法了
@Document(indexName = "house_vo", type="_doc")
public class HouseVo implements Serializable{ 
    // 這個很重要,這個是ID信息,如果不定義,那么會默認生成一個隨機字符串為ID,
    @Id 
    private String pkId;

    @Field(type = FieldType.Keyword)
    private String title; //標題

    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String remark;

    private Map<String,String> signMap2;    
    // 聲明為嵌套類型
    @Field(type = FieldType.Nested)
    private List<HousePhoto> photos;
    // 不聲明類型默認為object
    @Field(type = FieldType.Object)
    private HouseUser user;

    @Field(type = FieldType.Double)
    private BigDecimal price;

    @Field(type = FieldType.Date)
    private Date createTime;
    @Field(type = FieldType.Keyword)
    private String copyrightId;//是否原創

    @Field(type = FieldType.Keyword)
    private List<String> fkIdDicTypes; //資訊字典分類
    
    public HouseVo() {
        
    }
    public HouseVo(String pkId, String title, String remark, List<HousePhoto> photos, HouseUser user,
            BigDecimal price) {
        super();
        this.pkId = pkId;
        this.title = title;
        this.remark = remark;
        this.photos = photos;
        this.user = user;
        this.price = price;
    }

}


import java.io.Serializable;
import java.util.Date;

import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import lombok.Data;

@Data
public class HouseUser implements Serializable{ 
    

    @Field(type = FieldType.Keyword)
    private String name; 

    @Field(type = FieldType.Long)
    private Long age;

    @Field(type = FieldType.Date)
    private Date birthday;

    public HouseUser() {}
    public HouseUser(String name, Long age, Date birthday) {
        super();
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }
}

import java.io.Serializable;
import java.util.Date;

import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import lombok.Data;
@Data
public class HousePhoto implements Serializable{ 

    @Field(type = FieldType.Keyword)
    private String id;

    @Field(type = FieldType.Keyword)
    private String title; //標題

    @Field(type = FieldType.Text)
    private String remark;

    @Field(type = FieldType.Date)
    private Date upDate;
    
    public HousePhoto() {}
    
    public HousePhoto(String id, String title, String remark, Date upDate) {
        super();
        this.id = id;
        this.title = title;
        this.remark = remark;
        this.upDate = upDate;
    }
    
}
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM