Skip to content

Commit

Permalink
Merge branch 'Nacos' of https://github.com/moxi624/mogu_blog_v2 into …
Browse files Browse the repository at this point in the history
…Nacos
  • Loading branch information
moxi624 committed Mar 29, 2023
2 parents 499412c + 1eec089 commit 08c8e76
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 29 deletions.
1 change: 1 addition & 0 deletions mogu_admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-dependencies.version}</version>
<configuration>
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>com.moxi.mogublog.admin.AdminApplication</mainClass>
Expand Down
1 change: 1 addition & 0 deletions mogu_gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-dependencies.version}</version>
<configuration>
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>com.moxi.mogublog.gateway.GateWayApplication</mainClass>
Expand Down
1 change: 1 addition & 0 deletions mogu_monitor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-dependencies.version}</version>
<configuration>
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>com.moxi.mogublog.monitor.MonitorApplication</mainClass>
Expand Down
1 change: 1 addition & 0 deletions mogu_picture/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-dependencies.version}</version>
<configuration>
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>com.moxi.mogublog.picture.PictureApplication</mainClass>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import com.moxi.mogublog.utils.FileUtils;
import com.moxi.mogublog.utils.ResultUtil;
import com.moxi.mougblog.base.global.Constants;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.*;
import io.minio.messages.DeleteObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
Expand Down Expand Up @@ -89,10 +89,11 @@ public String deleteBatchFile(List<String> fileNameList) {
SystemConfig systemConfig = feignUtil.getSystemConfig();
MinioClient minioClient = MinioClient.builder().endpoint(systemConfig.getMinioEndPoint()).credentials(systemConfig.getMinioAccessKey(), systemConfig.getMinioSecretKey()).build();
try {
List<DeleteObject> objects = new LinkedList<>();
for (String fileName : fileNameList) {
minioClient.removeObject(
RemoveObjectArgs.builder().bucket(systemConfig.getMinioBucket()).object(fileName).build());
objects.add(new DeleteObject(fileName));
}
minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(systemConfig.getMinioBucket()).objects(objects).build());
} catch (Exception e) {
log.error("批量删除文件失败, 错误消息: {}", e.getMessage());
return ResultUtil.errorWithData(MessageConf.DELETE_DEFAULT_ERROR);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.moxi.mogublog.picture.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.google.gson.Gson;
import com.moxi.mogublog.commons.entity.SystemConfig;
import com.moxi.mogublog.picture.global.MessageConf;
Expand Down Expand Up @@ -27,9 +29,7 @@

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
* 七牛云工具类
Expand Down Expand Up @@ -133,25 +133,45 @@ public int deleteFile(String fileName, Map<String, String> qiNiuConfig) {
* @return
*/
public Boolean deleteFileList(List<String> fileNameList, Map<String, String> qiNiuConfig) {
if (CollUtil.isEmpty(fileNameList)){
return true;
}
//构造一个带指定Zone对象的配置类
Configuration cfg = setQiNiuArea(qiNiuConfig.get(SysConf.QI_NIU_AREA));
//获取上传凭证
String accessKey = qiNiuConfig.get(SysConf.QI_NIU_ACCESS_KEY);
String secretKey = qiNiuConfig.get(SysConf.QI_NIU_SECRET_KEY);
String bucket = qiNiuConfig.get(SysConf.QI_NIU_BUCKET);
int successCount = 0;
for (String fileName : fileNameList) {
String key = fileName;
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
Response delete = bucketManager.delete(bucket, key);
log.info("{七牛云文件 {} 删除成功", fileName);
successCount += 1;
} catch (QiniuException ex) {
//如果遇到异常,说明删除失败
log.error(ex.getMessage());

Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);

try {
List<String> deleteObjects = new LinkedList<>();
for (String fileName:fileNameList){
deleteObjects.add(fileName);
// 七牛云规定不超过1000
if (deleteObjects.size()==999){
// 构建批量删除项
BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
batchOperations.addDeleteOp(bucket, ArrayUtil.toArray(deleteObjects,String.class));
bucketManager.batch(batchOperations);
log.info("{七牛云文件 {} 删除成功", deleteObjects);
deleteObjects = new LinkedList<>();
}
}

// 删除剩下的部分
if (CollUtil.isNotEmpty(deleteObjects)){
BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
batchOperations.addDeleteOp(bucket, ArrayUtil.toArray(deleteObjects,String.class));
bucketManager.batch(batchOperations);
log.info("{七牛云文件 {} 删除成功", deleteObjects);
}
} catch (QiniuException e) {
//如果遇到异常,说明删除失败
log.error("删除失败:",e);
}
return successCount == fileNameList.size();
}
Expand Down
1 change: 1 addition & 0 deletions mogu_search/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-dependencies.version}</version>
<configuration>
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>com.moxi.mogublog.search.SearchApplication</mainClass>
Expand Down
1 change: 1 addition & 0 deletions mogu_sms/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-dependencies.version}</version>
<configuration>
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>com.moxi.mogublog.sms.SmsApplication</mainClass>
Expand Down
1 change: 1 addition & 0 deletions mogu_spider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-dependencies.version}</version>
<configuration>
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>com.moxi.mogublog.spider.SpiderApplication</mainClass>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,14 @@ public Cursor<TypedTuple<String>> zScan(String key, ScanOptions options) {
return redisTemplate.opsForZSet().scan(key, options);
}

