martin 3 years ago
parent
commit
972f193942

+ 0 - 32
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/configuration/feign/OpenFeignConfiguration.java

@@ -1,32 +0,0 @@
-package com.css.simulation.resource.scheduler.configuration.feign;
-
-import feign.Logger;
-import feign.RequestInterceptor;
-import feign.RequestTemplate;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.web.context.request.RequestContextHolder;
-import org.springframework.web.context.request.ServletRequestAttributes;
-import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
-
-import javax.servlet.http.HttpServletRequest;
-
-/**
- * 转发 token
- */
-@Configuration
-public class OpenFeignConfiguration implements RequestInterceptor, WebMvcConfigurer {
-
-
-    @Bean
-    public Logger.Level feignLoggerLevel() {
-        return Logger.Level.FULL;
-    }
-
-    @Override
-    public void apply(RequestTemplate requestTemplate) {
-        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
-        HttpServletRequest request = attributes.getRequest();
-        requestTemplate.header("Authorization", request.getHeader("Authorization"));
-    }
-}

+ 6 - 3
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/configuration/kubernetes/KubernetesConfiguration.java

@@ -5,7 +5,6 @@ import io.kubernetes.client.util.ClientBuilder;
 import io.kubernetes.client.util.KubeConfig;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
-import org.springframework.util.ResourceUtils;
 
 import java.io.File;
 import java.io.FileReader;
@@ -16,9 +15,13 @@ public class KubernetesConfiguration {
 
     @Bean
     public ApiClient apiClient() throws IOException {
-        File config = ResourceUtils.getFile("classpath:kubernetes/config");  // resource
+//        File config = ResourceUtils.getFile("classpath:kubernetes/config");  // 开发环境可用,生产环境不行,无法从jar 包读取
 //        File config = new File("D:\\idea-project\\simulation-cloud\\simulation-resource-scheduler\\src\\main\\resources\\kubernetes\\config");  //windows
-//        File config = new File("/root/.kube/config");   //linux
+        File config = new File("/root/.kube/config");   //linux
+//
+//        ClassPathResource classPathResource = new ClassPathResource("kubernetes/config");
+//        InputStream inputStream =classPathResource.getInputStream();
+//        FileUtil.writeInputStreamToLocalFile();
         FileReader fileReader = new FileReader(config);
         return ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(fileReader)).build();
     }

+ 18 - 20
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/consumer/ManualProjectConsumer.java

@@ -4,13 +4,11 @@ package com.css.simulation.resource.scheduler.consumer;
 import api.common.pojo.constants.DictConstants;
 import api.common.pojo.dto.ProjectMessageDTO;
 import api.common.util.JsonUtil;
-import api.common.util.LinuxUtil;
 import api.common.util.StringUtil;
 import api.common.util.TimeUtil;
 import com.css.simulation.resource.scheduler.mapper.*;
 import com.css.simulation.resource.scheduler.pojo.po.*;
 import com.css.simulation.resource.scheduler.pojo.to.*;
-import com.css.simulation.resource.scheduler.util.MinioUtil;
 import io.kubernetes.client.openapi.ApiClient;
 import io.kubernetes.client.openapi.apis.BatchV1Api;
 import io.kubernetes.client.openapi.models.V1Job;
@@ -220,24 +218,24 @@ public class ManualProjectConsumer {
         }
 
         // -------------------------------- 4 算法导入(一期按单机版做) --------------------------------
