diff --git a/pom.xml b/pom.xml index 045de88..af59905 100644 --- a/pom.xml +++ b/pom.xml @@ -9,6 +9,7 @@ opai-api ocr-service asr-service + translate-service diff --git a/translate-service/.classpath b/translate-service/.classpath new file mode 100644 index 0000000..f7e4a1d --- /dev/null +++ b/translate-service/.classpath @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/translate-service/.gitignore b/translate-service/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/translate-service/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/translate-service/.project b/translate-service/.project new file mode 100644 index 0000000..d586162 --- /dev/null +++ b/translate-service/.project @@ -0,0 +1,23 @@ + + + translate-service + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/translate-service/.settings/org.eclipse.core.resources.prefs b/translate-service/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..839d647 --- /dev/null +++ b/translate-service/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 +encoding//src/test/java=UTF-8 +encoding/=UTF-8 diff --git a/translate-service/.settings/org.eclipse.jdt.core.prefs b/translate-service/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..71df522 --- /dev/null +++ b/translate-service/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,9 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.methodParameters=generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/translate-service/.settings/org.eclipse.m2e.core.prefs b/translate-service/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/translate-service/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/translate-service/pom.xml b/translate-service/pom.xml new file mode 100644 index 0000000..76594d3 --- /dev/null +++ b/translate-service/pom.xml @@ -0,0 +1,187 @@ + + + 4.0.0 + + com.bw + opai-service-center + 0.0.1-SNAPSHOT + + com.bw + translate-service + 0.0.1-SNAPSHOT + translate-service + http://maven.apache.org + + UTF-8 + + + + org.springframework.boot + spring-boot-starter-web + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + org.springframework.boot + spring-boot-starter-actuator + + + org.projectlombok + lombok + + + com.alibaba + fastjson + 2.0.17 + + + org.apache.httpcomponents + httpclient + 4.5.3 + + + org.apache.httpcomponents + httpmime + 4.5.13 + + + commons-lang + commons-lang + 2.6 + + + + com.squareup.okhttp3 + okhttp + 4.9.3 + + + org.springframework.kafka + spring-kafka + + + + org.apache.poi + poi + 4.1.2 + + + org.apache.poi + poi-ooxml + 4.1.2 + + + + + org.apache.poi + ooxml-schemas + 1.4 + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + + org.springframework.boot + spring-boot-maven-plugin + + com.bw.translate.Application + ZIP + + + ${project.groupId} + ${project.artifactId} + + + + + + + repackage + + + + + + org.apache.maven.plugins + maven-dependency-plugin + 3.1.1 + + + copy + package + + copy-dependencies + + + jar + jar + runtime + ${project.build.directory}/libs + + + + + + + + diff --git a/translate-service/src/main/java/com/bw/translate/Application.java b/translate-service/src/main/java/com/bw/translate/Application.java new file mode 100644 index 0000000..66c37eb --- /dev/null +++ b/translate-service/src/main/java/com/bw/translate/Application.java @@ -0,0 +1,19 @@ +package com.bw.translate; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + + +/** + * 系统接口启动类 + * @author jian.mao + * @date 2025年12月30日 + * @description + */ +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/translate-service/src/main/java/com/bw/translate/cache/ConfigCache.java b/translate-service/src/main/java/com/bw/translate/cache/ConfigCache.java new file mode 100644 index 0000000..3e7c46b --- /dev/null +++ b/translate-service/src/main/java/com/bw/translate/cache/ConfigCache.java @@ -0,0 +1,35 @@ +package com.bw.translate.cache; + +import lombok.extern.slf4j.Slf4j; + +import java.util.Map; +import java.util.concurrent.LinkedBlockingDeque; + +/** + * @author jian.mao + * @date 2022年11月11日 + * @description 静态变量类 + */ +@Slf4j +public class ConfigCache { + + /**启动条件**/ + public static boolean isStart = true; + /*****任务队列*****/ + public static LinkedBlockingDeque> taskQueue = new LinkedBlockingDeque>(); + + + /** + * 队列录入任务 + * @param queue + * @param task + */ + public static void putQueue(LinkedBlockingDeque> queue,Map task){ + //next app 写入队列准备调出 + try { + queue.put(task); + } catch (InterruptedException e) { + log.error("队列写入data失败---"); + } + } +} diff --git a/translate-service/src/main/java/com/bw/translate/controller/TaskReceiveController.java b/translate-service/src/main/java/com/bw/translate/controller/TaskReceiveController.java new file mode 100644 index 0000000..3ff0b17 --- /dev/null +++ b/translate-service/src/main/java/com/bw/translate/controller/TaskReceiveController.java @@ -0,0 +1,39 @@ +package com.bw.translate.controller; + +import javax.annotation.Resource; + +import org.springframework.stereotype.Controller; +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.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.bw.translate.service.TaskReceiveService; + +import lombok.extern.slf4j.Slf4j; + +/** + * 任务接收控制层 + * @author jian.mao + * @date 2025年1月14日 + * @description + */ +@Controller +@RequestMapping("/task") +@Slf4j +public class TaskReceiveController { + @Resource + private TaskReceiveService taskReceiveService; + @PostMapping("/put") + @ResponseBody + public String put(@RequestBody String param){ + String response = taskReceiveService.put(param); + return response; + } + @RequestMapping(value = "/hello", method = RequestMethod.GET) + @ResponseBody + public String hello(String param, String token) { + return "123"; + } +} diff --git a/translate-service/src/main/java/com/bw/translate/entity/AppResultDoc.java b/translate-service/src/main/java/com/bw/translate/entity/AppResultDoc.java new file mode 100644 index 0000000..bbcaeff --- /dev/null +++ b/translate-service/src/main/java/com/bw/translate/entity/AppResultDoc.java @@ -0,0 +1,39 @@ +package com.bw.translate.entity; + + +import java.io.Serializable; +import java.util.Map; + + +import lombok.Data; + +/** + * ES 索引:opai_app_result + * 应用执行结果文档 + * + * @author jian.mao + */ +@Data +public class AppResultDoc implements Serializable { + + private static final long serialVersionUID = 1L; + + /** 任务ID */ + private String taskId; + + /** 应用id */ + private Integer appId; + + /** 状态 0 进行中,1成功,2失败 */ + private Integer status; + + /** 创建时间(毫秒时间戳) */ + private Long createTime; + + /** 执行结果(可索引) */ + private Map result; + + /** 逻辑删除标识:0-未删除 1-已删除 */ + private Integer del; + +} diff --git a/translate-service/src/main/java/com/bw/translate/entity/Constants.java b/translate-service/src/main/java/com/bw/translate/entity/Constants.java new file mode 100644 index 0000000..8b6b8b2 --- /dev/null +++ b/translate-service/src/main/java/com/bw/translate/entity/Constants.java @@ -0,0 +1,87 @@ +package com.bw.translate.entity; + + +/** + * 常量实体类 + * @author jian.mao + * @date 2022年11月15日 + * @description + */ +public class Constants { + + + /** + * 空字符串常量 + */ + public static final String EMPTY = ""; + + /************************应用参数*************************************/ + public static final String CODE = "code"; + public static final String ID = "id"; + public static final String MESSAGE = "message"; + /******************************api使用*******************************/ + public static final String CONTENT = "content"; + public static final String ERROR = "error"; + public static final String TRACE = "trace"; + /** + * 源内容 + */ + public static final String SRC_CONTENT = "srcContent"; + + /** + * 任务id + */ + public static final String TASKID = "taskId"; + + /** + * 翻译语种 + */ + public static final String TO = "to"; + + /** + * 模型参数 model + */ + public static final String MODEL = "model"; + + /** + * 模型参数 max_tokens + */ + public static final String MAX_TOKENS = "max_tokens"; + + /** + * 模型参数 messages + */ + public static final String MESSAGES = "messages"; + + /** + * 模型参数 user + */ + public static final String USER = "user"; + + /** + * 模型参数 role + */ + public static final String ROLE = "role"; + + /** + * 模型返回结果参数 choices + */ + public static final String CHOICES = "choices"; + + /** + * 模型认证参数 + */ + public static final String AUTHORIZATION = "authorization"; + + /** + * 预翻译内容 key + */ + public static final String PRE_TEXT_REP = "{{text}}"; + + /** + * 预翻译语种 key + */ + public static final String PRE_LANG_REP = "{{lang}}"; + + +} diff --git a/translate-service/src/main/java/com/bw/translate/handler/MainHandler.java b/translate-service/src/main/java/com/bw/translate/handler/MainHandler.java new file mode 100644 index 0000000..f1140a7 --- /dev/null +++ b/translate-service/src/main/java/com/bw/translate/handler/MainHandler.java @@ -0,0 +1,167 @@ +package com.bw.translate.handler; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.io.FileUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +import com.alibaba.fastjson.JSONObject; +import com.bw.translate.cache.ConfigCache; +import com.bw.translate.service.TranslateTaskService; +import com.bw.translate.utils.FileUtil; + +import lombok.extern.slf4j.Slf4j; + + +/** + * @author jian.mao + * @date 2025年1月13日 + * @description + */ +@Component +@Order(value = 1) +@RefreshScope +@Slf4j +public class MainHandler implements ApplicationRunner { + + @Value("${task.task-queue-path}") + private String taskPath; + @Autowired + private TranslateTaskService translateTaskService; + /***线程池参数***/ + @Value("${threadPool.corePoolSize}") + private int corePoolSize; + @Value("${threadPool.maximumPoolSize}") + private int maximumPoolSize; + @Value("${threadPool.keepAliveTime}") + private long keepAliveTime; + @Value("${threadPool.queueSize}") + private int queueSize; + + /** + *执行入口 + */ + @Override + public void run(ApplicationArguments args) throws Exception { + //线程池方式 + ThreadPoolExecutor executor = new ThreadPoolExecutor( + corePoolSize, + maximumPoolSize, + keepAliveTime, + TimeUnit.SECONDS, + new LinkedBlockingQueue<>(queueSize), + new ThreadPoolExecutor.CallerRunsPolicy() + ); + //消费创建任务队列数据 + Thread consumerThread = new Thread(() -> { + while (true) { + try { + // 从队列中获取任务 + Map task = ConfigCache.taskQueue.take(); + // 提交给线程池执行 + executor.execute(() -> translateTask(task)); + } catch (InterruptedException e) { + // 恢复中断状态 + Thread.currentThread().interrupt(); + log.error("任务消费线程被中断"); + break; + } + } + }); + consumerThread.start(); + log.info("任务消费线程启动-----"); + + //启动加载缓存任务 + readTask(taskPath, ConfigCache.taskQueue); + //停止处理 + waitDown(); + } + + /** + * 创建任务执行方法 + * @param task + */ + private void translateTask(Map task) { + translateTaskService.translate(task); + } + + + + /****************************************************************load******************************************************************************/ + /** + * 加载文件中的任务 + * @param path 文件地址 + * @param queue 队列 + */ + @SuppressWarnings("unchecked") + public static void readTask(String path, LinkedBlockingDeque> queue) { + File file = new File(path); + if (file.exists()) { + List tasks = null; + try { + tasks = FileUtils.readLines(file, "UTF-8"); + } catch (IOException e) { + e.printStackTrace(); + } + for (String taskStr : tasks) { + Map task = JSONObject.parseObject(taskStr); + try { + queue.put(task); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + file.delete(); + } + } + + /*******************************************************************stop************************************************************************/ + + /** + * 结束触发钩子 + */ + public void waitDown() { + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + // 停止线程 + ConfigCache.isStart = false; + log.info("stop-------"); + writeTsskToFile(); + } + }); + } + + + /** + * 任务持久化到硬盘 + */ + public void writeTsskToFile() { + while (true) { + if (ConfigCache.taskQueue.size() > 0) { + try { + Map task = ConfigCache.taskQueue.take(); + FileUtil.writeFile(taskPath, JSONObject.toJSONString(task)); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } else { + log.info("taskQueue write is file end"); + break; + } + } + } +} diff --git a/translate-service/src/main/java/com/bw/translate/service/TaskReceiveService.java b/translate-service/src/main/java/com/bw/translate/service/TaskReceiveService.java new file mode 100644 index 0000000..2057fc3 --- /dev/null +++ b/translate-service/src/main/java/com/bw/translate/service/TaskReceiveService.java @@ -0,0 +1,17 @@ +package com.bw.translate.service; + +/** + * 任务接收服务层 + * @author jian.mao + * @date 2025年1月14日 + * @description + */ +public interface TaskReceiveService { + + /** + * 任务新增 + * @param dataJson + * @return + */ + public String put(String dataJson); +} diff --git a/translate-service/src/main/java/com/bw/translate/service/TranslateTaskService.java b/translate-service/src/main/java/com/bw/translate/service/TranslateTaskService.java new file mode 100644 index 0000000..2d232f8 --- /dev/null +++ b/translate-service/src/main/java/com/bw/translate/service/TranslateTaskService.java @@ -0,0 +1,19 @@ +package com.bw.translate.service; + +import java.util.Map; + +/** + * 翻译识别处理接口 + * @author jian.mao + * @date 2025年2月18日 + * @description + */ +public interface TranslateTaskService { + + /** + * 翻译远端任务 + * @param task + */ + public void translate(Map task); + +} diff --git a/translate-service/src/main/java/com/bw/translate/service/impl/TaskReceiveServiceImpl.java b/translate-service/src/main/java/com/bw/translate/service/impl/TaskReceiveServiceImpl.java new file mode 100644 index 0000000..e723e8b --- /dev/null +++ b/translate-service/src/main/java/com/bw/translate/service/impl/TaskReceiveServiceImpl.java @@ -0,0 +1,55 @@ +package com.bw.translate.service.impl; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.stereotype.Service; + +import com.alibaba.fastjson.JSONObject; +import com.bw.translate.cache.ConfigCache; +import com.bw.translate.entity.Constants; +import com.bw.translate.service.TaskReceiveService; + +import lombok.extern.slf4j.Slf4j; + +/** + * 任务接收服务层实现类 + * @author jian.mao + * @date 2025年1月14日 + * @description + */ +@Service +@Slf4j +public class TaskReceiveServiceImpl implements TaskReceiveService { + + @Override + public String put(String dataJson) { + Map response = new HashMap<>(16); + int code = 200; + String message = "success"; + Map task = null; + try { + task = JSONObject.parseObject(dataJson); + } catch (Exception e) { + log.error("参数结构不合法,", e); + code = 100010; + message = "参数不合法"; + } + // 写入队列 + try { + if(task.containsKey(Constants.TRACE) && (boolean)task.get(Constants.TRACE)){ + ConfigCache.taskQueue.putFirst(task); + }else{ + ConfigCache.taskQueue.put(task); + } + } catch (InterruptedException e) { + log.error("任务写入队列异常,", e); + code = 100011; + message = "任务写入队列失败"; + } + response.put(Constants.CODE, code); + response.put(Constants.MESSAGE, message); + return JSONObject.toJSONString(response); + } + +} diff --git a/translate-service/src/main/java/com/bw/translate/service/impl/TranslateTaskServiceImpl.java b/translate-service/src/main/java/com/bw/translate/service/impl/TranslateTaskServiceImpl.java new file mode 100644 index 0000000..7bec3ad --- /dev/null +++ b/translate-service/src/main/java/com/bw/translate/service/impl/TranslateTaskServiceImpl.java @@ -0,0 +1,168 @@ +package com.bw.translate.service.impl; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.stereotype.Service; + +import com.alibaba.fastjson.JSONObject; +import com.bw.translate.entity.AppResultDoc; +import com.bw.translate.entity.Constants; +import com.bw.translate.service.TranslateTaskService; +import com.bw.translate.utils.DownLoadUtil; +import com.bw.translate.utils.FileUtil; + +import lombok.extern.slf4j.Slf4j; + +/** + * ocr执行实现类 + * @author jian.mao + * @date 2025年2月18日 + * @description + */ +@Service +@Slf4j +@RefreshScope + +public class TranslateTaskServiceImpl implements TranslateTaskService { + + + @Value("${api.module-url}") + private String moduleUrl; + + @Value("${api.param.model}") + private String model; + @Value("${api.param.max-tokens}") + private Integer maxTokens; + + @Value("${api.save-url}") + private String saveUrl; + + @Value("${prompt.path}") + private String promptPath; + + @Value("${api.param.authorization}") + private String authorization; + @Override + public void translate(Map task) { + // TODO Auto-generated method stub + try { + //源内容 + String srcContent =task.get(Constants.SRC_CONTENT).toString(); + //翻译语种 + String to = task.get(Constants.TO).toString(); + //模型请求 + Map headers = new HashMap(16); + +// headers.put(Constants.AUTHORIZATION, authorization); + headers.put(Constants.AUTHORIZATION, "Bearer sk-c5f56c1c8a6447b3a6c646a3f14085c4"); + headers.put("Content-Type", "application/json"); + Map param = new HashMap(16); + param.put(Constants.MODEL, model); + param.put(Constants.MAX_TOKENS, maxTokens); + + List> messages = new ArrayList>(); + Map message = new HashMap(16); + message.put(Constants.ROLE, Constants.USER); + String question = FileUtil.readPrompt(promptPath).replace(Constants.PRE_TEXT_REP, srcContent).replace(Constants.PRE_LANG_REP,to) ; + message.put(Constants.CONTENT, question); + messages.add(message); + param.put(Constants.MESSAGES, messages); + log.info("请求提:{}",param); + log.info("请求头:{}",headers); + String modelResStr = DownLoadUtil.doPost(moduleUrl, JSONObject.toJSONString(param),headers); + log.info("模型请求结果:{}",modelResStr); + JSONObject res = JSONObject.parseObject(modelResStr); + if(res.containsKey(Constants.CHOICES)) { + List> choices = (List>) res.get(Constants.CHOICES); + Map resMessage = (Map) choices.get(0).get(Constants.MESSAGE); + String transContent = (String) resMessage.get(Constants.CONTENT); + //成功 发送结果 + AppResultDoc entity = new AppResultDoc(); + entity.setTaskId((String)task.get(Constants.TASKID)); + entity.setAppId((Integer)task.get(Constants.ID)); + long now = System.currentTimeMillis(); + entity.setCreateTime(now); + Map result = new HashMap(16); + result.put(Constants.CONTENT, transContent); + result.put(Constants.SRC_CONTENT, srcContent); + entity.setResult(result); + entity.setStatus(1); + entity.setDel(0); + //回传给api服务保存 + DownLoadUtil.doPost(saveUrl, JSONObject.toJSONString(entity)); + }else { + throw new Exception("请求结果解析失败"); + } + + } catch (Throwable e) { + log.error("翻译任务异常。e:",e); + //失败直接发送结果 + AppResultDoc entity = new AppResultDoc(); + entity.setTaskId((String)task.get(Constants.TASKID)); + entity.setAppId((Integer)task.get(Constants.ID)); + long now = System.currentTimeMillis(); + entity.setCreateTime(now); + Map result = new HashMap(16); + result.put(Constants.ERROR, "翻译异常"); + entity.setResult(result); + entity.setStatus(2); + entity.setDel(0); + //回传给api服务保存 + DownLoadUtil.doPost(saveUrl, JSONObject.toJSONString(entity)); + } + + + } + + public static void main(String[] args) { + //源内容 + String srcContent ="你好"; + //翻译语种 + String to = "英语"; + //模型请求 + Map headers = new HashMap(16); + + headers.put(Constants.AUTHORIZATION, "Bearer sk-c5f56c1c8a6447b3a6c646a3f14085c4"); + headers.put("Content-Type", "application/json"); + Map param = new HashMap(16); + param.put(Constants.MODEL, "qwen-max-latest"); + param.put(Constants.MAX_TOKENS, 8192); + + List> messages = new ArrayList>(); + Map message = new HashMap(16); + message.put(Constants.ROLE, Constants.USER); + String question = "你是一个拥有20年以上多语种翻译与文风润色经验的专业翻译团队。\r\n" + + "\r\n" + + "你的任务是:\r\n" + + "将用户提供的【待翻译内容】准确翻译成【目标语种】。\r\n" + + "\r\n" + + "翻译要求:\r\n" + + "1. 忠实原意,不遗漏、不臆造、不歪曲信息\r\n" + + "2. 保留原文语气(如正式、口语、文学、技术、营销等)\r\n" + + "3. 如果原文有专业术语,使用目标语种中最权威、最自然的对应表达\r\n" + + "4. 如果原文有歧义,选择在语境中最合理的含义\r\n" + + "5. 不添加任何解释、标注或多余文字\r\n" + + "\r\n" + + "你将收到两个输入参数:\r\n" + + "- 待翻译内容:@text@\r\n" + + "- 目标语种:@lang@\r\n" + + "\r\n" + + "你必须:\r\n" + + "只输出翻译后的最终结果,不要包含任何前言、说明或多余内容。\r\n" + + "" ; + question = question.replace(Constants.PRE_TEXT_REP, srcContent).replace(Constants.PRE_LANG_REP,to); + message.put(Constants.CONTENT, question); + messages.add(message); + param.put(Constants.MESSAGES, messages); + System.out.println(JSONObject.toJSONString(param)); + System.out.println(JSONObject.toJSONString(headers)); + String modelResStr = DownLoadUtil.doPost("https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions", JSONObject.toJSONString(param),headers); + System.out.println(modelResStr); + } + +} diff --git a/translate-service/src/main/java/com/bw/translate/utils/DataUtil.java b/translate-service/src/main/java/com/bw/translate/utils/DataUtil.java new file mode 100644 index 0000000..769d599 --- /dev/null +++ b/translate-service/src/main/java/com/bw/translate/utils/DataUtil.java @@ -0,0 +1,48 @@ +package com.bw.translate.utils; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.JSONPath; + +import java.util.Map; + +/** + * @author:jinming + * @className:DataUtil + * @version:1.0 + * @description: 获取dataValue的值 + * @Date:2023/11/1 9:54 + */ +public class DataUtil { + /** + * + * @param key 传入的key + * @param dataMap 数据map + * @return 根据传入的参数进行判断解析,返回正确的dataValue + */ + public static Object getValue(String key, Map dataMap) { + Object dataValue; + String isJson = "#json#"; + if (key.contains(isJson)) { + //进行第一次拆分,获取#json#前面的部分 + String[] keySplit = key.split(isJson); + String firstDataKey = keySplit[0]; + String[] firstDataKeySplit = firstDataKey.split(":"); + //取出前半部分对应的JSON数据并转换为JSONObject + String dataJson = (String) dataMap.get(firstDataKeySplit[0]); + JSONObject dataJsonObject = JSON.parseObject(dataJson); + //根据key的后半部分取出对应JSONObject中的值 + String firstDataKeyJson = (String) JSONPath.eval(dataJsonObject, firstDataKeySplit[1]); + String secDataKey = keySplit[1]; + JSONObject firstDataJsonObject = JSON.parseObject(firstDataKeyJson); + dataValue = JSONPath.eval(firstDataJsonObject, secDataKey); + return dataValue; + } + String[] keySplit = key.split(":"); + String jsonPath = keySplit[1]; + String dataJson = (String) dataMap.get(keySplit[0]); + JSONObject dataJsonObject = JSON.parseObject(dataJson); + dataValue = JSONPath.eval(dataJsonObject, jsonPath); + return dataValue; + } +} \ No newline at end of file diff --git a/translate-service/src/main/java/com/bw/translate/utils/DateUtil.java b/translate-service/src/main/java/com/bw/translate/utils/DateUtil.java new file mode 100644 index 0000000..56d9114 --- /dev/null +++ b/translate-service/src/main/java/com/bw/translate/utils/DateUtil.java @@ -0,0 +1,177 @@ +package com.bw.translate.utils; + + +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Date; + +import lombok.extern.slf4j.Slf4j; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; + +/** + * 日期工具类 + * + * @author jian.mao + * @date 2022年11月15日 + * @description + */ +@Slf4j +public class DateUtil { + + /** + * @return + */ + public static String getTimeStrForNow() { + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH"); + return sdf.format(new Date()); + } + + + public static String getTimeStrForDay(long time) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); + + return sdf.format(new Date(time * 1000)); + } + + public static String getTimeStrForDay() { + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); + + return sdf.format(new Date()); + } + + + public static String getDateTime() { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String time = sdf.format(new Date()); + return time; + } + + public static String getDateTime(Long timestap) { + + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String time = sdf.format(new Date(timestap)); + return time; + } + + public static String getDate(Long timestap) { + + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + String time = sdf.format(new Date(timestap)); + return time; + } + + public static String getDateTimeForMonth() { + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM"); + String time = sdf.format(new Date()); + return time; + } + + /** + * 休眠 + * + * @param millis 毫秒 + */ + public static void sleep(long millis) { + try { + Thread.sleep(millis); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + /** + * 1. @Description:时间戳转时间 + * 2. @Author: ying.zhao + * 3. @Date: 2023/3/28 + */ + + public static String timestampToDate(String time) { + int thirteen = 13; + int ten = 10; + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); +// if (time.length() == thirteen) { + if (time.length() > ten) { + return sdf.format(new Date(Long.parseLong(time))); + } else { + return sdf.format(new Date(Integer.parseInt(time) * 1000L)); + } + } + + public static String parseCreated(String jsonTime){ + String formattedDateTime = getDateTime(); + try { + // 使用fastjson解析JSON数据 + JSONObject jsonObject = JSON.parseObject(jsonTime); + // 获取日期和时间的值 + JSONObject dateObject = jsonObject.getJSONObject("date"); + int day = dateObject.getIntValue("day"); + int month = dateObject.getIntValue("month"); + int year = dateObject.getIntValue("year"); + + JSONObject timeObject = jsonObject.getJSONObject("time"); + int hour = timeObject.getIntValue("hour"); + int minute = timeObject.getIntValue("minute"); + int second = timeObject.getIntValue("second"); + + // 创建LocalDateTime对象 + LocalDateTime dateTime = LocalDateTime.of(year, month, day, hour, minute, second); + + // 定义日期时间格式化器 + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + // 格式化日期时间 + formattedDateTime = dateTime.format(formatter); + } catch (Exception e) { + log.info("日期转换失败:{}",e); + } + return formattedDateTime; + } + + /** + * 字符串转换日期 + * @param format + * @param date + * @return + */ + public static Date strToDate(String format,String date){ + SimpleDateFormat sdf = new SimpleDateFormat(format); + if (date == null || date.equals("")){ + return new Date(); + }else{ + Date ru = null; + try { + ru = sdf.parse(date); + } catch (ParseException e) { + e.printStackTrace(); + } + return ru; + } + } + /** + * 日期格式话 + * @param format 日期格式 + * @param dater 要转换的日期,默认当前时间 + * @return + */ + public static String FormatDate(String format,Date date){ + String fromatDate = null; + SimpleDateFormat sdf = new SimpleDateFormat(format); + if (date == null){ + fromatDate = sdf.format(new Date()); + }else{ + fromatDate = sdf.format(date); + } + return fromatDate; + } + public static void main(String[] args) { + String time = timestampToDate("955814400000"); + System.out.println(time); + } +} diff --git a/translate-service/src/main/java/com/bw/translate/utils/DownLoadUtil.java b/translate-service/src/main/java/com/bw/translate/utils/DownLoadUtil.java new file mode 100644 index 0000000..ccf8c3a --- /dev/null +++ b/translate-service/src/main/java/com/bw/translate/utils/DownLoadUtil.java @@ -0,0 +1,1004 @@ +package com.bw.translate.utils; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpHost; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.StatusLine; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.AuthCache; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.HttpClient; +import org.apache.http.client.HttpRequestRetryHandler; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.config.Registry; +import org.apache.http.config.RegistryBuilder; +import org.apache.http.config.SocketConfig; +import org.apache.http.conn.socket.ConnectionSocketFactory; +import org.apache.http.conn.socket.LayeredConnectionSocketFactory; +import org.apache.http.conn.socket.PlainConnectionSocketFactory; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.auth.BasicScheme; +import org.apache.http.impl.client.BasicAuthCache; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.client.LaxRedirectStrategy; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.bw.translate.entity.Constants; + +import okhttp3.Call; +import okhttp3.Headers; +import okhttp3.MediaType; +import okhttp3.MultipartBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; + + + + + + + + + +/** + * 下载工具类 + * @author jian.mao + * @date 2023年9月19日 + * @description + */ +public class DownLoadUtil { + + private static String ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36"; + private final static Logger log = LoggerFactory.getLogger(DownLoadUtil.class); + /** 代理服务器(产品官网 www.16yun.cn) **/ + final static String PROXYHOST = "u270.40.tp.16yun.cn"; + final static Integer PROXYPORT = 6448; + /** 代理验证信息 **/ + final static String PROXYUSER = "16HFBVJC"; + final static String PROXYPASS = "897944"; + + private static PoolingHttpClientConnectionManager cm = null; + private static HttpRequestRetryHandler httpRequestRetryHandler = null; + private static HttpHost proxy = null; + + private static CredentialsProvider credsProvider = null; + private static RequestConfig reqConfig = null; + + static { + ConnectionSocketFactory plainsf = PlainConnectionSocketFactory + .getSocketFactory(); + LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory + .getSocketFactory(); + + Registry registry = RegistryBuilder.create().register("http", plainsf) + .register("https", sslsf).build(); + + cm = new PoolingHttpClientConnectionManager(registry); + cm.setMaxTotal(20); + cm.setDefaultMaxPerRoute(5); + + proxy = new HttpHost(PROXYHOST, PROXYPORT, "https"); + + credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials(AuthScope.ANY, + new UsernamePasswordCredentials(PROXYUSER, PROXYPASS)); + + reqConfig = RequestConfig.custom().setConnectionRequestTimeout(5000) + .setConnectTimeout(5000).setSocketTimeout(5000) + .setExpectContinueEnabled(false) + .setProxy(new HttpHost(PROXYHOST, PROXYPORT)).build(); + } + + /** + * 模拟客户端get请求 + * + * @param url + * 模拟请求得url + * @param headers + * 头部信息,没有可以不传 + * @return + */ + @SafeVarargs + public static String proxyDoGet(String url, Map... headers) { + // 设置超时时间 + int timeout = 30; + RequestConfig config = RequestConfig.custom() + .setConnectTimeout(timeout * 1000) + .setConnectionRequestTimeout(timeout * 1000) + .setSocketTimeout(timeout * 1000).build(); + SocketConfig socketConfig = SocketConfig.custom() + .setSoKeepAlive(false) + .setSoLinger(1) + .setSoReuseAddress(true) + .setSoTimeout(timeout * 1000) + .setTcpNoDelay(true).build(); + AuthCache authCache = new BasicAuthCache(); + authCache.put(proxy, new BasicScheme()); + HttpClientContext localContext = HttpClientContext.create(); + localContext.setAuthCache(authCache); + HttpClientBuilder httpBuilder = HttpClientBuilder.create(); + CloseableHttpClient httpClient = httpBuilder + .setDefaultSocketConfig(socketConfig) + .setDefaultRequestConfig(config) + .setDefaultCredentialsProvider(credsProvider).build(); + HttpGet httpGet = new HttpGet(url); + httpGet.setConfig(reqConfig); + if (headers != null && headers.length > 0) { + Map tempHeaders = headers[0]; + for (String key : tempHeaders.keySet()) { + httpGet.setHeader(key, tempHeaders.get(key).toString()); + } + } else { + httpGet.setHeader("Accept", + "application/json, text/javascript, */*; q=0.01"); + httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8"); + } + CloseableHttpResponse response = null; + String html = ""; + int notFundCode = 404; + int successCode = 200; + try { + response = httpClient.execute(httpGet, localContext); + // 从响应模型中获取响应实体 + HttpEntity responseEntity = response.getEntity(); + StatusLine statusLine = response.getStatusLine(); + System.out.println("响应状态为:" + response.getStatusLine()); + if (statusLine.getStatusCode() == successCode) { + if (responseEntity != null) { + html = EntityUtils.toString(responseEntity, "utf-8"); + System.out.println("响应内容长度为:" + + responseEntity.getContentLength()); + // 下载结果为空不正常 + if (html.equals(Constants.EMPTY)) { + html = "Download failed error is:reslut is null"; + } + } + } else if (statusLine.getStatusCode() == notFundCode) { + html = "

