Java客戶端通過(guò)HTTPS連接到Easysearch實(shí)現(xiàn)過(guò)程
Easysearch 簡(jiǎn)介
Easysearch 一直致力于提高易用性,這也是我們的核心宗旨,然而之前一直沒(méi)有官方的 Java 客戶端,也對(duì)用戶使用造成了一些困擾,現(xiàn)在,我們正式發(fā)布了第一個(gè) Java 客戶端 Easysearch-client:1.0.1。
這一里程碑式的更新為開發(fā)人員帶來(lái)了前所未有的便利性,使得與 Easysearch 集群的交互變得更加簡(jiǎn)潔和直觀。通過(guò) Easysearch-client,開發(fā)者可以直接使用 Java 方法和數(shù)據(jù)結(jié)構(gòu)來(lái)進(jìn)行交互,而不再需要依賴于傳統(tǒng)的 HTTP 方法和 JSON。
這一變化大大簡(jiǎn)化了操作流程,使得數(shù)據(jù)管理和索引更加高效。Java 客戶端的功能范圍包括處理數(shù)據(jù)操作,管理集群,包括查看和維護(hù)集群的健康狀態(tài),并對(duì) Security 模塊全面兼容。它提供了一系列 API,用于管理角色、用戶、權(quán)限、角色映射和賬戶。
這意味著安全性和訪問(wèn)控制現(xiàn)在可以更加細(xì)粒度地管理,確保了數(shù)據(jù)的安全性和合規(guī)性。
在這篇博客中,你將學(xué)習(xí)如何配置 Easysearch-client 客戶端以通過(guò) HTTPS 連接到 Easysearch。為了演示目的,我將首先設(shè)置一個(gè)帶有 SSL 證書的 Easysearch 服務(wù)器。如果你已經(jīng)有一個(gè)在運(yùn)行,你可以跳過(guò)這一步。
接下來(lái),我將引導(dǎo)你完成在 Java 應(yīng)用程序中配置和使用 Java 客戶端的步驟。
設(shè)置 Easysearch 服務(wù)器
首先從極限科技官網(wǎng)下載最新的 Mac 版本。我使用的是 1.6.1 版本,這是我寫這篇文章時(shí)的最新版本。
wget https://dl-global.infinilabs.com/easysearch/stable/easysearch-1.6.1-214-mac-amd64.zip
確保您的系統(tǒng)已經(jīng)安裝并設(shè)置了 java 環(huán)境變量,版本在 11 以上。
解壓下載文件。
unzip easysearch-1.6.1-214-mac-amd64.zip -d easysearch-1.6.1
cd 到 easysearch-1.6.1 執(zhí)行初始化腳本來(lái)生成證書并自動(dòng)下載插件。
bin/initialize.sh
腳本執(zhí)行后會(huì)自動(dòng)輸出隨機(jī)生成的 admin 用戶密碼。
啟動(dòng) Easysearch
bin/easysearch
此時(shí),您的服務(wù)器已經(jīng)準(zhǔn)備就緒。您可以查看 logs/initialize.log 里顯示的 curl 命令來(lái)進(jìn)行驗(yàn)證。
curl -ku admin:xxxxxxxxx https://localhost:9200
顯示類似的輸出響應(yīng)
{
"name" : "MacBook-Pro.local",
"cluster_name" : "easysearch",
"cluster_uuid" : "1gRYQ6ssTiKGqcyuEN0Dbg",
"version" : {
"distribution" : "easysearch",
"number" : "1.6.1",
"distributor" : "INFINI Labs",
"build_hash" : "14846e460e9976ba6d68c80bb9eca52af1179dcf",
"build_date" : "2023-10-19T14:43:02.636639Z",
"build_snapshot" : false,
"lucene_version" : "8.11.2",
"minimum_wire_lucene_version" : "7.7.0",
"minimum_lucene_index_compatibility_version" : "7.7.0"
},
"tagline" : "You Know, For Easy Search!"
}下面我們來(lái)看如何設(shè)置和使用客戶端。
設(shè)置 Java 客戶端
Easysearch 的 Java 客戶端可在 中央倉(cāng)庫(kù):https://repo1.maven.org/maven2/ 上獲得。將其作為依賴項(xiàng)添加到你的 Java 應(yīng)用程序中。
對(duì)于 Gradle 構(gòu)建系統(tǒng),在項(xiàng)目的 build.gradle 文件中包含以下依賴項(xiàng):
dependencies {
implementation 'com.infinilabs:easysearch-client:1.0.1'
implementation "org.apache.logging.log4j:log4j-api:2.19.0"
implementation "org.apache.logging.log4j:log4j-core:2.19.0"
implementation 'org.apache.httpcomponents:httpclient:4.5.10'
implementation 'org.apache.httpcomponents:httpcore-nio:4.4.12'
implementation 'org.apache.httpcomponents:httpasyncclient:4.1.4'
implementation 'joda-time:joda-time:2.10.4'
implementation ('org.apache.lucene:lucene-core:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-analyzers-common:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-backward-codecs:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-grouping:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-highlighter:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-join:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-memory:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-misc:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-queries:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-queryparser:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-sandbox:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-spatial3d:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-suggest:8.11.2') {
exclude group: '*', module: '*'
}
......
}對(duì)于 Maven 構(gòu)建系統(tǒng),在項(xiàng)目的 pom.xml 文件中包含以下依賴項(xiàng):
<dependencies>
<dependency>
<groupId>com.infinilabs</groupId>
<artifactId>easysearch-client</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore-nio</artifactId>
<version>4.4.12</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.1.4</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.4</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-backward-codecs</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-grouping</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-join</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-memory</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-misc</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queries</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-sandbox</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-spatial3d</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-suggest</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>接下來(lái),在你的 Java 應(yīng)用程序中創(chuàng)建一個(gè) client 實(shí)例,并使用它在 Easysearch 中創(chuàng)建索引并插入一些數(shù)據(jù)。但在此之前,為了使其工作,你需要將簽署服務(wù)器證書的根機(jī)構(gòu)證書添加到你的應(yīng)用程序信任庫(kù)中。讓我們看看如何配置 Java 應(yīng)用程序的信任庫(kù)。
為了使用 java client,你需要將根 CA 證書 ca.crt 添加到應(yīng)用程序信任庫(kù)中。這告訴你的 Java 應(yīng)用程序信任由此根機(jī)構(gòu)簽署的任何證書。easysearch-1.6.1/config/ 目錄下已經(jīng)生成了 ca.crt 文件。你可以將其添加到自定義信任庫(kù)中,并在 Java 應(yīng)用程序中使用該自定義信任庫(kù)。
使用 Java keytool 創(chuàng)建一個(gè)自定義信任庫(kù)并導(dǎo)入證書。keytool 不理解 .pem 格式,所以你需要首先使用 openssl 加密庫(kù)將證書轉(zhuǎn)換為 .der 格式,然后使用 Java keytool 將其添加到自定義信任庫(kù)中。假設(shè)您的操作系統(tǒng)已經(jīng)預(yù)裝了 openssl。
第 1 步:將 CA 證書從 .pem 格式轉(zhuǎn)換為 .der 格式。
openssl x509 -in easysearch-1.6.1/config/ca.crt -inform pem -out ca.der --outform der
第 2 步:創(chuàng)建自定義信任庫(kù)并添加 ca.der 證書。將 ca 證書添加到應(yīng)用程序信任庫(kù)中,表示應(yīng)用程序信任由此 CA 簽署的任何證書。
keytool -import -file ca.der -alias easysearch -keystore myTrustStore
過(guò)程中會(huì)提示您輸入密鑰庫(kù)口令: 我為了測(cè)試用輸入的 123456。
通過(guò)列出信任庫(kù)中的證書來(lái)確認(rèn)操作成功,這里的 123456 是我上面設(shè)置的密碼,會(huì)顯示出 easysearch 證書。
keytool -keystore myTrustStore -storepass 123456 -list
第 3 步:在 Java 應(yīng)用程序代碼中設(shè)置指向自定義信任庫(kù)的系統(tǒng)屬性,并連接集群,創(chuàng)建索引,插入數(shù)據(jù)。可以通過(guò)設(shè)置系統(tǒng)屬性,以指定 SSL/TLS 通信時(shí)使用的信任庫(kù):
System.setProperty("javax.net.ssl.trustStore", "/full/path/to/myTrustStore");
System.setProperty("javax.net.ssl.trustStorePassword", "123456");
HttpHost[] httpHostArray = new HttpHost[1];
// infini.cloud 和 CN=infini.cloud 保持一致
httpHostArray[0] = new HttpHost("infini.cloud", 9200, "https");
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "1933791fb2b9f6c6146d"));
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(httpHostArray)
.setHttpClientConfigCallback((HttpAsyncClientBuilder httpAsyncClientBuilder) -> {
httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
return httpAsyncClientBuilder;
}));
CreateIndexRequest createIndexRequest = new CreateIndexRequest("test-index");
createIndexRequest.settings(Settings.builder()
.put("index.number_of_shards", 1)
.put("index.number_of_replicas", 1)
);
//Create index
client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
// Bulk
BulkRequest bulkRequest = new BulkRequest();
for (int i = 0; i < 10; i++) {
IndexRequest indexRequest = new IndexRequest("test-index")
.id(Integer.toString(i))
.source("{\"field1\":\"value" + i + "\"}", XContentType.JSON);
bulkRequest.add(indexRequest);
}
BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(Strings.toString(bulkResponse));信任已簽署 Easysearch 正在使用的證書的 CA 的示例,當(dāng) CA 證書以 PEM 編碼文件的形式可用時(shí):
Path caCertificatePath = Paths.get("/easysearch-test/easysearch-1.6.1/config/ca.crt");
CertificateFactory factory = CertificateFactory.getInstance("X.509");
Certificate trustedCa;
try (InputStream is = Files.newInputStream(caCertificatePath)) {
trustedCa = factory.generateCertificate(is);
}
KeyStore trustStore = KeyStore.getInstance("pkcs12");
trustStore.load(null, null);
trustStore.setCertificateEntry("ca", trustedCa);
SSLContextBuilder sslContextBuilder = SSLContexts.custom()
.loadTrustMaterial(trustStore, null);
final SSLContext sslContext = sslContextBuilder.build();
HttpHost[] httpHostArray = new HttpHost[1];
httpHostArray[0] = new HttpHost("infini.cloud", 9200, "https");
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "1933791fb2b9f6c6146d"));
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(httpHostArray)
.setHttpClientConfigCallback((HttpAsyncClientBuilder httpAsyncClientBuilder) -> {
httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
httpAsyncClientBuilder.setSSLContext(sslContext);
return httpAsyncClientBuilder;
}));現(xiàn)在,您已經(jīng)成功設(shè)置了 Java 客戶端,并以安全的 HTTPS 通道連接到了 Easysearch 集群。除此之外,Java 客戶端還具備強(qiáng)大的權(quán)限控制管理 API,具體請(qǐng)參考我們的官網(wǎng)文檔:https://www.infinilabs.com/docs/latest/easysearch/references/client/security/
關(guān)于 Easysearch

