通过 SSH 隧道连接到 Elasticsearch
Posted
技术标签:
【中文标题】通过 SSH 隧道连接到 Elasticsearch【英文标题】:Connect to Elasticsearch through an SSH tunnel 【发布时间】:2021-11-13 04:44:27 【问题描述】:我正在尝试创建一个 SSH 会话并连接到 EC2 实例中的 elasticsearch。我正在使用此代码来执行此操作,但它失败了
java.util.concurrent.ExecutionException: java.net.ConnectException: Connection refused
ElasticsearchException[]; nested: ExecutionException[java.net.ConnectException: Connection refused]; nested: ConnectException[Connection refused];
at org.elasticsearch.client.RestHighLevelClient.performClientRequest(RestHighLevelClient.java:2078)
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1732)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1717)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1684)
at org.elasticsearch.client.LicenseClient.getBasicStatus(LicenseClient.java:191)
I am trying to create an SSH tunnel and connect to elasticsearch that is in EC2 instance. I am using this code to do that but it is failing with
java.util.concurrent.ExecutionException: java.net.ConnectException: Connection refused
ElasticsearchException[]; nested: ExecutionException[java.net.ConnectException: Connection refused]; nested: ConnectException[Connection refused];
at org.elasticsearch.client.RestHighLevelClient.performClientRequest(RestHighLevelClient.java:2078)
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1732)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1717)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1684)
at org.elasticsearch.client.LicenseClient.getBasicStatus(LicenseClient.java:191)
这是我正在使用的代码:
Session session = null;
String sshKeyFile = "/path/testSSH.pem";
try
JSch jsc = new JSch();
jsc.addIdentity(sshKeyFile);
session = jsc.getSession("user", "ec2-instance", 22);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
int i = session.setPortForwardingL(30000, "localhost", 22, null);
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user_name", "pass"));
RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200));
builder.setHttpClientConfigCallback(a -> a.setDefaultCredentialsProvider(credentialsProvider));
RestHighLevelClient client = new RestHighLevelClient(builder);
GetBasicStatusResponse response = client.license().getBasicStatus(RequestOptions.DEFAULT);
response.isEligibleToStartBasic();
System.out.println(response.toString());
catch (JSchException e)
e.printStackTrace();
可能我错过了什么。请在这里提供任何帮助。
【问题讨论】:
【参考方案1】:通常当出现Connection refused
异常时,表示:
您是说 Elasticsearch 安装在 EC2 实例中,但在您的代码中,连接连接到端口 9200
上的 localhost
。我假设您在本地机器上运行这段代码,并且您假设连接到 EC2 实例的主机,而不是 localhost。
【讨论】:
我在我的 EC2 实例上打开了一个 ssh 会话,Localhost 是 EC2 实例中连接到 Elasticsearch 的主机。创建 ssh 隧道后,如何调用 ES api 连接到运行在 EC2 中的 ES?以上是关于通过 SSH 隧道连接到 Elasticsearch的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 JDBC 连接通过 SSH 隧道连接到 Azure DB
Robot Framework:通过 SSH 隧道连接到 PostgreSQL 数据库
通过 SSH 隧道连接到 .Net Core 中的 AWS DocumentDb