照例附上項目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.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</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獲取天氣數據的方法。這裏的天氣數據後期將會由天氣數據API尾服務從緩存中獲取。
@Service
public class WeatherReportServiceImpl implements WeatherReportService {
@Override
public Weather getDataByCityId(String cityId) {
// TODO 改爲由天氣數據API微服務來提供
Weather data = new Weather();
data.setAqi("81");
data.setCity("深圳");
data.setGanmao("容易感冒!多穿衣");
data.setWendu("22");
List<Forecast> forecastList = new ArrayList<>();
Forecast forecast = new Forecast();
forecast.setDate("25日星期天");
forecast.setType("晴");
forecast.setFengxiang("無風");
forecast.setHigh("高溫 11度");
forecast.setLow("低溫 1度");
forecastList.add(forecast);
forecast = new Forecast();
forecast.setDate("26日星期天");
forecast.setType("晴");
forecast.setFengxiang("無風");
forecast.setHigh("高溫 11度");
forecast.setLow("低溫 1度");
forecastList.add(forecast);
forecast = new Forecast();
forecast.setDate("27日星期天");
forecast.setType("晴");
forecast.setFengxiang("無風");
forecast.setHigh("高溫 11度");
forecast.setLow("低溫 1度");
forecastList.add(forecast);
forecast = new Forecast();
forecast.setDate("28日星期天");
forecast.setType("晴");
forecast.setFengxiang("無風");
forecast.setHigh("高溫 11度");
forecast.setLow("低溫 1度");
forecastList.add(forecast);
forecast = new Forecast();
forecast.setDate("29日星期天");
forecast.setType("晴");
forecast.setFengxiang("無風");
forecast.setHigh("高溫 11度");
forecast.setLow("低溫 1度");
forecastList.add(forecast);
data.setForecast(forecastList);
return data;
}
}
在Controller中提供根據城市Id獲取相關天氣預報數據並進行前端UI界面展示的接口。
@RestController
@RequestMapping("/report")
public class WeatherReportController {
private final static Logger logger = LoggerFactory.getLogger(WeatherReportController.class);
@Autowired
private WeatherReportService weatherReportService;
@GetMapping("/cityId/{cityId}")
public ModelAndView getReportByCityId(@PathVariable("cityId") String cityId, Model model) throws Exception {
// 獲取城市ID列表
// TODO 改爲由城市數據API微服務來提供數據
List<City> cityList = null;
try {
// TODO 改爲由城市數據API微服務提供數據
cityList = new ArrayList<>();
City city = new City();
city.setCityId("101280601");
city.setCityName("深圳");
cityList.add(city);
} catch (Exception e) {
logger.error("Exception!", e);
}
model.addAttribute("title", "豬豬的天氣預報");
model.addAttribute("cityId", cityId);
model.addAttribute("cityList", cityList);
model.addAttribute("report", weatherReportService.getDataByCityId(cityId));
return new ModelAndView("weather/report", "reportModel", model);
}
}