照例附上项目github链接
本项目实现的是将一个简单的天气预报系统一步一步改造成一个SpringCloud微服务系统的过程,本节主要讲的是通过引入Quartz实现天气数据的同步。
存在问题
当用户请求我们的数据的时候才去拉最新的数据,并将其更新到Redis缓存中,效率较低。且缓存中的数据只要存在就不再次做请求,不对数据进行更新,但是天气数据大概是每半个小时就做一次更新的,所以我们传给用户的数据可能不是较新的,数据存在一定误差。
解决方案
通过作业调度框架Quartz实现天气数据的自动同步。
前期工作
要实现定时拉取接口中的数据到Redis缓存中,需要一个城市Id的列表。通过对城市Id列表的遍历,调用weatherDataService中根据城市Id同步数据到Redis中的syncDataByCityId方法,我们就能实现所有城市数据的同步了。
城市列表的构建
由于在程序运行的过程中动态调用服务是有延时的,所以需要减少动态调用服务,因此我们将城市列表缓存到本地。
xml文件的构建
使用xml文件将列表存储到本地中,需要的时候再从本地进行读取,这样会比调用第三方的服务更快。
xml文件如下:
创建如下两个类,并且根据xml的内容定义其属性。
package com.demo.vo;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="d")
@XmlAccessorType(XmlAccessType.FIELD)
public class City {
@XmlAttribute(name="d1")
private String cityId;
@XmlAttribute(name="d2")
private String cityName;
@XmlAttribute(name="d3")
private String cityCode;
@XmlAttribute(name="d4")
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;
}
}
package com.demo.vo;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "c")
@XmlAccessorType(XmlAccessType.FIELD)
public class CityList {
@XmlElement(name = "d")
private List cityList;
public List getCityList() {
return cityList;
}
public void setCityList(List cityList) {
this.cityList = cityList;
}
}
引入工具类,实现将xml转换成java对象的过程。
public class XmlBuilder {
/**
* 将XML转为指定的POJO
* @param clazz
* @param xmlStr
* @return
* @throws Exception
*/
public static Object xmlStrToOject(Class> clazz, String xmlStr) throws Exception {
Object xmlObject = null;
Reader reader = null;
JAXBContext context = JAXBContext.newInstance(clazz);
// XML 转为对象的接口
Unmarshaller unmarshaller = context.createUnmarshaller();
reader = new StringReader(xmlStr);
xmlObject = unmarshaller.unmarshal(reader);
if (null != reader) {
reader.close();
}
return xmlObject;
}
}
获取城市列表的接口
创建CityDataService,定义获取城市列表的方法。
@Service
public class CityDataServiceImpl implements CityDataService{
@Override
public List listCity() throws Exception {
Resource resource=new ClassPathResource("citylist.xml");
BufferedReader br=new BufferedReader(new InputStreamReader(resource.getInputStream(), "utf-8"));
StringBuffer buffer=new StringBuffer();
String line="";
while((line=br.readLine())!=null) {
buffer.append(line);
}
br.close();
CityList cityList=(CityList)XmlBuilder.xmlStrToOject(CityList.class, buffer.toString());
return cityList.getCityList();
}
}
根据城市Id同步天气数据的接口
首先通过城市Id构建对应天气数据的url,然后通过restTemplate的getForEntity方法发起请求,获取返回的内容后使用set方法将其保存到Redis服务器中。
@Override
public void syncDataByCityId(String cityId) {
String url=WEATHER_URI+"citykey=" + cityId;
this.saveWeatherData(url);
}
//将天气数据保存到缓存中,不管缓存中是否存在数据
private void saveWeatherData(String url) {
//将url作为天气的key进行保存
String key=url;
String strBody=null;
ValueOperationsops=stringRedisTemplate.opsForValue();
//通过客户端的get方法发起请求
ResponseEntityrespString=restTemplate.getForEntity(url, String.class);
//判断请求状态
if(respString.getStatusCodeValue()==200) {
strBody=respString.getBody();
}
ops.set(key, strBody,TIME_OUT,TimeUnit.SECONDS);
}
Quartz的引入
Quartz是一个Quartz是一个完全由java编写的开源作业调度框架,在这里的功能相当于一个定时器,定时执行指定的任务。
创建同步天气数据的任务
在Quartz中每个任务就是一个job,在这里我们创建一个同步天气数据的job。
通过cityDataService的listCity方法获取xml文件中所有城市的列表,通过对城市列表的迭代得到所有城市的Id,然后通过weatherDataService的syncDataByCityId方法将对应Id的城市天气数据更新到Redis缓存中
//同步天气数据
public class WeatherDataSyncJob extends QuartzJobBean{
private final static Logger logger = LoggerFactory.getLogger(WeatherDataSyncJob.class);
@Autowired
private CityDataService cityDataService;
@Autowired
private WeatherDataService weatherDataService;
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
logger.info("Weather Data Sync Job. Start!");
//城市ID列表
ListcityList=null;
try {
//获取xml中的城市ID列表
cityList=cityDataService.listCity();
} catch (Exception e) {
// e.printStackTrace();
logger.error("Exception!", e);
}
//遍历所有城市ID获取天气
for(City city:cityList) {
String cityId=city.getCityId();
logger.info("Weather Data Sync Job, cityId:" + cityId);
//实现根据cityid定时同步天气数据到缓存中
weatherDataService.syncDataByCityId(cityId);
}
logger.info("Weather Data Sync Job. End!");
}
}
配置Quartz
TIME设置的是更新的频率,表示每隔TIME秒就执行任务一次。
@Configuration
public class QuartzConfiguration {
private static final int TIME = 1800; // 更新频率
// JobDetail
@Bean
public JobDetail weatherDataSyncJobDetail() {
return JobBuilder.newJob(WeatherDataSyncJob.class).withIdentity("weatherDataSyncJob")
.storeDurably().build();
}
// Trigger
@Bean
public Trigger weatherDataSyncTrigger() {
SimpleScheduleBuilder schedBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(TIME).repeatForever();
return TriggerBuilder.newTrigger().forJob(weatherDataSyncJobDetail())
.withIdentity("weatherDataSyncTrigger").withSchedule(schedBuilder).build();
}
}
测试结果
天气数据同步结果