1. docker 安装ElasticSearch
- 这里采用单例模式安装
docker run -di -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e "discovery.type=single-node" -p 9200:9200 -p 9300:9300 --name=elasticsearch elasticsearch:8.2.3
- 进入容器,对elasticsearch进行配置
docker exec -u root -it elasticsearch bash
apt-get update
apt-get install vim -y
vim config/elasticsearch.yml
将elasticsearch安全性配置修改为false
#
# -------------------------------------------------------------------------------
--
# Enable security features
xpack.security.enabled: false
xpack.security.enrollment.enabled: false
# Enable encryption for HTTP API client connections, such as Kibana, Logstash, aa
nd Agents
xpack.security.http.ssl:
enabled: false
keystore.path: certs/http.p12
# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
enabled: false
verification_mode: certificate
keystore.path: certs/transport.p12
truststore.path: certs/transport.p12
#----------------------- END SECURITY AUTO CONFIGURATION ------------------------
完成后,保存配置文件并重启
打开9200地址,既可看到如下图所示,即启动成功
2. docker 安装kibana
docker run -d -p 5601:5601 --link elasticsearch:es -e ELASTICSEARCH_URL=http://es:9200 --name kibana kibana:8.2.3
完成后,打开5600地址,可以看到kibana首页
3. Spring Boot 整合ElasticSearch
- pom添加依赖文件
co.elastic.clients
elasticsearch-java
8.2.3
com.fasterxml.jackson.core
jackson-databind
2.12.3
- 添加配置类
@Configuration
public class ElasticConfig {
// 主机,默认127.0.0.1
@Value("${elasticsearch.host:127.0.0.1}")
private String host;
// 端口,默认9200
@Value("${elasticsearch.port:9200}")
private Integer port;
// 协议,默认http
@Value("${elasticsearch.scheme:http}")
private String scheme;
@Bean
public RestClient getRestClient() {
return RestClient.builder(
new HttpHost(host, port, scheme)
).build();
}
@Bean
public ElasticsearchTransport getElasticsearchTransport() {
// 使用 transport 加载 json 的对象映射器
return new RestClientTransport(getRestClient(),
new JacksonJsonpMapper());
}
@Bean
public ElasticsearchClient getElasticsearchClient() {
// 创建操作用的 ElasticsearchClient 对象
return new ElasticsearchClient(getElasticsearchTransport());
}
}
- 添加spring配置
elasticsearch:
host: 192.168.0.197
port: 9200
scheme: http
- 编写测试类
@Autowired
private ElasticsearchClient elasticsearchClient;
@Test
public void sendSingle() {
try {
Map data = new HashMap<>();
data.put("测试一下", "测试一下");
IndexRequest
4. 在kibana中查看添加的索引,并建立视图
-
点击stack management
-
点击索引管理,既可看见我们刚刚添加的索引
-
点击数据视图,创建数据视图
4.输入索引名称,创建视图
-
点击discover,即可以看到我们创建的测试视图