照例附上项目github链接
本项目实现的是将一个简单的天气预报系统一步一步改造成一个SpringCloud微服务系统的过程,本节主要讲的是单块架构改造成微服务架构的过程,最终将原来单块架构的天气预报服务拆分为四个微服务:城市数据API微服务,天气数据采集微服务,天气数据API微服务,天气预报微服务。
本章主要讲解天气数据采集微服务的实现。
各微服务的主要功能
天气数据采集微服务的实现
配置pom文件
对原来单块架构的天气预报服务进行改进,去除多余的依赖,最终的pom文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>sifoudemo02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sifoudemo02</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
<!-- <groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions> -->
</plugin>
</plugins>
</build>
</project>
提供接口
在Service中保留根据城市的Id同步天气数据的接口。实现方式为通过城市Id调用第三方接口获取对应天气数据,并将其同步到Redis缓存中。
@Service
public class WeatherDataCollectionServiceImpl implements WeatherDataCollectionService{
private static final String WEATHER_URL = "http://wthrcdn.etouch.cn/weather_mini?";
//设置缓存无效的时间
private static final long TIME_OUT = 1800L; // 1800s
//httpClient的客户端
@Autowired
private RestTemplate restTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
//根据城市的Id同步天气数据
@Override
public void syncDataByCityId(String cityId) {
String url = WEATHER_URL + "citykey=" + cityId;
this.saveWeatherData(url);
}
//把天气数据放在缓存中
private void saveWeatherData(String url) {
//将rul作为天气数据的key进行保存
String key=url;
String strBody=null;
ValueOperations<String, String>ops=stringRedisTemplate.opsForValue();
//调用服务接口来获取数据
ResponseEntity<String>respString=restTemplate.getForEntity(url, String.class);
//判断请求状态
if(respString.getStatusCodeValue()==200) {
strBody=respString.getBody();
}
ops.set(key, strBody,TIME_OUT,TimeUnit.SECONDS);
}
}
定时任务
保留根据城市列表同步全部天气数据的定时任务,这里的城市列表后期会通过调用城市数据API微服务得到。
//同步天气数据
public class WeatherDataSyncJob extends QuartzJobBean{
private final static Logger logger = LoggerFactory.getLogger(WeatherDataSyncJob.class);
@Autowired
private WeatherDataCollectionService weatherDataCollectionService;
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
logger.info("Weather Data Sync Job. Start!");
//城市ID列表
//TODO 改为由城市数据API微服务来提供城市列表的数据
List<City>cityList=null;
try {
//TODO 改为由城市数据API微服务来提供城市列表的数据
//获取xml中的城市ID列表
//cityList=cityDataService.listCity();
cityList=new ArrayList<>();
City city=new City();
city.setCityId("101280601");
cityList.add(city);
} 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定时同步天气数据到缓存中
weatherDataCollectionService.syncDataByCityId(cityId);
}
logger.info("Weather Data Sync Job. End!");
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。