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支持
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>esspringboot_proc</artifactId>
<version>1.0-SNAPSHOT</version>
<!--统一版本-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- spring boot -->
<spring-boot.version>2.4.2</spring-boot.version>
<!-- tools -->
<lombok.version>1.18.24</lombok.version>
<hutool.version>5.8.5</hutool.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
</plugins>
</build>
</project>
- 添加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<Hotel,String> {
//注意方法名称对应ES中的查询方法
List<Hotel> 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<Hotel> 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<Hotel> findTitle(){
return hotelService.findByTitle("富豪");
}
}