开发环境
JDK8+Gradle4+Redis 3.2.100Apache HttpClient 4.5.3Spring Boot Web StarterSpring Boot Data Redis StarterSpring Boot Quartz StarterQuartz 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;
@Service
public 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()获取缓存中的数据
ValueOperations<String,String> ops = stringRedisTemplate.opsForValue();
//调用服务接口来获取
ResponseEntity<String> 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微服务来提供数据
List<City> cityList=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
运行应用:
运行结果如下: