JavaDB 整合 ElasticSearch
Java 提供了一个 Elastic
类,用于简化与 ElasticSearch 的整合过程以及执行相关的 ES 操作。本文档将介绍如何在 Java 项目中通过配置类来实现与 ElasticSearch 的整合。
使用 JavaDB 整合 ElasticSearch
添加依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.10.0</version>
</dependency>
<dependency>
<groupId>com.litongjava</groupId>
<artifactId>java-db</artifactId>
<version>{java-db.version}</version>
</dependency>
配置文件 (app.properties
)
首先,在项目的配置文件中定义 ElasticSearch 的连接信息:
ELASTICSEARCH_DSN=http://elastic:YourElasticPassword@192.168.1.2:9200
配置类 (ElasticSearchConfig.java
)
接下来,编写配置类来初始化 ElasticSearch 客户端并将其与应用程序集成:
import java.io.IOException;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import com.litongjava.es.client.Elastic;
import com.litongjava.es.client.EsDSNUtils;
import com.litongjava.es.client.EsDSN;
import com.litongjava.jfinal.aop.annotation.AConfiguration;
import com.litongjava.jfinal.aop.annotation.AInitialization;
import com.litongjava.tio.boot.server.TioBootServer;
import com.litongjava.tio.utils.environment.EnvUtils;
import com.litongjava.tio.utils.hutool.StrUtil;
import lombok.extern.slf4j.Slf4j;
@AConfiguration
@Slf4j
public class ElasticSearchConfig {
@Initialization
public void config() {
// 从环境变量或配置文件中读取 DSN
String dsn = EnvUtils.getStr("ELASTICSEARCH_DSN");
if (StrUtil.isEmpty(dsn)) {
log.error("ElasticSearch DSN 未设置");
return;
}
// 解析 DSN
EsDSN esDSN = EsDSNUtils.parse(dsn);
// 创建 HttpHost 实例
HttpHost httpHost = new HttpHost(esDSN.getHost(), esDSN.getPort(), esDSN.getSchema());
// 配置 RestClientBuilder
RestClientBuilder builder = RestClient.builder(httpHost);
String username = esDSN.getUsername();
String password = esDSN.getPassword();
if (StrUtil.isNotEmpty(username)) {
// 设置凭证提供者
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
builder.setHttpClientConfigCallback(httpClientBuilder ->
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
}
// 创建 RestHighLevelClient
RestHighLevelClient client = new RestHighLevelClient(builder);
// 将客户端添加到 Elastic 管理类
Elastic.setClient(client);
// 确保在关闭应用时关闭客户端
TioBootServer.me().addDestroyMethod(() -> {
try {
client.close();
} catch (IOException e) {
log.error("关闭 ElasticSearch 客户端时出错", e);
}
});
// 测试连接状态
try {
boolean isConnected = client.ping(RequestOptions.DEFAULT);
log.info("ElasticSearch 连接状态: {}", isConnected);
} catch (IOException e) {
log.error("无法连接到 ElasticSearch", e);
}
}
}
说明
- 配置文件:通过
app.properties
文件中的ELASTICSEARCH_DSN
来指定 ElasticSearch 的连接信息,包括协议、用户名、密码和主机地址。 - 配置类:
ElasticSearchConfig
类负责读取配置文件中的 DSN,解析连接信息,并初始化RestHighLevelClient
。该客户端将被设置到Elastic
类中进行管理。 - 资源管理:在应用关闭时,通过
TioBootServer
的addDestroyMethod
方法确保客户端被正确关闭。 - 连接测试:配置类在初始化过程中会测试 ElasticSearch 的连接状态,并将结果记录到日志中。
通过以上步骤,Java 应用程序就可以方便地与 ElasticSearch 整合,实现对 ElasticSearch 的各种操作。
SpringBoot 使用 Java-Db 整合 ES
添加依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.10.0</version>
</dependency>
<dependency>
<groupId>com.litongjava</groupId>
<artifactId>java-db</artifactId>
<version>{java-db.version}</version>
</dependency>
添加配置类
使用 Spring-boot 的 PostConstruct 注解
import java.io.IOException;
import javax.annotation.PostConstruct;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Configuration;
import com.litongjava.es.client.Elastic;
import com.litongjava.es.client.EsDSN;
import com.litongjava.es.client.EsDSNUtils;
import com.litongjava.jfinal.aop.annotation.AConfiguration;
import com.litongjava.jfinal.aop.annotation.AInitialization;
import com.litongjava.tio.utils.environment.EnvUtils;
import com.litongjava.tio.utils.hutool.StrUtil;
import lombok.extern.slf4j.Slf4j;
@AConfiguration
@Slf4j
@Configuration
public class ElasticSearchConfig {
@Initialization
@PostConstruct
public void config() {
// 从环境变量或配置文件中读取DSN
String dsn = EnvUtils.getStr("ELASTICSEARCH_DSN");
if (dsn == null || dsn.isEmpty()) {
System.out.println("DSN is not set");
return;
}
// 解析DSN
EsDSN esInfo = EsDSNUtils.parse(dsn);
// 创建 HttpHost 实例
HttpHost httpHost = new HttpHost(esInfo.getHost(), esInfo.getPort(), esInfo.getSchema());
// 配置 RestClientBuilder
RestClientBuilder builder = RestClient.builder(httpHost);
String username = esInfo.getUsername();
String password = esInfo.getPassword();
if (StrUtil.isNotEmpty(username)) {
// 设置凭证提供者
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
}
// 创建 RestHighLevelClient
RestHighLevelClient client = new RestHighLevelClient(builder);
// 添加到EsClient
Elastic.setClient(client);
// 测试连接状态
try {
log.info("ping es server:{}", client.ping(RequestOptions.DEFAULT));
} catch (IOException e) {
e.printStackTrace();
}
}
}