✨ 优化 minio 和 syslog

This commit is contained in:
如梦技术 committed 2019-01-18 09:28:06 +08:00
1 parent 074fe4af7d
commit 492fca9cfb
4 files changed
+41 -40

No files matched your search

@@ -23,10 +23,12 @@ import com.pig4cloud.pigx.common.core.util.SpringContextHolder;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.log.event.SysLogEvent;
import com.pig4cloud.pigx.common.log.util.SysLogUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.ApplicationEventPublisher;
/**
* 操作日志使用spring event异步入库
@@ -35,7 +37,9 @@ import org.aspectj.lang.annotation.Aspect;
*/
@Slf4j
@Aspect
@AllArgsConstructor
public class SysLogAspect {
private final ApplicationEventPublisher publisher;
@Around("@annotation(sysLog)")
public Object around(ProceedingJoinPoint point, SysLog sysLog) throws Throwable {
@@ -50,7 +54,7 @@ public class SysLogAspect {
Object obj = point.proceed();
Long endTime = System.currentTimeMillis();
logVo.setTime(endTime - startTime);
SpringContextHolder.publishEvent(new SysLogEvent(logVo));
publisher.publishEvent(new SysLogEvent(logVo));
return obj;
}
@@ -20,15 +20,15 @@
package com.pig4cloud.pigx.common.log.event;
import com.pig4cloud.pigx.admin.api.entity.SysLog;
import org.springframework.context.ApplicationEvent;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* @author lengleng
* 系统日志事件
*/
public class SysLogEvent extends ApplicationEvent {
public SysLogEvent(SysLog source) {
super(source);
}
@Getter
@AllArgsConstructor
public class SysLogEvent {
private final SysLog sysLog;
}
@@ -28,7 +28,6 @@ import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
/**
* @author lengleng
* 异步监听日志事件
@@ -42,7 +41,7 @@ public class SysLogListener {
@Order
@EventListener(SysLogEvent.class)
public void saveSysLog(SysLogEvent event) {
SysLog sysLog = (SysLog) event.getSource();
SysLog sysLog = event.getSysLog();
remoteLogService.saveLog(sysLog, SecurityConstants.FROM_IN);
}
}
@@ -25,6 +25,9 @@ import io.minio.messages.Bucket;
import io.minio.messages.Item;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import java.io.InputStream;
import java.util.ArrayList;
@@ -36,14 +39,12 @@ import java.util.Optional;
*
* @author lengleng
*/
@NoArgsConstructor
@AllArgsConstructor
public class MinioTemplate {
private String endpoint;
private String accessKey;
private String secretKey;
@RequiredArgsConstructor
public class MinioTemplate implements InitializingBean {
private final String endpoint;
private final String accessKey;
private final String secretKey;
private MinioClient client;
/**
* 创建bucket
@@ -52,7 +53,6 @@ public class MinioTemplate {
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#makeBucket
*/
public void createBucket(String bucketName) throws Exception {
MinioClient client = getMinioClient();
if (!client.bucketExists(bucketName)) {
client.makeBucket(bucketName);
}
@@ -64,7 +64,7 @@ public class MinioTemplate {
* https://docs.minio.io/cn/java-client-api-reference.html#listBuckets
*/
public List<Bucket> getAllBuckets() throws Exception {
return getMinioClient().listBuckets();
return client.listBuckets();
}
/**
@@ -72,7 +72,7 @@ public class MinioTemplate {
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#listBuckets
*/
public Optional<Bucket> getBucket(String bucketName) throws Exception {
return getMinioClient().listBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst();
return client.listBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst();
}
/**
@@ -80,7 +80,7 @@ public class MinioTemplate {
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#removeBucket
*/
public void removeBucket(String bucketName) throws Exception {
getMinioClient().removeBucket(bucketName);
client.removeBucket(bucketName);
}
/**
@@ -89,16 +89,16 @@ public class MinioTemplate {
* @param bucketName bucket名称
* @param prefix 前缀
* @param recursive 是否递归查询
* @return
* @return MinioItem 列表
* @throws Exception
*/
public List<MinioItem> getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) throws Exception {
List objectList = new ArrayList();
Iterable<Result<Item>> objectsIterator = getMinioClient()
List<MinioItem> objectList = new ArrayList<>();
Iterable<Result<Item>> objectsIterator = client
.listObjects(bucketName, prefix, recursive);
while (objectsIterator.iterator().hasNext()) {
objectList.add(objectsIterator.iterator().next().get());
objectList.add(new MinioItem(objectsIterator.iterator().next().get()));
}
return objectList;
}
@@ -113,7 +113,7 @@ public class MinioTemplate {
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#getObject
*/
public String getObjectURL(String bucketName, String objectName, Integer expires) throws Exception {
return getMinioClient().presignedGetObject(bucketName, objectName, expires);
return client.presignedGetObject(bucketName, objectName, expires);
}
/**
@@ -125,7 +125,7 @@ public class MinioTemplate {
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#getObject
*/
public InputStream getObject(String bucketName, String objectName) throws Exception {
return getMinioClient().getObject(bucketName, objectName);
return client.getObject(bucketName, objectName);
}
/**
@@ -137,7 +137,7 @@ public class MinioTemplate {
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
*/
public void putObject(String bucketName, String objectName, InputStream stream) throws Exception {
getMinioClient().putObject(bucketName, objectName, stream, stream.available(), "application/octet-stream");
client.putObject(bucketName, objectName, stream, stream.available(), "application/octet-stream");
}
/**
@@ -151,7 +151,7 @@ public class MinioTemplate {
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
*/
public void putObject(String bucketName, String objectName, InputStream stream, long size, String contextType) throws Exception {
getMinioClient().putObject(bucketName, objectName, stream, size, contextType);
client.putObject(bucketName, objectName, stream, size, contextType);
}
/**
@@ -162,7 +162,7 @@ public class MinioTemplate {
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#statObject
*/
public ObjectStat getObjectInfo(String bucketName, String objectName) throws Exception {
return getMinioClient().statObject(bucketName, objectName);
return client.statObject(bucketName, objectName);
}
/**
@@ -173,17 +173,15 @@ public class MinioTemplate {
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#removeObject
*/
public void removeObject(String bucketName, String objectName) throws Exception {
getMinioClient().removeObject(bucketName, objectName);
client.removeObject(bucketName, objectName);
}
/**
* 获取minio 原生客户端
*
* @return
* @throws Exception
*/
public MinioClient getMinioClient() throws Exception {
return new MinioClient(endpoint, accessKey, secretKey);
@Override
public void afterPropertiesSet() throws Exception {
Assert.hasText(endpoint, "Minio url 为空。");
Assert.hasText(accessKey, "Minio accessKey 为空。");
Assert.hasText(secretKey, "Minio accessKey 为空。");
this.client = new MinioClient(endpoint, accessKey, secretKey);
}
}