# WeatherRepoter **Repository Path**: xvjialing/WeatherRepoter ## Basic Information - **Project Name**: WeatherRepoter - **Description**: SpringBoot for WeatherRepoter - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2018-02-12 - **Last Updated**: 2026-03-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # WeatherRepoter ## 通过天气预报API获取天气预报 实况天气获取:[http://www.weather.com.cn/data/sk/](http://www.weather.com.cn/data/sk/)城市代码.html 城市信息获取:[http://www.weather.com.cn/data/cityinfo/](http://www.weather.com.cn/data/cityinfo/)城市代码.html 详细指数获取:[http://www.weather.com.cn/data/zs/](http://www.weather.com.cn/data/zs/)城市代码.html 根据城市名获取天气: http://wthrcdn.etouch.cn/weather_mini?city= 根据城市代码获取天气: http://wthrcdn.etouch.cn/weather_mini?citykey= * 定义网络服务接口WeatherDataService ```java import com.xvjialing.lytech.weatherreport.bean.WeatherData; public interface WeatherDataService { public WeatherData getWeatherDataByCityCode(String cityCode); public WeatherData getWeatherDataByCityName(String cityName); } ``` * 实现网络接口 ```java import com.alibaba.fastjson.JSON; import com.xvjialing.lytech.weatherreport.bean.WeatherData; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service public class WeatherDataServiceImpl implements WeatherDataService{ @Value("${weather.cityurl}") private String cityUrl; @Value("${weather.citykeyurl}") private String citykeyurl; @Override public WeatherData getWeatherDataByCityCode(String cityCode){ return getWeatherData(citykeyurl+ cityCode); } @Override public WeatherData getWeatherDataByCityName(String cityName){ return getWeatherData(cityUrl+cityName); } private WeatherData getWeatherData(String url) { OkHttpClient okHttpClient=new OkHttpClient(); Request request=new Request.Builder() .url(url) .build(); Response response =null; WeatherData weatherData=null; try { response = okHttpClient.newCall(request).execute(); }catch (Exception e){ e.printStackTrace(); } if (response.isSuccessful()){ try { weatherData= JSON.parseObject(response.body().string(),WeatherData.class); }catch (Exception e){ e.printStackTrace(); } } return weatherData; } } ``` * 创建天气预报访问接口 ```java import com.xvjialing.lytech.weatherreport.bean.WeatherData; import com.xvjialing.lytech.weatherreport.net.WeatherDataService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/api/v1/weather",produces = "application/json") public class WeatherController { @Autowired WeatherDataService weatherDataService; @GetMapping("/cityCode/{cityCode}") public WeatherData getWeatherByCityCode(@PathVariable("cityCode") String cityCode){ return weatherDataService.getWeatherDataByCityCode(cityCode); } @GetMapping("/cityName/{cityName}") public WeatherData getWeatherByCityName(@PathVariable("cityName") String cityName){ return weatherDataService.getWeatherDataByCityName(cityName); } } ``` ## 集成redis > 通过集成redis提高系统的并发能力,因为redis使用内存存储数据,所以取数据很迅速。 * 从内存中取数据,及时响应 * 先从redis缓存中查找数据,不在的情况下再从第三方网络服务获取数据,减少服务调用 ### 配置redis ```yaml spring: redis: host: 127.0.0.1 port: 6379 avalible-time: 20 ``` ### redis使用 ```java import com.alibaba.fastjson.JSON; import com.xvjialing.lytech.weatherreport.bean.WeatherData; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; @Service public class WeatherDataServiceImpl implements WeatherDataService{ private Logger logger = LoggerFactory.getLogger(WeatherDataServiceImpl.class); @Value("${weather.cityurl}") private String cityUrl; @Value("${weather.citykeyurl}") private String citykeyurl; @Value("${spring.redis.avalible-time}") private long avalibleTime; @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private RedisTemplate redisTemplate; @Override public WeatherData getWeatherDataByCityCode(String cityCode){ return getWeatherData(citykeyurl+ cityCode); } @Override public WeatherData getWeatherDataByCityName(String cityName){ return getWeatherData(cityUrl+cityName); } private WeatherData getWeatherData(String url) { Response response =null; String strBody=null; WeatherData weatherData=null; ValueOperations operations = redisTemplate.opsForValue(); //先查缓存,缓存中有的取缓存中的数据 if (stringRedisTemplate.hasKey(url)){ strBody=operations.get(url); logger.info("fromRedis"); }else { //缓存没有,再调用服务接口 OkHttpClient okHttpClient=new OkHttpClient(); Request request=new Request.Builder() .url(url) .build(); try { response = okHttpClient.newCall(request).execute(); }catch (Exception e){ e.printStackTrace(); } if (response.isSuccessful()){ try { strBody=response.body().string(); //数据写入缓存 operations.set(url,strBody,avalibleTime, TimeUnit.SECONDS); //avalibleTime为数据有效时间,超出这个时间数据就自动销毁 }catch (Exception e){ e.printStackTrace(); } } logger.info("fromNet"); } weatherData= JSON.parseObject(strBody,WeatherData.class); return weatherData; } } ``` ## 实现天气数据定时同步 > 之前是通过用户访问的方式被动获取第三方数据,现在采用定时任务的方式主动获取第三方天气数据。