摘要:本章主要講解天氣預(yù)報(bào)微服務(wù)的實(shí)現(xiàn)。獲取城市列表改為由城市數(shù)據(jù)微服務(wù)來提供數(shù)據(jù)改為由城市數(shù)據(jù)微服務(wù)提供數(shù)據(jù)深圳豬豬的天氣預(yù)報(bào)
照例附上項(xiàng)目github鏈接
本項(xiàng)目實(shí)現(xiàn)的是將一個(gè)簡單的天氣預(yù)報(bào)系統(tǒng)一步一步改造成一個(gè)SpringCloud微服務(wù)系統(tǒng)的過程,本節(jié)主要講的是單塊架構(gòu)改造成微服務(wù)架構(gòu)的過程,最終將原來單塊架構(gòu)的天氣預(yù)報(bào)服務(wù)拆分為四個(gè)微服務(wù):城市數(shù)據(jù)API微服務(wù),天氣數(shù)據(jù)采集微服務(wù),天氣數(shù)據(jù)API微服務(wù),天氣預(yù)報(bào)微服務(wù)。
本章主要講解天氣預(yù)報(bào)微服務(wù)的實(shí)現(xiàn)。
對原來單塊架構(gòu)的天氣預(yù)報(bào)服務(wù)進(jìn)行改進(jìn),去除多余的依賴,最終的pom文件如下:
4.0.0 com.demo sifoudemo02 0.0.1-SNAPSHOT jar sifoudemo02 Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.5.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-devtools true org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.slf4j slf4j-jdk14 1.7.7 org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-maven-plugin true
在Service中提供根據(jù)城市Id獲取天氣數(shù)據(jù)的方法。這里的天氣數(shù)據(jù)后期將會(huì)由天氣數(shù)據(jù)API尾服務(wù)從緩存中獲取。
@Service public class WeatherReportServiceImpl implements WeatherReportService { @Override public Weather getDataByCityId(String cityId) { // TODO 改為由天氣數(shù)據(jù)API微服務(wù)來提供 Weather data = new Weather(); data.setAqi("81"); data.setCity("深圳"); data.setGanmao("容易感冒!多穿衣"); data.setWendu("22"); ListforecastList = new ArrayList<>(); Forecast forecast = new Forecast(); forecast.setDate("25日星期天"); forecast.setType("晴"); forecast.setFengxiang("無風(fēng)"); forecast.setHigh("高溫 11度"); forecast.setLow("低溫 1度"); forecastList.add(forecast); forecast = new Forecast(); forecast.setDate("26日星期天"); forecast.setType("晴"); forecast.setFengxiang("無風(fēng)"); forecast.setHigh("高溫 11度"); forecast.setLow("低溫 1度"); forecastList.add(forecast); forecast = new Forecast(); forecast.setDate("27日星期天"); forecast.setType("晴"); forecast.setFengxiang("無風(fēng)"); forecast.setHigh("高溫 11度"); forecast.setLow("低溫 1度"); forecastList.add(forecast); forecast = new Forecast(); forecast.setDate("28日星期天"); forecast.setType("晴"); forecast.setFengxiang("無風(fēng)"); forecast.setHigh("高溫 11度"); forecast.setLow("低溫 1度"); forecastList.add(forecast); forecast = new Forecast(); forecast.setDate("29日星期天"); forecast.setType("晴"); forecast.setFengxiang("無風(fēng)"); forecast.setHigh("高溫 11度"); forecast.setLow("低溫 1度"); forecastList.add(forecast); data.setForecast(forecastList); return data; } }
在Controller中提供根據(jù)城市Id獲取相關(guān)天氣預(yù)報(bào)數(shù)據(jù)并進(jìn)行前端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 改為由城市數(shù)據(jù)API微服務(wù)來提供數(shù)據(jù) ListcityList = null; try { // TODO 改為由城市數(shù)據(jù)API微服務(wù)提供數(shù)據(jù) 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", "豬豬的天氣預(yù)報(bào)"); model.addAttribute("cityId", cityId); model.addAttribute("cityList", cityList); model.addAttribute("report", weatherReportService.getDataByCityId(cityId)); return new ModelAndView("weather/report", "reportModel", model); } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/73600.html
摘要:本章主要講解天氣數(shù)據(jù)微服務(wù)的實(shí)現(xiàn)。在我們拆分成微服務(wù)架構(gòu)之后調(diào)用第三方接口的行為由天氣數(shù)據(jù)采集微服務(wù)中的定時(shí)任務(wù)進(jìn)行。因此在天氣數(shù)據(jù)微服務(wù)中我們的天氣數(shù)據(jù)直接從緩存中進(jìn)行獲取,若在緩存中獲取不到對應(yīng)城市的數(shù)據(jù),則直接拋出錯(cuò)誤。 照例附上項(xiàng)目github鏈接 本項(xiàng)目實(shí)現(xiàn)的是將一個(gè)簡單的天氣預(yù)報(bào)系統(tǒng)一步一步改造成一個(gè)SpringCloud微服務(wù)系統(tǒng)的過程,本節(jié)主要講的是單塊架構(gòu)改造成微服務(wù)...
摘要:照例附上項(xiàng)目鏈接本項(xiàng)目實(shí)現(xiàn)的是將一個(gè)簡單的天氣預(yù)報(bào)系統(tǒng)一步一步改造成一個(gè)微服務(wù)系統(tǒng)的過程,第一節(jié)將介紹普通天氣預(yù)報(bào)系統(tǒng)的簡單實(shí)現(xiàn)。創(chuàng)建在其中提供如下接口根據(jù)城市獲取城市天氣數(shù)據(jù)的接口。配置創(chuàng)建的配置類。 照例附上項(xiàng)目github鏈接 本項(xiàng)目實(shí)現(xiàn)的是將一個(gè)簡單的天氣預(yù)報(bào)系統(tǒng)一步一步改造成一個(gè)SpringCloud微服務(wù)系統(tǒng)的過程,第一節(jié)將介紹普通天氣預(yù)報(bào)系統(tǒng)的簡單實(shí)現(xiàn)。 數(shù)據(jù)來源: 數(shù)...
摘要:接下來繼續(xù)介紹三種架構(gòu)模式,分別是查詢分離模式微服務(wù)模式多級緩存模式。分布式應(yīng)用程序可以基于實(shí)現(xiàn)諸如數(shù)據(jù)發(fā)布訂閱負(fù)載均衡命名服務(wù)分布式協(xié)調(diào)通知集群管理選舉分布式鎖和分布式隊(duì)列等功能。 SpringCloud 分布式配置 SpringCloud 分布式配置 史上最簡單的 SpringCloud 教程 | 第九篇: 服務(wù)鏈路追蹤 (Spring Cloud Sleuth) 史上最簡單的 S...
摘要:它就是史上最簡單的教程第三篇服務(wù)消費(fèi)者后端掘金上一篇文章,講述了通過去消費(fèi)服務(wù),這篇文章主要講述通過去消費(fèi)服務(wù)。概覽和架構(gòu)設(shè)計(jì)掘金技術(shù)征文后端掘金是基于的一整套實(shí)現(xiàn)微服務(wù)的框架。 Spring Boot 配置文件 – 在坑中實(shí)踐 - 后端 - 掘金作者:泥瓦匠鏈接:Spring Boot 配置文件 – 在坑中實(shí)踐版權(quán)歸作者所有,轉(zhuǎn)載請注明出處本文提綱一、自動(dòng)配置二、自定義屬性三、ran...
摘要:創(chuàng)建服務(wù)注冊中心創(chuàng)建一個(gè)基礎(chǔ)的工程,命名為,并在中引入需要的依賴內(nèi)容通過注解啟動(dòng)一個(gè)服務(wù)注冊中心提供給其他應(yīng)用進(jìn)行對話。 1.Spring Cloud簡介 Spring Cloud是一個(gè)基于Spring Boot實(shí)現(xiàn)的云應(yīng)用開發(fā)工具,它為基于JVM的云應(yīng)用開發(fā)中涉及的配置管理、服務(wù)發(fā)現(xiàn)、斷路器、智能路由、微代理、控制總線、全局鎖、決策競選、分布式會(huì)話和集群狀態(tài)管理等操作提供了一種簡單的開發(fā)方...
閱讀 1181·2021-09-10 10:51
閱讀 913·2019-08-30 15:53
閱讀 2739·2019-08-30 12:50
閱讀 989·2019-08-30 11:07
閱讀 2002·2019-08-30 10:50
閱讀 3611·2019-08-29 18:47
閱讀 1324·2019-08-29 18:44
閱讀 1612·2019-08-29 17:01