页面404,正常结束请求即可

"; + } else { + throw new Exception("请求错误,code码为:" + statusLine.getStatusCode()); + } + } catch (Exception e) { + e.printStackTrace(); + html = "Download failed error is:reslut is null"; + }finally{ + try { + response.close(); + httpClient.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + return html; + + } + + + public static String httpsslProxyGet(String url, Map... headers) throws Exception { + //采用绕过验证的方式处理https请求 + SSLContext sslcontext = createIgnoreVerifySSL(); + + // 设置协议http和https对应的处理socket链接工厂的对象 + Registry socketFactoryRegistry = RegistryBuilder.create() + .register("http", PlainConnectionSocketFactory.INSTANCE) + .register("https", new SSLConnectionSocketFactory(sslcontext)) + .build(); + PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); + connManager.setMaxTotal(50); + connManager.setDefaultMaxPerRoute(10); + HttpClients.custom().setConnectionManager(connManager); + // 设置超时时间 + int timeout = 30; + RequestConfig config = RequestConfig.custom() + .setConnectTimeout(timeout * 1000) + .setConnectionRequestTimeout(timeout * 1000) + .setSocketTimeout(timeout * 1000).build(); + SocketConfig socketConfig = SocketConfig.custom() + .setSoKeepAlive(false) + .setSoLinger(1) + .setSoReuseAddress(true) + .setSoTimeout(timeout * 1000) + .setTcpNoDelay(true).build(); + AuthCache authCache = new BasicAuthCache(); + authCache.put(proxy, new BasicScheme()); + HttpClientContext localContext = HttpClientContext.create(); + localContext.setAuthCache(authCache); + HttpClientBuilder httpBuilder = HttpClientBuilder.create(); + CloseableHttpClient httpClient = httpBuilder + .setConnectionManager(connManager) + .setDefaultSocketConfig(socketConfig) + .setDefaultRequestConfig(config) + .setDefaultCredentialsProvider(credsProvider).build(); + HttpGet httpGet = new HttpGet(url); + httpGet.setConfig(reqConfig); + if (headers != null && headers.length > 0) { + Map tempHeaders = headers[0]; + for (String key : tempHeaders.keySet()) { + httpGet.setHeader(key, tempHeaders.get(key).toString()); + } + } else { + httpGet.setHeader("Accept", + "application/json, text/javascript, */*; q=0.01"); + httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8"); + } + CloseableHttpResponse response = null; + String html = ""; + int notFundCode = 404; + int successCode = 200; + try { + response = httpClient.execute(httpGet, localContext); + // 从响应模型中获取响应实体 + HttpEntity responseEntity = response.getEntity(); + StatusLine statusLine = response.getStatusLine(); + System.out.println("响应状态为:" + response.getStatusLine()); + if (statusLine.getStatusCode() == successCode) { + if (responseEntity != null) { + html = EntityUtils.toString(responseEntity, "utf-8"); + System.out.println("响应内容长度为:" + + responseEntity.getContentLength()); + // 下载结果为空不正常 + if (html.equals(Constants.EMPTY)) { + html = "Download failed error is:reslut is null"; + } + } + } else if (statusLine.getStatusCode() == notFundCode) { + html = "

页面404,正常结束请求即可

"; + } else { + throw new Exception("请求错误,code码为:" + statusLine.getStatusCode()); + } + } catch (Exception e) { + e.printStackTrace(); + html = "Download failed error is:reslut is null"; + }finally{ + try { + response.close(); + httpClient.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + return html; + + } + + + /** + * json参数方式POST提交 + * @param url + * @param params + * @return + */ + public static String doPost(String url, String params, Map... headers){ + String strResult = ""; + //设置超时时间 + int timeout = 60; + RequestConfig config = RequestConfig.custom(). + setConnectTimeout(timeout * 1000). + setConnectionRequestTimeout(timeout * 1000). + setSocketTimeout(timeout * 1000).build(); + SocketConfig socketConfig = SocketConfig.custom() + .setSoKeepAlive(false) + .setSoLinger(1) + .setSoReuseAddress(true) + .setSoTimeout(timeout * 1000) + .setTcpNoDelay(true).build(); +// AuthCache authCache = new BasicAuthCache(); +// authCache.put(proxy, new BasicScheme()); +// HttpClientContext localContext = HttpClientContext.create(); +// localContext.setAuthCache(authCache); + // 1. 获取默认的client实例 + HttpClientBuilder httpBuilder = HttpClientBuilder.create(); + httpBuilder.setUserAgent(ua); + HttpClient client = httpBuilder.setDefaultSocketConfig(socketConfig).setDefaultRequestConfig(config).build(); +// HttpClient client = httpBuilder.setDefaultSocketConfig(socketConfig).setDefaultRequestConfig(config).setConnectionManager(cm) +// .setDefaultCredentialsProvider(credsProvider).build(); + // 2. 创建httppost实例 + HttpPost httpPost = new HttpPost(url); +// httpPost.setConfig(reqConfig); + if (headers != null && headers.length > 0) { + Map tempHeaders = headers[0]; + for (String key : tempHeaders.keySet()) { + httpPost.setHeader(key, tempHeaders.get(key).toString()); + } + } else { + httpPost.addHeader("Content-Type", "application/json;charset=utf-8"); + } + HttpResponse resp = null; + try { + httpPost.setEntity(new StringEntity(params,"utf-8")); + resp = client.execute(httpPost); +// resp = client.execute(httpPost,localContext); + StatusLine statusLine = resp.getStatusLine(); + System.out.println("响应状态为:" + resp.getStatusLine()); + int notFundCode = 300; + int successCode = 200; + if(statusLine.getStatusCode() >= successCode && statusLine.getStatusCode() < notFundCode){ + // 7. 获取响应entity + HttpEntity respEntity = resp.getEntity(); + strResult = EntityUtils.toString(respEntity, "UTF-8"); + if(strResult.equals(Constants.EMPTY)){ + strResult = "Download failed error is:reslut is null"; + } + }else{ + throw new Exception("请求错误,code码为:"+statusLine.getStatusCode()); + } + } catch (Exception e) { + e.printStackTrace(); + strResult = "Download failed error is:"+ThrowMessageUtil.getErrmessage(e); + } + return strResult; + } + public static String httpPost(String url,String params) { + String html=""; + html = doPost(url,params); + int i = 1; + while(true){ + if(html.contains("Download failed error is:")){ + log.error("DownLoadUtil------------->download is failure,url is:"+url); + DateUtil.sleep(5000); + i++; + }else{ + break; + } + if(i > 5){ + break; + } + html = doPost(url,params); + } + return html; + } + /** + * 绕过验证 + * + * @return + * @throws NoSuchAlgorithmException + * @throws KeyManagementException + */ + public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException { + SSLContext sc = SSLContext.getInstance("SSLv3"); + + // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法 + X509TrustManager trustManager = new X509TrustManager() { + @Override + public void checkClientTrusted( + java.security.cert.X509Certificate[] paramArrayOfX509Certificate, + String paramString) throws CertificateException { + } + + @Override + public void checkServerTrusted( + java.security.cert.X509Certificate[] paramArrayOfX509Certificate, + String paramString) throws CertificateException { + } + + @Override + public java.security.cert.X509Certificate[] getAcceptedIssuers() { + return null; + } + }; + + sc.init(null, new TrustManager[] { trustManager }, null); + return sc; + } + /** + * 模拟请求 + * + * @param url 资源地址 + * @param map 参数列表 + * @param encoding 编码 + * @return + * @throws NoSuchAlgorithmException + * @throws KeyManagementException + * @throws IOException + * @throws ClientProtocolException + */ + public static String httpsslGet(String url,Map ... headers) { + String html=""; + CloseableHttpClient client = null; + HttpEntity responseEntity = null; + CloseableHttpResponse response = null; + try { + log.debug("DownLoadUtil------------->设置下载相关信息, start...."); + //采用绕过验证的方式处理https请求 + SSLContext sslcontext = createIgnoreVerifySSL(); + + // 设置协议http和https对应的处理socket链接工厂的对象 + Registry socketFactoryRegistry = RegistryBuilder.create() + .register("http", PlainConnectionSocketFactory.INSTANCE) + .register("https", new SSLConnectionSocketFactory(sslcontext)) + .build(); + PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); + connManager.setMaxTotal(50); + connManager.setDefaultMaxPerRoute(10); + HttpClients.custom().setConnectionManager(connManager); + //设置超时时间 + int timeout = 30; + RequestConfig config = RequestConfig.custom(). + setConnectTimeout(timeout * 1000). + setConnectionRequestTimeout(timeout * 1000). + setSocketTimeout(timeout * 1000).build(); + SocketConfig socketConfig = SocketConfig.custom() + .setSoKeepAlive(false) + .setSoLinger(1) + .setSoReuseAddress(true) + .setSoTimeout(10000) + .setTcpNoDelay(true).build(); + // 设置重定向策略 + LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy(); + //创建自定义的httpclient对象 + client = HttpClients.custom().setConnectionManager(connManager).setDefaultRequestConfig(config).setRedirectStrategy(redirectStrategy).setDefaultSocketConfig(socketConfig).setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36").build(); +// CloseableHttpClient client = HttpClients.createDefault(); + + HttpGet httpGet = new HttpGet(url); + if(headers != null && headers.length > 0){ + Map tempHeaders = headers[0]; + for (String key : tempHeaders.keySet()) { + httpGet.setHeader(key,tempHeaders.get(key).toString()); + } + }else{ + httpGet.setHeader("Accept", "application/json, text/javascript, */*; q=0.01"); + httpGet.setHeader("Accept-Language","zh-CN,zh;q=0.9,en;q=0.8"); + httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"); + } + log.debug("DownLoadUtil------------->设置下载相关信息, end...."); + try { + int notFundCode = 404; + int successCode = 200; + log.debug("DownLoadUtil------------->下载执行,start...."); + httpGet.setConfig(config); + response = client.execute(httpGet); + log.debug("DownLoadUtil------------->下载执行,end...."); + // 从响应模型中获取响应实体 + StatusLine statusLine = response.getStatusLine(); + log.debug("DownLoadUtil------------->响应状态为:" + response.getStatusLine()+",下载请求没问题url:"+url+",read is start ...."); + System.out.println("响应状态为:" + response.getStatusLine()); + responseEntity = response.getEntity(); + log.debug("DownLoadUtil------------->响应状态为:" + response.getStatusLine()+",下载请求没问题url:"+url+",read is end ...."); + if(statusLine.getStatusCode() == successCode){ + if (responseEntity != null) { + html=EntityUtils.toString(responseEntity,"utf-8"); + System.out.println("响应内容长度为:" + responseEntity.getContentLength()); + } + }else if(statusLine.getStatusCode() == notFundCode){ + html = "

页面404,正常结束请求即可

"; + }else{ + throw new Exception("请求错误,code码为:"+statusLine.getStatusCode()); + } + } catch (Exception e) { + e.printStackTrace(); + html = "Download failed error is:"+ThrowMessageUtil.getErrmessage(e); + + } + } catch (Exception e) { + e.printStackTrace(); + html = "Download failed error is:"+ThrowMessageUtil.getErrmessage(e); + }finally{ + try { + responseEntity.getContent().close(); + response.close(); + client.close(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + + return html; + } + + public static String httpSSLGet(String url,Map ... headers) { + String html=""; + html = httpsslGet(url,headers); + int i = 1; + while(true){ + if(html.contains("Download failed error is:")){ + log.error("DownLoadUtil------------->download is failure,url is:"+url); + DateUtil.sleep(30000); + i++; + }else{ + break; + } + if(i > 5){ + break; + } + html = httpsslGet(url,headers); + } + return html; + } + public static String doPostFrom(String url,Map param,Map ... headers){ + //设置超时时间 + int timeout = 15; + RequestConfig config = RequestConfig.custom(). + setConnectTimeout(timeout * 1000). + setConnectionRequestTimeout(timeout * 1000). + setSocketTimeout(timeout * 1000).build(); + SocketConfig socketConfig = SocketConfig.custom() + .setSoKeepAlive(false) + .setSoLinger(1) + .setSoReuseAddress(true) + .setSoTimeout(10000) + .setTcpNoDelay(true).build(); +// AuthCache authCache = new BasicAuthCache(); +// authCache.put(proxy, new BasicScheme()); +// HttpClientContext localContext = HttpClientContext.create(); +// localContext.setAuthCache(authCache); + HttpClientBuilder httpBuilder = HttpClientBuilder.create(); + httpBuilder.setUserAgent(ua); +// HttpClient httpClient = httpBuilder.setDefaultSocketConfig(socketConfig).setDefaultRequestConfig(config).setConnectionManager(cm) +// .setDefaultCredentialsProvider(credsProvider).build(); + HttpClient httpClient = httpBuilder.setDefaultSocketConfig(socketConfig).setDefaultRequestConfig(config).build(); + HttpPost httpPost = new HttpPost(url); +// httpPost.setConfig(reqConfig); + if(headers != null && headers.length > 0){ + Map tempHeaders = headers[0]; + for (String key : tempHeaders.keySet()) { + httpPost.setHeader(key,tempHeaders.get(key).toString()); + } + }else{ + httpPost.addHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"); + httpPost.addHeader("accept-Language", "zh-CN,zh;q=0.9,en;q=0.8"); + httpPost.addHeader("content-type", "application/x-www-form-urlencoded"); + httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"); +// httpPost.addHeader("Referer", "http://www.neeq.com.cn/rule/Business_rules.html"); + } + // 创建请求参数 + List list = new LinkedList<>(); + for (String key : param.keySet()) { + BasicNameValuePair param1 = new BasicNameValuePair(key,param.get(key).toString()); + list.add(param1); + } + // 使用URL实体转换工具 + String html=""; + try { + UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8"); + httpPost.setEntity(entityParam); + HttpResponse response = httpClient.execute(httpPost); +// HttpResponse response = httpClient.execute(httpPost,localContext); + // 从响应模型中获取响应实体 + HttpEntity responseEntity = response.getEntity(); + StatusLine statusLine = response.getStatusLine(); + System.out.println("响应状态为:" + response.getStatusLine()); + int notFundCode = 404; + int successCode = 200; + if(statusLine.getStatusCode() == successCode){ + if (responseEntity != null) { + html=EntityUtils.toString(responseEntity,"utf-8"); + } + }else{ + throw new Exception("请求错误,code码为:"+statusLine.getStatusCode()); + } + + } catch (Exception e) { + e.printStackTrace(); + html = "Download failed error is:"+ThrowMessageUtil.getErrmessage(e); + } + + return html; + + } + public static String httpPostForm(String url,Map params,Map ... headers) { + String html=""; + html = doPostFrom(url,params); + int i = 1; + while(true){ + if(html.contains("Download failed error is:")){ + log.error("DownLoadUtil------------->download is failure,url is:"+url); + DateUtil.sleep(5000); + i++; + }else{ + break; + } + if(i > 5){ + break; + } + html = doPostFrom(url,params,headers); + } + return html; + } + + public static String dosslPost(String url,String params,Map ... headers) { + String html=""; + CloseableHttpClient client = null; + HttpEntity responseEntity = null; + CloseableHttpResponse response = null; + try { + //采用绕过验证的方式处理https请求 + SSLContext sslcontext = createIgnoreVerifySSL(); + // 设置协议http和https对应的处理socket链接工厂的对象 + Registry socketFactoryRegistry = RegistryBuilder.create() + .register("http", PlainConnectionSocketFactory.INSTANCE) + .register("https", new SSLConnectionSocketFactory(sslcontext)) + .build(); + PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); + HttpClients.custom().setConnectionManager(connManager); + //设置超时时间 + int timeout = 5; + RequestConfig config = RequestConfig.custom(). + setConnectTimeout(timeout * 1000). + setConnectionRequestTimeout(timeout * 1000). + setSocketTimeout(timeout * 1000).build(); + SocketConfig socketConfig = SocketConfig.custom() + .setSoKeepAlive(false) + .setSoLinger(1) + .setSoReuseAddress(true) + .setSoTimeout(10000) + .setTcpNoDelay(true).build(); + //创建自定义的httpclient对象 + client = HttpClients.custom().setConnectionManager(connManager).setDefaultRequestConfig(config).setDefaultSocketConfig(socketConfig).build(); +// CloseableHttpClient client = HttpClients.createDefault(); + // 2. 创建httppost实例 + HttpPost httpPost = new HttpPost(url); +// httpPost.setConfig(reqConfig); + httpPost.addHeader("Content-Type", "application/json;charset=utf-8"); + if(headers != null && headers.length > 0){ + Map tempHeaders = headers[0]; + for (String key : tempHeaders.keySet()) { + httpPost.setHeader(key,tempHeaders.get(key).toString()); + } + }else{ + httpPost.setHeader("Accept", "application/json, text/javascript, */*; q=0.01"); + httpPost.setHeader("Accept-Language","zh-CN,zh;q=0.9,en;q=0.8"); + httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"); + } + + try { + httpPost.setEntity(new StringEntity(params,"utf-8")); + response = client.execute(httpPost); + int notFundCode = 404; + int successCode = 200; + // 从响应模型中获取响应实体 + StatusLine statusLine = response.getStatusLine(); + System.out.println("响应状态为:" + response.getStatusLine()); + responseEntity = response.getEntity(); + if(statusLine.getStatusCode() == successCode){ + if (responseEntity != null) { + html=EntityUtils.toString(responseEntity,"utf-8"); + System.out.println("响应内容长度为:" + responseEntity.getContentLength()); + } + }else if(statusLine.getStatusCode() == notFundCode){ + html = "

