博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Cloud学习笔记8——天气预报系统微服务(2)天气数据采集微服务
阅读量:3942 次
发布时间:2019-05-24

本文共 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()获取缓存中的数据 ValueOperations
ops = 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包中的WeatherDataServiceWeatherDataServiceImpl两个类就可以删除了

修改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
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包中的CityDataServiceCityDataServiceImpl两个类就可以删除了

修改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包下的其他类(CityListForecastWeatherWeatherResponseYesterday)全都删除

com.study.spring.cloud.weather.util包连同其下的类XmlBuilder删除

com.study.spring.cloud.weather.service包下的WeatherReportServiceWeatherReportServiceImpl两个类删除

com.study.spring.cloud.weather.controller包下的WeatherControllerWeatherReportController两个类删除

前端目录下:

在这里插入图片描述
修改application.properties配置文件,将

#热部署静态文件spring.thymeleaf.cache=false

内容删除

此时src目录结构如下:

在这里插入图片描述

运行

注意一定要先运行Redis

运行应用:

在这里插入图片描述
运行结果如下:
在这里插入图片描述

转载地址:http://vgiwi.baihongyu.com/

你可能感兴趣的文章
利用本地centos镜像升级centOS
查看>>
FreeBSD常用操作
查看>>
VC及esxi升级的必要性和步骤
查看>>
hp DL338服务器修改ilo管理地址
查看>>
vmware convert P2V 错误二三事
查看>>
让kali2020中的zsh有补完功能
查看>>
python解开压缩文件6位纯数字密码
查看>>
5620系列密码清除
查看>>
vncsever-centos&debian
查看>>
华为snmp模板
查看>>
kvm&xen挂载镜像文件
查看>>
华为路由器配置NAT使内网用户通过外网IP地址方式访问内网服务器示例
查看>>
virt命令
查看>>
15个保障服务器安全的方法:
查看>>
在VMware Workstation 中部署VCSA6.5
查看>>
openstack&ceph
查看>>
ME60 双机热备 奇偶mac负载分担
查看>>
oracle11G安装en
查看>>
关于丢失或者损坏etc/fstab文件后
查看>>
VMware-ESXi-6.5 集成第三方驱动方法
查看>>