摘要:照例附上項目鏈接本項目實現(xiàn)的是將一個簡單的天氣預報系統(tǒng)一步一步改造成一個微服務系統(tǒng)的過程,第一節(jié)將介紹普通天氣預報系統(tǒng)的簡單實現(xiàn)。創(chuàng)建在其中提供如下接口根據(jù)城市獲取城市天氣數(shù)據(jù)的接口。配置創(chuàng)建的配置類。
照例附上項目github鏈接
本項目實現(xiàn)的是將一個簡單的天氣預報系統(tǒng)一步一步改造成一個SpringCloud微服務系統(tǒng)的過程,第一節(jié)將介紹普通天氣預報系統(tǒng)的簡單實現(xiàn)。
數(shù)據(jù)來源:數(shù)據(jù)來源1:http://wthrcdn.etouch.cn/weather_mini?city=深圳
數(shù)據(jù)來源2:http://wthrcdn.etouch.cn/weather_mini?citykey=101280601
數(shù)據(jù)來源3:http://mobile.weather.com.cn/js/citylist.xml
數(shù)據(jù)格式根據(jù)返回的數(shù)據(jù)格式在vo包下面創(chuàng)建pojo。
創(chuàng)建WeatherDataService在其中提供如下接口:
1)根據(jù)城市Id獲取城市天氣數(shù)據(jù)的接口。
@Override public WeatherResponse getDataByCityId(String cityId) { String url=WEATHER_URI+ "citykey=" + cityId; return this.doGetWeather(url); }
2)根據(jù)城市名稱獲取天氣數(shù)據(jù)的接口。
@Override public WeatherResponse getDataByCityName(String cityName) { String url = WEATHER_URI + "city=" + cityName; return this.doGetWeather(url); }
其中doGetWeather方法為抽離出來的請求天氣數(shù)據(jù)的方法。
private WeatherResponse doGetWeahter(String uri) { ResponseEntityControllerrespString = restTemplate.getForEntity(uri, String.class); ObjectMapper mapper = new ObjectMapper(); WeatherResponse resp = null; String strBody = null; if (respString.getStatusCodeValue() == 200) { strBody = respString.getBody(); } try { resp = mapper.readValue(strBody, WeatherResponse.class); } catch (IOException e) { e.printStackTrace(); } return resp; }
在controller中分別提供根據(jù)城市id與名稱獲取天氣數(shù)據(jù)的接口。
@RestController @RequestMapping("/weather") public class WeatherController { @Autowired private WeatherDataService weatherDataService; @GetMapping("/cityId/{cityId}") public WeatherResponse getWeatherByCityId(@PathVariable("cityId") String cityId) { return weatherDataService.getDataByCityId(cityId); } @GetMapping("/cityName/{cityName}") public WeatherResponse getWeatherByCityName(@PathVariable("cityName") String cityName) { return weatherDataService.getDataByCityName(cityName); } }配置
創(chuàng)建Rest的配置類。
@Configuration public class RestConfiguration { @Autowired private RestTemplateBuilder builder; @Bean public RestTemplate restTemplate() { return builder.build(); } }請求結(jié)果:
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/73409.html
摘要:本章主要講解天氣數(shù)據(jù)微服務的實現(xiàn)。在我們拆分成微服務架構(gòu)之后調(diào)用第三方接口的行為由天氣數(shù)據(jù)采集微服務中的定時任務進行。因此在天氣數(shù)據(jù)微服務中我們的天氣數(shù)據(jù)直接從緩存中進行獲取,若在緩存中獲取不到對應城市的數(shù)據(jù),則直接拋出錯誤。 照例附上項目github鏈接 本項目實現(xiàn)的是將一個簡單的天氣預報系統(tǒng)一步一步改造成一個SpringCloud微服務系統(tǒng)的過程,本節(jié)主要講的是單塊架構(gòu)改造成微服務...
摘要:本章主要講解天氣預報微服務的實現(xiàn)。獲取城市列表改為由城市數(shù)據(jù)微服務來提供數(shù)據(jù)改為由城市數(shù)據(jù)微服務提供數(shù)據(jù)深圳豬豬的天氣預報 照例附上項目github鏈接 本項目實現(xiàn)的是將一個簡單的天氣預報系統(tǒng)一步一步改造成一個SpringCloud微服務系統(tǒng)的過程,本節(jié)主要講的是單塊架構(gòu)改造成微服務架構(gòu)的過程,最終將原來單塊架構(gòu)的天氣預報服務拆分為四個微服務:城市數(shù)據(jù)API微服務,天氣數(shù)據(jù)采集微服務,...
摘要:接下來繼續(xù)介紹三種架構(gòu)模式,分別是查詢分離模式微服務模式多級緩存模式。分布式應用程序可以基于實現(xiàn)諸如數(shù)據(jù)發(fā)布訂閱負載均衡命名服務分布式協(xié)調(diào)通知集群管理選舉分布式鎖和分布式隊列等功能。 SpringCloud 分布式配置 SpringCloud 分布式配置 史上最簡單的 SpringCloud 教程 | 第九篇: 服務鏈路追蹤 (Spring Cloud Sleuth) 史上最簡單的 S...
摘要:創(chuàng)建服務注冊中心創(chuàng)建一個基礎的工程,命名為,并在中引入需要的依賴內(nèi)容通過注解啟動一個服務注冊中心提供給其他應用進行對話。 1.Spring Cloud簡介 Spring Cloud是一個基于Spring Boot實現(xiàn)的云應用開發(fā)工具,它為基于JVM的云應用開發(fā)中涉及的配置管理、服務發(fā)現(xiàn)、斷路器、智能路由、微代理、控制總線、全局鎖、決策競選、分布式會話和集群狀態(tài)管理等操作提供了一種簡單的開發(fā)方...
摘要:它就是史上最簡單的教程第三篇服務消費者后端掘金上一篇文章,講述了通過去消費服務,這篇文章主要講述通過去消費服務。概覽和架構(gòu)設計掘金技術(shù)征文后端掘金是基于的一整套實現(xiàn)微服務的框架。 Spring Boot 配置文件 – 在坑中實踐 - 后端 - 掘金作者:泥瓦匠鏈接:Spring Boot 配置文件 – 在坑中實踐版權(quán)歸作者所有,轉(zhuǎn)載請注明出處本文提綱一、自動配置二、自定義屬性三、ran...
閱讀 2735·2021-10-11 10:59
閱讀 2774·2021-09-22 15:49
閱讀 2718·2021-08-13 13:25
閱讀 1360·2019-08-30 13:14
閱讀 2449·2019-08-29 18:45
閱讀 3089·2019-08-29 18:36
閱讀 1549·2019-08-29 13:21
閱讀 1233·2019-08-26 11:44