Просмотр исходного кода

feat: "添加代理服务器,修改webclientUtil的逻辑"

Leonezhurui 2 лет назад
Родитель
Сommit
7cb0743695

+ 0 - 14
src/main/java/com/njuzr/eaibackend/controller/OnlyOfficeController.java

@@ -30,8 +30,6 @@ import java.util.Scanner;
 @RestController
 @RequestMapping("/api/onlyoffice")
 public class OnlyOfficeController {
-    private final WebClientUtil webClientUtil = new WebClientUtil("http://47.111.23.171:8082/coauthoring/CommandService.ashx");
-
     private final FileUtil fileUtil = new FileUtil();
 
     private final OssUtil ossUtil;
@@ -77,18 +75,6 @@ public class OnlyOfficeController {
         }
     }
 
-    @PostMapping("/forcesave")
-    public MyResponse forceSave(@RequestBody MyRequestObject requestObject) {
-        String url = "";
-        MyResponseObject response = webClientUtil.post(url, requestObject, MyResponseObject.class);
-        log.info("请求返回信息如下:"+response.toString());
-        if (response.getError() != 0 && response.getError() != 4)
-            throw MyException.create(HttpStatus.INTERNAL_SERVER_ERROR, "服务器调用OnlyOffice内部错误");
-        if (response.getError() == 4)
-            throw MyException.create(HttpStatus.BAD_REQUEST, "OnlyOffice编辑器内容未修改");
-
-        return MyResponse.success("触发forceSave成功");
-    }
 
     @Data
     @AllArgsConstructor

+ 15 - 7
src/main/java/com/njuzr/eaibackend/service/AIRequestService.java

