feat(netty-demo): netty-demo

netty-demo
This commit is contained in:
ovo 2024-12-08 19:48:19 +08:00
parent 4834c66f04
commit 951dd819ff
6 changed files with 103 additions and 12 deletions

12
pom.xml
View File

@ -243,6 +243,18 @@
<version>1.1.1</version>
</dependency>
<!-- Elasticsearch -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.17.9</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.17.9</version>
</dependency>
</dependencies>
<build>

View File

@ -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;
})
);
}
}

View File

@ -9,7 +9,7 @@ import lombok.Data;
import java.time.LocalDateTime;
@Data
@IndexName("videos")
@IndexName("video_index")
public class VideoDocument {
@IndexId

View File

@ -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 {

View File

@ -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<VideoDTO> searchVideosByEs(String keyword) {
return videoSearchService.search(keyword);
if (videoSearchService != null) {
return videoSearchService.search(keyword);
}
// 降级到数据库搜索
return getVideoList(1, 10, keyword).getRecords();
}
@Override
public List<VideoDTO> 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);
}
}
}

View File

@ -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: