連接外部應用程式
您的應用程式和驅動程式使用連接字串來建立連接IBM Cloud® Databases for Elasticsearch。 每一個部署都有專用的驅動程式和應用程式連線字串。 連接字串顯示在端點您的部署面板概述頁面,也可以從 雲端資料庫 CLI 插件,以及 應用程式介面。
您在部署中建立的任何認證都可以使用連線字串。 雖然您可以將管理使用者用於所有連線及應用程式,但最好特別建立使用者,讓您的應用程式可以連接。 有關更多信息,請參閱 取得連接字串。
使用語言的驅動程式連接
驅動程式連接到您的部署所需的所有資訊都位於在服務憑證頁。 表格包含參照的明細。
| 欄位名稱 | 索引 | 說明 |
|---|---|---|
Type |
連接類型 - 用於Elasticsearch,它是“uri”。 | |
Scheme |
URI 方案 - 用於Elasticsearch,它是“https”。 | |
Path |
uri 的路徑。 | |
Authentication |
Username |
您用來連接的使用者名稱。 |
Authentication |
Password |
使用者的密碼-可能顯示為 $PASSWORD。 |
Authentication |
Method |
如何進行鑑別; 由驅動程式處理「直接」鑑別。 |
Hosts |
0... |
要連接的主機名稱及埠。 |
Composed |
0... |
結合 Scheme、Authentication、Host 及 Path 的 URI。 |
Certificate |
Name |
用於資料庫部署的服務專屬憑證的已分配名稱。 |
Certificate |
基數 64 | 憑證的 base64 編碼版本。 |
0...表示陣列中可能有一或多個這些項目。
當在連線資訊的 "composed" 欄位中找到 URI 格式的連線字串時,許多 Elasticsearch 驅動程式都可以建立與部署的連線。 請參閱下列範例:
https://admin:$PASSWORD@d5eeee66-5bc4-498a-b73b-1307848f1eac.8f7bfd8f3faa4218aec56e069eb46187.databases.appdomain.cloud:31821
下列範例使用 Java 來連接。
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.client.JestClient;
import io.searchbox.cluster.Health;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.ssl.SSLContextBuilder;
import javax.net.ssl.SSLContext;
import java.io.File;
import java.security.*;
import java.security.cert.CertificateException;
import java.io.IOException;
public class ESConnect {
public static void main(String[] args) {
// Add CA cert to truststore using something like:
// keytool -import -alias mycert -file /path/to/cert -keystore ./mycert -storetype pkcs12 -storepass mysecret
// and use the path of the keystore below
File truststore = new File("/Users/code/java-example/icdcerts");
try {
// use your secret and add the variable containing the truststore that you created above with the secret as a CharArray
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(truststore, "mysecret".toCharArray()).build();
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);
// set up a Jest factory
JestClientFactory factory = new JestClientFactory();
//configure and build Jest HTTP client with IBM Cloud Databases for Elasticsearch connection strings
factory.setHttpClientConfig(
// add the Elasticsearch host and port
new HttpClientConfig.Builder("https://60d1b41b-2478-4767-9fc0-d99b1d00b6d1.bkvfu0nd0m8k95k94ujg.databases.appdomain.cloud:31347")
.multiThreaded(true)
// Add the credentials username and password
.defaultCredentials("admin", "mypassword")
.sslSocketFactory(sslSocketFactory)
.build());
// create a JestClient
JestClient client = factory.getObject();
// create the call for the cluster health
Health health = new Health.Builder().build();
// get the cluster health as a JestResult
JestResult result = client.execute(health);
// print out the cluster's health
System.out.printf("\n\n<------ CLUSTER HEALTH ------>\n%s\n\n", result.getJsonObject());
// shutdown the connection
client.close();
} catch (IOException | KeyStoreException | NoSuchAlgorithmException | KeyManagementException | CertificateException e) {
e.printStackTrace();
}
}
}
下列範例使用 Python 程式庫 elasticsearch-py 來連接。
from elasticsearch import Elasticsearch
from ssl import create_default_context
context = create_default_context(cafile="path/to/cert.pem")
es = Elasticsearch(
['60d1b41b-2478-4767-9fc0-d99b1d00b6d1.bkvfu0nd0m8k95k94ujg.databases.appdomain.cloud'],
http_auth=('admin', 'password'),
port='31347',
ssl_context=context
)
health = es.cluster.health()
print(health)
驅動程式 TLS 和服務專屬憑證支援
Databases for Elasticsearch 的所有連線都已啟用 TLS 1.2,因此您用來連接的驅動程式需要能夠支援加密。 您的部署還隨附服務專屬憑證,因此驅動程式可以在連線時驗證伺服器。
如需相關資訊,請參閱 Cloud Databases 憑證常見問題(FAQ)。
使用服務專屬憑證
- 複製證書資訊_端點_面板或Base64服務憑證連接資訊欄位。
- 必要的話,將 Base64 字串解碼為文字。
- 將憑證儲存至檔案。 (您可以使用所提供的「名稱」或您自己的檔名)。
- 提供驅動程式或用戶端的憑證路徑。
CLI 外掛程式支援服務專屬憑證
您可以使用指令 ibmcloud cdb deployment-cacert "your-service-name",使用 CLI 外掛程式來顯示部署的已解碼憑證。 它會將 base64 解碼成文字。 將指令的輸出複製並儲存至檔案,並提供檔案的驅動程式路徑。
其他司機
Elasticsearch 擁有大量的語言驅動程式。 此表格涵蓋一些最常見的語言驅動程式。
| 語言 | 驅動程式 | 文件 |
|---|---|---|
| Node | elasticsearch-js |
鏈結 |
| Ruby | elasticsearch-ruby |
鏈結 |
| Ruby on Rails | elasticsearch-rails | 鏈結 |
| Python | elasticsearch-py |
鏈結 |
| Java | Jest |
鏈結 |
| 進行 | elastic |
鏈結 |