INFINI Easysearch 是一個(gè)分布式的近實(shí)時(shí)搜索與分析引擎,核心引擎基于開源的 Apache Lucene。Easysearch 的目標(biāo)是提供一個(gè)輕量級(jí)的 Elasticsearch 可替代版本,并繼續(xù)完善和支持更多的企業(yè)級(jí)功能。 與 Elasticsearch 相比,Easysearch 更關(guān)注在搜索業(yè)務(wù)場(chǎng)景的優(yōu)化和繼續(xù)保持其產(chǎn)品的簡(jiǎn)潔與易用性。
官網(wǎng)文檔:https://www.infinilabs.com/docs/latest/easysearch
下載地址:https://www.infinilabs.com/download
以上就是Java客戶端通過(guò)HTTPS連接到Easysearch實(shí)現(xiàn)過(guò)程的詳細(xì)內(nèi)容,更多關(guān)于Java HTTPS連接Easysearch的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于FeignClient調(diào)用超時(shí)的處理方案
這篇文章主要介紹了基于FeignClient調(diào)用超時(shí)的處理方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java List簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Java中可變數(shù)組的原理就是不斷的創(chuàng)建新的數(shù)組,將原數(shù)組加到新的數(shù)組中,下文對(duì)Java List用法做了詳解。需要的朋友參考下吧2017-05-05
Java中斷言(assert)的使用場(chǎng)景與注意事項(xiàng)
文章詳細(xì)介紹了Java斷言(assert)的使用方法、適用場(chǎng)景及注意事項(xiàng),斷言是一種調(diào)試工具,用于開發(fā)和測(cè)試階段快速定位代碼邏輯錯(cuò)誤,但非業(yè)務(wù)級(jí)校驗(yàn),不應(yīng)用于對(duì)外接口、業(yè)務(wù)容錯(cuò)等場(chǎng)景,應(yīng)嚴(yán)格區(qū)分?jǐn)嘌耘c常規(guī)異常,避免濫用,確保代碼質(zhì)量和生產(chǎn)環(huán)境穩(wěn)定性2026-05-05
使用spring stream發(fā)送消息代碼實(shí)例
這篇文章主要介紹了使用spring stream發(fā)送消息代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
Java基于外觀模式實(shí)現(xiàn)美食天下食譜功能實(shí)例詳解
這篇文章主要介紹了Java基于外觀模式實(shí)現(xiàn)美食天下食譜功能,較為詳細(xì)的講述了外觀模式的概念、原理并結(jié)合實(shí)例形似詳細(xì)分析了Java基于外觀模式實(shí)現(xiàn)美食天下食譜功能的具體操作步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2018-05-05
java實(shí)現(xiàn)水仙花數(shù)的計(jì)算
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)水仙花數(shù)的計(jì)算,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
Spring?Cloud?Ribbon?負(fù)載均衡使用策略示例詳解
Spring?Cloud?Ribbon?是基于Netflix?Ribbon?實(shí)現(xiàn)的一套客戶端負(fù)載均衡工具,Ribbon客戶端組件提供了一系列的完善的配置,如超時(shí),重試等,這篇文章主要介紹了Spring?Cloud?Ribbon?負(fù)載均衡使用策略示例詳解,需要的朋友可以參考下2023-03-03

