本文共 5188 字,大约阅读时间需要 17 分钟。
JDK8+
Gradle4+
Redis 3.2.100
Apache HttpClient 4.5.3
Spring Boot Web Starter
Spring Boot Data Redis Starter
Spring Boot Quartz Starter
Quartz Scheduler
新建项目文件夹:
将micro-weather-report
项目中的源码文件复制粘贴到新项目文件夹中: 修改build.gradle
配置,删除thymeleaf
的依赖:
//依赖关系dependencies { //该依赖用于编译阶段 compile('org.springframework.boot:spring-boot-starter-web') //HttpClient compile('org.apache.httpcomponents:httpclient:4.5.6') //Redis compile('org.springframework.boot:spring-boot-starter-data-redis') //Quartz compile('org.springframework.boot:spring-boot-starter-quartz') //该依赖用于测试阶段 testCompile('org.springframework.boot:spring-boot-starter-test')}
在com.study.spring.cloud.weather.service
包下新建类WeatherDataCollectionService
:
package com.study.spring.cloud.weather.service;public interface WeatherDataCollectionService { //根据城市ID同步天气 void syncDataByCityId(String cityId);}
在com.study.spring.cloud.weather.service
包下新建类WeatherDataCollectionServiceImpl
:
package com.study.spring.cloud.weather.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.http.ResponseEntity;import org.springframework.web.client.RestTemplate;import java.util.concurrent.TimeUnit;@Servicepublic class WeatherDataCollectionServiceImpl implements WeatherDataCollectionService { private static final String WEATHER_URI="http://wthrcdn.etouch.cn/weather_mini?"; private static final long TIME_OUT=1800L; @Autowired //对rest客户端的封装 private RestTemplate restTemplate; @Autowired //对redis api的封装 private StringRedisTemplate stringRedisTemplate; @Override public void syncDataByCityId(String cityId) { String uri=WEATHER_URI + "citykey=" + cityId; this.saveWeatherData(uri); } //把天气数据放在缓存中 private void saveWeatherData(String uri){ String key=uri; String strBody=null; //ValueOperations类可通过get()获取缓存中的数据 ValueOperationsops = stringRedisTemplate.opsForValue(); //调用服务接口来获取 ResponseEntity respString = restTemplate.getForEntity(uri, String.class); //判断ResponseEntity的状态码是否为200,为200时取出strBody if(respString.getStatusCodeValue()==200) { strBody=respString.getBody(); } //数据写入缓存 ops.set(key, strBody, TIME_OUT, TimeUnit.SECONDS); }}
此处代码复制粘贴自WeatherDataServiceImpl
类,此类完成后,com.study.spring.cloud.weather.service
包中的WeatherDataService
和WeatherDataServiceImpl
两个类就可以删除了
修改com.study.spring.cloud.weather.job
包下的WeatherDataSyncJob
类:
package com.study.spring.cloud.weather.job;import com.study.spring.cloud.weather.service.WeatherDataCollectionService;import com.study.spring.cloud.weather.vo.City;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.scheduling.quartz.QuartzJobBean;import java.util.ArrayList;import java.util.List;public class WeatherDataSyncJob extends QuartzJobBean { //在应用中添加日志 private final static Logger logger=LoggerFactory.getLogger(WeatherDataCollectionService.class); @Autowired private WeatherDataCollectionService weatherDataCollectionService; @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { logger.info("Weather Data Sync Job. Start!"); //获取城市列表 //TODO 改为由城市数据API微服务来提供数据 ListcityList=null; try { //TODO 改为由城市数据API微服务来提供数据 cityList = new ArrayList<>(); City city=new City(); city.setCityId("101020100"); cityList.add(city); } catch (Exception e) { logger.error("Exception!",e); } //遍历城市id获取天气 for(City city:cityList){ String cityId=city.getCityId(); logger.info("Weather Data Sync Job, cityId:"+cityId); weatherDataCollectionService.syncDataByCityId(cityId); } logger.info("Weather Data Sync Job. End!"); }}
此类完成后,com.study.spring.cloud.weather.service
包中的CityDataService
和CityDataServiceImpl
两个类就可以删除了
修改com.study.spring.cloud.weather.vo
包下的City
类,去掉其中xml
相关解析代码:
package com.study.spring.cloud.weather.vo;public class City { private String cityId; private String cityName; private String cityCode; private String province; public String getCityId() { return cityId; } public void setCityId(String cityId) { this.cityId = cityId; } public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } public String getCityCode() { return cityCode; } public void setCityCode(String cityCode) { this.cityCode = cityCode; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; }}
com.study.spring.cloud.weather.vo
包下的其他类(CityList
、Forecast
、Weather
、 WeatherResponse
、Yesterday
)全都删除
com.study.spring.cloud.weather.util
包连同其下的类XmlBuilder
删除
com.study.spring.cloud.weather.service
包下的WeatherReportService
和WeatherReportServiceImpl
两个类删除
com.study.spring.cloud.weather.controller
包下的WeatherController
和WeatherReportController
两个类删除
前端目录下:
修改application.properties
配置文件,将 #热部署静态文件spring.thymeleaf.cache=false
内容删除
此时src
目录结构如下:
注意一定要先运行Redis
运行应用:
运行结果如下:转载地址:http://vgiwi.baihongyu.com/