mirror of
https://github.com/201206030/novel-cloud.git
synced 2025-04-27 01:40:50 +00:00
集成阿里云OSS,增加文件微服务
This commit is contained in:
parent
1bda806862
commit
e790ae0b05
@ -26,6 +26,7 @@ novel-cloud
|
|||||||
├── novel-gateway -- 基于Spring Cloud Gateway构建的网关服务
|
├── novel-gateway -- 基于Spring Cloud Gateway构建的网关服务
|
||||||
├── novel-monitor -- 基于Spring Boot Admin构建的监控中心
|
├── novel-monitor -- 基于Spring Boot Admin构建的监控中心
|
||||||
├── novel-search -- 基于Elastic Search构建的搜索微服务
|
├── novel-search -- 基于Elastic Search构建的搜索微服务
|
||||||
|
├── novel-file -- 基于阿里云OSS构建的文件微服务
|
||||||
├── novel-home -- 门户首页微服务
|
├── novel-home -- 门户首页微服务
|
||||||
├── novel-news -- 新闻中心微服务
|
├── novel-news -- 新闻中心微服务
|
||||||
├── novel-user -- 用户中心微服务
|
├── novel-user -- 用户中心微服务
|
||||||
|
Binary file not shown.
@ -70,4 +70,23 @@ public interface BookApi {
|
|||||||
* */
|
* */
|
||||||
@GetMapping("api/book/listUserCommentByPage")
|
@GetMapping("api/book/listUserCommentByPage")
|
||||||
List<BookComment> listUserCommentByPage(@RequestParam("userId") Long userId,@RequestParam("page") int page, @RequestParam("pageSize") int pageSize);
|
List<BookComment> listUserCommentByPage(@RequestParam("userId") Long userId,@RequestParam("page") int page, @RequestParam("pageSize") int pageSize);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询网络图片的小说
|
||||||
|
*
|
||||||
|
* @param localPicPrefix
|
||||||
|
* @param limit 查询条数
|
||||||
|
* @return 返回小说集合
|
||||||
|
* */
|
||||||
|
@GetMapping("api/book/queryNetworkPicBooks")
|
||||||
|
List<Book> queryNetworkPicBooks(@RequestParam("localPicPrefix") String localPicPrefix,@RequestParam("limit") int limit);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新图片路径
|
||||||
|
* @param picUrl 图片路径
|
||||||
|
* @param bookId 小说ID
|
||||||
|
*/
|
||||||
|
@PostMapping("api/book/updateBookPic")
|
||||||
|
void updateBookPic(@RequestParam("picUrl") String picUrl,@RequestParam("bookId") Long bookId);
|
||||||
}
|
}
|
||||||
|
@ -91,4 +91,27 @@ public class BookApi {
|
|||||||
List<BookComment> listUserCommentByPage(@RequestParam("userId") Long userId,@RequestParam("page") int page, @RequestParam("pageSize") int pageSize){
|
List<BookComment> listUserCommentByPage(@RequestParam("userId") Long userId,@RequestParam("page") int page, @RequestParam("pageSize") int pageSize){
|
||||||
return bookService.listUserCommentByPage(userId,page,pageSize);
|
return bookService.listUserCommentByPage(userId,page,pageSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询网络图片的小说
|
||||||
|
*
|
||||||
|
* @param localPicPrefix
|
||||||
|
* @param limit 查询条数
|
||||||
|
* @return 返回小说集合
|
||||||
|
* */
|
||||||
|
@GetMapping("queryNetworkPicBooks")
|
||||||
|
List<Book> queryNetworkPicBooks(@RequestParam("localPicPrefix") String localPicPrefix,@RequestParam("limit") int limit){
|
||||||
|
return bookService.queryNetworkPicBooks(localPicPrefix,limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新图片路径
|
||||||
|
* @param picUrl 图片路径
|
||||||
|
* @param bookId 小说ID
|
||||||
|
*/
|
||||||
|
@PostMapping("updateBookPic")
|
||||||
|
void updateBookPic(@RequestParam("picUrl") String picUrl,@RequestParam("bookId") Long bookId){
|
||||||
|
bookService.updateBookPic(picUrl,bookId);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,10 @@ import org.springframework.stereotype.Component;
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author 11797
|
* 消息监听器
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
* @version 1.0
|
||||||
|
* @since 2020/6/2
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@ -136,4 +136,21 @@ public interface BookService {
|
|||||||
* @return 评论数据
|
* @return 评论数据
|
||||||
* */
|
* */
|
||||||
List<BookComment> listUserCommentByPage(Long userId, int page, int pageSize);
|
List<BookComment> listUserCommentByPage(Long userId, int page, int pageSize);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询网络图片的小说
|
||||||
|
*
|
||||||
|
* @param localPicPrefix
|
||||||
|
* @param limit 查询条数
|
||||||
|
* @return 返回小说集合
|
||||||
|
* */
|
||||||
|
List<Book> queryNetworkPicBooks(String localPicPrefix, Integer limit);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新图片路径
|
||||||
|
* @param picUrl 图片路径
|
||||||
|
* @param bookId 小说ID
|
||||||
|
*/
|
||||||
|
void updateBookPic(String picUrl, Long bookId);
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,7 @@ import static org.mybatis.dynamic.sql.select.SelectDSL.select;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 小说服务接口实现
|
* 小说服务接口实现
|
||||||
|
*
|
||||||
* @author xiongxiaoyang
|
* @author xiongxiaoyang
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
* @since 2020/5/28
|
* @since 2020/5/28
|
||||||
@ -63,14 +64,14 @@ public class BookServiceImpl implements BookService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Book> queryBookByIds(List<Long> ids) {
|
public List<Book> queryBookByIds(List<Long> ids) {
|
||||||
return bookMapper.selectMany(select(BookDynamicSqlSupport.id,BookDynamicSqlSupport.catId,BookDynamicSqlSupport.catName,
|
return bookMapper.selectMany(select(BookDynamicSqlSupport.id, BookDynamicSqlSupport.catId, BookDynamicSqlSupport.catName,
|
||||||
BookDynamicSqlSupport.bookName,BookDynamicSqlSupport.authorName,
|
BookDynamicSqlSupport.bookName, BookDynamicSqlSupport.authorName,
|
||||||
BookDynamicSqlSupport.lastIndexId,BookDynamicSqlSupport.lastIndexName,BookDynamicSqlSupport.lastIndexUpdateTime,
|
BookDynamicSqlSupport.lastIndexId, BookDynamicSqlSupport.lastIndexName, BookDynamicSqlSupport.lastIndexUpdateTime,
|
||||||
BookDynamicSqlSupport.picUrl,BookDynamicSqlSupport.bookDesc,BookDynamicSqlSupport.score)
|
BookDynamicSqlSupport.picUrl, BookDynamicSqlSupport.bookDesc, BookDynamicSqlSupport.score)
|
||||||
.from(book)
|
.from(book)
|
||||||
.where(BookDynamicSqlSupport.id,isIn(ids))
|
.where(BookDynamicSqlSupport.id, isIn(ids))
|
||||||
.build()
|
.build()
|
||||||
.render(RenderingStrategies.MYBATIS3)
|
.render(RenderingStrategies.MYBATIS3)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,12 +105,12 @@ public class BookServiceImpl implements BookService {
|
|||||||
BookDynamicSqlSupport.authorId, BookDynamicSqlSupport.authorName,
|
BookDynamicSqlSupport.authorId, BookDynamicSqlSupport.authorName,
|
||||||
BookDynamicSqlSupport.picUrl, BookDynamicSqlSupport.bookDesc,
|
BookDynamicSqlSupport.picUrl, BookDynamicSqlSupport.bookDesc,
|
||||||
BookDynamicSqlSupport.wordCount, BookDynamicSqlSupport.lastIndexUpdateTime)
|
BookDynamicSqlSupport.wordCount, BookDynamicSqlSupport.lastIndexUpdateTime)
|
||||||
.from(book)
|
.from(book)
|
||||||
.where(BookDynamicSqlSupport.wordCount, isGreaterThan(0))
|
.where(BookDynamicSqlSupport.wordCount, isGreaterThan(0))
|
||||||
.orderBy(sortSpecification)
|
.orderBy(sortSpecification)
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.build()
|
.build()
|
||||||
.render(RenderingStrategies.MYBATIS3);
|
.render(RenderingStrategies.MYBATIS3);
|
||||||
return bookMapper.selectMany(selectStatement);
|
return bookMapper.selectMany(selectStatement);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,12 +133,12 @@ public class BookServiceImpl implements BookService {
|
|||||||
.build()
|
.build()
|
||||||
.render(RenderingStrategies.MYBATIS3);
|
.render(RenderingStrategies.MYBATIS3);
|
||||||
List<Book> books = bookMapper.selectMany(selectStatement);
|
List<Book> books = bookMapper.selectMany(selectStatement);
|
||||||
return books.size() >0 ? books.get(0) : null;
|
return books.size() > 0 ? books.get(0) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addVisitCount(Long bookId, int addCount) {
|
public void addVisitCount(Long bookId, int addCount) {
|
||||||
bookMapper.addVisitCount(bookId,addCount);
|
bookMapper.addVisitCount(bookId, addCount);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,12 +174,12 @@ public class BookServiceImpl implements BookService {
|
|||||||
//分页查询小说评论数据
|
//分页查询小说评论数据
|
||||||
PageHelper.startPage(page, pageSize);
|
PageHelper.startPage(page, pageSize);
|
||||||
List<BookComment> bookCommentList = bookCommentMapper.selectMany(
|
List<BookComment> bookCommentList = bookCommentMapper.selectMany(
|
||||||
select(BookCommentDynamicSqlSupport.id,BookCommentDynamicSqlSupport.bookId,
|
select(BookCommentDynamicSqlSupport.id, BookCommentDynamicSqlSupport.bookId,
|
||||||
BookCommentDynamicSqlSupport.createUserId,
|
BookCommentDynamicSqlSupport.createUserId,
|
||||||
BookCommentDynamicSqlSupport.commentContent,BookCommentDynamicSqlSupport.replyCount,
|
BookCommentDynamicSqlSupport.commentContent, BookCommentDynamicSqlSupport.replyCount,
|
||||||
BookCommentDynamicSqlSupport.createTime)
|
BookCommentDynamicSqlSupport.createTime)
|
||||||
.from(BookCommentDynamicSqlSupport.bookComment)
|
.from(BookCommentDynamicSqlSupport.bookComment)
|
||||||
.where(BookCommentDynamicSqlSupport.bookId,isEqualTo(bookId))
|
.where(BookCommentDynamicSqlSupport.bookId, isEqualTo(bookId))
|
||||||
.orderBy(BookCommentDynamicSqlSupport.createTime.descending())
|
.orderBy(BookCommentDynamicSqlSupport.createTime.descending())
|
||||||
.build()
|
.build()
|
||||||
.render(RenderingStrategies.MYBATIS3));
|
.render(RenderingStrategies.MYBATIS3));
|
||||||
@ -190,11 +191,11 @@ public class BookServiceImpl implements BookService {
|
|||||||
|
|
||||||
//将评论数据和评论人数据关联起来
|
//将评论数据和评论人数据关联起来
|
||||||
List<BookCommentVO> resultList = new ArrayList<>(bookCommentList.size());
|
List<BookCommentVO> resultList = new ArrayList<>(bookCommentList.size());
|
||||||
for(BookComment bookComment : bookCommentList){
|
for (BookComment bookComment : bookCommentList) {
|
||||||
BookCommentVO bookCommentVO = new BookCommentVO();
|
BookCommentVO bookCommentVO = new BookCommentVO();
|
||||||
BeanUtils.copyProperties(bookComment,bookCommentVO);
|
BeanUtils.copyProperties(bookComment, bookCommentVO);
|
||||||
User user = userMap.get(bookComment.getCreateUserId());
|
User user = userMap.get(bookComment.getCreateUserId());
|
||||||
if(user != null){
|
if (user != null) {
|
||||||
bookCommentVO.setCreateUserName(user.getUsername());
|
bookCommentVO.setCreateUserName(user.getUsername());
|
||||||
bookCommentVO.setCreateUserPhoto(user.getUserPhoto());
|
bookCommentVO.setCreateUserPhoto(user.getUserPhoto());
|
||||||
}
|
}
|
||||||
@ -260,9 +261,9 @@ public class BookServiceImpl implements BookService {
|
|||||||
.render(RenderingStrategies.MYBATIS3);
|
.render(RenderingStrategies.MYBATIS3);
|
||||||
List<BookIndex> list = bookIndexMapper.selectMany(selectStatement);
|
List<BookIndex> list = bookIndexMapper.selectMany(selectStatement);
|
||||||
if (list.size() == 0) {
|
if (list.size() == 0) {
|
||||||
result.put("preBookIndexId",0L);
|
result.put("preBookIndexId", 0L);
|
||||||
} else {
|
} else {
|
||||||
result.put("preBookIndexId",list.get(0).getId());
|
result.put("preBookIndexId", list.get(0).getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
selectStatement = select(BookIndexDynamicSqlSupport.id)
|
selectStatement = select(BookIndexDynamicSqlSupport.id)
|
||||||
@ -275,9 +276,9 @@ public class BookServiceImpl implements BookService {
|
|||||||
.render(RenderingStrategies.MYBATIS3);
|
.render(RenderingStrategies.MYBATIS3);
|
||||||
list = bookIndexMapper.selectMany(selectStatement);
|
list = bookIndexMapper.selectMany(selectStatement);
|
||||||
if (list.size() == 0) {
|
if (list.size() == 0) {
|
||||||
result.put("nextBookIndexId",0L);
|
result.put("nextBookIndexId", 0L);
|
||||||
} else {
|
} else {
|
||||||
result.put("nextBookIndexId",list.get(0).getId());
|
result.put("nextBookIndexId", list.get(0).getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -310,14 +311,44 @@ public class BookServiceImpl implements BookService {
|
|||||||
public List<BookComment> listUserCommentByPage(Long userId, int page, int pageSize) {
|
public List<BookComment> listUserCommentByPage(Long userId, int page, int pageSize) {
|
||||||
PageHelper.startPage(page, pageSize);
|
PageHelper.startPage(page, pageSize);
|
||||||
return bookCommentMapper.selectMany(
|
return bookCommentMapper.selectMany(
|
||||||
select(BookCommentDynamicSqlSupport.id,BookCommentDynamicSqlSupport.bookId,
|
select(BookCommentDynamicSqlSupport.id, BookCommentDynamicSqlSupport.bookId,
|
||||||
BookCommentDynamicSqlSupport.createUserId,
|
BookCommentDynamicSqlSupport.createUserId,
|
||||||
BookCommentDynamicSqlSupport.commentContent,BookCommentDynamicSqlSupport.replyCount,
|
BookCommentDynamicSqlSupport.commentContent, BookCommentDynamicSqlSupport.replyCount,
|
||||||
BookCommentDynamicSqlSupport.createTime)
|
BookCommentDynamicSqlSupport.createTime)
|
||||||
.from(BookCommentDynamicSqlSupport.bookComment)
|
.from(BookCommentDynamicSqlSupport.bookComment)
|
||||||
.where(BookCommentDynamicSqlSupport.createUserId,isEqualTo(userId))
|
.where(BookCommentDynamicSqlSupport.createUserId, isEqualTo(userId))
|
||||||
.orderBy(BookCommentDynamicSqlSupport.createTime.descending())
|
.orderBy(BookCommentDynamicSqlSupport.createTime.descending())
|
||||||
.build()
|
.build()
|
||||||
.render(RenderingStrategies.MYBATIS3));
|
.render(RenderingStrategies.MYBATIS3));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Book> queryNetworkPicBooks(String localPicPrefix, Integer limit) {
|
||||||
|
|
||||||
|
|
||||||
|
return bookMapper.selectMany(
|
||||||
|
select(BookDynamicSqlSupport.id, BookDynamicSqlSupport.picUrl)
|
||||||
|
.from(book)
|
||||||
|
.where(BookDynamicSqlSupport.picUrl, isLike("http%"))
|
||||||
|
.and(BookDynamicSqlSupport.picUrl, isNotLike(localPicPrefix))
|
||||||
|
.limit(limit)
|
||||||
|
.build()
|
||||||
|
.render(RenderingStrategies.MYBATIS3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBookPic(String picUrl, Long bookId) {
|
||||||
|
|
||||||
|
bookMapper.update(update(book)
|
||||||
|
.set(BookDynamicSqlSupport.picUrl)
|
||||||
|
.equalTo(picUrl)
|
||||||
|
.set(BookDynamicSqlSupport.updateTime)
|
||||||
|
.equalTo(new Date())
|
||||||
|
.where(BookDynamicSqlSupport.id, isEqualTo(bookId))
|
||||||
|
.build()
|
||||||
|
.render(RenderingStrategies.MYBATIS3));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,10 @@ import org.springframework.context.annotation.Bean;
|
|||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author 11797
|
* rabbitmq配置类
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
* @version 1.0
|
||||||
|
* @since 2020/6/2
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "host", matchIfMissing = false)
|
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "host", matchIfMissing = false)
|
||||||
|
@ -9,6 +9,7 @@ import org.springframework.http.HttpHeaders;
|
|||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -23,23 +24,20 @@ import java.util.Objects;
|
|||||||
public class FileUtil {
|
public class FileUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 网络图片转本地
|
* 网络图片转临时文件
|
||||||
* */
|
* */
|
||||||
public static String network2Local(String picSrc,String picSavePath,String visitPrefix) {
|
public static File networkPic2Temp(String picSrc) {
|
||||||
|
File picFile = null;
|
||||||
InputStream input = null;
|
InputStream input = null;
|
||||||
OutputStream out = null;
|
OutputStream out = null;
|
||||||
try {
|
try {
|
||||||
//本地图片保存
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
|
HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
|
||||||
ResponseEntity<Resource> resEntity = RestTemplateUtil.getInstance(Charsets.ISO_8859_1.name()).exchange(picSrc, HttpMethod.GET, requestEntity, Resource.class);
|
ResponseEntity<Resource> resEntity = RestTemplateUtil.getInstance(Charsets.ISO_8859_1.name()).exchange(picSrc, HttpMethod.GET, requestEntity, Resource.class);
|
||||||
input = Objects.requireNonNull(resEntity.getBody()).getInputStream();
|
input = Objects.requireNonNull(resEntity.getBody()).getInputStream();
|
||||||
Date currentDate = new Date();
|
picFile = File.createTempFile("temp",picSrc.substring(picSrc.lastIndexOf(".")));
|
||||||
picSrc = visitPrefix + DateUtils.formatDate(currentDate, "yyyy") + "/" + DateUtils.formatDate(currentDate, "MM") + "/" + DateUtils.formatDate(currentDate, "dd") + "/"
|
|
||||||
+ UUIDUtil.getUUID32()
|
|
||||||
+ picSrc.substring(picSrc.lastIndexOf("."));
|
|
||||||
File picFile = new File(picSavePath + picSrc);
|
|
||||||
File parentFile = picFile.getParentFile();
|
File parentFile = picFile.getParentFile();
|
||||||
|
|
||||||
if (!parentFile.exists()) {
|
if (!parentFile.exists()) {
|
||||||
parentFile.mkdirs();
|
parentFile.mkdirs();
|
||||||
}
|
}
|
||||||
@ -49,10 +47,15 @@ public class FileUtil {
|
|||||||
out.write(b, 0, n);
|
out.write(b, 0, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out.flush();
|
||||||
|
if( ImageIO.read(picFile) == null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
log.error(e.getMessage(),e);
|
log.error(e.getMessage(),e);
|
||||||
|
|
||||||
picSrc = "/images/default.gif";
|
return null;
|
||||||
}finally {
|
}finally {
|
||||||
if(input != null){
|
if(input != null){
|
||||||
try {
|
try {
|
||||||
@ -69,11 +72,15 @@ public class FileUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(picFile != null) {
|
||||||
|
//程序退出时删除临时文件
|
||||||
|
picFile.deleteOnExit();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return picSrc;
|
return picFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
82
novel-file/pom.xml
Normal file
82
novel-file/pom.xml
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>novel-cloud</artifactId>
|
||||||
|
<groupId>com.java2nb.novel</groupId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>novel-file</artifactId>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.java2nb.novel</groupId>
|
||||||
|
<artifactId>book-api</artifactId>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.mybatis.dynamic-sql</groupId>
|
||||||
|
<artifactId>mybatis-dynamic-sql</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-netflix-hystrix</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--aliyunOSS-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun.oss</groupId>
|
||||||
|
<artifactId>aliyun-sdk-oss</artifactId>
|
||||||
|
<version>${aliyun-sdk-oss.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-fileupload</groupId>
|
||||||
|
<artifactId>commons-fileupload</artifactId>
|
||||||
|
<version>${commons-fileupload.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.java2nb.novel;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件微服务启动器
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
* @version 1.0
|
||||||
|
* @since 2020/6/2
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableFeignClients
|
||||||
|
@EnableScheduling
|
||||||
|
public class FileApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(FileApplication.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.java2nb.novel.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OSS配置
|
||||||
|
*
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
* @version 1.0
|
||||||
|
* @since 2020/6/2
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix="novel.file")
|
||||||
|
public class OssProperties {
|
||||||
|
|
||||||
|
private String endpoint;
|
||||||
|
|
||||||
|
private String keyId;
|
||||||
|
|
||||||
|
private String keySecret;
|
||||||
|
|
||||||
|
private String fileHost;
|
||||||
|
|
||||||
|
private String bucketName;
|
||||||
|
|
||||||
|
private String webUrl;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.java2nb.novel.feign;
|
||||||
|
|
||||||
|
|
||||||
|
import com.java2nb.novel.book.api.BookApi;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 小说服务Feign客户端
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
* @version 1.0
|
||||||
|
* @since 2020/5/28
|
||||||
|
*/
|
||||||
|
@FeignClient(value = "book-service")
|
||||||
|
public interface BookFeignClient extends BookApi {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,77 @@
|
|||||||
|
package com.java2nb.novel.schedule;
|
||||||
|
|
||||||
|
import com.java2nb.novel.book.entity.Book;
|
||||||
|
import com.java2nb.novel.common.utils.Constants;
|
||||||
|
import com.java2nb.novel.common.utils.FileUtil;
|
||||||
|
import com.java2nb.novel.feign.BookFeignClient;
|
||||||
|
import com.java2nb.novel.service.FileService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
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;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将爬取的网络图片转存到OSS任务
|
||||||
|
*
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
* @version 1.0
|
||||||
|
* @since 2020/6/2
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
|
public class CrawlPicTransSchedule {
|
||||||
|
|
||||||
|
private final BookFeignClient bookFeignClient;
|
||||||
|
|
||||||
|
private final RedissonClient redissonClient;
|
||||||
|
|
||||||
|
private final FileService fileService;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 10分钟转一次
|
||||||
|
*/
|
||||||
|
@Scheduled(fixedRate = 1000 * 60 * 10)
|
||||||
|
@SneakyThrows
|
||||||
|
public void trans() {
|
||||||
|
|
||||||
|
RLock lock = redissonClient.getLock("crawlPicTrans");
|
||||||
|
lock.lock();
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
List<Book> networkPicBooks = bookFeignClient.queryNetworkPicBooks(Constants.LOCAL_PIC_PREFIX, 100);
|
||||||
|
for (Book book : networkPicBooks) {
|
||||||
|
|
||||||
|
String filePath = "/images/default.gif";
|
||||||
|
File file = FileUtil.networkPic2Temp(book.getPicUrl());
|
||||||
|
if (file != null) {
|
||||||
|
|
||||||
|
filePath = fileService.uploadFile(file);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bookFeignClient.updateBookPic(filePath, book.getId());
|
||||||
|
//3秒钟转化一张图片,10分钟转化200张
|
||||||
|
Thread.sleep(3000);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
|
||||||
|
lock.unlock();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.java2nb.novel.service;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件服务接口
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
* @version 1.0
|
||||||
|
* @since 2020/6/2
|
||||||
|
*/
|
||||||
|
public interface FileService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件
|
||||||
|
* @param file 上传文件
|
||||||
|
* @return 上传地址
|
||||||
|
* */
|
||||||
|
String uploadFile(File file);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,70 @@
|
|||||||
|
package com.java2nb.novel.service.impl;
|
||||||
|
|
||||||
|
import com.aliyun.oss.OSSClient;
|
||||||
|
import com.aliyun.oss.model.CannedAccessControlList;
|
||||||
|
import com.aliyun.oss.model.CreateBucketRequest;
|
||||||
|
import com.aliyun.oss.model.PutObjectRequest;
|
||||||
|
import com.aliyun.oss.model.PutObjectResult;
|
||||||
|
import com.java2nb.novel.common.utils.Constants;
|
||||||
|
import com.java2nb.novel.common.utils.FileUtil;
|
||||||
|
import com.java2nb.novel.common.utils.UUIDUtil;
|
||||||
|
import com.java2nb.novel.config.OssProperties;
|
||||||
|
import com.java2nb.novel.service.FileService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件服务接口实现类
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
* @version 1.0
|
||||||
|
* @since 2020/6/2
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
|
public class FileServiceImpl implements FileService {
|
||||||
|
|
||||||
|
private final OssProperties ossProperties;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String uploadFile(File file) {
|
||||||
|
|
||||||
|
String fileName = file.getName();
|
||||||
|
String filePath =
|
||||||
|
Constants.LOCAL_PIC_PREFIX + UUIDUtil.getUUID32() + fileName.substring(fileName.lastIndexOf("."));
|
||||||
|
|
||||||
|
filePath = filePath.replaceFirst("/","");
|
||||||
|
|
||||||
|
OSSClient ossClient = new OSSClient(ossProperties.getEndpoint(), ossProperties.getKeyId(), ossProperties.getKeySecret());
|
||||||
|
try {
|
||||||
|
//容器不存在,就创建
|
||||||
|
if (!ossClient.doesBucketExist(ossProperties.getBucketName())) {
|
||||||
|
ossClient.createBucket(ossProperties.getBucketName());
|
||||||
|
CreateBucketRequest createBucketRequest = new CreateBucketRequest(ossProperties.getBucketName());
|
||||||
|
createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
|
||||||
|
ossClient.createBucket(createBucketRequest);
|
||||||
|
}
|
||||||
|
//上传文件
|
||||||
|
PutObjectResult result = ossClient.putObject(new PutObjectRequest(ossProperties.getBucketName(), filePath, file));
|
||||||
|
//设置权限 这里是公开读
|
||||||
|
ossClient.setBucketAcl(ossProperties.getBucketName(), CannedAccessControlList.PublicRead);
|
||||||
|
|
||||||
|
if(result != null) {
|
||||||
|
return ossProperties.getWebUrl() + "/" + filePath;
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
//关闭
|
||||||
|
ossClient.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
return "/images/default.gif";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
4
novel-file/src/main/resources/application.yml
Normal file
4
novel-file/src/main/resources/application.yml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
spring:
|
||||||
|
profiles:
|
||||||
|
include: [common]
|
||||||
|
|
13
novel-file/src/main/resources/bootstrap.yml
Normal file
13
novel-file/src/main/resources/bootstrap.yml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: novel-file
|
||||||
|
profiles:
|
||||||
|
active: dev
|
||||||
|
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
config:
|
||||||
|
ext‐config[0]:
|
||||||
|
data‐id: novel-redis.yml
|
||||||
|
group: novel-common
|
||||||
|
refresh: true
|
64
novel-file/src/main/resources/logback-boot.xml
Normal file
64
novel-file/src/main/resources/logback-boot.xml
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<!-- 彩色日志依赖的渲染类 -->
|
||||||
|
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
|
||||||
|
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
|
||||||
|
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
|
||||||
|
<!-- 彩色日志格式 -->
|
||||||
|
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}" />
|
||||||
|
|
||||||
|
<!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,%i索引【从数字0开始递增】,,, -->
|
||||||
|
<!-- appender是configuration的子节点,是负责写日志的组件。 -->
|
||||||
|
<!-- ConsoleAppender:把日志输出到控制台 -->
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<!--
|
||||||
|
<pattern>%d %p (%file:%line\)- %m%n</pattern>
|
||||||
|
-->
|
||||||
|
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
|
||||||
|
<!-- 控制台也要使用UTF-8,不要使用GBK,否则会中文乱码 -->
|
||||||
|
<charset>UTF-8</charset>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<!-- RollingFileAppender:滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件 -->
|
||||||
|
<!-- 以下的大概意思是:1.先按日期存日志,日期变了,将前一天的日志文件名重命名为XXX%日期%索引,新的日志仍然是demo.log -->
|
||||||
|
<!-- 2.如果日期没有发生变化,但是当前日志的文件大小超过1KB时,对当前日志进行分割 重命名 -->
|
||||||
|
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||||
|
|
||||||
|
<File>logs/novel-file.log</File>
|
||||||
|
<!-- rollingPolicy:当发生滚动时,决定 RollingFileAppender 的行为,涉及文件移动和重命名。 -->
|
||||||
|
<!-- TimeBasedRollingPolicy: 最常用的滚动策略,它根据时间来制定滚动策略,既负责滚动也负责出发滚动 -->
|
||||||
|
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||||
|
<!-- 活动文件的名字会根据fileNamePattern的值,每隔一段时间改变一次 -->
|
||||||
|
<!-- 文件名:logs/demo.2017-12-05.0.log -->
|
||||||
|
<fileNamePattern>logs/debug.%d.%i.log</fileNamePattern>
|
||||||
|
<!-- 每产生一个日志文件,该日志文件的保存期限为30天 -->
|
||||||
|
<maxHistory>30</maxHistory>
|
||||||
|
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
|
||||||
|
<!-- maxFileSize:这是活动文件的大小,默认值是10MB,测试时可改成1KB看效果 -->
|
||||||
|
<maxFileSize>10MB</maxFileSize>
|
||||||
|
</timeBasedFileNamingAndTriggeringPolicy>
|
||||||
|
</rollingPolicy>
|
||||||
|
<encoder>
|
||||||
|
<!-- pattern节点,用来设置日志的输入格式 -->
|
||||||
|
<pattern>
|
||||||
|
%d %p (%file:%line\)- %m%n
|
||||||
|
</pattern>
|
||||||
|
<!-- 记录日志的编码:此处设置字符集 - -->
|
||||||
|
<charset>UTF-8</charset>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
<!-- 控制台输出日志级别 -->
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
<appender-ref ref="FILE" />
|
||||||
|
</root>
|
||||||
|
<!-- 指定项目中某个包,当有日志操作行为时的日志记录级别 -->
|
||||||
|
<!-- com.maijinjie.springboot 为根包,也就是只要是发生在这个根包下面的所有日志操作行为的权限都是DEBUG -->
|
||||||
|
<!-- 级别依次为【从高到低】:FATAL > ERROR > WARN > INFO > DEBUG > TRACE -->
|
||||||
|
<logger name="com.java2nb" level="DEBUG">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
<appender-ref ref="FILE" />
|
||||||
|
</logger>
|
||||||
|
</configuration>
|
@ -16,7 +16,10 @@ import org.springframework.stereotype.Component;
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author 11797
|
* 消息监听器
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
* @version 1.0
|
||||||
|
* @since 2020/6/2
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package com.java2nb.novel.search.service;
|
package com.java2nb.novel.search.service;
|
||||||
|
|
||||||
|
|
||||||
import com.github.pagehelper.PageInfo;
|
|
||||||
import com.java2nb.novel.book.entity.Book;
|
import com.java2nb.novel.book.entity.Book;
|
||||||
import com.java2nb.novel.common.bean.PageBean;
|
import com.java2nb.novel.common.bean.PageBean;
|
||||||
import com.java2nb.novel.search.vo.EsBookVO;
|
import com.java2nb.novel.search.vo.EsBookVO;
|
||||||
|
1
pom.xml
1
pom.xml
@ -19,6 +19,7 @@
|
|||||||
<module>novel-pay</module>
|
<module>novel-pay</module>
|
||||||
<module>novel-search</module>
|
<module>novel-search</module>
|
||||||
<module>novel-author</module>
|
<module>novel-author</module>
|
||||||
|
<module>novel-file</module>
|
||||||
|
|
||||||
</modules>
|
</modules>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
|
BIN
temp/localPic/2020/06/02/4e9ae3c8fe054aefaff9f2f8d5488695.jpg
Normal file
BIN
temp/localPic/2020/06/02/4e9ae3c8fe054aefaff9f2f8d5488695.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 23 KiB |
Loading…
x
Reference in New Issue
Block a user