martin пре 3 година
родитељ
комит
99a7a37e45

+ 1 - 1
api-common/pom.xml

@@ -17,7 +17,7 @@
     </properties>
 
     <dependencies>
-        
+
         <!-- http 客户端 -->
         <dependency>
             <groupId>org.apache.httpcomponents</groupId>

+ 22 - 11
simulation-resource-common/pom.xml

@@ -62,30 +62,41 @@
         </dependency>
         <!-- nacos - 结束 -->
 
-        <!-- web 服务 -->
+
+
+        <!-- api-common -->
+        <dependency>
+            <groupId>com.css</groupId>
+            <artifactId>api-common</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-openfeign</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-loadbalancer</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
-        <!-- 参数校验 -->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-validation</artifactId>
         </dependency>
-        <!-- lombok -->
         <dependency>
-            <groupId>org.projectlombok</groupId>
-            <artifactId>lombok</artifactId>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-actuator</artifactId>
         </dependency>
-
-
-        <!-- api-common -->
         <dependency>
-            <groupId>com.css</groupId>
-            <artifactId>api-common</artifactId>
-            <version>${project.version}</version>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
         </dependency>
 
+
     </dependencies>
 
 </project>

+ 50 - 49
simulation-resource-common/src/main/java/com/css/simulation/resource/common/controller/MinioController.java

@@ -1,49 +1,50 @@
-//package com.css.simulation.resource.common.controller;
-//
-//import com.css.simulation.resource.common.util.MinioUtil;
-//import io.minio.MinioClient;
-//import io.minio.errors.*;
-//import org.springframework.validation.annotation.Validated;
-//import org.springframework.web.bind.annotation.RequestBody;
-//import org.springframework.web.bind.annotation.RequestMapping;
-//import org.springframework.web.bind.annotation.RestController;
-//import org.springframework.web.multipart.MultipartFile;
-//
-//import javax.annotation.Resource;
-//import java.io.IOException;
-//import java.io.InputStream;
-//import java.security.InvalidKeyException;
-//import java.security.NoSuchAlgorithmException;
-//
-//@RestController
-//@RequestMapping("/minio")
-//public class MinioController {
-//
-//    @Resource
-//    private MinioClient minioClient;
-//
-//    @RequestMapping("/upload")
-//    public void upload(
-//            MultipartFile multipartFile,
-//            @RequestBody @Validated MinioParameter minioParameter
-//    ) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
-//        MinioUtil.uploadFromMultipartFile(
-//                minioClient,
-//                multipartFile,
-//                minioParameter.getBucketName(),
-//                minioParameter.getObjectName()
-//        );
-//    }
-//
-//    @RequestMapping("/download")
-//    public InputStream download(
-//            @RequestBody @Validated MinioParameter minioParameter
-//    ) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
-//        return MinioUtil.downloadToStream(
-//                minioClient,
-//                minioParameter.getBucketName(),
-//                minioParameter.getObjectName()
-//        );
-//    }
-//
-//}
+package com.css.simulation.resource.common.controller;
+
+import com.css.simulation.resource.common.pojo.parameter.MinioParameter;
+import com.css.simulation.resource.common.util.MinioUtil;
+import io.minio.MinioClient;
+import io.minio.errors.*;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.annotation.Resource;
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+
+@RestController
+@RequestMapping("/minio")
+public class MinioController {
+
+    @Resource
+    private MinioClient minioClient;
+
+    @RequestMapping("/upload1")
+    public void upload(
+            MultipartFile multipartFile,
+            @RequestBody @Validated MinioParameter minioParameter
+    ) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
+        MinioUtil.uploadFromMultipartFile(
+                minioClient,
+                multipartFile,
+                minioParameter.getBucketName(),
+                minioParameter.getObjectName()
+        );
+    }
+
+    @RequestMapping("/download")
+    public InputStream download(
+            @RequestBody @Validated MinioParameter minioParameter
+    ) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
+        return MinioUtil.downloadToStream(
+                minioClient,
+                minioParameter.getBucketName(),
+                minioParameter.getObjectName()
+        );
+    }
+
+}

+ 17 - 12
simulation-resource-common/src/main/java/com/css/simulation/resource/common/controller/RedisController.java

