转载

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 私有仓库也加上了代理到官网库 17 18 不行,然后到官网将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 19 就这样简单的解决了。 重点============================== 自定义配置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;
    }
    
}
 
正文到此结束
Loading...