--9--10
parent
c28de5eee2
commit
7ade61b99c
|
@ -13,6 +13,7 @@ import com.muyu.resp.EngineConfigScopeResp;
|
||||||
import com.muyu.resp.VersionClassCreateResp;
|
import com.muyu.resp.VersionClassCreateResp;
|
||||||
import com.muyu.service.EngineVersionService;
|
import com.muyu.service.EngineVersionService;
|
||||||
import com.muyu.test.JavaClass;
|
import com.muyu.test.JavaClass;
|
||||||
|
import com.muyu.utils.DownloadOssSynchronization;
|
||||||
import com.muyu.utils.OssUpload;
|
import com.muyu.utils.OssUpload;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
@ -31,6 +32,8 @@ public class EngineVersionController extends BaseController {
|
||||||
@Autowired
|
@Autowired
|
||||||
EngineVersionService engineVersionService;
|
EngineVersionService engineVersionService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
DownloadOssSynchronization downloadOssSynchronization;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询引擎规则配置列表
|
* 查询引擎规则配置列表
|
||||||
|
@ -161,6 +164,7 @@ public class EngineVersionController extends BaseController {
|
||||||
OssUpload.uploadFiles(engineVersion.getRuleContent(), engineVersion.getVersionCode());
|
OssUpload.uploadFiles(engineVersion.getRuleContent(), engineVersion.getVersionCode());
|
||||||
OSSFileDownload.streamingDownload(engineVersion.getName());
|
OSSFileDownload.streamingDownload(engineVersion.getName());
|
||||||
JavaClass.compile(engineVersion.getName());
|
JavaClass.compile(engineVersion.getName());
|
||||||
|
OssUpload.uploadFiles(engineVersion.getName(), engineVersion.getVersionCode());
|
||||||
engineVersion.setId(null);
|
engineVersion.setId(null);
|
||||||
boolean insert = engineVersionService.insert(engineVersion);
|
boolean insert = engineVersionService.insert(engineVersion);
|
||||||
return insert;
|
return insert;
|
||||||
|
|
|
@ -6,6 +6,7 @@ import lombok.extern.log4j.Log4j2;
|
||||||
import javax.tools.JavaCompiler;
|
import javax.tools.JavaCompiler;
|
||||||
|
|
||||||
import javax.tools.ToolProvider;
|
import javax.tools.ToolProvider;
|
||||||
|
|
||||||
@Log4j2
|
@Log4j2
|
||||||
public class JavaClass {
|
public class JavaClass {
|
||||||
public static Result<Object> compile(String fileName) {
|
public static Result<Object> compile(String fileName) {
|
||||||
|
|
|
@ -0,0 +1,81 @@
|
||||||
|
package com.muyu.utils;
|
||||||
|
|
||||||
|
import com.aliyun.oss.OSS;
|
||||||
|
import com.aliyun.oss.OSSClientBuilder;
|
||||||
|
import com.aliyun.oss.model.GetObjectRequest;
|
||||||
|
import com.aliyun.oss.model.OSSObject;
|
||||||
|
import com.aliyun.oss.model.ObjectListing;
|
||||||
|
import com.aliyun.oss.model.OSSObjectSummary;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.boot.ApplicationArguments;
|
||||||
|
import org.springframework.boot.ApplicationRunner;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Log4j2
|
||||||
|
@Component
|
||||||
|
public class DownloadOssSynchronization implements ApplicationRunner {
|
||||||
|
|
||||||
|
private static final String bucketName = "011811";
|
||||||
|
// OSS文件路径
|
||||||
|
|
||||||
|
|
||||||
|
private static final String endPoint = "oss-cn-beijing.aliyuncs.com";
|
||||||
|
|
||||||
|
|
||||||
|
private static final String accessKeyId = "LTAI5t8LmMHfW8ckPaAZR6oQ";
|
||||||
|
|
||||||
|
|
||||||
|
private static final String accessKeySecret = "pwzEwkpxcTFgurkARyr7sG7V6syc9x";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务器项目路径放文件
|
||||||
|
*/
|
||||||
|
private static String serverClassPath = "home/lib/com/muyu/abstracts/";
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(ApplicationArguments args) throws Exception {
|
||||||
|
log.info("开始从Oss下载规则引擎");
|
||||||
|
OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
|
||||||
|
|
||||||
|
// 获取存储桶信息
|
||||||
|
ObjectListing objectListing = ossClient.listObjects(bucketName);
|
||||||
|
// 循环获取Oss存储桶中的所有对象
|
||||||
|
for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
|
||||||
|
String key = objectSummary.getKey();
|
||||||
|
downloadFile(ossClient, bucketName, key, serverClassPath);
|
||||||
|
}
|
||||||
|
// 关闭OSSClient
|
||||||
|
ossClient.shutdown();
|
||||||
|
//下载完成实现批量批量类加载
|
||||||
|
}
|
||||||
|
|
||||||
|
private void downloadFile(OSS ossClient, String bucketName, String key, String serverPath) {
|
||||||
|
log.info("开始下载文件[]{}:"+key);
|
||||||
|
//创建服务器项目容器的文件目录路径
|
||||||
|
File serverFile = new File(serverPath, key);
|
||||||
|
//如果目录不存在,则创建目录
|
||||||
|
if (!serverFile.getParentFile().exists()) {
|
||||||
|
serverFile.getParentFile().mkdirs();
|
||||||
|
}
|
||||||
|
//下载文件到项目中
|
||||||
|
OSSObject ossObject = ossClient.getObject(new GetObjectRequest(bucketName, key));
|
||||||
|
|
||||||
|
try (FileOutputStream fos = new FileOutputStream(serverFile);) {
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
int byteRead;
|
||||||
|
while ((byteRead = ossObject.getObjectContent().read(buffer)) != -1) {
|
||||||
|
fos.write(buffer, 0, byteRead);
|
||||||
|
}
|
||||||
|
log.info("文件 " + key + "已经下载到" + serverFile.getAbsolutePath());
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("下载文件失败:" + key);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,92 @@
|
||||||
|
package com.muyu.utils;
|
||||||
|
|
||||||
|
import com.aliyun.oss.OSS;
|
||||||
|
import com.aliyun.oss.OSSClientBuilder;
|
||||||
|
import com.aliyun.oss.model.PutObjectRequest;
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:LiDongJia
|
||||||
|
* @Package:com.muyu.cloud.etl.rule.upload
|
||||||
|
* @Project:cloud-rule
|
||||||
|
* @name:ALiYunUpload
|
||||||
|
* @Date:2024/8/30
|
||||||
|
*/
|
||||||
|
public class OssUpload {
|
||||||
|
|
||||||
|
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写
|
||||||
|
private static final String endPoint = "oss-cn-beijing.aliyuncs.com";
|
||||||
|
|
||||||
|
|
||||||
|
private static final String accessKeyId = "LTAI5t8LmMHfW8ckPaAZR6oQ";
|
||||||
|
|
||||||
|
|
||||||
|
private static final String accessKeySecret = "pwzEwkpxcTFgurkARyr7sG7V6syc9x";
|
||||||
|
// Bucket名称
|
||||||
|
private static final String bucketName = "011811";
|
||||||
|
|
||||||
|
public static Result<Object> uploadFiles(String content, String fileName) {
|
||||||
|
|
||||||
|
OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
|
||||||
|
|
||||||
|
// 上传到OSS后文件保存的目录,例如:folder/subfolder/,最后以斜杠结尾
|
||||||
|
String objectName = "build/rule/version/" + fileName + ".java";
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 将内容转换为字节数组输入流
|
||||||
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
|
// 构造上传请求
|
||||||
|
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
|
||||||
|
|
||||||
|
// 执行上传操作
|
||||||
|
// ossClient.putObject(putObjectRequest);
|
||||||
|
|
||||||
|
return Result.success(null, "文件" + fileName + ".java" + "存储成功");
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
return Result.error(null, "文件" + fileName + ".java" + "存储失败");
|
||||||
|
} finally {
|
||||||
|
// 关闭OSSClient
|
||||||
|
if (ossClient != null) {
|
||||||
|
ossClient.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Result<Object> uploadClassFiles(String versionClass) {
|
||||||
|
// 创建OSSClient实例。
|
||||||
|
OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
|
||||||
|
String objectName = "build/rule/version/" + versionClass + ".class";
|
||||||
|
|
||||||
|
// 创建上传文件的输入流(这里以文件为例)。
|
||||||
|
File file = new File("/home/lib/com/muyu/cloud/rule/engine/" + versionClass + ".class");
|
||||||
|
|
||||||
|
// 上传文件。
|
||||||
|
ossClient.putObject(new PutObjectRequest(bucketName, objectName, file));
|
||||||
|
|
||||||
|
System.out.println("上传成功");
|
||||||
|
return Result.success("上传成功");
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.out.println("上传失败");
|
||||||
|
return Result.error("上传失败");
|
||||||
|
} finally {
|
||||||
|
// 关闭OSSClient。
|
||||||
|
if (ossClient != null) {
|
||||||
|
ossClient.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue