Weather forecast microservice: build spring cloud microservice from 0

As usual Project github link This project realizes the process of transforming a simple weather forecast system into a spring cloud microservice syste...

As usual Project github link

This project realizes the process of transforming a simple weather forecast system into a spring cloud microservice system step by step. This section mainly talks about the process of transforming a single block architecture into a microservice architecture. Finally, the weather forecast service of the original single block architecture is divided into four microservices: City data API microservice, weather data collection microservice, weather data API microservice , weather forecast microservice.

This chapter focuses on the realization of weather forecast microservice.


The realization of weather forecast microservice

Configure pom file

Improve the weather forecast service of the original single block architecture to remove redundant dependency. The final pom file is as follows:

<?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>



Provide interface

Provide a method to obtain weather data according to the city Id in the Service. The weather data here will be obtained from the cache later by the weather data API tail Service.

@Service public class WeatherReportServiceImpl implements WeatherReportService { @Override public Weather getDataByCityId(String cityId) { // TODO is provided by weather data API microservice instead Weather data = new Weather(); data.setAqi("81"); data.setCity("Shenzhen"); data.setGanmao("Easy to catch cold! Dressing more"); data.setWendu("22"); List<Forecast> forecastList = new ArrayList<>(); Forecast forecast = new Forecast(); forecast.setDate("25 Sunday, Sunday"); forecast.setType("Fine"); forecast.setFengxiang("No wind"); forecast.setHigh("High temperature 11 degrees"); forecast.setLow("Low temperature 1 degrees"); forecastList.add(forecast); forecast = new Forecast(); forecast.setDate("26 Sunday, Sunday"); forecast.setType("Fine"); forecast.setFengxiang("No wind"); forecast.setHigh("High temperature 11 degrees"); forecast.setLow("Low temperature 1 degrees"); forecastList.add(forecast); forecast = new Forecast(); forecast.setDate("27 Sunday, Sunday"); forecast.setType("Fine"); forecast.setFengxiang("No wind"); forecast.setHigh("High temperature 11 degrees"); forecast.setLow("Low temperature 1 degrees"); forecastList.add(forecast); forecast = new Forecast(); forecast.setDate("28 Sunday, Sunday"); forecast.setType("Fine"); forecast.setFengxiang("No wind"); forecast.setHigh("High temperature 11 degrees"); forecast.setLow("Low temperature 1 degrees"); forecastList.add(forecast); forecast = new Forecast(); forecast.setDate("29 Sunday, Sunday"); forecast.setType("Fine"); forecast.setFengxiang("No wind"); forecast.setHigh("High temperature 11 degrees"); forecast.setLow("Low temperature 1 degrees"); forecastList.add(forecast); data.setForecast(forecastList); return data; } }

The Controller provides an interface to obtain relevant weather forecast data according to the city Id and display the front-end UI.

@RestController @RequestMapping("/report") public class WeatherReportController { private final static Logger logger = LoggerFactory.getLogger(WeatherReportController.class); @Autowired private WeatherReportService weatherReportService; @GetMapping("/cityId/") public ModelAndView getReportByCityId(@PathVariable("cityId") String cityId, Model model) throws Exception { // Get City ID list // TODO is changed to city data API microservice to provide data List<City> cityList = null; try { // TODO to provide data for city data API microservice cityList = new ArrayList<>(); City city = new City(); city.setCityId("101280601"); city.setCityName("Shenzhen"); cityList.add(city); } catch (Exception e) { logger.error("Exception!", e); } model.addAttribute("title", "Pig weather forecast"); model.addAttribute("cityId", cityId); model.addAttribute("cityList", cityList); model.addAttribute("report", weatherReportService.getDataByCityId(cityId)); return new ModelAndView("weather/report", "reportModel", model); } }



3 December 2019, 19:35 | Views: 1481

Add new comment

For adding a comment, please log in
or create account

0 comments