@@ -23,26 +23,31 @@ public class RedisController {
     @RequestMapping("/get")
     public ResponseBodyVO<String> get(@RequestParam("key") String key) {
         //1 获取 key 对应的值
-        String value1 = redisTemplate.opsForValue().get(key);
-        //2 存储键值对并设置时间。
-        redisTemplate.opsForValue().set("key2", "value2", Duration.ofMinutes(30L));
-        //3 获取指定 key 的剩余时间,单位秒。
-        Long time3 = redisTemplate.getExpire("key3");
-        //4 根据 key 删除
-        redisTemplate.delete("key4");
-        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
+        String value = redisTemplate.opsForValue().get(key);
+
+        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, value);
     }
 
     @RequestMapping("/set")
-    public ResponseBodyVO<String> set(@RequestParam("key") String key) {
+    public ResponseBodyVO<String> set(@RequestBody @Validated RedisParameter redisParameter) {
         //2 存储键值对并设置时间。
-        redisTemplate.opsForValue().set("key2", "value2", Duration.ofMinutes(30L));
+        redisTemplate.opsForValue().set(redisParameter.getKey(), redisParameter.getValue(), Duration.ofMinutes(30L));
+        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
+    }
+
+    @RequestMapping("/getExpire")
+    public ResponseBodyVO<Long> getExpire(@RequestParam("key") String key) {
         //3 获取指定 key 的剩余时间,单位秒。
         Long time3 = redisTemplate.getExpire("key3");
+        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, time3);
+    }
+
+
+    @RequestMapping("/delete")
+    public ResponseBodyVO<String> delete(@RequestParam("key") String key) {
         //4 根据 key 删除
-        redisTemplate.delete("key4");
+        redisTemplate.delete(key);
         return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
     }
 
-
 }

+ 18 - 0
simulation-resource-common/src/main/java/com/css/simulation/resource/common/pojo/parameter/MinioParameter.java

@@ -0,0 +1,18 @@
+package com.css.simulation.resource.common.pojo.parameter;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import javax.validation.constraints.NotBlank;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class MinioParameter {
+
+    @NotBlank(message ="bucket 名称不能为空!")
+    private String bucketName;
+    @NotBlank(message ="object 名称不能为空!")
+    private String objectName;
+}

+ 119 - 119
simulation-resource-common/src/main/java/com/css/simulation/resource/common/util/MinioUtil.java