-        // 私有仓库导入算法镜像(搭建私有仓库)
-        String algorithmId = projectMessageDTO.getAlgorithmId();    // 算法 id
-        //4-1 根据算法 id 获取算法文件地址、是否已导入成镜像。
-        AlgorithmPO algorithmPO = algorithmMapper.selectById(algorithmId);
-        String minioPath = algorithmPO.getMinioPath();
-        String dockerImage;
-        if ("0".equals(algorithmPO.getDockerImport())) {
-            dockerImage = "algorithm_" + algorithmId + ":latest";
-            String algorithmTarLinuxTempPath = linuxTempPath + minioPath;
-            // 下载算法文件到本地( 2 到仓库服务器)
-            MinioUtil.downloadToFile(minioClient, bucketName, minioPath, algorithmTarLinuxTempPath);
-            //4-2 本地执行 docker load 算法文件成镜像(集群版可改成用 docker-java 操作仓库)
-            LinuxUtil.execute("docker import " + algorithmTarLinuxTempPath + " " + dockerImage);
-        } else if ("1".equals(algorithmPO.getDockerImport()) && StringUtil.isNotEmpty(algorithmPO.getDockerImage())) {
-            dockerImage = algorithmPO.getDockerImage();
-        } else {
-            throw new RuntimeException("算法 " + algorithmId + "的 mysql 数据有误!");
-        }
+//        // 私有仓库导入算法镜像(搭建私有仓库)
+//        String algorithmId = projectMessageDTO.getAlgorithmId();    // 算法 id
+//        //4-1 根据算法 id 获取算法文件地址、是否已导入成镜像。
+//        AlgorithmPO algorithmPO = algorithmMapper.selectById(algorithmId);
+//        String minioPath = algorithmPO.getMinioPath();
+//        String dockerImage;
+//        if ("0".equals(algorithmPO.getDockerImport())) {
+//            dockerImage = "algorithm_" + algorithmId + ":latest";
+//            String algorithmTarLinuxTempPath = linuxTempPath + minioPath;
+//            // 下载算法文件到本地( 2 到仓库服务器)
+//            MinioUtil.downloadToFile(minioClient, bucketName, minioPath, algorithmTarLinuxTempPath);
+//            //4-2 本地执行 docker load 算法文件成镜像(集群版可改成用 docker-java 操作仓库)
+//            LinuxUtil.execute("docker import " + algorithmTarLinuxTempPath + " " + dockerImage);
+//        } else if ("1".equals(algorithmPO.getDockerImport()) && StringUtil.isNotEmpty(algorithmPO.getDockerImage())) {
+//            dockerImage = algorithmPO.getDockerImage();
+//        } else {
+//            throw new RuntimeException("算法 " + algorithmId + "的 mysql 数据有误!");
+//        }
         // -------------------------------- 5 创建 pod 开始执行 --------------------------------
         int completions = sceneList.size();     // 结束标
         int parallelism = projectMessageDTO.getParallelism();    // 并行度

+ 62 - 0
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/controller/KafkaController.java

@@ -0,0 +1,62 @@
+package com.css.simulation.resource.scheduler.controller;
+
+import api.common.pojo.dto.ProjectMessageDTO;
+import api.common.util.JsonUtil;
+import com.css.simulation.resource.scheduler.consumer.ManualProjectConsumer;
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.kafka.core.KafkaTemplate;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RequestMapping("/kafka")
+@RestController
+@Slf4j
+public class KafkaController {
+
+    @Autowired
+    KafkaTemplate<String, String> kafkaTemplate;
+    @Autowired
+    ManualProjectConsumer manualProjectConsumer;
+
+    @PostMapping("/hello")
+    public void hello() {
+        kafkaTemplate.send("hello", "hello world!").addCallback(success -> {
+            // 消息发送到的topic
+            String topic = success.getRecordMetadata().topic();
+            // 消息发送到的分区
+            int partition = success.getRecordMetadata().partition();
+            // 消息在分区内的offset
+            long offset = success.getRecordMetadata().offset();
+            log.info("------- 发送消息成功:\n"
+                    + "主题 topic 为:" + topic + "\n"
+                    + "分区 partition 为:" + partition + "\n"
+                    + "偏移量为:" + offset);
+        }, failure -> {
+            log.error("------- 发送消息失败:" + failure.getMessage());
+        });
+    }
+
+
+    @PostMapping("/send")
+    @SneakyThrows
+    public void send(@RequestBody ProjectMessageDTO projectMessageDTO) {
+        kafkaTemplate.send(projectMessageDTO.getProjectId(), JsonUtil.beanToJson(projectMessageDTO)).addCallback(success -> {
+            // 消息发送到的topic
+            String topic = success.getRecordMetadata().topic();
+            // 消息发送到的分区
+            int partition = success.getRecordMetadata().partition();
+            // 消息在分区内的offset
+            long offset = success.getRecordMetadata().offset();
+            log.info("------- 发送消息成功:\n"
+                    + "主题 topic 为:" + topic + "\n"
+                    + "分区 partition 为:" + partition + "\n"
+                    + "偏移量为:" + offset);
+        }, failure -> {
+            log.error("------- 发送消息失败:" + failure.getMessage());
+        });
+    }
+}

