Przeglądaj źródła

天气台账获取失败时重试

lvy 2 miesięcy temu
rodzic
commit
bdca4d023e

+ 35 - 21
blade-common/src/main/java/org/springblade/common/utils/YiKeYunApiUtils.java

@@ -7,6 +7,7 @@ import lombok.extern.slf4j.Slf4j;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.net.ConnectException;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
@@ -17,6 +18,24 @@ public class YiKeYunApiUtils {
     private static final String API_YIKEYUN_APPID = "97543277";
     private static final String API_YIKEYUN_APPSECRET = "PXd7GE2j";
 
+    public static Map<String, String> getTodayWeatherByAdcodeTry(String adcode) {
+        for (int i = 0; i < 3; i++) {
+            try {
+                return getTodayWeatherByAdcode(adcode);
+            } catch (IOException e) {
+                // 重试
+                try {
+                    Thread.sleep(500);
+                } catch (InterruptedException ex) {
+                    e.printStackTrace();
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+                return null;
+            }
+        }
+        return null;
+    }
 
 
     /**
@@ -25,29 +44,24 @@ public class YiKeYunApiUtils {
      * @param adcode
      * @return
      */
-    public static Map<String, String> getTodayWeatherByAdcode(String adcode) {
+    public static Map<String, String> getTodayWeatherByAdcode(String adcode) throws IOException {
         String getUrl = String.format("http://v1.yiketianqi.com/free/day?appid=%s&appsecret=%s&unescape=1&adcode=%s", API_YIKEYUN_APPID, API_YIKEYUN_APPSECRET, adcode);
         Map<String, String> map = new HashMap<>();
-        try {
-            URL url = new URL(getUrl);
-            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
-            String res;
-            StringBuilder sb = new StringBuilder();
-            while ((res = in.readLine()) != null) {
-                sb.append(res.trim());
-            }
-            log.info("获取当天天气 ======= sb.toString():" + sb);
-            JSONObject jsonData = JSONObject.parseObject(sb.toString());
-            map.put("temp", jsonData.get("tem").toString());
-            map.put("weather", jsonData.get("wea").toString());
-            map.put("high", jsonData.get("tem_day").toString());
-            map.put("low", jsonData.get("tem_night").toString());
-            map.put("windLevel", jsonData.get("win_speed").toString());
-            map.put("nums", jsonData.getInteger("nums").toString());
-        } catch (IOException e) {
-            e.printStackTrace();
-            return null;
+        URL url = new URL(getUrl);
+        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
+        String res;
+        StringBuilder sb = new StringBuilder();
+        while ((res = in.readLine()) != null) {
+            sb.append(res.trim());
         }
+        log.info("获取当天天气 ======= sb.toString():" + sb);
+        JSONObject jsonData = JSONObject.parseObject(sb.toString());
+        map.put("temp", jsonData.get("tem").toString());
+        map.put("weather", jsonData.get("wea").toString());
+        map.put("high", jsonData.get("tem_day").toString());
+        map.put("low", jsonData.get("tem_night").toString());
+        map.put("windLevel", jsonData.get("win_speed").toString());
+        map.put("nums", jsonData.getInteger("nums").toString());
         return map;
     }
 
@@ -119,7 +133,7 @@ public class YiKeYunApiUtils {
             });
 
 
-        } catch (IOException e) {
+        } catch (Exception e) {
             e.printStackTrace();
             return null;
         }

+ 17 - 11
blade-service/blade-business/src/main/java/org/springblade/business/service/impl/WeatherInfoServiceImpl.java

@@ -139,20 +139,21 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
                 }
                 Map<String, String> weatherMap = cachedWeatherMap.get(area.getCity_code());
                 if (weatherMap == null) {
-                    //获取天气信息(百度天气)
                     String nums = stringRedisTemplate.opsForValue().get("blade-business::contractArea:yiKeYun:todayWeatherNums");
-                    if (nums != null && Integer.parseInt(nums) <= 19000) {
-                        weatherMap = YiKeYunApiUtils.getTodayWeatherByAdcode(area.getCity_code() + "000000");
+                    if (nums == null || Integer.parseInt(nums) <= 19000) {
+                        // 重试三次
+                        weatherMap = YiKeYunApiUtils.getTodayWeatherByAdcodeTry(area.getCity_code() + "000000");
+                        if (weatherMap != null) {
+                            cachedWeatherMap.put(area.getCity_code(), weatherMap);
+                            if (weatherMap.get("nums") != null) {
+                                stringRedisTemplate.opsForValue().set("blade-business::contractArea:yiKeYun:todayWeatherNums", weatherMap.get("nums"), getSecondsUntilMidnight(), TimeUnit.SECONDS);
+                            }
+                        }
                     }
+                    //获取天气信息(百度天气)
 //                    if (weatherMap == null) {
 //                        weatherMap = BaiduApiUtil.getTodayWeather(area.getCity_code());
 //                    }
-                    if (weatherMap != null) {
-                        cachedWeatherMap.put(area.getCity_code(), weatherMap);
-                        if (weatherMap.get("nums") != null) {
-                            stringRedisTemplate.opsForValue().set("blade-business::contractArea:yiKeYun:todayWeatherNums", weatherMap.get("nums"), getSecondsUntilMidnight(), TimeUnit.SECONDS);
-                        }
-                    }
                 }
                 if (weatherMap != null) {
                     //计算平均气温
@@ -430,11 +431,16 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
             city = city.substring(0, city.length() - 1);
         }
         Integer count = jdbcTemplate.queryForObject("select count(*) from m_yikeyun_weather_city where cityZh like '" + county + "%'", Integer.class);
-        String cityId = null;
+        String cityId;
         if (count == null || count == 0) {
+            log.warn("获取历史天气失败:" + area.getCity()+"-"+area.getCounty());
             return null;
         } else if (count > 1) {
-            cityId = jdbcTemplate.queryForObject("select id from m_yikeyun_weather_city where cityZh like '" + county + "%' and leaderZh like '" + city + "%' limit 1", String.class);
+            try {
+                cityId = jdbcTemplate.queryForObject("select id from m_yikeyun_weather_city where cityZh like '" + county + "%' and leaderZh like '" + city + "%' limit 1", String.class);
+            } catch (Exception e) {
+                cityId = jdbcTemplate.queryForObject("select id from m_yikeyun_weather_city where cityZh like '" + county + "%' and leaderZh like '" + county + "%' limit 1", String.class);
+            }
         } else {
             cityId = jdbcTemplate.queryForObject("select id from m_yikeyun_weather_city where cityZh like '" + county + "%'", String.class);
         }