Browse Source

api retry

wangyunpeng 11 months ago
parent
commit
2ac43586b2

+ 25 - 23
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/remote/ArticleListRemoteService.java

@@ -16,9 +16,7 @@ import org.apache.http.util.EntityUtils;
 import org.springframework.stereotype.Service;
 
 import java.nio.charset.StandardCharsets;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Objects;
+import java.util.*;
 
 @Service
 @Slf4j
@@ -48,32 +46,36 @@ public class ArticleListRemoteService {
         bodyParam.put("top_n", 10000);
         bodyParam.put("use_max_time", true);
         bodyParam.put("use_min_time", true);
-        try {
-            HttpPost httpPost = new HttpPost(articleListUrl);
-            StringEntity stringEntity = new StringEntity(bodyParam.toJSONString(), StandardCharsets.UTF_8);
-            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
-            httpPost.setEntity(stringEntity);
-            CloseableHttpResponse response = client.execute(httpPost);
-            StatusLine statusLine = response.getStatusLine();
-            if (statusLine.getStatusCode() == 200) {
-                HttpEntity responseEntity = response.getEntity();
-                if (Objects.nonNull(responseEntity)) {
-                    String responseBody = EntityUtils.toString(responseEntity, "UTF-8");
-                    log.info("articleList 返回的数据:{}", responseBody);
-                    JSONObject articleListResponse = JSONObject.parseObject(responseBody);
-                    if (Objects.nonNull(articleListResponse)) {
-                        JSONArray articleList = articleListResponse.getJSONArray("article_list");
-                        if (CollectionUtils.isNotEmpty(articleList)) {
-                            return articleList.toJavaList(Article.class);
+        int retry = 0;
+        while (retry < 3) {
+            retry++;
+            try {
+                HttpPost httpPost = new HttpPost(articleListUrl);
+                StringEntity stringEntity = new StringEntity(bodyParam.toJSONString(), StandardCharsets.UTF_8);
+                httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
+                httpPost.setEntity(stringEntity);
+                CloseableHttpResponse response = client.execute(httpPost);
+                StatusLine statusLine = response.getStatusLine();
+                if (statusLine.getStatusCode() == 200) {
+                    HttpEntity responseEntity = response.getEntity();
+                    if (Objects.nonNull(responseEntity)) {
+                        String responseBody = EntityUtils.toString(responseEntity, "UTF-8");
+                        log.info("articleList 返回的数据:{}", responseBody);
+                        JSONObject articleListResponse = JSONObject.parseObject(responseBody);
+                        if (Objects.nonNull(articleListResponse)) {
+                            JSONArray articleList = articleListResponse.getJSONArray("article_list");
+                            if (CollectionUtils.isNotEmpty(articleList)) {
+                                return articleList.toJavaList(Article.class);
+                            }
                         }
                     }
                 }
+            } catch (Exception e) {
+                log.error("articleList error", e);
             }
-        } catch (Exception e) {
-            log.error("articleList error", e);
         }
         log.info("articleList耗时:{}", System.currentTimeMillis() - start);
-        return null;
+        return Collections.emptyList();
     }
 
 }

+ 26 - 22
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/remote/NLPRemoteService.java

@@ -37,33 +37,37 @@ public class NLPRemoteService {
         bodyParam.put("interest_type", "by_avg");
         bodyParam.put("sim_type", "mean");
         bodyParam.put("rate", 0.1);
-        try {
-            HttpPost httpPost = new HttpPost(scoreListUrl);
-            StringEntity stringEntity = new StringEntity(bodyParam.toJSONString(), StandardCharsets.UTF_8);
-            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
-            httpPost.setEntity(stringEntity);
-            CloseableHttpResponse response = client.execute(httpPost);
-            StatusLine statusLine = response.getStatusLine();
-            if (statusLine.getStatusCode() == 200) {
-                HttpEntity responseEntity = response.getEntity();
-                if (Objects.nonNull(responseEntity)) {
-                    String responseBody = EntityUtils.toString(responseEntity, "UTF-8");
-                    log.info("scoreList 返回的数据:{}", responseBody);
-                    JSONObject scoreListResponse = JSONObject.parseObject(responseBody);
-                    if (Objects.nonNull(scoreListResponse)) {
-                        JSONObject accountScoreList = scoreListResponse.getJSONObject(accountName);
-                        if (Objects.nonNull(accountScoreList)) {
-                            List<Double> scoreList = accountScoreList.getJSONArray("score_list").toJavaList(Double.class);
-                            for (int i = 0; i < contentList.size(); i++) {
-                                result.put(contentList.get(i).getId(), scoreList.get(i));
+        int retry = 0;
+        while (retry < 3) {
+            retry++;
+            try {
+                HttpPost httpPost = new HttpPost(scoreListUrl);
+                StringEntity stringEntity = new StringEntity(bodyParam.toJSONString(), StandardCharsets.UTF_8);
+                httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
+                httpPost.setEntity(stringEntity);
+                CloseableHttpResponse response = client.execute(httpPost);
+                StatusLine statusLine = response.getStatusLine();
+                if (statusLine.getStatusCode() == 200) {
+                    HttpEntity responseEntity = response.getEntity();
+                    if (Objects.nonNull(responseEntity)) {
+                        String responseBody = EntityUtils.toString(responseEntity, "UTF-8");
+                        log.info("scoreList 返回的数据:{}", responseBody);
+                        JSONObject scoreListResponse = JSONObject.parseObject(responseBody);
+                        if (Objects.nonNull(scoreListResponse)) {
+                            JSONObject accountScoreList = scoreListResponse.getJSONObject(accountName);
+                            if (Objects.nonNull(accountScoreList)) {
+                                List<Double> scoreList = accountScoreList.getJSONArray("score_list").toJavaList(Double.class);
+                                for (int i = 0; i < contentList.size(); i++) {
+                                    result.put(contentList.get(i).getId(), scoreList.get(i));
+                                }
+                                return result;
                             }
-                            return result;
                         }
                     }
                 }
+            } catch (Exception e) {
+                log.error("score error", e);
             }
-        } catch (Exception e) {
-            log.error("score error", e);
         }
         log.info("score耗时:{}", System.currentTimeMillis() - start);
         return Collections.emptyMap();