@@ -28,17 +28,23 @@ public class AIRequestService {
     @Value("${openai.chatgpt.api.key}")
     private String key;
 
-    private final WebClientUtil chatgptClient = new WebClientUtil("https://api.openai.com");
-    private final WebClientUtil chatglmClient = new WebClientUtil("http://10.58.0.2:6678");
-    private final WebClientUtil qwenClient = new WebClientUtil("http://10.58.0.2:6679");
+    private final WebClientUtil chatgptClient;
+    private final WebClientUtil chatglmClient;
+    private final WebClientUtil qwenClient;
 
     private final String prefix = "/v1/chat/completions";
 
+    public AIRequestService(WebClientUtil chatgptClient, WebClientUtil chatglmClient, WebClientUtil qwenClient) {
+        this.chatgptClient = chatgptClient;
+        this.chatglmClient = chatglmClient;
+        this.qwenClient = qwenClient;
+    }
+
     public AIResponse requestChatGPT(List<AIEntry> messages) {
         MyRequestObject requestObject = new MyRequestObject(model, messages);
-
+        String chatgptUrl = "https://api.openai.com";
         try {
-            return chatgptClient.postWithToken(prefix, requestObject, AIResponse.class, key);
+            return chatgptClient.postWithToken(chatgptUrl+prefix, requestObject, AIResponse.class, key);
         } catch (Exception e) {
             log.error("WebClient请求失败,AI请求失败~");
             throw new MyException(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()+":"+"服务器请求AI出错");
@@ -48,9 +54,10 @@ public class AIRequestService {
 
     public AIResponse requestChatGLM(List<AIEntry> messages) {
         MyRequestObject requestObject = new MyRequestObject("ChatGLM3", messages);
+        String chatglmUrl = "http://10.58.0.2:6678";
 
         try {
-            return chatglmClient.post(prefix, requestObject, AIResponse.class);
+            return chatglmClient.post(chatglmUrl+prefix, requestObject, AIResponse.class);
         } catch (Exception e) {
             log.error("WebClient请求失败,AI请求失败~");
             throw new MyException(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()+":"+"服务器请求AI出错");
@@ -59,9 +66,10 @@ public class AIRequestService {
 
     public AIResponse requestQWen(List<AIEntry> messages) {
         MyRequestObject requestObject = new MyRequestObject("QWen14B", messages);
+        String qwenUrl = "http://10.58.0.2:6679";
 
         try {
-            return qwenClient.post(prefix, requestObject, AIResponse.class);
+            return qwenClient.post(qwenUrl+prefix, requestObject, AIResponse.class);
         } catch (Exception e) {
             log.error("WebClient请求失败,AI请求失败~");
             throw new MyException(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()+":"+"服务器请求AI出错");

+ 7 - 2
src/main/java/com/njuzr/eaibackend/service/EmailService.java

@@ -25,10 +25,15 @@ import org.springframework.stereotype.Service;
 @Slf4j
 @Service
 public class EmailService {
-    private final WebClientUtil webClientUtil = new WebClientUtil("https://message.seec.seecoder.cn/message/mail");
+    private final WebClientUtil webClientUtil;
+
+    @Autowired
+    public EmailService(WebClientUtil webClientUtil) {
+        this.webClientUtil = webClientUtil;
+    }
 
     private void sendEmail(MyRequestObject requestObject) {
-        MyResponseObject response = webClientUtil.post("", requestObject, MyResponseObject.class);
+        MyResponseObject response = webClientUtil.post("https://message.seec.seecoder.cn/message/mail", requestObject, MyResponseObject.class);
         if (response.code == 1) {
             log.info("WebClient请求成功,邮件发送成功!");
         } else {

+ 4 - 3
src/main/java/com/njuzr/eaibackend/service/TranslateService.java

@@ -26,11 +26,12 @@ import java.util.Objects;
 @Slf4j
 @Service
 public class TranslateService {
-    private final WebClientUtil webClientUtil = new WebClientUtil("http://localhost:5000/api/translate");
+    private final WebClientUtil webClientUtil;
 
     private final TranslationMapper translationMapper;
 
-    public TranslateService(TranslationMapper translationMapper) {
+    public TranslateService(WebClientUtil webClientUtil, TranslationMapper translationMapper) {
+        this.webClientUtil = webClientUtil;
         this.translationMapper = translationMapper;
     }
 
@@ -57,7 +58,7 @@ public class TranslateService {
 //                curtime
 //                );
 
-        MyResponseObject response = webClientUtil.post("", translationDTO, MyResponseObject.class);
+        MyResponseObject response = webClientUtil.post("http://localhost:5000/api/translate", translationDTO, MyResponseObject.class);
 
         if (Objects.equals(response.getCode(), 200)) {
             Translation translation = new Translation();

+ 3 - 2
src/main/java/com/njuzr/eaibackend/service/impl/UserServiceImpl.java

@@ -48,16 +48,17 @@ public class UserServiceImpl implements UserService {
 
     private final RedisTemplate<String, Object> redisTemplate;
 
-    private final EmailService emailService = new EmailService();
+    private final EmailService emailService;
 
     private static final long SEND_INTERVAL = 60; // 允许再次发送的时间间隔,单位秒
 
     private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
 
     @Autowired
-    public UserServiceImpl(UserMapper userMapper, RedisTemplate<String, Object> redisTemplate) {
+    public UserServiceImpl(UserMapper userMapper, RedisTemplate<String, Object> redisTemplate, EmailService emailService) {
         this.userMapper = userMapper;
         this.redisTemplate = redisTemplate;
+        this.emailService = emailService;
     }
 
 

+ 37 - 42
src/main/java/com/njuzr/eaibackend/utils/WebClientUtil.java

@@ -1,20 +1,14 @@
 package com.njuzr.eaibackend.utils;
 
-import io.netty.handler.ssl.SslContextBuilder;
-import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.MediaType;
 import org.springframework.http.client.reactive.ReactorClientHttpConnector;
+import org.springframework.stereotype.Component;
 import org.springframework.web.reactive.function.client.WebClient;
 import org.springframework.web.reactive.function.client.WebClientResponseException;
 import reactor.netty.http.client.HttpClient;
 import reactor.netty.transport.ProxyProvider;
 
-import java.nio.file.Path;
-import java.util.HashMap;
-import java.util.function.Consumer;
 
 /**
  * @author: Leonezhurui
@@ -23,51 +17,52 @@ import java.util.function.Consumer;
  */
 
 @Slf4j
+@Component
 public class WebClientUtil {
 
-    @Value("${proxy.enable}")
-    private Boolean enableProxy;
-
-    @Value("${proxy.host}")
-    private String proxyHost;
-
-    @Value("${proxy.port}")
-    private int proxyPort;
-
     private final WebClient webClient;
 
-    public WebClientUtil() {
-        this.webClient = WebClient.builder().build();
+    public WebClientUtil(@Value("${proxy.enabled}") boolean proxyEnabled,
+                         @Value("${proxy.host}") String proxyHost,
+                         @Value("${proxy.port}") int proxyPort) {
+        HttpClient httpClient = HttpClient.create();
+        if (proxyEnabled) {
+            httpClient = httpClient.proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP)
+                    .host(proxyHost)
+                    .port(proxyPort));
+        }
+        this.webClient = WebClient.builder()
+                .clientConnector(new ReactorClientHttpConnector(httpClient))
+                .build();
     }
 
-    // 构造函数,使用baseUrl初始化WebClient
-    public WebClientUtil(String baseUrl) {
-//        SslContextBuilder sslContextBuilder = SslContextBuilder
-//                .forClient()
-//                .trustManager(InsecureTrustManagerFactory.INSTANCE); // 信任所有证书
+
+//    public WebClientUtil() {
+//        this.webClient = WebClient.builder().build();
+//    }
 //
-//        HttpClient httpClient = HttpClient.create()
-//                .secure(sslContextSpec -> sslContextSpec.sslContext(sslContextBuilder));
+//    public WebClientUtil(WebClient webClient) {
+//        this.webClient = webClient;
+//    }
+//
+//    public WebClientUtil(String url) {
 //        this.webClient = WebClient.builder()
-//                .clientConnector(new ReactorClientHttpConnector(httpClient))
-//                .baseUrl(baseUrl)
+//                .baseUrl(url)
 //                .build();
+//    }
 
-        if (enableProxy) {
-            HttpClient httpClient = HttpClient.create()
-                    .proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP)
-                            .host(proxyHost)
-                            .port(proxyPort));
-            this.webClient = WebClient.builder()
-                    .clientConnector(new ReactorClientHttpConnector(httpClient))
-                    .baseUrl(baseUrl)
-                    .build();
-        } else {
-            this.webClient = WebClient.builder()
-                    .baseUrl(baseUrl)
-                    .build();
-        }
-    }
+//    public static WebClientUtil createWebClientWithProxy(String url, String proxyHost, int proxyPort) {
+//        HttpClient httpClient = HttpClient.create()
+//                .proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP)
+//                        .host(proxyHost)
+//                        .port(proxyPort));
+//
+//        WebClient webClient1 =WebClient.builder()
+//                .clientConnector(new ReactorClientHttpConnector(httpClient))
+//                .baseUrl(url)
+//                .build();
+//        return new WebClientUtil(webClient1);
+//    }
 
     // GET请求方法
     public <T> T get(String uri, Class<T> responseType) {