docker安裝elasticsearch及其客戶端
本文將主要記錄elasticsearch安裝過(guò)程中遇到的一些問(wèn)題
1. 安裝配置elasticsearch
(1)docker下載相關(guān)鏡像
docker pull elasticsearch:7.6.2
(2)創(chuàng)建持久化文件
mkdir -p /mydata/elasticsearch/config
mkdir -p /mydata/elasticsearch/data
初始化配置
echo "http.host: 0.0.0.0" >> /mydata/elasticsearch/config/elasticsearch.yml
注意這里“:”后的空格弧烤,否則會(huì)報(bào)語(yǔ)法錯(cuò)誤
(3)啟動(dòng)鏡像
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms84m -Xmx512m" \
-v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
-v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-d elasticsearch:7.6.2
-p:端口映射
-e discovery.type=single-node 單點(diǎn)模式啟動(dòng)
-e ES_JAVA_OPTS="-Xms84m -Xmx512m":設(shè)置啟動(dòng)占用的內(nèi)存范圍(實(shí)驗(yàn)環(huán)境啟動(dòng)后可能因?yàn)樵品?wù)器內(nèi)存過(guò)小而占滿)
-v 目錄掛載
-d 后臺(tái)運(yùn)行
啟動(dòng)后使用docker ps查看發(fā)現(xiàn)未正常啟動(dòng)继低,查看啟動(dòng)日志
docker logs elasticsearch
發(fā)現(xiàn)是文件拒絕訪問(wèn)異常,為該文件夾設(shè)置所有用戶都有讀寫(xiě)執(zhí)行權(quán)限
chmod -R 777 /mydata/elasticsearch/
重新啟動(dòng)elasticsearch
docker restart elasticsearch
瀏覽器訪問(wèn) ip:9200測(cè)試哨苛,訪問(wèn)成功
2. 安裝使用可視化工具
(1)下載同版本鏡像
docker pull kibana:7.6.2
(2)初始化配置
mkdir -p /mydata/kibana
touch /mydata/kibana/kibana.yml
vim /mydata/kibana/kibana.yml
server.host: 0.0.0.0
elasticsearch.hosts: http://你的ip:9200
保存退出
(3)啟動(dòng)kibana
docker run --name kibana -v /mydata/kibana/kibana.yml:/usr/share/kibana/config/kibana.yml -p 5601:5601 -d kibana:7.6.2
啟動(dòng)后可通過(guò)docker ps查看是否啟動(dòng)成功
等待一段時(shí)間后瀏覽器訪問(wèn) ip:5601 進(jìn)行測(cè)試
3.SpringBoot中使用Elasticsearch Java Rest Client
(1)導(dǎo)入相關(guān)依賴
<!-- elasticsearch依賴-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.6.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.6.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.6.2</version>
</dependency>
此處官方文檔中只寫(xiě)了導(dǎo)入elasticsearch-rest-high-level-client依賴踏揣,但在實(shí)際測(cè)試運(yùn)行中會(huì)報(bào)NoClassDefFoundError錯(cuò)誤,檢查相關(guān)依賴后發(fā)現(xiàn)elasticsearch-rest-high-level-client中的elasticsearch依賴并不是7.6.2,因此需要再手工引入對(duì)應(yīng)版本的依賴,導(dǎo)入完成后可正常運(yùn)行
(2)創(chuàng)建相關(guān)Bean
import org.apache.http.HttpHost;
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author: 李豐翼
* @DateTime: 2020/7/13 0013 09:36
* @Description: TODO
*/
@Configuration
public class ClientConfig {
public static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
// builder.addHeader("Authorization", "Bearer " + TOKEN);
// builder.setHttpAsyncResponseConsumerFactory(
// new HttpAsyncResponseConsumerFactory
// .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
COMMON_OPTIONS = builder.build();
}
@Bean
public RestHighLevelClient esRestClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http")
));
return client;
}
}
(3)測(cè)試是否正常運(yùn)行
直接打印client對(duì)象看是否成功注入
@SpringBootTest
class GulimallSearchApplicationTests {
@Resource
private RestHighLevelClient client;
@Test
void contextLoads() throws IOException {
System.out.println(client);
}
}
elasticsearch使用教程可參考官方文檔
https://www.elastic.co/guide/en/elasticsearch/reference/7.x/query-dsl.html
kibana參考文檔
https://www.elastic.co/guide/en/kibana/7.x/introduction.html
Java REST Client參考文檔
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.x/java-rest-high.html