页面404,正常结束请求即可

"; + }else{ + throw new Exception("请求错误,code码为:"+statusLine.getStatusCode()); + } + } catch (Exception e) { + e.printStackTrace(); + html = "Download failed error is:"+ThrowMessageUtil.getErrmessage(e); + + } + } catch (Exception e) { + e.printStackTrace(); + html = "Download failed error is:"+ThrowMessageUtil.getErrmessage(e); + }finally{ + try { + responseEntity.getContent().close(); + response.close(); + client.close(); + } catch (UnsupportedOperationException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + + return html; + } + public static String dosslPostForm(String url,Map param,Map ... headers) { + String html=""; + try { + //采用绕过验证的方式处理https请求 + SSLContext sslcontext = createIgnoreVerifySSL(); + + // 设置协议http和https对应的处理socket链接工厂的对象 + Registry socketFactoryRegistry = RegistryBuilder.create() + .register("http", PlainConnectionSocketFactory.INSTANCE) + .register("https", new SSLConnectionSocketFactory(sslcontext)) + .build(); + PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); + HttpClients.custom().setConnectionManager(connManager); + //设置超时时间 + int timeout = 5; + RequestConfig config = RequestConfig.custom(). + setConnectTimeout(timeout * 1000). + setConnectionRequestTimeout(timeout * 1000). + setSocketTimeout(timeout * 1000).build(); + SocketConfig socketConfig = SocketConfig.custom() + .setSoKeepAlive(false) + .setSoLinger(1) + .setSoReuseAddress(true) + .setSoTimeout(10000) + .setTcpNoDelay(true).build(); + //创建自定义的httpclient对象 + CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).setDefaultRequestConfig(config).setDefaultSocketConfig(socketConfig).build(); +// CloseableHttpClient client = HttpClients.createDefault(); + // 2. 创建httppost实例 + HttpPost httpPost = new HttpPost(url); +// httpPost.setConfig(reqConfig); + if(headers != null && headers.length > 0){ + Map tempHeaders = headers[0]; + for (String key : tempHeaders.keySet()) { + httpPost.setHeader(key,tempHeaders.get(key).toString()); + } + }else{ + httpPost.setHeader("Accept", "application/json, text/javascript, */*; q=0.01"); + httpPost.setHeader("Accept-Language","zh-CN,zh;q=0.9,en;q=0.8"); + httpPost.addHeader("content-type", "application/x-www-form-urlencoded"); + httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"); + } + + // 创建请求参数 + List list = new LinkedList<>(); + for (String key : param.keySet()) { + BasicNameValuePair param1 = new BasicNameValuePair(key,param.get(key).toString()); + list.add(param1); + } + // 使用URL实体转换工具 + try { + UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8"); + httpPost.setEntity(entityParam); + HttpResponse response = client.execute(httpPost); +// HttpResponse response = httpClient.execute(httpPost,localContext); + // 从响应模型中获取响应实体 + int notFundCode = 404; + int successCode = 200; + HttpEntity responseEntity = response.getEntity(); + StatusLine statusLine = response.getStatusLine(); + System.out.println("响应状态为:" + response.getStatusLine()); + if(statusLine.getStatusCode() == successCode){ + if (responseEntity != null) { + html=EntityUtils.toString(responseEntity,"utf-8"); + } + }else{ + throw new Exception("请求错误,code码为:"+statusLine.getStatusCode()); + } + + } catch (Exception e) { + e.printStackTrace(); + html = "Download failed error is:"+ThrowMessageUtil.getErrmessage(e); + } + } catch (Exception e) { + e.printStackTrace(); + html = "Download failed error is:"+ThrowMessageUtil.getErrmessage(e); + } + + + return html; + } + public static String httpSSLPostForm(String url,Map params,Map ...headers) { + String html=""; + try { + html = dosslPostForm(url,params,headers); + } catch (Exception e) { + e.printStackTrace(); + // TODO: handle exception + html = "Download failed error is:Exception!"; + } + int i = 1; + while(true){ + if(html.contains("Download failed error is:")){ + log.error("DownLoadUtil------------->download is failure,url is:"+url); + DateUtil.sleep(30000); + i++; + }else{ + break; + } + if(i > 5){ + break; + } + try { + html = dosslPostForm(url,params,headers); + } catch (Exception e) { + e.printStackTrace(); + // TODO: handle exception + html = "Download failed error is:Exception!"; + } + } + return html; + } + public static String httpSSLPost(String url,String params,Map ...headers) { + String html=""; + try { + html = dosslPost(url,params,headers); + } catch (Throwable e) { + e.printStackTrace(); + // TODO: handle exception + html = "Download failed error is:Exception!"; + } + int i = 1; + while(true){ + if(html.contains("Download failed error is:")){ + log.error("DownLoadUtil------------->download is failure,url is:"+url); + DateUtil.sleep(30000); + i++; + }else{ + break; + } + if(i > 5){ + break; + } + try { + html = dosslPost(url,params,headers); + } catch (Throwable e) { + e.printStackTrace(); + // TODO: handle exception + html = "Download failed error is:Exception!"; + } + } + return html; + } + + /** + * 模拟客户端get请求 + * @param url 模拟请求得url + * @param headers 头部信息,没有可以不传 + * @return + */ + public static String doGet(String url,Map ... headers){ + //设置超时时间 + int timeout = 15; + RequestConfig config = RequestConfig.custom(). + setConnectTimeout(timeout * 1000). + setConnectionRequestTimeout(timeout * 1000). + setSocketTimeout(timeout * 1000).build(); + SocketConfig socketConfig = SocketConfig.custom() + .setSoKeepAlive(false) + .setSoLinger(1) + .setSoReuseAddress(true) + .setSoTimeout(10000) + .setTcpNoDelay(true).build(); + HttpClientBuilder httpBuilder = HttpClientBuilder.create(); + httpBuilder.setUserAgent(ua); + HttpClient httpClient = httpBuilder.setDefaultSocketConfig(socketConfig).setDefaultRequestConfig(config).build(); + HttpGet httpGet = new HttpGet(url); + if(headers != null && headers.length > 0){ + Map tempHeaders = headers[0]; + for (String key : tempHeaders.keySet()) { + httpGet.setHeader(key,tempHeaders.get(key).toString()); + } + }else{ + httpGet.setHeader("Accept", "application/json, text/javascript, */*; q=0.01"); + httpGet.setHeader("Accept-Language","zh-CN,zh;q=0.9,en;q=0.8"); + } + String html=""; + try { + HttpResponse response = httpClient.execute(httpGet); + // 从响应模型中获取响应实体 + HttpEntity responseEntity = response.getEntity(); + StatusLine statusLine = response.getStatusLine(); + System.out.println("响应状态为:" + response.getStatusLine()); + int notFundCode = 404; + int successCode = 200; + int successCodeMax = 300; + if(statusLine.getStatusCode() >= successCode && statusLine.getStatusCode() < successCodeMax){ + if (responseEntity != null) { + html=EntityUtils.toString(responseEntity,"utf-8"); + if(html.equals("")){ + html = "Download failed error is:reslut is null"; + } + } + }else if(statusLine.getStatusCode() == notFundCode){ + html = "

页面404,正常结束请求即可

"; + }else{ + throw new Exception("请求错误,code码为:"+statusLine.getStatusCode()); + } + } catch (Exception e) { + e.printStackTrace(); + html = "Download failed error is:"+ThrowMessageUtil.getErrmessage(e); + } + return html; + + } + + /** + * 文件下载 + * @param fileURL 文件链接 + * @param destinationFilePath 文件存储地址 + * @throws IOException + */ + public static void downloadFile(String fileURL, String destinationFilePath) throws IOException { + // 设置连接超时和读取超时 + RequestConfig config = RequestConfig.custom() + // 设置连接超时为10秒 + .setConnectTimeout(10000) + // 设置读取超时为30秒 + .setSocketTimeout(30000) + .build(); + + // 创建 HttpClient 实例 + try (CloseableHttpClient httpClient = HttpClients.custom() + .setDefaultRequestConfig(config) + .build()) { + + // 创建 HttpGet 请求 + HttpGet request = new HttpGet(URI.create(fileURL)); + + // 执行请求 + try (CloseableHttpResponse response = httpClient.execute(request)) { + // 获取响应的输入流 + InputStream inputStream = response.getEntity().getContent(); + try (FileOutputStream outputStream = new FileOutputStream(destinationFilePath)) { + byte[] buffer = new byte[4096]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + } + log.info("文件下载成功---{}" , destinationFilePath); + } + } + } + + /** + * 文件上传 + * @param filePath + * @param gofastUrl + * @return + */ + public static String upLoadFile(String filePath,String gofastUrl) { + File file = new File(filePath); + String realFilename = filePath.substring(filePath.lastIndexOf(File.separator) + 1); + MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM); + builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"file\";filename=\"" + realFilename + "\""), + RequestBody.create(MediaType.parse("image/png"), file) + + ).addFormDataPart("output", "json").build(); + RequestBody body = builder.build(); + Request request = new Request.Builder().url(gofastUrl).post(body).header("Expect", "100-continue").build(); + OkHttpClient.Builder okBuilder = new OkHttpClient.Builder(); + OkHttpClient client = okBuilder.connectTimeout(600, TimeUnit.MILLISECONDS) + .readTimeout(600, TimeUnit.SECONDS).build(); + Call call = client.newCall(request); + String html = ""; + Response response = null; + try { + response = call.execute(); + html = response.body().string(); + } catch (IOException e) { + log.info("upload fail:{}", filePath); + e.printStackTrace(); + } finally { + response.close(); + } + return html; + } + public static void main(String[] args) throws Exception { + + } +} diff --git a/translate-service/src/main/java/com/bw/translate/utils/EncryptionUtil.java b/translate-service/src/main/java/com/bw/translate/utils/EncryptionUtil.java new file mode 100644 index 0000000..9fb9612 --- /dev/null +++ b/translate-service/src/main/java/com/bw/translate/utils/EncryptionUtil.java @@ -0,0 +1,27 @@ +package com.bw.translate.utils; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * @author jian.mao + * @date 2023年3月10日 + * @description + */ +public class EncryptionUtil { + public static String md5(String text) { + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + md.update(text.getBytes()); + byte[] bytes = md.digest(); + StringBuilder sb = new StringBuilder(); + for (byte b : bytes) { + sb.append(String.format("%02x", b & 0xff)); + } + return sb.toString(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + return null; + } + } +} diff --git a/translate-service/src/main/java/com/bw/translate/utils/FileUtil.java b/translate-service/src/main/java/com/bw/translate/utils/FileUtil.java new file mode 100644 index 0000000..a4416e4 --- /dev/null +++ b/translate-service/src/main/java/com/bw/translate/utils/FileUtil.java @@ -0,0 +1,66 @@ +package com.bw.translate.utils; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.List; + +import org.apache.commons.io.FileUtils; + +/** + * 文件工具类 + * @author jian.mao + * @date 2023年7月14日 + * @description + */ +public class FileUtil { + + /** + * 数据写入文件 + * @param Path 文件路径 + * @param result 数据 + * @throws IOException + */ + public static void writeFile(String path,String result){ + try { + FileWriter fw = new FileWriter(path,true); + fw.write(result+"\n"); + fw.flush(); + fw.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void delFile(String path) { + try { + File file = new File(path); + file.delete(); + } catch (Exception e) { + // TODO: handle exception + e.printStackTrace(); + } + } + + /** + * 读取prompt + * @param path + * @return + */ + public static String readPrompt(String path){ + StringBuffer prompt = new StringBuffer(); + File file = new File(path); + if(file.exists()){ + List tasks = null; + try { + tasks = FileUtils.readLines(file,"UTF-8"); + } catch (IOException e) { + e.printStackTrace(); + } + for (String taskStr : tasks) { + prompt.append(taskStr); + } + } + return prompt.toString(); + } +} diff --git a/translate-service/src/main/java/com/bw/translate/utils/OtherUtils.java b/translate-service/src/main/java/com/bw/translate/utils/OtherUtils.java new file mode 100644 index 0000000..6450c7d --- /dev/null +++ b/translate-service/src/main/java/com/bw/translate/utils/OtherUtils.java @@ -0,0 +1,33 @@ +package com.bw.translate.utils; + +import java.security.MessageDigest; + +/** + * 其他工具类 + * @author jian.mao + * @date 2023年9月19日 + * @description + */ +public class OtherUtils { + + + + public static String getMd5(String string) { + try { + MessageDigest md5 = MessageDigest.getInstance("MD5"); + byte[] bs = md5.digest(string.getBytes("UTF-8")); + StringBuilder sb = new StringBuilder(40); + for (byte x : bs) { + if ((x & 0xff) >> 4 == 0) { + sb.append("0").append(Integer.toHexString(x & 0xff)); + } else { + sb.append(Integer.toHexString(x & 0xff)); + } + } + return sb.toString(); + } catch (Exception e) { + + return "nceaform" + System.currentTimeMillis(); + } + } +} diff --git a/translate-service/src/main/java/com/bw/translate/utils/QueueUtil.java b/translate-service/src/main/java/com/bw/translate/utils/QueueUtil.java new file mode 100644 index 0000000..752b0ae --- /dev/null +++ b/translate-service/src/main/java/com/bw/translate/utils/QueueUtil.java @@ -0,0 +1,18 @@ +package com.bw.translate.utils; + +import java.util.Map; +import java.util.concurrent.LinkedBlockingDeque; + +/** + * @author:jinming + * @className:QueueUtil + * @version:1.0 + * @description: + * @Date:2023/7/13 15:00 + */ +public class QueueUtil { + + public static LinkedBlockingDeque> taskQueue = new LinkedBlockingDeque>(); + + public static LinkedBlockingDeque sendQueue = new LinkedBlockingDeque(); +} \ No newline at end of file diff --git a/translate-service/src/main/java/com/bw/translate/utils/ThrowMessageUtil.java b/translate-service/src/main/java/com/bw/translate/utils/ThrowMessageUtil.java new file mode 100644 index 0000000..a6bd3f2 --- /dev/null +++ b/translate-service/src/main/java/com/bw/translate/utils/ThrowMessageUtil.java @@ -0,0 +1,23 @@ +package com.bw.translate.utils; + +import java.io.PrintWriter; +import java.io.StringWriter; + +/** + * @author jian.mao + * @date 2023年3月22日 + * @description + */ +public class ThrowMessageUtil { + + /** + * 获取异常信息 + * @param t + * @return + */ + public static String getErrmessage(Throwable t){ + StringWriter stringWriter=new StringWriter(); + t.printStackTrace(new PrintWriter(stringWriter,true)); + return stringWriter.getBuffer().toString(); + } +} diff --git a/translate-service/src/main/resources/bootstrap.yml b/translate-service/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..18b3ebd --- /dev/null +++ b/translate-service/src/main/resources/bootstrap.yml @@ -0,0 +1,52 @@ +# ==================== 必须文件:bootstrap.yml ==================== +# 这个文件用于配置Nacos客户端,优先级最高 +spring: + application: + name: translate-service # 服务名,对应Nacos中的Data ID + + cloud: + nacos: + # ======== 配置中心 ======== + config: + server-addr: 127.0.0.1:8848 # Nacos地址 + username: nacos # 用户名 + password: nacos # 密码 + group: public_dev # 分组 + namespace: opai # 命名空间(默认public) + file-extension: yaml # 配置文件格式 + timeout: 5000 # 超时时间(ms) + + # 核心配置:开启动态刷新 + refresh-enabled: true # 必须为true! + + # 主配置文件(从Nacos加载) + data-id: ${spring.application.name}.${spring.cloud.nacos.config.file-extension} + + # 共享配置文件(可选) + shared-configs[0]: + data-id: application.yaml # 公共配置 + group: public_dev # 公共分组 + namespace: opai + refresh: true # 公共配置也要刷新 + + # 扩展配置(可选) + # extension-configs[0]: + # data-id: datasource.yaml + # group: dev + # refresh: true + + # ======== 服务发现 ======== + discovery: + server-addr: ${spring.cloud.nacos.config.server-addr} + username: ${spring.cloud.nacos.config.username} + password: ${spring.cloud.nacos.config.password} + group: ${spring.cloud.nacos.config.group} + namespace: ${spring.cloud.nacos.config.namespace} + +logging: + level: + root: info + com.alibaba.nacos.client.config.impl: WARN + file: + path: ../logs + \ No newline at end of file diff --git a/translate-service/src/main/resources/logback-spring.xml b/translate-service/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..c4f5c3a --- /dev/null +++ b/translate-service/src/main/resources/logback-spring.xml @@ -0,0 +1,36 @@ + + + + + + + + + true + + ${logging.level} + + + ${log-path}/translate-service.log + + + ${log-path}/translate-service.%d{yyyy-MM-dd} + 7 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %line %-5level %logger{50} - %msg%n + UTF-8 + + + + + + + +