整合ES
環(huán)境準(zhǔn)備
- 安裝配置
ES
:https://blog.csdn.net/qq_50864152/article/details/136724528
- 安裝配置
Kibana
:https://blog.csdn.net/qq_50864152/article/details/136727707
- 新建項(xiàng)目:新建名為
web
的SpringBoot3
項(xiàng)目
elasticsearch-java
公共配置
- 介紹:一個(gè)開(kāi)源的高擴(kuò)展的分布式全文檢索引擎,可以近乎實(shí)時(shí)的存儲(chǔ) 和檢索數(shù)據(jù)
- 依賴(lài):
web
模塊引入elasticsearch-java
依賴(lài)---
但其版本必須與你下載的ES
的版本一致
<!-- 若不存在Spring Data ES的某個(gè)版本支持你下的ES版本,則使用 -->
<!-- ES 官方提供的在JAVA環(huán)境使用的依賴(lài) -->
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.11.1</version>
</dependency>
<!-- 和第一個(gè)依賴(lài)是一起的,為了解決springboot項(xiàng)目的兼容性問(wèn)題 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
- 配置:
web
模塊dev
目錄application-dal
添加
使用
open+@Value("${elasticsearch.open}")
的方式不能放到Nacos
配置中心
# elasticsearch配置
elasticsearch:
# 自定義屬性---設(shè)置是否開(kāi)啟ES,false表不開(kāi)竅ES
open: true
# es集群名稱(chēng),如果下載es設(shè)置了集群名稱(chēng),則使用配置的集群名稱(chēng)
clusterName: es
hosts: 127.0.0.1:9200
# es 請(qǐng)求方式
scheme: http
# es 連接超時(shí)時(shí)間
connectTimeOut: 1000
# es socket 連接超時(shí)時(shí)間
socketTimeOut: 30000
# es 請(qǐng)求超時(shí)時(shí)間
connectionRequestTimeOut: 500
# es 最大連接數(shù)
maxConnectNum: 100
# es 每個(gè)路由的最大連接數(shù)
maxConnectNumPerRoute: 100
- 配置:
web
模塊config
包下新建ElasticSearchConfig
類(lèi)
package cn.bytewisehub.pai.web.config;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
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.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticSearchConfig {
// 是否開(kāi)啟ES
private Boolean open;
// es 集群host ip 地址
private String hosts;
// es用戶(hù)名
private String userName;
// es密碼
private String password;
// es 請(qǐng)求方式
private String scheme;
// es集群名稱(chēng)
private String clusterName;
// es 連接超時(shí)時(shí)間
private int connectTimeOut;
// es socket 連接超時(shí)時(shí)間
private int socketTimeOut;
// es 請(qǐng)求超時(shí)時(shí)間
private int connectionRequestTimeOut;
// es 最大連接數(shù)
private int maxConnectNum;
// es 每個(gè)路由的最大連接數(shù)
private int maxConnectNumPerRoute;
// es api key
private String apiKey;
public RestClientBuilder creatBaseConfBuilder(String scheme){
// 1. 單節(jié)點(diǎn)ES Host獲取
String host = hosts.split(":")[0];
String port = hosts.split(":")[1];
// The value of the schemes attribute used by noSafeRestClient() is http
// but The value of the schemes attribute used by safeRestClient() is https
HttpHost httpHost = new HttpHost(host, Integer.parseInt(port),scheme);
// 2. 創(chuàng)建構(gòu)建器對(duì)象
//RestClientBuilder: ES客戶(hù)端庫(kù)的構(gòu)建器接口,用于構(gòu)建RestClient實(shí)例;允許你配置與Elasticsearch集群的連接,設(shè)置請(qǐng)求超時(shí),設(shè)置身份驗(yàn)證,配置代理等
RestClientBuilder builder = RestClient.builder(httpHost);
// 連接延時(shí)配置
builder.setRequestConfigCallback(requestConfigBuilder -> {
requestConfigBuilder.setConnectTimeout(connectTimeOut);
requestConfigBuilder.setSocketTimeout(socketTimeOut);
requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
return requestConfigBuilder;
});
// 3. HttpClient 連接數(shù)配置
builder.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.setMaxConnTotal(maxConnectNum);
httpClientBuilder.setMaxConnPerRoute(maxConnectNumPerRoute);
return httpClientBuilder;
});
return builder;
}
}
- 測(cè)試:
web
模塊test
目錄下新建ElasticSearchTest
類(lèi)
@Slf4j
@SpringBootTest
public class ElasticSearchTest {
@Value("${elasticsearch.open}")
// 是否開(kāi)啟ES,默認(rèn)開(kāi)啟
String open = "true";
}
直接連接ES
- 設(shè)置:
ES
Elasticsearch.yml
的xpack.security.enabled
屬性設(shè)置為false
xpack.security.enabled
:
● 默認(rèn)true
:必須使用賬號(hào)連接ES
● 若為false
:必須使用http://localhost:9200/
訪問(wèn)ES
服務(wù)+
啟動(dòng)Kibana
服務(wù)會(huì)失敗+
不需要使用賬號(hào)連接,但必須使用HTTP
連接
- 添加:
ElasticSearchConfig
類(lèi)添加下列方法
/**
* @function: 創(chuàng)建使用http連接來(lái)直接連接ES服務(wù)器的客戶(hù)端
* 如果@Bean沒(méi)有指定bean的名稱(chēng),那么這個(gè)bean的名稱(chēng)就是方法名
*/
@Bean(name = "directConnectionESClient")
public ElasticsearchClient directConnectionESClient(){
RestClientBuilder builder = creatBaseConfBuilder((scheme == "http")?"http":"http");
//Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(builder.build(), new JacksonJsonpMapper());
//And create the API client
ElasticsearchClient esClient = new ElasticsearchClient(transport);
return esClient;
};
- 添加:
ElasticSearchTest
類(lèi)中添加下列代碼---
索引名必須小寫(xiě) - 運(yùn)行:設(shè)置跳過(guò)測(cè)試
--->
手動(dòng)運(yùn)行/不跳過(guò)--->
直接install
,但不運(yùn)行測(cè)試
@Resource(name = "directConnectionESClient")
ElasticsearchClient directConnectionESClient;
@Test
public void directConnectionTest() throws IOException {
if (open.equals("true")) {
//創(chuàng)建索引
CreateIndexResponse response = directConnectionESClient.indices().create(c -> c.index("direct_connection_index"));
log.info(response.toString());
}
else{
log.info("es is closed");
}
}
賬號(hào)密碼連接ES
- 設(shè)置:
ES
Elasticsearch.yml
的xpack.security.enabled
屬性使用默認(rèn)值+
xpack.security.http.ssl.enabled
設(shè)置為false
注意:若
xpack.security.enabled
屬性為false
,則xpack.security.http.ssl.enabled
屬性不生效,即相當(dāng)于設(shè)置為false
;所有以xpack
開(kāi)頭的屬性都不會(huì)生效
ES
Elasticsearch.yml
的xpack.security.http.ssl.enabled
:
● 默認(rèn)true
:必須使用https://localhost:9200/
訪問(wèn)ES
服務(wù)+
啟動(dòng)Kibana
服務(wù)會(huì)成功+
需要使用賬號(hào)連接+
必須使用HTTPS
連接
● 若為false
:必須使用http://localhost:9200/
訪問(wèn)ES
服務(wù)+
啟動(dòng)Kibana
服務(wù)會(huì)失敗+需要使用賬號(hào)連接,但必須使用HTTP連接
- 配置:
dev
目錄application-dal
中添加下列配置
# elasticsearch配置
elasticsearch:
userName: #自己的賬號(hào)名
password: #自己的密碼
- 添加:
ElasticSearchTest
類(lèi)中添加下列代碼---
索引名必須小寫(xiě)+
不能有空格 - 運(yùn)行:設(shè)置跳過(guò)測(cè)試
--->
手動(dòng)運(yùn)行/不跳過(guò)--->
直接install
,但不運(yùn)行測(cè)試
@Resource(name = "accountConnectionESClient")
ElasticsearchClient accountConnectionESClient;
@Test
public void accountConnectionTest() throws IOException {
if (open.equals("true")) {
//創(chuàng)建索引
CreateIndexResponse response = accountConnectionESClient.indices().create(c -> c.index("account_connection_index"));
log.info(response.toString());
}
else{
log.info("es is closed");
}
}
證書(shū)賬號(hào)連接ES
- 設(shè)置:
ES
Elasticsearch.yml
的xpack.security.enabled
和xpack.security.http.ssl.enabled
配置項(xiàng)使用默認(rèn)值
設(shè)置為
true
后,ES
就走https
,若scheme
為http
,則報(bào)Unrecognized
SSLmessage
錯(cuò)誤
- 配置:將
dev
目錄application-dal
中elasticsearch.scheme
配置項(xiàng)改成https
- 證書(shū)添加:終端輸入
keytool -importcert -alias es_https_ca -keystore "D:\computelTool\Java\JDK\JDK21\lib\security\cacerts" -file "D:\computelTool\database\elasticsearch8111\config\certs\http_ca.crt"
keytool -delete -alias es_https_ca -keystore "D:\computelTool\Java\JDK\JDK21\lib\security\cacerts"
---
與上面的命令相反
- 拷貝:將
ESconfig
目錄下certs
目錄下的http_ca.crt
文件拷貝到web
模塊resource
目錄 - 添加:
ElasticSearchConfig
類(lèi)添加下列方法
/**
* @function: 創(chuàng)建用于安全連接(證書(shū) + 賬號(hào))ES服務(wù)器的客戶(hù)端
* 如果@Bean沒(méi)有指定bean的名稱(chēng),那么這個(gè)bean的名稱(chēng)就是方法名
*/
@Bean(name = "accountAndCertificateConnectionESClient")
public ElasticsearchClient accountAndCertificateConnectionESClient() {
RestClientBuilder builder = creatBaseConfBuilder( (scheme == "https")?"https":"https");
// 1.賬號(hào)密碼的配置
//CredentialsProvider: 用于提供 HTTP 身份驗(yàn)證憑據(jù)的接口; 允許你配置用戶(hù)名和密碼,以便在與服務(wù)器建立連接時(shí)進(jìn)行身份驗(yàn)證
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
// 2.設(shè)置自簽證書(shū),并且還包含了賬號(hào)密碼
builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder
.setSSLContext(buildSSLContext())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.setDefaultCredentialsProvider(credentialsProvider));
RestClientTransport transport = new RestClientTransport(builder.build(), new JacksonJsonpMapper());
//And create the API client
ElasticsearchClient esClient = new ElasticsearchClient(transport);
return esClient;
}
private static SSLContext buildSSLContext() {
// 讀取http_ca.crt證書(shū)
ClassPathResource resource = new ClassPathResource("http_ca.crt");
SSLContext sslContext = null;
try {
// 證書(shū)工廠
CertificateFactory factory = CertificateFactory.getInstance("X.509");
Certificate trustedCa;
try (InputStream is = resource.getInputStream()) {
trustedCa = factory.generateCertificate(is);
}
// 密鑰庫(kù)
KeyStore trustStore = KeyStore.getInstance("pkcs12");
trustStore.load(null, "liuxiansheng".toCharArray());
trustStore.setCertificateEntry("ca", trustedCa);
SSLContextBuilder sslContextBuilder = SSLContexts.custom()
.loadTrustMaterial(trustStore, null);
sslContext = sslContextBuilder.build();
} catch (CertificateException | IOException | KeyStoreException | NoSuchAlgorithmException |
KeyManagementException e) {
log.error("ES連接認(rèn)證失敗", e);
}
return sslContext;
}
- 測(cè)試:
ElasticSearchTest
類(lèi)添加
@Resource(name = "accountAndCertificateConnectionESClient")
ElasticsearchClient accountAndCertificateConnectionESClient;
@Test
public void accountAndCertificateConnectionTest() throws IOException {
if (open.equals("true")) {
//創(chuàng)建索引
CreateIndexResponse response = accountAndCertificateConnectionESClient.indices().create(c -> c.index("account_and_certificate_connection_index"));
log.info(response.toString());
System.out.println(response.toString());
}
else{
log.info("es is closed");
}
}
Spring Data ES
公共配置
- 依賴(lài):
web
模塊引入該依賴(lài)---
但其版本必須與你下載的ES
的版本一致
版本:點(diǎn)擊
https://spring.io/projects/spring-data-elasticsearch#learn
,點(diǎn)擊GA
版本的Reference Doc
,點(diǎn)擊version
查看Spring Data ES
與ES
版本的支持關(guān)系
參考:
https://www.yuque.com/itwanger/vn4p17/wslq2t/https://blog.csdn.net/qq_40885085/article/details/105023026
<!-- 若存在Spring Data ES的某個(gè)版本支持你下的ES版本,則使用 -->
<!-- Spring官方在ES官方提供的JAVA環(huán)境使用的依賴(lài)的基礎(chǔ)上做了封裝 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- ESTestEntity用到 -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<!-- ESTestEntity用到 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
<!--provided:指定該依賴(lài)項(xiàng)在編譯時(shí)是必需的,才會(huì)生效, 但在運(yùn)行時(shí)不需要,也不會(huì)生效-->
<!--這樣 Lombok 會(huì)在編譯期靜悄悄地將帶 Lombok 注解的源碼文件正確編譯為完整的 class 文件 -->
</dependency>
- 新建:
we
b模塊TestEntity
目錄新建ESTestEntity
@Data
@EqualsAndHashCode(callSuper = false)
@Document(indexName = "test")
public class ESTestEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long id;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String content;
private String title;
private String excerpt;
}
- 新建:
web
模塊TestRepository
包下新建ESTestRepository
接口
import cn.bytewisehub.pai.web.TestEntity.ESTestEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface ESTestRepository extends ElasticsearchRepository<ESTestEntity, Long> {
}
直接連接ES
- 配置:
ES
Elasticsearch.yml
的xpack.security.enabled
屬性設(shè)置為false
xpack.security.enabled
:
● 默認(rèn)true
:必須使用賬號(hào)連接ES
● 若為false
:必須使用http://localhost:9200/
訪問(wèn)ES
服務(wù)+
啟動(dòng)Kibana
服務(wù)會(huì)失敗+
不需要使用賬號(hào)連接,但必須使用HTTP
連接文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-850077.html
- 配置:
web
模塊dev
目錄application-dal
添加
spring:
elasticsearch:
uris:
- http://127.0.0.1:9200
- 新建:
web
模塊test
目錄新建ElasticsearchTemplateTest
import cn.bytewisehub.pai.web.TestEntity.ESTestEntity;
import cn.bytewisehub.pai.web.TestRepository.ESTestRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
@SpringBootTest
public class ElasticsearchTemplateTest {
@Autowired
ESTestRepository esTestRepository;
@Autowired
ElasticsearchTemplate elasticsearchTemplate;
@Test
void save() {
ESTestEntity esTestEntity = new ESTestEntity();
esTestEntity.setId(1L);
esTestEntity.setContent("不安全連接");
esTestEntity.setTitle("world");
esTestEntity.setExcerpt("test");
System.out.println(elasticsearchTemplate.save(esTestEntity));
}
@Test
void insert() {
ESTestEntity esTestEntity = new ESTestEntity();
esTestEntity.setId(2L);
esTestEntity.setContent("不安全連接");
esTestEntity.setTitle("world");
esTestEntity.setExcerpt("test");
System.out.println(esTestRepository.save(esTestEntity));
}
}
- 訪問(wèn):點(diǎn)擊
http://localhost:9200/test/_search
---
查詢(xún)索引庫(kù)test
HTTP
連接ES
- 配置:
ES
Elasticsearch.yml
的xpack.security.enabled
屬性使用默認(rèn)值+
xpack.security.http.ssl.enabled
設(shè)置為false
ES
Elasticsearch.yml
的xpack.security.http.ssl.enabled
:
● 默認(rèn)true
:必須使用https://localhost:9200/
訪問(wèn)ES
服務(wù)+
啟動(dòng)Kibana
服務(wù)會(huì)成功+
需要使用賬號(hào)連接+
必須使用HTTPS
連接
● 若為false
:必須使用http://localhost:9200/
訪問(wèn)ES
服務(wù)+
啟動(dòng)Kibana
服務(wù)會(huì)失敗+
需要使用賬號(hào)連接,但必須使用HTTP
連接文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-850077.html
- 配置:
web
模塊dev
目錄application-dal
添加
spring:
elasticsearch:
uris:
- http://127.0.0.1:9200
username: # 賬號(hào)用戶(hù)名
password: #賬號(hào)密碼
- 修改:
web
模塊test
目錄下ElasticsearchTemplateTest
修改成這樣,其他參見(jiàn)直接連接
import cn.bytewisehub.pai.web.TestEntity.ESTestEntity;
import cn.bytewisehub.pai.web.TestRepository.ESTestRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
@SpringBootTest
public class ElasticsearchTemplateTest {
@Autowired
ESTestRepository esTestRepository;
@Autowired
ElasticsearchTemplate elasticsearchTemplate;
@Test
void save() {
ESTestEntity esTestEntity = new ESTestEntity();
esTestEntity.setId(1L);
esTestEntity.setContent("不安全連接");
esTestEntity.setTitle("world");
esTestEntity.setExcerpt("test");
System.out.println(elasticsearchTemplate.save(esTestEntity));
}
@Test
void insert() {
ESTestEntity esTestEntity = new ESTestEntity();
esTestEntity.setId(2L);
esTestEntity.setContent("不安全連接");
esTestEntity.setTitle("world");
esTestEntity.setExcerpt("test");
System.out.println(esTestRepository.save(esTestEntity));
}
}
- 訪問(wèn):點(diǎn)擊
http://localhost:9200/test/_search
---
查詢(xún)索引庫(kù)test
HTTPS
連接ES
- 配置:
ES
Elasticsearch.yml
的xpack.security.enabled
和xpack.security.http.ssl.enabled
屬性使用默認(rèn)值 - 配置:
web
模塊dev
目錄application-dal
添加
spring:
elasticsearch:
uris:
- https://127.0.0.1:9200
username: # 賬號(hào)用戶(hù)名
password: #賬號(hào)密碼
- 修改:
web
模塊test
目錄下ElasticsearchTemplateTest
修改成這樣,其他參見(jiàn)直接連接
import cn.bytewisehub.pai.web.TestEntity.ESTestEntity;
import cn.bytewisehub.pai.web.TestRepository.ESTestRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
@SpringBootTest
public class ElasticsearchTemplateTest {
@Autowired
ESTestRepository esTestRepository;
@Autowired
ElasticsearchTemplate elasticsearchTemplate;
@Test
void save() {
ESTestEntity esTestEntity = new ESTestEntity();
esTestEntity.setId(1L);
esTestEntity.setContent("不安全連接");
esTestEntity.setTitle("world");
esTestEntity.setExcerpt("test");
System.out.println(elasticsearchTemplate.save(esTestEntity));
}
@Test
void insert() {
ESTestEntity esTestEntity = new ESTestEntity();
esTestEntity.setId(2L);
esTestEntity.setContent("不安全連接");
esTestEntity.setTitle("world");
esTestEntity.setExcerpt("test");
System.out.println(esTestRepository.save(esTestEntity));
}
}
- 訪問(wèn):點(diǎn)擊
http://localhost:9200/test/_search
---
查詢(xún)索引庫(kù)test
到了這里,關(guān)于SpringBoot3整合Elasticsearch8.x之全面保姆級(jí)教程的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!