1 docker的ES7添加用户名和密码
- 修改/home/es/config/elasticsearch.yml文件
network.host: 0.0.0.0
network.publish_host: 192.168.101.157
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
# 设置用户名和密码
http.cors.allow-headers: Authorization
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
docker restart es7.16.3
- 执行设置用户名和密码的命令
# 进入容器
docker exec -it es7.16.3 bash
# 进入到ES对应目录
cd /usr/share/elasticsearch/bin
//设置用户名和密码的命令
elasticsearch-setup-passwords interactive
# 输入ES的各个模块的密码
- 已知原密码修改现有密码
curl -XPOST -u elastic "127.0.0.1:9200/_security/user/elastic/_password" -H 'Content-Type: application/json' -d'{"password" : "elastic"}'
2 忘记ES密码,需要进行重置
- 修改配置文件config/elasticsearch.yml,注释掉支持x-pack的xpack.security.enabled: true配置,保存退出
- 重启ES容器
- 启动成功后,使用curl查看当前Elasticsearch的索引
curl -XGET "127.0.0.1:9200/_cat/indices" -H 'Content-Type: application/json'
- 控制台打印,多了一个.security-7的索引
curl -XGET "127.0.0.1:9200/_cat/indices" -H 'Content-Type: application/json'
> green open .security-7 J7VZVkwGT0um4GyH3nptnQ 1 0 6 0 20.5kb 20.5kb
- 删除.security-7的索引
curl -XDELETE 127.0.0.1:9200/.security-7
- 重置密码
3 修改kibana对应配置
- 修改/home/kibana/config/kibana.yml配置文件
server.name: kibana
server.host: "0"
server.shutdownTimeout: "5s"
elasticsearch.hosts: ["http://192.168.101.157:9200"]
monitoring.ui.container.elasticsearch.enabled: true
i18n.locale: "zh-CN"
elasticsearch.username: elastic
elasticsearch.password: elastic
- 重启kibana容器
4 SpringBoot整合ES
- 修改pom.xml添加springBoot的ES支持
4.0.0
com.demo
esspringboot_proc
1.0-SNAPSHOT
UTF-8
UTF-8
1.8
2.4.2
1.18.24
5.8.5
org.springframework.boot
spring-boot-starter-parent
2.4.2
org.projectlombok
lombok
${lombok.version}
cn.hutool
hutool-all
${hutool.version}
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-elasticsearch
${project.artifactId}
org.springframework.boot
spring-boot-maven-plugin
${spring-boot.version}
- 添加src/main/resources/application.yml
server:
port: 8100
spring:
application:
name: esspringboot-proc
elasticsearch:
rest:
uris: http://192.168.101.157:9200
username: elastic
password: elastic
- 创建对应索引的实体类
一个Spring Data Elasticsearch的POJO必须定义一个被@Id修饰的字段,它是和索引中的_id相对应的。当搜索完成时,Spring Data Elasticsearch会将该POJO的字段填充为搜索结果显示的数据,包括ID字段(填充为文档的_id值)
package com.demo.po;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Data
@Document(indexName = "hotol")
public class Hotel {
/*对应ES索引中的中_id*/
@Id
private String id;
private String title;
private String city;
private String price;
}
- 定义操作接口
在EsRepository接口中,可以按照业务需求定义方法,Spring Data Elasticsearch会自动找到其类库中合适的实现类。
package com.demo.service;
import com.demo.po.Hotel;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
public interface HotelService extends CrudRepository {
//注意方法名称对应ES中的查询方法
List findByTitleLike(String title);
}
- 自定义业务方法
在Service中就可以定义HotelService 类型的成员变量了,由Spring Boot完成注入,在findByTitle()方法中调用EsRepository的对应方法完成搜索。
package com.demo.service;
import com.demo.po.Hotel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class HotelServiceImpl {
@Autowired
private HotelService hotelService;
public List findByTitle(String title){
return hotelService.findByTitleLike(title);
}
}
- 自定义控制器
package com.demo.controller;
import com.demo.po.Hotel;
import com.demo.service.HotelServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class HotelController {
@Autowired
private HotelServiceImpl hotelService;
@GetMapping("/findtitle")
public List findTitle(){
return hotelService.findByTitle("富豪");
}
}