I want to connect company's elasticsearch.
Version
elasticsearch : 8.5.3
quarkus : 3.5.1
maven : 3.9.5
JAVA : 17.0.9
I clone from quarkus-quickstarts
Here is my code
// forTestSllPassword
private String keyStorePass = "test12345";
// 建立一個ssl憑證,Create SSL context
public SSLContext CreateSSL() {
SSLContext test = null;
try {
Path trustStorePath = Paths.get("src/main/resources/client.p12");
// 設定類型,官方文件指定要 pkcs12
KeyStore truststore = KeyStore.getInstance("pkcs12");
// 讀取信任檔
try (InputStream is = Files.newInputStream(trustStorePath)) {
truststore.load(is, keyStorePass.toCharArray());
System.out.println(is);
}
// 建立SSL憑證
SSLContextBuilder sslBuilder = SSLContexts.custom()
.loadTrustMaterial(truststore, null);
final SSLContext sslContext = sslBuilder.build();
test = sslContext;
} catch (KeyStoreException | IOException | NoSuchAlgorithmException | java.security.cert.CertificateException | KeyManagementException e) {
e.printStackTrace(); // 處理例外
}
return test;
}
@Inject
// create restClient and set SSL into it
RestClient restClient= RestClient.builder(
new HttpHost("myHost", 9200, "https"))
.setHttpClientConfigCallback(new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(
HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setSSLContext(CreateSSL());
}
})
.build();
// use demo is connect elastic
private List<Fruit> search(String term, String match) throws IOException {
System.out.println(CreateSSL());
// 建立搜尋語句
Request request = new Request(
"GET",
"/");
// 設定進去client裡面
Response response = restClient.performRequest(request);
// 搜尋並取得結果
String responseBody = EntityUtils.toString(response.getEntity());
// 解析查到的資料
List<Fruit> results = new ArrayList<>(1);
return results;
}
application.properties
# 啟用ssl, enable ssl
quarkus.ssl.native=true
# 設定elasticsearch設定, set user and host
quarkus.elasticsearch.hosts = myHost:9200
quarkus.elasticsearch.username = 'elastic'
quarkus.elasticsearch.password = 'myPassword'
quarkus.elasticsearch.protocol = 'https'
#設定ssl,設定port, set port and ssl
quarkus.http.port=8443
quarkus.http.ssl.protocols=TLSv1.3,TLSv1.2
quarkus.http.ssl.certificate.key-store-file-type=JKS
quarkus.http.ssl.certificate.key-files=keystore.jks
quarkus.http.ssl.certificate.key-store-password=test12345
quarkus.http.ssl-port=443
But when I test,an error occured
java.io.IOException: 'https' protocol is not supported
quarkus notes that https will be auto opened when using elasticsearch restclient
After I dissable
quarkus.elasticsearch.protocol
I got this error
org.apache.http.ConnectionClosedException: Connection is closed
Can someone help resolve this question?Thanks