照例附上项目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尾服务从缓存中获取。@Servicepublic 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); }}