+ 1 - 1
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/controller/TaskController.java

@@ -20,7 +20,7 @@ public class TaskController {
     @Value("${hello}")
     private String hello;
 
-    @GetMapping("/hello")
+    @RequestMapping("/hello")
     public String hello(){
         return hello;
     }

+ 0 - 26
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/controller/TestConsumerController.java

@@ -1,26 +0,0 @@
-//package com.css.simulation.resource.scheduler.controller;
-//
-//import api.common.pojo.dto.ProjectMessageDTO;
-//import api.common.util.JsonUtil;
-//import com.css.simulation.resource.scheduler.consumer.ManualProjectConsumer;
-//import io.kubernetes.client.openapi.ApiException;
-//import org.springframework.beans.factory.annotation.Autowired;
-//import org.springframework.web.bind.annotation.PostMapping;
-//import org.springframework.web.bind.annotation.RequestBody;
-//import org.springframework.web.bind.annotation.RequestMapping;
-//import org.springframework.web.bind.annotation.RestController;
-//
-//import java.io.IOException;
-//
-//@RequestMapping("/test")
-//@RestController
-//public class TestConsumerController {
-//
-//    @Autowired
-//    ManualProjectConsumer manualProjectConsumer;
-//
-//    @PostMapping("/consumer")
-//    public void hello(@RequestBody ProjectMessageDTO projectMessageDTO) throws IOException, ApiException {
-//        manualProjectConsumer.testParseProject(JsonUtil.beanToJson(projectMessageDTO));
-//    }
-//}

+ 2 - 0
simulation-resource-scheduler/src/main/resources/bootstrap-aliyun.yaml

@@ -4,6 +4,8 @@ spring:
       discovery:
         server-addr: 47.94.105.148:8848
         namespace: 3698bfc2-a612-487a-b2a2-aaad16cd9d9d
+        ip: 182.92.203.182
+        port: 8002
       config:
         server-addr: 47.94.105.148:8848
         namespace: 3698bfc2-a612-487a-b2a2-aaad16cd9d9d

+ 1 - 1
simulation-resource-scheduler/src/main/resources/bootstrap.yml

@@ -2,4 +2,4 @@ spring:
   application:
     name: simulation-resource-scheduler
   profiles:
-    active: dev
+    active: aliyun

+ 1 - 1
simulation-resource-scheduler/src/main/resources/kubernetes/config

@@ -2,7 +2,7 @@ apiVersion: v1
 clusters:
 - cluster:
     certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMvakNDQWVhZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJeU1ETXlNekEzTURNMU9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GUnAzUk1OMmdtWElpRmlnVjcwKwoyQkE4OHhyV0QzRVprb2p3RnVyNlN0SjR4TlRZOWRmYzc3ZnhMU2dpRU56ZXROZ3BieUdBZDZQdGl3MXN6dDRuCktBa1d4c1hiSmVLTFUySys2cFpSTk9wRUdiQ2x3dkFHVDBRSDJ2MFdqZGFmaGNCS1JOa0E4ZWZmeXpkVitXSEgKeERJPQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
-    server: https://172.16.0.62:6443
+    server: https://182.92.203.182:6443
   name: kubernetes
 contexts:
 - context: