浏览代码

冲突合并

zhenglingfeng 8 年之前
父节点
当前提交
5e595cad3b

+ 8 - 0
hos-arbiter/src/main/java/com/yihu/hos/arbiter/services/ServiceFlowService.java

@ -1,6 +1,8 @@
package com.yihu.hos.arbiter.services;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.BasicDBObject;
import com.mongodb.WriteResult;
import com.yihu.hos.arbiter.models.BrokerServer;
import com.yihu.hos.core.http.HTTPResponse;
import com.yihu.hos.core.http.HttpClientKit;
@ -63,7 +65,13 @@ public class ServiceFlowService {
        update.set("flowType", serviceFlow.getFlowType());
        update.set("tenant", serviceFlow.getTenant());
        //删除旧记录
        Update delete = new Update();
        for (ServiceFlow.HandleFile handleFile : serviceFlow.getHandleFiles()) {
            delete.pull("handleFiles",new BasicDBObject("className",handleFile.getClassName()));
            Query deleteQuery = Query.query(Criteria.where("className").is(handleFile.getClassName()));
            WriteResult result = mongoOperations.updateFirst(deleteQuery, delete, ServiceFlow.HandleFile.class);
            update.addToSet("handleFiles", handleFile);
        }

+ 0 - 1
hos-broker/src/main/java/com/yihu/hos/broker/common/camelrouter/CenterMongoRouter.java

@ -1,7 +1,6 @@
package com.yihu.hos.broker.common.camelrouter;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.bson.types.ObjectId;
import org.json.JSONArray;

+ 1 - 1
hos-broker/src/main/java/com/yihu/hos/broker/services/ServiceMonitorService.java

@ -60,7 +60,7 @@ public class ServiceMonitorService {
            bandwidth(beginTime, endTime, codeList);
            qps(beginTime, endTime, codeList);
            delay(beginTime, endTime, codeList);
//            delay(beginTime, endTime, codeList);
            usage(beginTime, endTime, codeList);
        } catch (Exception e) {
            e.printStackTrace();

+ 29 - 21
hos-broker/src/main/java/com/yihu/hos/broker/services/camel/ESBCamelService.java

@ -6,7 +6,6 @@ import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.result.DeleteResult;
import com.yihu.hos.broker.configurations.MongoConfiguration;
import com.yihu.hos.core.constants.CoreConstant;
import com.yihu.hos.core.datatype.ClassFileUtil;
@ -53,7 +52,7 @@ public class ESBCamelService {
    private MongoClient mongoClient;
    private String serviceFlow = "serviceFlow";
    private String configuration = "configuration";
    private String configuration = "runtime";
    @Autowired
    private ObjectMapper objectMapper;
    @Value("${hos.esb.rest-url}")
@ -108,26 +107,28 @@ public class ESBCamelService {
            if (serviceFlowValid.is()) return Result.error("必要的入参数据不正确,请检查!");
            ServiceFlow serviceFlow = serviceFlowValid.getServiceFlow();
            ServiceFlow.HandleFile handleFile = serviceFlowValid.getHandleFile();
            SystemCamelContext.stopRouter(serviceFlow.getRouteCode());
            SystemCamelContext.removeRouter(serviceFlow.getRouteCode());
            boolean created;
            if ("java".equals(handleFile.getFileType())) {
                created = this.generateClassFile(handleFile);
            } else {
                created = this.createClassFile(handleFile);
            }
            if (!created) {
                return Result.error("服务流程变更增加失败!");
            ArrayList<ServiceFlow.HandleFile> handleFiles = serviceFlowValid.getHandleFiles();
            for (ServiceFlow.HandleFile handleFile : handleFiles) {
                if ("java".equals(handleFile.getFileType())) {
                    created = this.generateClassFile(handleFile);
                } else {
                    created = this.createClassFile(handleFile);
                }
                if (!created) {
                    return Result.error("服务流程变更增加失败!");
                }
            }
            SystemCamelContext.stopRouter(serviceFlow.getRouteCode());
            SystemCamelContext.removeRouter(serviceFlow.getRouteCode());
            this.addRouter(serviceFlowValid.getHandleFiles());
            SystemCamelContext.startRouter(serviceFlow.getRouteCode());
            return Result.error("服务流程变更增加成功!");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e);
//            logger.error(e);
            return Result.error("服务流程变更增加失败!");
        }
    }
@ -260,7 +261,6 @@ public class ESBCamelService {
        SystemCamelContext.putClassMapping(handleFile.getRouteCode(), handleFile.getPackageName(), handleFile.getUsage(), handleFile.getClassName());
        URL resource = SystemCamelContext.getResource(this);
//        FileOutputStream outputStream = ClassFileUtil.createFile(resource, handleFile.getPackageName(), handleFile.getClassName(), ClassFileUtil.JAVA_FILE);
        String fileName = DES.decrypt(handleFile.getFilePath(), DES.COMMON_PASSWORD);
//        MongoDatabase db = mongoConfig.mongoClient().getDatabase(dbName);
//
//        boolean read = GridFSUtil.readFileContent(db, outputStream, fileName);
@ -285,14 +285,15 @@ public class ESBCamelService {
            String classPath =  resource.getPath()+ packagePath + "/" + handleFile.getClassName() + ClassFileUtil.CLASS_FILE;
//            GridFSUtil.uploadFile(classPath, handleFile.getClassName() + handleFile.getRouteCode() + ClassFileUtil.CLASS_FILE, null);
//            TODO 上传到本地mongodb和中心mongodb
            String enFileName = DES.encrypt(handleFile.getClassName() + handleFile.getRouteCode() + ClassFileUtil.CLASS_FILE, DES.COMMON_PASSWORD);
            String fileName = DES.decrypt(handleFile.getFilePath(), DES.COMMON_PASSWORD).replaceAll(ClassFileUtil.JAVA_FILE,ClassFileUtil.CLASS_FILE);
            String enFileName = DES.encrypt(fileName, DES.COMMON_PASSWORD);
            String uploadUrl = centerUrl + "/tenant/upload/" + enFileName;
            ClassFileUtil.uploadFile(uploadUrl,new File(classPath),handleFile.getClassName() + handleFile.getRouteCode() + ClassFileUtil.CLASS_FILE);
            ClassFileUtil.uploadFile(uploadUrl,new File(classPath),fileName);
        }
        return succ;
    }
    private void deleteClassFile(ServiceFlow.HandleFile handleFile) {
    private void deleteClassFile(ServiceFlow.HandleFile handleFile) throws Exception {
//        String className = SystemCamelContext.getClassMapping(handleFile.getRouteCode(), handleFile.getClassName(), handleFile.getUsage());
//        if (StringUtil.isEmpty(className)) {
//            return;
@ -305,9 +306,16 @@ public class ESBCamelService {
        ClassFileUtil.deleteClassfile(classPath);
        ClassFileUtil.deleteClassfile(javaPath);
        //TODO 从中心删除
        String uploadUrl = centerUrl + "/tenant/delFile/" + javaPath;
        ClassFileUtil.deleteFile(uploadUrl);
        //删除class文件
        String delJavaUrl = centerUrl + "/tenant/delFile/" + handleFile.getFilePath();
        ClassFileUtil.deleteFile(delJavaUrl);
        //删除java文件
        String classFilePath = DES.decrypt( handleFile.getFilePath(),DES.COMMON_PASSWORD);
        String javaFilePath = classFilePath.replace(ClassFileUtil.CLASS_FILE,ClassFileUtil.JAVA_FILE);
        String enJavaPath = DES.encrypt(javaFilePath,DES.COMMON_PASSWORD);
        String delClassUrl = centerUrl + "/tenant/delFile/" + enJavaPath;
         ClassFileUtil.deleteFile(delClassUrl);
        // 完成
        logger.info("===================" + handleFile.getPackageName() + CoreConstant.DOT + handleFile.getClassName() + ".class 删除过程结束");
    }
@ -321,7 +329,7 @@ public class ESBCamelService {
        try {
            MongoDatabase db = mongoClient.getDatabase(configuration);
            MongoCollection<Document> collection = db.getCollection(serviceFlow);
            DeleteResult result = collection.deleteMany(Filters.eq("routeCode", routeCode));
            collection.deleteMany(Filters.eq("routeCode", routeCode));
        } catch (Exception e) {
            e.printStackTrace();
        }

+ 55 - 17
hos-core/src/main/java/com/yihu/hos/core/datatype/ClassFileUtil.java

@ -1,6 +1,7 @@
package com.yihu.hos.core.datatype;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.hos.core.file.FileUtil;
import com.yihu.hos.core.log.Logger;
import com.yihu.hos.core.log.LoggerFactory;
@ -15,12 +16,10 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Map;
public class ClassFileUtil {
    public static final String CLASS_FILE = ".class";
@ -216,8 +215,8 @@ public class ClassFileUtil {
    public static String uploadFile(String uploadUrl, File file,String fileName) {
        try {
            byte[] bytes = upload(uploadUrl, file,fileName);//文件内容
            if (bytes!=null){
            boolean succ = upload(uploadUrl, file,fileName);//文件内容
            if (succ){
                return fileName;
            }else {
                return null;
@ -260,12 +259,12 @@ public class ClassFileUtil {
    }
    /**
     *  http 上传文件请求
     *  http 上传文件到中心请求
     * @param url  请求地址
     * @param file  文件名
     * @return
     */
    public static byte[] upload( String url,File file,String fileName) {
    public static boolean upload( String url,File file,String fileName) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpResponse response = null;
@ -279,11 +278,20 @@ public class ClassFileUtil {
            httpPost.setEntity(reqEntity1.build());
            response = httpClient.execute(httpPost);
            response.setHeader( "Content-Type", "application/octet-stream;charset=UTF-8");
            response.setHeader( "Content-Type", "application/json;charset=UTF-8");
            entity = response.getEntity();
            byte[] bytes= FileUtil.getBytesByStream(entity.getContent());
//            EntityUtils.toString(response.getEntity(), "UTF-8");
            return bytes;
            int code = response.getStatusLine().getStatusCode();
            Map<String,Object> responMap = parseRespon(entity);
            if (code == 200){
                boolean succ = (boolean) responMap.get("successFlg");
                if (!succ){
                    System.out.println("请求成功,上传失败!message:"+responMap.get("message").toString());;
                }
                return succ;
            }else {
                System.out.println("上传请求失败,status:"+code);
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
@ -295,10 +303,15 @@ public class ClassFileUtil {
            }
        }
        return null;
        return false;
    }
    public static String deleteFile( String url) {
    /**
     * http请求 从管理中心删除文件
     * @param url
     * @return
     */
    public static boolean deleteFile( String url) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpResponse response = null;
@ -307,9 +320,20 @@ public class ClassFileUtil {
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(1000*60*5).setConnectTimeout(1000*60*5).build();//设置请求和传输超时时间
            httpPost.setConfig(requestConfig);
            response = httpClient.execute(httpPost);
            response.setHeader( "Content-Type", "application/octet-stream;charset=UTF-8");
            response.setHeader( "Content-Type", "application/json;charset=UTF-8");
            entity = response.getEntity();
            return entity.toString();
            int code = response.getStatusLine().getStatusCode();
            Map<String,Object> responMap = parseRespon(entity);
            if (code == 200){
                boolean succ = (boolean) responMap.get("successFlg");
                if (!succ){
                    System.out.println("请求成功,删除失败!message:"+responMap.get("message").toString());;
                }
                return succ;
            }else {
                System.out.println("删除请求失败,status:"+code);
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
@ -321,7 +345,21 @@ public class ClassFileUtil {
            }
        }
        return null;
        return false;
    }
    public static  Map<String,Object> parseRespon(HttpEntity entity ) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
        String line = null;
        while ((line = br.readLine()) != null) {
            stringBuilder.append(line);
        }
        String responseText = stringBuilder.toString();
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String,Object> map = objectMapper.readValue(responseText,Map.class);
        return map;
    }
}

+ 1 - 1
src/main/java/com/yihu/hos/datacollect/service/DatacollectManager.java

@ -247,7 +247,7 @@ public class DatacollectManager{
    @Transactional
    public ActionResult updateJob(RsJobConfig obj, String cron, String jobDataset) throws Exception {
        Integer flowId = flowManage.updateCamelFile(obj.getFlowTempId(), obj.getFlowId(), cron);
        Integer flowId = flowManage.updateCamelFile(obj.getId(),obj.getFlowTempId(), obj.getFlowId(), cron);
        if (flowId!=null){
            datacollectDao.updateEntity(obj);
            saveJobDataset(obj.getId(), jobDataset);

+ 39 - 16
src/main/java/com/yihu/hos/system/service/FlowManager.java

@ -524,38 +524,60 @@ public class FlowManager {
     * @throws Exception
     */
    public Integer updateCamelFile(Integer flowTempId, Integer flowId, String newCron) throws Exception {
    public Integer updateCamelFile(String jobId,Integer flowTempId, Integer flowId, String newCron) throws Exception {
         /* 修改route文件无需重新生成flowClass记录,文件名根据className+routeId 生成;*/
        List<SystemServiceFlowTemp> flowTempRouters = flowTempDao.getFlowTemps(flowTempId, ServiceFlowConstant.FLOW_TYPE_ROUTE);
        List<SystemServiceFlowTemp> flowTempProces = flowTempDao.getFlowTemps(flowTempId, ServiceFlowConstant.FLOW_TYPE_PROCESSOR);
        SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, flowId);
        //route模板文件记录是否存在。不存在就返回。
        if (!flowTempRouters.isEmpty()) {
            SystemServiceFlowTemp flowTemp = flowTempRouters.get(0);
            StringBuilder basePath = new StringBuilder();
            ;
            if (flowTemp.getPackageName() != null) {
                String packagePath[] = flowTemp.getPackageName().split("\\.");
                for (int i = 0; i < packagePath.length; i++) {
                    basePath.append(packagePath[i]).append("/");
            ArrayList<ServiceFlow.HandleFile> handleFiles = new ArrayList<>();
            ServiceFlow serviceFlow = new ServiceFlow();
            //新增processor记录
            for (SystemServiceFlowTemp process : flowTempProces) {
                String deName = DES.decrypt(process.getClassPath(), DES.COMMON_PASSWORD);//吉阿米果的文件名
                String newFileName = genProcessorJavaFile(jobId, flow.getCode(), deName, process.getClassName());
                //生成新的java文件
                String enNewFileName = DES.encrypt(newFileName, DES.COMMON_PASSWORD);
                if (newFileName != null) {
                    //发送消息
                    serviceFlow.setRouteCode(flow.getCode());
                    serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
                    ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
                    handleFile.setRouteCode(flow.getCode());
                    handleFile.setFileType(ServiceFlowConstant.JAVA);
                    handleFile.setPackageName(process.getPackageName());
                    handleFile.setClassName(process.getClassName());
                    handleFile.setFilePath(enNewFileName);
                    handleFile.setUsage(ServiceFlowConstant.FLOW_TYPE_PROCESSOR);
                    handleFiles.add(handleFile);
                    serviceFlow.setHandleFiles(handleFiles);
                } else {
                    System.out.println("生成processor的java文件过程出错");
                    return null;
                }
            }
            String deName = DES.decrypt(flowTemp.getClassPath(), DES.COMMON_PASSWORD);//吉阿米果的文件名
            String deName = DES.decrypt(flowTemp.getClassPath(), DES.COMMON_PASSWORD);//吉阿米果的文件名
            //生成新的route文件
            String newFileName = genRouteJavaFile(flow.getCode(), flowTemp.getClassName(), deName, newCron);
            String enNewFileName = DES.encrypt(newFileName, DES.COMMON_PASSWORD);
            ServiceFlow serviceFlow = new ServiceFlow();
            serviceFlow.setRouteCode(flow.getCode());
            serviceFlow.setFlowType(ServiceFlowConstant.JAVA);
            ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
            handleFile.setRouteCode(flow.getCode());
            handleFile.setFileType(ServiceFlowConstant.JAVA);
            handleFile.setPackageName(basePath.toString());
            handleFile.setPackageName(flowTemp.getPackageName());
            handleFile.setClassName(flowTemp.getClassName() + flow.getCode());
            handleFile.setFilePath(enNewFileName);
            handleFile.setUsage(ServiceFlowConstant.FLOW_TYPE_ROUTE);
            ArrayList<ServiceFlow.HandleFile> handleFiles = new ArrayList<>();
            handleFiles.add(handleFile);
            serviceFlow.setHandleFiles(handleFiles);
            serviceFlowEventService.serviceFlowModifiedAdd(serviceFlow);
@ -583,7 +605,9 @@ public class FlowManager {
            //新增processor记录
            String deName = DES.decrypt(flowTemp.getClassPath(), DES.COMMON_PASSWORD);//吉阿米果的文件名
            String enClassName = DES.encrypt(deName.replace(".java", ".class"), DES.COMMON_PASSWORD);//生成机密过的classPath
            String newFileName = genRouteJavaFile(newFlow.getCode(), flowTemp.getClassName(), deName, newCron);
            String enClassName = DES.encrypt(newFileName.replace(".java", ".class"), DES.COMMON_PASSWORD);//生成机密过的classPath
            SystemServiceFlowClass newFlowClass = new SystemServiceFlowClass();
            newFlowClass.setPackageName(flowTemp.getPackageName());
            newFlowClass.setClassName(flowTemp.getClassName() + newFlow.getCode());
@ -593,8 +617,7 @@ public class FlowManager {
            flowClassDao.saveEntity(newFlowClass);
            newFlowClass.setIsUpdate("1");
            //生成新的route文件
            String newFileName = genRouteJavaFile(newFlow.getCode(), flowTemp.getClassName(), deName, newCron);
            String enNewFileName = DES.encrypt(newFileName, DES.COMMON_PASSWORD);
            String enNewFileName = DES.encrypt(newFileName, DES.COMMON_PASSWORD);//加密文件名
            if (newFileName != null) {
                ServiceFlow serviceFlow = new ServiceFlow();
@ -653,8 +676,9 @@ public class FlowManager {
                }
                String deName = DES.decrypt(process.getClassPath(), DES.COMMON_PASSWORD);//吉阿米果的文件名
                String enClassName = DES.encrypt(deName.replace(".java", ".class"), DES.COMMON_PASSWORD);//生成机密过的classPath
                String newFileName = genProcessorJavaFile(jobId, newFlow.getCode(), deName, process.getClassName());
                String enClassName = DES.encrypt(newFileName.replace(".java", ".class"), DES.COMMON_PASSWORD);//生成机密过的classPath
                SystemServiceFlowClass processClass = new SystemServiceFlowClass();
                processClass.setPackageName(process.getPackageName());
                processClass.setClassName(process.getClassName());
@ -663,7 +687,6 @@ public class FlowManager {
                processClass.setType(ServiceFlowConstant.FLOW_TYPE_PROCESSOR);
                processClass.setIsUpdate("1");
                //生成新的java文件
                String newFileName = genProcessorJavaFile(jobId, newFlow.getCode(), deName, processClass.getClassName());
                String enNewFileName = DES.encrypt(newFileName, DES.COMMON_PASSWORD);
                if (newFileName != null) {