forked from 201206030/novel-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5dbddbd
commit e4dd5bc
Showing
10 changed files
with
197 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,9 +28,6 @@ spring: | |
|
||
|
||
|
||
|
||
|
||
|
||
pic: | ||
save: | ||
type: 2 #图片保存方式, 1不保存,使用网络图片 ,2本地保存 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
novel-front/src/main/java/com/java2nb/novel/core/config/RabbitConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.java2nb.novel.core.config; | ||
|
||
import org.springframework.amqp.core.*; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @author 11797 | ||
*/ | ||
@Configuration | ||
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "enable", havingValue = "1") | ||
public class RabbitConfig { | ||
|
||
|
||
/** | ||
* 更新数据库队列 | ||
*/ | ||
@Bean | ||
public Queue updateDbQueue() { | ||
return new Queue("UPDATE-DB-QUEUE", true); | ||
} | ||
|
||
/** | ||
* 更新数据库队列 | ||
*/ | ||
@Bean | ||
public Queue updateEsQueue() { | ||
return new Queue("UPDATE-ES-QUEUE", true); | ||
} | ||
|
||
|
||
/** | ||
* 增加点击量交换机 | ||
*/ | ||
@Bean | ||
public FanoutExchange addVisitExchange() { | ||
return new FanoutExchange("ADD-BOOK-VISIT-EXCHANGE"); | ||
} | ||
|
||
/** | ||
* 更新搜索引擎队列绑定到增加点击量交换机中 | ||
*/ | ||
@Bean | ||
public Binding updateEsBinding() { | ||
|
||
return BindingBuilder.bind(updateEsQueue()).to(addVisitExchange()); | ||
} | ||
|
||
/** | ||
* 更新数据库绑定到增加点击量交换机中 | ||
*/ | ||
@Bean | ||
public Binding updateDbBinding() { | ||
return BindingBuilder.bind(updateDbQueue()).to(addVisitExchange()); | ||
} | ||
|
||
|
||
} |
96 changes: 96 additions & 0 deletions
96
novel-front/src/main/java/com/java2nb/novel/core/listener/BookVisitAddListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.java2nb.novel.core.listener; | ||
|
||
import com.java2nb.novel.core.cache.CacheKey; | ||
import com.java2nb.novel.core.cache.CacheService; | ||
import com.java2nb.novel.entity.Book; | ||
import com.java2nb.novel.service.BookService; | ||
import com.java2nb.novel.vo.EsBookVO; | ||
import com.rabbitmq.client.Channel; | ||
import io.searchbox.client.JestClient; | ||
import io.searchbox.core.Index; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.amqp.core.Message; | ||
import org.springframework.beans.BeanUtils; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.amqp.rabbit.annotation.RabbitListener; | ||
|
||
|
||
import java.text.SimpleDateFormat; | ||
|
||
|
||
/** | ||
* @author 11797 | ||
*/ | ||
@Component | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "enable", havingValue = "1") | ||
public class BookVisitAddListener { | ||
|
||
private final BookService bookService; | ||
|
||
private final CacheService cacheService; | ||
|
||
private final JestClient jestClient; | ||
|
||
|
||
|
||
|
||
/** | ||
* 更新数据库 | ||
* 流量削峰,每本小说1个小时更新一次 | ||
*/ | ||
@SneakyThrows | ||
@RabbitListener(queues = {"UPDATE-DB-QUEUE"}) | ||
public void updateDb(Long bookId, Channel channel, Message message) { | ||
|
||
log.debug("收到更新数据库消息:" + bookId); | ||
Thread.sleep(1000 * 2); | ||
Integer visitCount = (Integer) cacheService.getObject(CacheKey.BOOK_ADD_VISIT_COUNT+bookId); | ||
if(visitCount == null){ | ||
visitCount = 0 ; | ||
} | ||
cacheService.setObject(CacheKey.BOOK_ADD_VISIT_COUNT+bookId,++visitCount); | ||
if(cacheService.get(CacheKey.ES_IS_UPDATE_VISIT + bookId) == null) { | ||
bookService.addVisitCount(bookId); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* 更新搜索引擎 | ||
* 流量削峰,每本小说1个小时更新一次 | ||
*/ | ||
@RabbitListener(queues = {"UPDATE-ES-QUEUE"}) | ||
public void updateEs(Long bookId, Channel channel, Message message) { | ||
|
||
log.debug("收到更新搜索引擎消息:" + bookId); | ||
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 = bookService.queryBookDetail(bookId); | ||
EsBookVO esBookVO = new EsBookVO(); | ||
BeanUtils.copyProperties(book, esBookVO, "lastIndexUpdateTime"); | ||
esBookVO.setLastIndexUpdateTime(new SimpleDateFormat("yyyy/MM/dd HH:mm").format(book.getLastIndexUpdateTime())); | ||
Index action = new Index.Builder(esBookVO).index("novel").type("book").id(book.getId().toString()).build(); | ||
jestClient.execute(action); | ||
cacheService.set(CacheKey.ES_IS_UPDATE_VISIT + bookId, "1", 60 * 60); | ||
}catch (Exception e){ | ||
cacheService.del(CacheKey.ES_IS_UPDATE_VISIT + bookId); | ||
log.error("更新搜索引擎失败"+bookId); | ||
} | ||
|
||
|
||
|
||
|
||
} | ||
|
||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters