This commit is contained in:
xuelijun
2025-04-19 14:50:56 +08:00
parent 344067d314
commit 98f55d2d09
8 changed files with 267 additions and 0 deletions

View File

@@ -26,6 +26,7 @@
<module>tashow-data-mybatis</module>
<module>tashow-data-redis</module>
<module>tashow-data-excel</module>
<module>tashow-data-es</module>
</modules>

View File

@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.tashow.cloud</groupId>
<artifactId>tashow-framework</artifactId>
<version>${revision}</version>
</parent>
<artifactId>tashow-data-es</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>es 封装拓展</description>
<dependencies>
<!-- Spring Data Elasticsearch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version> <!-- 最新版本 -->
</dependency>
<!-- 芋道基础依赖 -->
<dependency>
<groupId>com.tashow.cloud</groupId>
<artifactId>tashow-common</artifactId>
</dependency>
<!-- 其他必要依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,61 @@
package com.tashow.cloud.es.config;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import javax.annotation.PreDestroy;
@Slf4j
@AutoConfiguration
public class ElasticsearchAutoConfiguration {
private RestClient restClient;
@Bean
public ElasticsearchClient elasticsearchClient(ElasticsearchProperties properties) {
// 1. 构建 HTTP 主机数组
HttpHost[] hosts = properties.getUris().stream()
.map(uri -> {
if (!uri.startsWith("http")) {
throw new IllegalArgumentException("URI 必须包含协议 (http/https)");
}
return HttpHost.create(uri);
})
.toArray(HttpHost[]::new);
// 2. 创建低级 REST 客户端 (无认证)
this.restClient = RestClient.builder(hosts)
.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
.setConnectTimeout(properties.getConnectTimeout())
.setSocketTimeout(properties.getSocketTimeout()))
.build();
// 3. 创建 Transport 层
ElasticsearchTransport transport = new RestClientTransport(
restClient,
new JacksonJsonpMapper() // 使用 Jackson 处理 JSON
);
log.info("[Elasticsearch] 客户端初始化完成,节点: {}", properties.getUris());
return new ElasticsearchClient(transport);
}
@PreDestroy
public void destroy() {
if (restClient != null) {
try {
restClient.close();
log.info("[Elasticsearch] 客户端已关闭");
} catch (Exception e) {
log.error("[Elasticsearch] 客户端关闭异常", e);
}
}
}
}

View File

@@ -0,0 +1,31 @@
/*
package com.tashow.cloud.es.config;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticsearchConfig {
@Bean
public ElasticsearchClient elasticsearchClient() {
// 创建低级客户端
RestClient restClient = RestClient.builder(
new HttpHost("43.139.42.137", 9200)
).build();
// 使用 Jackson 映射器创建传输层
ElasticsearchTransport transport = new RestClientTransport(
restClient, new JacksonJsonpMapper()
);
// 创建高级客户端
return new ElasticsearchClient(transport);
}
}*/

View File

@@ -0,0 +1,65 @@
/*
package com.tashow.cloud.es.config;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.extern.slf4j.Slf4j;
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.RestClient;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import java.util.Arrays;
@Slf4j
@AutoConfiguration
@EnableConfigurationProperties(ElasticsearchProperties.class)
public class ElasticsearchConfigTest {
@Bean
public ElasticsearchClient elasticsearchClient(ElasticsearchProperties properties) {
// 1. 创建低级 REST 客户端
RestClient restClient = RestClient.builder(buildHttpHosts(properties))
.setHttpClientConfigCallback(httpClientBuilder -> {
// 认证配置
if (properties.getUsername() != null) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(properties.getUsername(), properties.getPassword())
);
httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
}
return httpClientBuilder;
})
.build();
// 2. 创建 Transport 层
ElasticsearchTransport transport = new RestClientTransport(
restClient,
new JacksonJsonpMapper() // 使用 Jackson 处理 JSON
);
// 3. 返回新客户端
return new ElasticsearchClient(transport);
}
private HttpHost[] buildHttpHosts(ElasticsearchProperties properties) {
return Arrays.stream(properties.getUris())
.map(uri -> {
try {
return HttpHost.create(uri);
} catch (Exception e) {
throw new IllegalArgumentException("Invalid Elasticsearch URI: " + uri, e);
}
})
.toArray(HttpHost[]::new);
}
}*/

View File

@@ -0,0 +1,31 @@
package com.tashow.cloud.es.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
@Data
public class ElasticsearchProperties {
/**
* 是否启用 Elasticsearch
*/
private Boolean enabled = true;
/**
* 节点地址列表 (格式: http://ip:port)
*/
private List<String> uris = List.of("http://43.139.42.137:9200");
/**
* 连接超时时间 (ms)
*/
private Integer connectTimeout = 3000;
/**
* 通信超时时间 (ms)
*/
private Integer socketTimeout = 10000;
}

View File

@@ -0,0 +1,34 @@
package com.tashow.cloud.es.service;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import org.springframework.stereotype.Service;
import java.io.IOException;
@Service
public class ElasticsearchService {
private final ElasticsearchClient client;
public ElasticsearchService(ElasticsearchClient client) {
this.client = client;
}
/**
* 向 Elasticsearch 索引中插入数据
*
* @param indexName 索引名称
* @param id 文档 ID
* @param jsonData JSON 格式的文档数据
* @return 插入结果
* @throws IOException 如果发生 I/O 错误
*/
public IndexResponse insertDocument(String indexName, String id, String jsonData) throws IOException {
return client.index(i -> i
.index(indexName)
.id(id)
.document(jsonData)
);
}
}

View File

@@ -0,0 +1,3 @@
com.tashow.cloud.es.config.ElasticsearchAutoConfiguration
com.tashow.cloud.es.service.ElasticsearchService
com.tashow.cloud.es.config.ElasticsearchProperties