Skip to content

Commit 36a446b

Browse files
committed
feat: ✨ 添加 common-es 模块 优化 ai 微服务和 search 微服务中的调用
1 parent d99aaa9 commit 36a446b

8 files changed

Lines changed: 59 additions & 36 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.algorithm.cloud</groupId>
9+
<artifactId>algorithm-common</artifactId>
10+
<version>1.0.0</version>
11+
</parent>
12+
13+
<artifactId>algorithm-common-elasticsearch</artifactId>
14+
<name>algorithm-common-elasticsearch</name>
15+
<description>Algorithm RAG-Enhanced Interactive Teaching System</description>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>org.apache.commons</groupId>
25+
<artifactId>commons-lang3</artifactId>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.projectlombok</groupId>
30+
<artifactId>lombok</artifactId>
31+
<optional>true</optional>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-configuration-processor</artifactId>
37+
<optional>true</optional>
38+
</dependency>
39+
</dependencies>
40+
41+
</project>

algorithm-service/algorithm-search-service/src/main/java/com/stephen/cloud/search/config/ElasticsearchConfiguration.java renamed to algorithm-common/algorithm-common-elasticsearch/src/main/java/com/stephen/cloud/common/elasticsearch/config/ElasticsearchConfiguration.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package com.stephen.cloud.search.config;
1+
package com.stephen.cloud.common.elasticsearch.config;
22

33
import co.elastic.clients.elasticsearch.ElasticsearchClient;
44
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
55
import co.elastic.clients.transport.rest_client.RestClientTransport;
66
import com.fasterxml.jackson.databind.DeserializationFeature;
77
import com.fasterxml.jackson.databind.ObjectMapper;
8-
import com.stephen.cloud.search.config.properties.ElasticsearchProperties;
8+
import com.stephen.cloud.common.elasticsearch.properties.ElasticsearchProperties;
99
import jakarta.annotation.Resource;
1010
import lombok.extern.slf4j.Slf4j;
1111
import org.apache.commons.lang3.StringUtils;
@@ -24,12 +24,6 @@
2424

2525
import java.util.List;
2626

27-
/**
28-
* Elasticsearch 配置
29-
* 手动配置 RestClient 以确保认证信息被正确应用,解决 Actuator 401 问题
30-
*
31-
* @author StephenQiu30
32-
*/
3327
@Configuration
3428
@EnableConfigurationProperties(ElasticsearchProperties.class)
3529
@ConditionalOnProperty(prefix = "spring.elasticsearch", name = "enable", havingValue = "true", matchIfMissing = true)
@@ -47,7 +41,6 @@ public RestClient restClient() {
4741
uris = List.of("http://localhost:9200");
4842
}
4943

50-
// 调试日志:验证 Nacos 属性是否正确注入
5144
log.info("[Elasticsearch] 正在初始化 RestClient. Hosts: {}, User: {}",
5245
uris, elasticsearchProperties.getUsername());
5346

@@ -57,12 +50,10 @@ public RestClient restClient() {
5750

5851
RestClientBuilder builder = RestClient.builder(hosts);
5952

60-
// 配置连接超时
6153
builder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
6254
.setConnectTimeout(10000)
6355
.setSocketTimeout(60000));
6456

65-
// 核心修复:手动配置认证信息
6657
builder.setHttpClientConfigCallback(httpClientBuilder -> {
6758
if (StringUtils.isNotBlank(elasticsearchProperties.getUsername())) {
6859
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

algorithm-service/algorithm-search-service/src/main/java/com/stephen/cloud/search/config/properties/ElasticsearchProperties.java renamed to algorithm-common/algorithm-common-elasticsearch/src/main/java/com/stephen/cloud/common/elasticsearch/properties/ElasticsearchProperties.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,22 @@
1-
package com.stephen.cloud.search.config.properties;
1+
package com.stephen.cloud.common.elasticsearch.properties;
22

33
import lombok.Data;
44
import org.springframework.boot.context.properties.ConfigurationProperties;
55

66
import java.util.List;
77

8-
/**
9-
* Elasticsearch 配置属性
10-
*
11-
* @author StephenQiu30
12-
*/
138
@Data
149
@ConfigurationProperties(prefix = "spring.elasticsearch")
1510
public class ElasticsearchProperties {
1611

17-
/**
18-
* 是否启用索引管理功能
19-
*/
2012
private boolean enable = true;
2113

22-
/**
23-
* 冲突时是否强制重建索引(生产环境慎用)
24-
*/
2514
private boolean recreateOnConflict = false;
2615

27-
/**
28-
* ES 地址列表
29-
*/
3016
private List<String> uris;
3117

32-
/**
33-
* 用户名
34-
*/
3518
private String username;
3619

37-
/**
38-
* 密码
39-
*/
4020
private String password;
4121

4222
}

algorithm-common/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<module>algorithm-common-rabbitmq</module>
2727
<module>algorithm-common-websocket</module>
2828
<module>algorithm-common-log</module>
29+
<module>algorithm-common-elasticsearch</module>
2930
</modules>
3031

3132
</project>

algorithm-service/algorithm-ai-service/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@
125125
<groupId>com.algorithm.cloud</groupId>
126126
<artifactId>algorithm-common-core</artifactId>
127127
</dependency>
128+
129+
<dependency>
130+
<groupId>com.algorithm.cloud</groupId>
131+
<artifactId>algorithm-common-elasticsearch</artifactId>
132+
</dependency>
128133
</dependencies>
129134

130135
<build>

algorithm-service/algorithm-search-service/pom.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@
4848
<artifactId>algorithm-api-post</artifactId>
4949
</dependency>
5050

51-
<!-- Elasticsearch -->
5251
<dependency>
53-
<groupId>org.springframework.boot</groupId>
54-
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
52+
<groupId>com.algorithm.cloud</groupId>
53+
<artifactId>algorithm-common-elasticsearch</artifactId>
5554
</dependency>
5655

5756
<!-- Apache Commons Lang3 -->

algorithm-service/algorithm-search-service/src/main/java/com/stephen/cloud/search/manager/EsIndexManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import cn.hutool.core.io.resource.ResourceUtil;
44
import co.elastic.clients.elasticsearch.ElasticsearchClient;
55
import com.stephen.cloud.api.search.constant.EsIndexConstant;
6-
import com.stephen.cloud.search.config.properties.ElasticsearchProperties;
6+
import com.stephen.cloud.common.elasticsearch.properties.ElasticsearchProperties;
77
import jakarta.annotation.PostConstruct;
88
import jakarta.annotation.Resource;
99
import lombok.extern.slf4j.Slf4j;

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,12 @@
411411
<version>${algorithm-cloud.version}</version>
412412
</dependency>
413413

414+
<dependency>
415+
<groupId>com.algorithm.cloud</groupId>
416+
<artifactId>algorithm-common-elasticsearch</artifactId>
417+
<version>${algorithm-cloud.version}</version>
418+
</dependency>
419+
414420
<!-- API 模块 -->
415421
<dependency>
416422
<groupId>com.algorithm.cloud</groupId>

0 commit comments

Comments
 (0)