/**
* 获取redisTemplate
* @return
*/
public StringRedisTemplate getRedisTemplate() {
return redisTemplate;
}

/**
* 获取Redis List 序列化
*
Expand Down
1 change: 1 addition & 0 deletions mogu_web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-dependencies.version}</version>
<configuration>
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>com.moxi.mogublog.web.WebApplication</mainClass>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.moxi.mogublog.xo.service.impl;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.CharsetUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
Expand All @@ -24,16 +26,22 @@
import com.moxi.mougblog.base.global.Constants;
import com.moxi.mougblog.base.holder.RequestHolder;
import com.moxi.mougblog.base.serviceImpl.SuperServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
* 管理员表 服务实现类
Expand All @@ -42,6 +50,7 @@
* @since 2018-09-04
*/
@Service
@Slf4j
public class AdminServiceImpl extends SuperServiceImpl<AdminMapper, Admin> implements AdminService {

@Autowired
Expand All @@ -66,27 +75,57 @@ public Admin getAdminByUid(String uid) {

@Override
public String getOnlineAdminList(AdminVO adminVO) {
// 获取Redis中匹配的所有key
Set<String> keys = redisUtil.keys(RedisConf.LOGIN_TOKEN_KEY + "*");
List<String> onlineAdminJsonList = redisUtil.multiGet(keys);
// 拼装分页信息
int pageSize = adminVO.getPageSize().intValue();
int currentPage = adminVO.getCurrentPage().intValue();
int total = onlineAdminJsonList.size();
AtomicReference<Integer> total = new AtomicReference<Integer>(0);
int startIndex = Math.max((currentPage - 1) * pageSize, 0);
int endIndex = Math.min(currentPage * pageSize, total);
//TODO 截取出当前分页下的内容,后面考虑用Redis List做分页
List<String> onlineAdminSubList = onlineAdminJsonList.subList(startIndex, endIndex);

// 获取Redis中匹配的key
Set<String> keys = redisUtil.getRedisTemplate().execute((RedisCallback<Set<String>>) connection -> {
Set<String> keySetTemp = new ConcurrentSkipListSet<>();
int index = 0;
try (Cursor<byte[]> cursor = connection.scan(ScanOptions.scanOptions()
.match(RedisConf.LOGIN_TOKEN_KEY + "*")
.count(100000)
.build())) {

// 获取登录用户的key
while (cursor.hasNext()) {
index ++;
// 先偏移起始位置个数据
if (index<startIndex){
cursor.next();
continue;
}
String key = new String(cursor.next(), CharsetUtil.UTF_8);

// 获取需要的key
if (keySetTemp.size() <= pageSize) {
keySetTemp.add(key);
}

}
total.set(index);
} catch (Exception e) {
log.error("Redis Scan get Exception:{}", ExceptionUtil.stacktraceToOneLineString(e), e);
return new ConcurrentSkipListSet<>();
}
return keySetTemp;
});

// 获取在线用户数据
List<String> onlineAdminJsonList = redisUtil.multiGet(keys);
List<OnlineAdmin> onlineAdminList = new ArrayList<>();
for (String item : onlineAdminSubList) {
for (String item : onlineAdminJsonList) {
OnlineAdmin onlineAdmin = JsonUtils.jsonToPojo(item, OnlineAdmin.class);
// 数据脱敏【移除用户的token令牌】
onlineAdmin.setToken("");
onlineAdminList.add(onlineAdmin);
}
Page<OnlineAdmin> page = new Page<>();
page.setCurrent(currentPage);
page.setTotal(total);
page.setTotal(total.get());
page.setSize(pageSize);
page.setRecords(onlineAdminList);
return ResultUtil.successWithData(page);
Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<spring.mock.version>2.0.8</spring.mock.version>
<knife4j.version>3.0.2</knife4j.version>
<spring-cloud-alibaba.version>2.2.4.RELEASE</spring-cloud-alibaba.version>
<spring-boot-dependencies.version>2.2.5.RELEASE</spring-boot-dependencies.version>
</properties>


Expand Down Expand Up @@ -78,7 +79,7 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<version>${spring-boot-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down

0 comments on commit 08c8e76

Please sign in to comment.