@@ -1,119 +1,119 @@
-//package com.css.simulation.resource.common.util;
-//
-//import io.minio.*;
-//import io.minio.errors.ErrorResponseException;
-//import io.minio.errors.InsufficientDataException;
-//import io.minio.errors.InvalidResponseException;
-//import io.minio.errors.XmlParserException;
-//import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
-//import org.springframework.web.multipart.MultipartFile;
-//
-//import java.io.IOException;
-//import java.io.InputStream;
-//import java.rmi.ServerException;
-//import java.security.InvalidKeyException;
-//import java.security.NoSuchAlgorithmException;
-//
-//public class MinioUtil {
-//
-//    /**
-//     * 判断 bucket 是否存在
-//     */
-//    public static boolean isBucketExist(MinioClient minioClient, String bucketName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
-//        return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
-//    }
-//
-//    /**
-//     * 创建 bucket
-//     */
-//    public static void createBucket(MinioClient minioClient, String bucketName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
-//        if (!isBucketExist(minioClient, bucketName)) {
-//            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
-//        }
-//    }
-//
-//    /**
-//     * 通过文件路径上传文件上传文件
-//     */
-//    public static void uploadFromFile(
-//            MinioClient minioClient,
-//            String sourceFilePath,
-//            String bucketName,
-//            String objectName
-//    ) throws IOException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
-//        minioClient.uploadObject(UploadObjectArgs.builder()
-//                .filename(sourceFilePath)
-//                .bucket(bucketName)
-//                .object(objectName)
-//                .build());
-//    }
-//
-//    /**
-//     * 通过文件路径上传文件上传文件
-//     *
-//     * @param partSize 分片最小 5MB
-//     */
-//    public static void uploadFromStream(
-//            MinioClient minioClient,
-//            InputStream inputStream,
-//            long objectSize,
-//            long partSize,
-//            String bucketName,
-//            String objectName
-//    ) throws IOException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
-//        minioClient.putObject(PutObjectArgs.builder()
-//                .stream(inputStream, objectSize, partSize)
-//                .bucket(bucketName)
-//                .object(objectName)
-//                .build());
-//    }
-//
-//    /**
-//     * 通过文件路径上传文件上传文件
-//     */
-//    public static void uploadFromMultipartFile(
-//            MinioClient minioClient,
-//            MultipartFile multipartFile,
-//            String bucketName,
-//            String objectName
-//    ) throws IOException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
-//        InputStream inputStream = multipartFile.getInputStream();
-//        long objectSize = multipartFile.getSize();
-//        long partSize = 5 * 1024 * 1024L; // 分片最小 5M
-//        minioClient.putObject(PutObjectArgs.builder()
-//                .stream(inputStream, objectSize, partSize)
-//                .bucket(bucketName)
-//                .object(objectName)
-//                .build());
-//    }
-//
-//    /**
-//     * 下载文件
-//     */
-//    public static void downloadToFile(
-//            MinioClient minioClient,
-//            String bucketName,
-//            String objectName,
-//            String targetFilePath
-//    ) throws IOException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
-//        minioClient.downloadObject(DownloadObjectArgs.builder()
-//                .bucket(bucketName)
-//                .object(objectName)
-//                .filename(targetFilePath)
-//                .build());
-//    }
-//
-//    /**
-//     * 下载文件
-//     */
-//    public static InputStream downloadToStream(
-//            MinioClient minioClient,
-//            String bucketName,
-//            String objectName
-//    ) throws IOException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
-//        return minioClient.getObject(GetObjectArgs.builder()
-//                .bucket(bucketName)
-//                .object(objectName)
-//                .build());
-//    }
-//}
+package com.css.simulation.resource.common.util;
+
+import io.minio.*;
+import io.minio.errors.ErrorResponseException;
+import io.minio.errors.InsufficientDataException;
+import io.minio.errors.InvalidResponseException;
+import io.minio.errors.XmlParserException;
+import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.rmi.ServerException;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+
+public class MinioUtil {
+
+    /**
+     * 判断 bucket 是否存在
+     */
+    public static boolean isBucketExist(MinioClient minioClient, String bucketName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
+        return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
+    }
+
+    /**
+     * 创建 bucket
+     */
+    public static void createBucket(MinioClient minioClient, String bucketName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
+        if (!isBucketExist(minioClient, bucketName)) {
+            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
+        }
+    }
+
+    /**
+     * 通过文件路径上传文件上传文件
+     */
+    public static void uploadFromFile(
+            MinioClient minioClient,
+            String sourceFilePath,
+            String bucketName,
+            String objectName
+    ) throws IOException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
+        minioClient.uploadObject(UploadObjectArgs.builder()
+                .filename(sourceFilePath)
+                .bucket(bucketName)
+                .object(objectName)
+                .build());
+    }
+
+    /**
+     * 通过文件路径上传文件上传文件
+     *
+     * @param partSize 分片最小 5MB
+     */
+    public static void uploadFromStream(
+            MinioClient minioClient,
+            InputStream inputStream,
+            long objectSize,
+            long partSize,
+            String bucketName,
+            String objectName
+    ) throws IOException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
+        minioClient.putObject(PutObjectArgs.builder()
+                .stream(inputStream, objectSize, partSize)
+                .bucket(bucketName)
+                .object(objectName)
+                .build());
+    }
+
+    /**
+     * 通过文件路径上传文件上传文件
+     */
+    public static void uploadFromMultipartFile(
+            MinioClient minioClient,
+            MultipartFile multipartFile,
+            String bucketName,
+            String objectName
+    ) throws IOException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
+        InputStream inputStream = multipartFile.getInputStream();
+        long objectSize = multipartFile.getSize();
+        long partSize = 5 * 1024 * 1024L; // 分片最小 5M
+        minioClient.putObject(PutObjectArgs.builder()
+                .stream(inputStream, objectSize, partSize)
+                .bucket(bucketName)
+                .object(objectName)
+                .build());
+    }
+
+    /**
+     * 下载文件
+     */
+    public static void downloadToFile(
+            MinioClient minioClient,
+            String bucketName,
+            String objectName,
+            String targetFilePath
+    ) throws IOException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
+        minioClient.downloadObject(DownloadObjectArgs.builder()
+                .bucket(bucketName)
+                .object(objectName)
+                .filename(targetFilePath)
+                .build());
+    }
+
+    /**
+     * 下载文件
+     */
+    public static InputStream downloadToStream(
+            MinioClient minioClient,
+            String bucketName,
+            String objectName
+    ) throws IOException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
+        return minioClient.getObject(GetObjectArgs.builder()
+                .bucket(bucketName)
+                .object(objectName)
+                .build());
+    }
+}