mirror of
https://github.com/201206030/novel-cloud.git
synced 2025-07-13 21:56:38 +00:00
引入rabbitmq/应用解偶/流量削峰,引入redisson实现分布式锁
This commit is contained in:
@ -0,0 +1,67 @@
|
||||
package com.java2nb.novel.search.listener;
|
||||
|
||||
import com.java2nb.novel.book.entity.Book;
|
||||
import com.java2nb.novel.common.cache.CacheKey;
|
||||
import com.java2nb.novel.common.cache.CacheService;
|
||||
import com.java2nb.novel.search.feign.BookFeignClient;
|
||||
import com.java2nb.novel.search.service.SearchService;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class BookVisitAddListener {
|
||||
|
||||
|
||||
private final CacheService cacheService;
|
||||
|
||||
private final SearchService searchService;
|
||||
|
||||
private final RedissonClient redissonClient;
|
||||
|
||||
private final BookFeignClient bookFeignClient;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 更新搜索引擎
|
||||
* 流量削峰,每本小说1个小时更新一次
|
||||
*/
|
||||
@RabbitListener(queues = {"UPDATE-ES-QUEUE"})
|
||||
public void updateEs(Long bookId, Channel channel, Message message) {
|
||||
|
||||
log.debug("收到更新搜索引擎消息:" + bookId);
|
||||
RLock lock = redissonClient.getLock("addVisitCountToEs");
|
||||
lock.lock();
|
||||
if (cacheService.get(CacheKey.ES_IS_UPDATE_VISIT + bookId) == null) {
|
||||
cacheService.set(CacheKey.ES_IS_UPDATE_VISIT + bookId, "1", 60 * 60);
|
||||
try {
|
||||
Thread.sleep(1000 * 5);
|
||||
Book book = bookFeignClient.queryBookById(bookId);
|
||||
searchService.importToEs(book);
|
||||
}catch (Exception e){
|
||||
cacheService.del(CacheKey.ES_IS_UPDATE_VISIT + bookId);
|
||||
log.error("更新搜索引擎失败"+bookId);
|
||||
}
|
||||
|
||||
}
|
||||
lock.unlock();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -7,6 +7,8 @@ import com.java2nb.novel.search.feign.BookFeignClient;
|
||||
import com.java2nb.novel.search.service.SearchService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -16,6 +18,7 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* 小说数据导入搜索引擎定时任务
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @version 1.0
|
||||
* @since 2020/5/27
|
||||
@ -30,49 +33,47 @@ public class BookToEsSchedule {
|
||||
private final CacheService cacheService;
|
||||
|
||||
|
||||
|
||||
private final SearchService searchService;
|
||||
|
||||
private final RedissonClient redissonClient;
|
||||
|
||||
|
||||
/**
|
||||
* 1分钟导入一次
|
||||
*/
|
||||
@Scheduled(fixedRate = 1000 * 60)
|
||||
public void saveToEs() {
|
||||
//TODO 引入Redisson框架实现分布式锁
|
||||
//可以重复更新,只是效率可能略有降低,所以暂不实现分布式锁
|
||||
if (cacheService.get(CacheKey.ES_TRANS_LOCK) == null) {
|
||||
cacheService.set(CacheKey.ES_TRANS_LOCK, "1", 60 * 20);
|
||||
try {
|
||||
//查询需要更新的小说
|
||||
Date lastDate = (Date) cacheService.getObject(CacheKey.ES_LAST_UPDATE_TIME);
|
||||
if (lastDate == null) {
|
||||
lastDate = new SimpleDateFormat("yyyy-MM-dd").parse("2020-01-01");
|
||||
}
|
||||
RLock lock = redissonClient.getLock("saveToEs");
|
||||
lock.lock();
|
||||
|
||||
|
||||
List<Book> books = bookFeignClient.queryBookByMinUpdateTime(lastDate, 100);
|
||||
for (Book book : books) {
|
||||
searchService.importToEs(book);
|
||||
lastDate = book.getUpdateTime();
|
||||
Thread.sleep(5000);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
cacheService.setObject(CacheKey.ES_LAST_UPDATE_TIME, lastDate);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
try {
|
||||
//查询需要更新的小说
|
||||
Date lastDate = (Date) cacheService.getObject(CacheKey.ES_LAST_UPDATE_TIME);
|
||||
if (lastDate == null) {
|
||||
lastDate = new SimpleDateFormat("yyyy-MM-dd").parse("2020-01-01");
|
||||
}
|
||||
cacheService.del(CacheKey.ES_TRANS_LOCK);
|
||||
|
||||
|
||||
List<Book> books = bookFeignClient.queryBookByMinUpdateTime(lastDate, 100);
|
||||
for (Book book : books) {
|
||||
searchService.importToEs(book);
|
||||
lastDate = book.getUpdateTime();
|
||||
Thread.sleep(5000);
|
||||
|
||||
}
|
||||
|
||||
|
||||
cacheService.setObject(CacheKey.ES_LAST_UPDATE_TIME, lastDate);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
lock.unlock();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ spring:
|
||||
ext‐config[0]:
|
||||
data‐id: novel-redis.yml
|
||||
group: novel-common
|
||||
refresh: true
|
||||
ext‐config[1]:
|
||||
data‐id: novel-rabbitmq.yml
|
||||
group: novel-common
|
||||
refresh: true
|
Reference in New Issue
Block a user