diff --git a/tashow-framework/pom.xml b/tashow-framework/pom.xml index d28e9d9..0e47e10 100644 --- a/tashow-framework/pom.xml +++ b/tashow-framework/pom.xml @@ -26,6 +26,7 @@ tashow-data-mybatis tashow-data-redis tashow-data-excel + tashow-data-es diff --git a/tashow-framework/tashow-data-es/pom.xml b/tashow-framework/tashow-data-es/pom.xml new file mode 100644 index 0000000..5cb06d8 --- /dev/null +++ b/tashow-framework/tashow-data-es/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + com.tashow.cloud + tashow-framework + ${revision} + + tashow-data-es + jar + + ${project.artifactId} + es 封装拓展 + + + + + org.springframework.boot + spring-boot-starter-data-elasticsearch + + + javax.annotation + javax.annotation-api + 1.3.2 + + + + com.tashow.cloud + tashow-common + + + + + org.projectlombok + lombok + true + + + diff --git a/tashow-framework/tashow-data-es/src/main/java/com/tashow/cloud/es/config/ElasticsearchAutoConfiguration.java b/tashow-framework/tashow-data-es/src/main/java/com/tashow/cloud/es/config/ElasticsearchAutoConfiguration.java new file mode 100644 index 0000000..992e232 --- /dev/null +++ b/tashow-framework/tashow-data-es/src/main/java/com/tashow/cloud/es/config/ElasticsearchAutoConfiguration.java @@ -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); + } + } + } +} \ No newline at end of file diff --git a/tashow-framework/tashow-data-es/src/main/java/com/tashow/cloud/es/config/ElasticsearchConfig.java b/tashow-framework/tashow-data-es/src/main/java/com/tashow/cloud/es/config/ElasticsearchConfig.java new file mode 100644 index 0000000..6e3979f --- /dev/null +++ b/tashow-framework/tashow-data-es/src/main/java/com/tashow/cloud/es/config/ElasticsearchConfig.java @@ -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); + } +}*/ diff --git a/tashow-framework/tashow-data-es/src/main/java/com/tashow/cloud/es/config/ElasticsearchConfigTest.java b/tashow-framework/tashow-data-es/src/main/java/com/tashow/cloud/es/config/ElasticsearchConfigTest.java new file mode 100644 index 0000000..debaab0 --- /dev/null +++ b/tashow-framework/tashow-data-es/src/main/java/com/tashow/cloud/es/config/ElasticsearchConfigTest.java @@ -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); + } +}*/ diff --git a/tashow-framework/tashow-data-es/src/main/java/com/tashow/cloud/es/config/ElasticsearchProperties.java b/tashow-framework/tashow-data-es/src/main/java/com/tashow/cloud/es/config/ElasticsearchProperties.java new file mode 100644 index 0000000..2a52ff6 --- /dev/null +++ b/tashow-framework/tashow-data-es/src/main/java/com/tashow/cloud/es/config/ElasticsearchProperties.java @@ -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 uris = List.of("http://43.139.42.137:9200"); + + /** + * 连接超时时间 (ms) + */ + private Integer connectTimeout = 3000; + + /** + * 通信超时时间 (ms) + */ + private Integer socketTimeout = 10000; +} diff --git a/tashow-framework/tashow-data-es/src/main/java/com/tashow/cloud/es/service/ElasticsearchService.java b/tashow-framework/tashow-data-es/src/main/java/com/tashow/cloud/es/service/ElasticsearchService.java new file mode 100644 index 0000000..b1488df --- /dev/null +++ b/tashow-framework/tashow-data-es/src/main/java/com/tashow/cloud/es/service/ElasticsearchService.java @@ -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) + ); + } +} diff --git a/tashow-framework/tashow-data-es/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/tashow-framework/tashow-data-es/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..00c3efd --- /dev/null +++ b/tashow-framework/tashow-data-es/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1,3 @@ +com.tashow.cloud.es.config.ElasticsearchAutoConfiguration +com.tashow.cloud.es.service.ElasticsearchService +com.tashow.cloud.es.config.ElasticsearchProperties