diff --git a/pom.xml b/pom.xml index 2a08e6e..d3b1568 100644 --- a/pom.xml +++ b/pom.xml @@ -243,6 +243,18 @@ 1.1.1 + + + org.elasticsearch.client + elasticsearch-rest-high-level-client + 7.17.9 + + + org.elasticsearch + elasticsearch + 7.17.9 + + diff --git a/src/main/java/com/guwan/backend/config/ElasticsearchConfig.java b/src/main/java/com/guwan/backend/config/ElasticsearchConfig.java new file mode 100644 index 0000000..ee5bab2 --- /dev/null +++ b/src/main/java/com/guwan/backend/config/ElasticsearchConfig.java @@ -0,0 +1,39 @@ +package com.guwan.backend.config; + +import org.apache.http.HttpHost; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestHighLevelClient; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ConditionalOnProperty; + +@Configuration +@ConditionalOnProperty(name = "easy-es.enable", havingValue = "true") +public class ElasticsearchConfig { + + @Value("${easy-es.address}") + private String address; + + @Value("${easy-es.username:}") + private String username; + + @Value("${easy-es.password:}") + private String password; + + @Bean + public RestHighLevelClient restHighLevelClient() { + String[] parts = address.split(":"); + String host = parts[0]; + int port = Integer.parseInt(parts[1]); + + return new RestHighLevelClient( + RestClient.builder(new HttpHost(host, port, "http")) + .setRequestConfigCallback(requestConfigBuilder -> { + requestConfigBuilder.setConnectTimeout(5000); + requestConfigBuilder.setSocketTimeout(60000); + return requestConfigBuilder; + }) + ); + } +} \ No newline at end of file diff --git a/src/main/java/com/guwan/backend/es/document/VideoDocument.java b/src/main/java/com/guwan/backend/es/document/VideoDocument.java index d8a7857..09f05be 100644 --- a/src/main/java/com/guwan/backend/es/document/VideoDocument.java +++ b/src/main/java/com/guwan/backend/es/document/VideoDocument.java @@ -9,7 +9,7 @@ import lombok.Data; import java.time.LocalDateTime; @Data -@IndexName("videos") +@IndexName("video_index") public class VideoDocument { @IndexId diff --git a/src/main/java/com/guwan/backend/service/VideoSearchService.java b/src/main/java/com/guwan/backend/service/VideoSearchService.java index 0a53ad3..2733347 100644 --- a/src/main/java/com/guwan/backend/service/VideoSearchService.java +++ b/src/main/java/com/guwan/backend/service/VideoSearchService.java @@ -10,12 +10,14 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import java.util.List; import java.util.stream.Collectors; @Slf4j @Service +@ConditionalOnProperty(name = "easy-es.enable", havingValue = "true") @RequiredArgsConstructor public class VideoSearchService { diff --git a/src/main/java/com/guwan/backend/service/impl/VideoServiceImpl.java b/src/main/java/com/guwan/backend/service/impl/VideoServiceImpl.java index 6517e0d..2f1c41e 100644 --- a/src/main/java/com/guwan/backend/service/impl/VideoServiceImpl.java +++ b/src/main/java/com/guwan/backend/service/impl/VideoServiceImpl.java @@ -16,6 +16,7 @@ import com.guwan.backend.util.SecurityUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -30,7 +31,9 @@ public class VideoServiceImpl implements VideoService { private final MinioUtil minioUtil; private final SecurityUtil securityUtil; private final VideoLikeMapper videoLikeMapper; - private final VideoSearchService videoSearchService; + + @Autowired(required = false) + private VideoSearchService videoSearchService; @Override @Transactional @@ -62,7 +65,7 @@ public class VideoServiceImpl implements VideoService { videoMapper.insert(video); // 保存到ES - videoSearchService.saveOrUpdate(video); + updateEs(video); return convertToDTO(video); } catch (Exception e) { @@ -115,7 +118,7 @@ public class VideoServiceImpl implements VideoService { videoMapper.deleteById(id); // 从ES中删除 - videoSearchService.delete(id); + deleteFromEs(id); } @Override @@ -152,7 +155,7 @@ public class VideoServiceImpl implements VideoService { videoMapper.updateById(video); // 更新ES中的观看次数 - videoSearchService.updateViewCount(id, video.getViewCount()); + updateEsViewCount(id, video.getViewCount()); } } @@ -199,7 +202,7 @@ public class VideoServiceImpl implements VideoService { videoMapper.updateById(video); // 更新ES中的点赞次数 - videoSearchService.updateLikeCount(id, video.getLikeCount()); + updateEsLikeCount(id, video.getLikeCount()); } // 添加新方法:检查用户是否已点赞 @@ -218,12 +221,20 @@ public class VideoServiceImpl implements VideoService { @Override public List searchVideosByEs(String keyword) { - return videoSearchService.search(keyword); + if (videoSearchService != null) { + return videoSearchService.search(keyword); + } + // 降级到数据库搜索 + return getVideoList(1, 10, keyword).getRecords(); } @Override public List getSimilarVideosByEs(Long id, int limit) { - return videoSearchService.findSimilar(id, limit); + if (videoSearchService != null) { + return videoSearchService.findSimilar(id, limit); + } + // 降级到随机推荐 + return getVideoList(1, limit, null).getRecords(); } private VideoDTO convertToDTO(Video video) { @@ -238,4 +249,28 @@ public class VideoServiceImpl implements VideoService { return dto; } + + private void updateEs(Video video) { + if (videoSearchService != null) { + videoSearchService.saveOrUpdate(video); + } + } + + private void deleteFromEs(Long id) { + if (videoSearchService != null) { + videoSearchService.delete(id); + } + } + + private void updateEsViewCount(Long id, Integer viewCount) { + if (videoSearchService != null) { + videoSearchService.updateViewCount(id, viewCount); + } + } + + private void updateEsLikeCount(Long id, Integer likeCount) { + if (videoSearchService != null) { + videoSearchService.updateLikeCount(id, likeCount); + } + } } \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 095cce9..11b9daf 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -132,15 +132,18 @@ srs: url: http://localhost:1985 # SRS HTTP API地址 easy-es: - enable: true - address: localhost:9200 - username: elastic # 如果有的话 - password: elastic # 如果有的话 + enable: false # 改为false禁用Easy-Es + address: localhost:9200 # ES地址 + username: ${ES_USERNAME:} # ES用户名,可选 + password: ${ES_PASSWORD:} # ES密码,可选 global-config: process-index-mode: manual print-dsl: true distributed: false response-log: true + db-config: + map-underscore-to-camel-case: true + index-prefix: video_ # 索引前缀 netty: danmaku: