/**
* @Title: WeatherDataServiceImpl.java
* @Package com.liumm.micro.weather.service.impl
* @Description: TODO(用一句话描述该文件做什么)
* @author liumm
* @date 2018年3月11日 下午9:08:56
* @version V1.0
*/
package com.liumm.micro.weather.service.impl;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.liumm.micro.weather.service.WeatherDataService;
import com.liumm.micro.weather.vo.WeatherResponse;
/**
* @ClassName: WeatherDataServiceImpl
* @Description: 业务实现类
* @author liumm
* @date 2018年3月11日 下午9:08:56
*
*/
@Service
public class WeatherDataServiceImpl implements WeatherDataService {
private final static Logger LOGGER = LoggerFactory.getLogger(WeatherDataServiceImpl.class);
private static final String WEATHER_URI = "http://wthrcdn.etouch.cn/weather_mini?";
@Autowired
private RestTemplate restTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* @Title: getWeatherDataByCityName
* @Description:
* @param cityName
* @return
*/
@Override
public WeatherResponse getWeatherDataByCityName(String cityName) {
// 请求url地址
String url = WEATHER_URI + "city=" + cityName;
WeatherResponse entity = getWeatherDataByUrl(url);
// 消息实体返回
return entity;
}
/**
* @Title: getWeatherDataByCityId
* @Description:
* @param cityId
* @return
*/
@Override
public WeatherResponse getWeatherDataByCityId(String cityId) {
// 请求url地址
String url = WEATHER_URI + "citykey=" + cityId;
WeatherResponse entity = getWeatherDataByUrl(url);
// 消息实体返回
return entity;
}
/**
* @Title: syncWeatherDataById
* @Description:
* @param id
*/
@Override
public void syncWeatherDataById(String id) {
// 返回实体消息
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
String strBody = null;
// 请求url地址
String url = WEATHER_URI + "citykey=" + id;
// 缓存不存在,请求数据
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
strBody = responseEntity.getBody();
if (responseEntity.getStatusCodeValue() == 200) {
// 缓存不存在的数据,请求成功之后放在缓存中,让后设置超时时间为30分钟
ops.set(url, strBody, 30, TimeUnit.MINUTES);
}
}
/**
* @Title: syncWeatherDataByName
* @Description:
* @param name
*/
@Override
public void syncWeatherDataByName(String name) {
// 返回实体消息
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
String strBody = null;
// 请求url地址
String url = WEATHER_URI + "city=" + name;
// 缓存不存在,请求数据
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
strBody = responseEntity.getBody();
if (responseEntity.getStatusCodeValue() == 200) {
// 缓存不存在的数据,请求成功之后放在缓存中,让后设置超时时间为30分钟
ops.set(url, strBody, 30, TimeUnit.MINUTES);
}
}
/**
* @Description: (这里用一句话描述这个方法的作用)
* @param url 请求的URL地址
* @return:
*/
private WeatherResponse getWeatherDataByUrl(String url) {
// 返回实体消息
WeatherResponse entity = null;
String strBody = null;
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
// 先判断缓存中是否存在
if (stringRedisTemplate.hasKey(url)) {
strBody = ops.get(url);
} else {
// 缓存不存在,请求数据
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
strBody = responseEntity.getBody();
if (responseEntity.getStatusCodeValue() == 200) {
// 缓存不存在的数据,请求成功之后放在缓存中,让后设置超时时间为30分钟
ops.set(url, strBody, 30, TimeUnit.MINUTES);
}
}
// 转换为对应的实体消息
ObjectMapper mapper = new ObjectMapper();
// 具体的值进行判断
try {
entity = mapper.readValue(strBody, WeatherResponse.class);
} catch (IOException e) {
LOGGER.error("实体转换异常", e);
}
return entity;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
java+微服务实战项目代码.zip (474个子文件)
WeatherDataServiceImpl.class 4KB
WeatherDataServiceImpl.class 4KB
WeatherDataServiceImpl.class 4KB
WeatherDataServiceImpl.class 3KB
WeatherDataSyncJob.class 2KB
WeatherDataSyncJob.class 2KB
HelloControllerTest.class 2KB
HelloControllerTest.class 2KB
HelloControllerTest.class 2KB
HelloControllerTest.class 2KB
WeatherData.class 2KB
WeatherData.class 2KB
WeatherData.class 2KB
WeatherData.class 2KB
WeatherReportController.class 2KB
CityDataServiceImpl.class 2KB
CityDataServiceImpl.class 2KB
ForecastWeather.class 2KB
ForecastWeather.class 2KB
ForecastWeather.class 2KB
ForecastWeather.class 2KB
YesterdayWeather.class 2KB
YesterdayWeather.class 2KB
YesterdayWeather.class 2KB
YesterdayWeather.class 2KB
City.class 1KB
City.class 1KB
XmlBuilder.class 1KB
XmlBuilder.class 1KB
WeatherReportServiceImpl.class 1KB
WeatherDataController.class 1KB
WeatherDataController.class 1KB
WeatherDataController.class 1KB
WeatherDataController.class 1KB
WeatherResponse.class 1KB
WeatherResponse.class 1KB
WeatherResponse.class 1KB
WeatherResponse.class 1KB
RedisTest.class 1KB
RedisTest.class 1KB
RedisTest.class 1KB
CityList.class 1KB
CityList.class 1KB
WeatherScheduler.class 899B
WeatherScheduler.class 899B
MicroWeatherBasicApplication.class 882B
MicroWeatherBasicApplication.class 882B
MicroWeatherBasicApplication.class 882B
RestConfiguration.class 862B
RestConfiguration.class 862B
RestConfiguration.class 862B
RestConfiguration.class 862B
MicroWeatherBasicApplication.class 756B
WeatherDataService.class 370B
WeatherDataService.class 370B
CityDataService.class 310B
CityDataService.class 310B
WeatherDataService.class 284B
WeatherDataService.class 284B
WeatherReportService.class 249B
.classpath 1KB
.classpath 1KB
.classpath 1KB
.classpath 1KB
.classpath 1KB
.classpath 1KB
.classpath 1KB
.classpath 1KB
.classpath 1KB
.classpath 1KB
.classpath 1KB
.classpath 1KB
.classpath 1KB
.classpath 1KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
共 474 条
- 1
- 2
- 3
- 4
- 5
资源评论
- 隔壁老猿2024-02-06资源内容详细全面,与描述一致,对我很有用,有一定的使用价值。
GeekyGuru
- 粉丝: 1683
- 资源: 1099
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功