Spring Cloud学习笔记8——天气预报系统微服务(2)天气数据采集微服务

开发环境

  • 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;

@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包中的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<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包中的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

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值