Java客戶端通過HTTPS連接到Easysearch實現(xiàn)過程
Easysearch 簡介
Easysearch 一直致力于提高易用性,這也是我們的核心宗旨,然而之前一直沒有官方的 Java 客戶端,也對用戶使用造成了一些困擾,現(xiàn)在,我們正式發(fā)布了第一個 Java 客戶端 Easysearch-client:1.0.1。
這一里程碑式的更新為開發(fā)人員帶來了前所未有的便利性,使得與 Easysearch 集群的交互變得更加簡潔和直觀。通過 Easysearch-client,開發(fā)者可以直接使用 Java 方法和數(shù)據(jù)結構來進行交互,而不再需要依賴于傳統(tǒng)的 HTTP 方法和 JSON。
這一變化大大簡化了操作流程,使得數(shù)據(jù)管理和索引更加高效。Java 客戶端的功能范圍包括處理數(shù)據(jù)操作,管理集群,包括查看和維護集群的健康狀態(tài),并對 Security 模塊全面兼容。它提供了一系列 API,用于管理角色、用戶、權限、角色映射和賬戶。
這意味著安全性和訪問控制現(xiàn)在可以更加細粒度地管理,確保了數(shù)據(jù)的安全性和合規(guī)性。
在這篇博客中,你將學習如何配置 Easysearch-client 客戶端以通過 HTTPS 連接到 Easysearch。為了演示目的,我將首先設置一個帶有 SSL 證書的 Easysearch 服務器。如果你已經(jīng)有一個在運行,你可以跳過這一步。
接下來,我將引導你完成在 Java 應用程序中配置和使用 Java 客戶端的步驟。
設置 Easysearch 服務器
首先從極限科技官網(wǎng)下載最新的 Mac 版本。我使用的是 1.6.1 版本,這是我寫這篇文章時的最新版本。
wget https://dl-global.infinilabs.com/easysearch/stable/easysearch-1.6.1-214-mac-amd64.zip
確保您的系統(tǒng)已經(jīng)安裝并設置了 java 環(huán)境變量,版本在 11 以上。
解壓下載文件。
unzip easysearch-1.6.1-214-mac-amd64.zip -d easysearch-1.6.1
cd 到 easysearch-1.6.1 執(zhí)行初始化腳本來生成證書并自動下載插件。
bin/initialize.sh
腳本執(zhí)行后會自動輸出隨機生成的 admin 用戶密碼。
啟動 Easysearch
bin/easysearch
此時,您的服務器已經(jīng)準備就緒。您可以查看 logs/initialize.log 里顯示的 curl 命令來進行驗證。
curl -ku admin:xxxxxxxxx https://localhost:9200
顯示類似的輸出響應
{ "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!" }
下面我們來看如何設置和使用客戶端。
設置 Java 客戶端
Easysearch 的 Java 客戶端可在 中央倉庫:https://repo1.maven.org/maven2/ 上獲得。將其作為依賴項添加到你的 Java 應用程序中。
對于 Gradle 構建系統(tǒng),在項目的 build.gradle 文件中包含以下依賴項:
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: '*' } ...... }
對于 Maven 構建系統(tǒng),在項目的 pom.xml 文件中包含以下依賴項:
<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>
接下來,在你的 Java 應用程序中創(chuàng)建一個 client 實例,并使用它在 Easysearch 中創(chuàng)建索引并插入一些數(shù)據(jù)。但在此之前,為了使其工作,你需要將簽署服務器證書的根機構證書添加到你的應用程序信任庫中。讓我們看看如何配置 Java 應用程序的信任庫。
為了使用 java client,你需要將根 CA 證書 ca.crt 添加到應用程序信任庫中。這告訴你的 Java 應用程序信任由此根機構簽署的任何證書。easysearch-1.6.1/config/ 目錄下已經(jīng)生成了 ca.crt 文件。你可以將其添加到自定義信任庫中,并在 Java 應用程序中使用該自定義信任庫。
使用 Java keytool 創(chuàng)建一個自定義信任庫并導入證書。keytool 不理解 .pem 格式,所以你需要首先使用 openssl 加密庫將證書轉(zhuǎn)換為 .der 格式,然后使用 Java keytool 將其添加到自定義信任庫中。假設您的操作系統(tǒng)已經(jīng)預裝了 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)建自定義信任庫并添加 ca.der 證書。將 ca 證書添加到應用程序信任庫中,表示應用程序信任由此 CA 簽署的任何證書。
keytool -import -file ca.der -alias easysearch -keystore myTrustStore
過程中會提示您輸入密鑰庫口令: 我為了測試用輸入的 123456。
通過列出信任庫中的證書來確認操作成功,這里的 123456 是我上面設置的密碼,會顯示出 easysearch 證書。
keytool -keystore myTrustStore -storepass 123456 -list
第 3 步:在 Java 應用程序代碼中設置指向自定義信任庫的系統(tǒng)屬性,并連接集群,創(chuàng)建索引,插入數(shù)據(jù)。可以通過設置系統(tǒng)屬性,以指定 SSL/TLS 通信時使用的信任庫:
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 的示例,當 CA 證書以 PEM 編碼文件的形式可用時:
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)成功設置了 Java 客戶端,并以安全的 HTTPS 通道連接到了 Easysearch 集群。除此之外,Java 客戶端還具備強大的權限控制管理 API,具體請參考我們的官網(wǎng)文檔:https://www.infinilabs.com/docs/latest/easysearch/references/client/security/
關于 Easysearch
INFINI Easysearch 是一個分布式的近實時搜索與分析引擎,核心引擎基于開源的 Apache Lucene。Easysearch 的目標是提供一個輕量級的 Elasticsearch 可替代版本,并繼續(xù)完善和支持更多的企業(yè)級功能。 與 Elasticsearch 相比,Easysearch 更關注在搜索業(yè)務場景的優(yōu)化和繼續(xù)保持其產(chǎn)品的簡潔與易用性。
官網(wǎng)文檔:https://www.infinilabs.com/docs/latest/easysearch
下載地址:https://www.infinilabs.com/download
以上就是Java客戶端通過HTTPS連接到Easysearch實現(xiàn)過程的詳細內(nèi)容,更多關于Java HTTPS連接Easysearch的資料請關注腳本之家其它相關文章!
相關文章
淺談Java中OutOfMemoryError問題產(chǎn)生原因
本文主要介紹了淺談Java中OutOfMemoryError問題產(chǎn)生原因,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06Java實現(xiàn)短信驗證碼和國際短信群發(fā)功能的示例
本篇文章主要介紹了Java實現(xiàn)短信驗證碼和國際短信群發(fā)功能的示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02WebSocket+Vue+SpringBoot實現(xiàn)語音通話的使用示例
本文主要介紹了WebSocket+Vue+SpringBoot實現(xiàn)語音通話的使用示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-11-11詳細分析Java并發(fā)集合ArrayBlockingQueue的用法
這篇文章主要介紹了詳細分析Java并發(fā)集合ArrayBlockingQueue的用法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04