摘要:本章主要講解天氣數(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)行獲取,若在緩存中獲取不到對(duì)應(yīng)城市的數(shù)據(jù),則直接拋出錯(cuò)誤。
照例附上項(xiàng)目github鏈接
本項(xiàng)目實(shí)現(xiàn)的是將一個(gè)簡(jiǎn)單的天氣預(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ù)。
本章主要講解天氣數(shù)據(jù)API微服務(wù)的實(shí)現(xiàn)。
對(duì)原來單塊架構(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-data-redis org.springframework.boot spring-boot-maven-plugin true
在service中保留如下接口:
(1)根據(jù)城市Id查詢天氣的接口getDataByCityId
(2)根據(jù)城市名稱查詢天氣的接口getDataByCityName
注意:原來我們的天氣數(shù)據(jù)是先從緩存中獲取的,若查詢不到則調(diào)用第三方接口獲取天氣信息,并將其保存到緩存中。
在我們拆分成微服務(wù)架構(gòu)之后調(diào)用第三方接口的行為由天氣數(shù)據(jù)采集微服務(wù)中的定時(shí)任務(wù)進(jìn)行。
因此在天氣數(shù)據(jù)API微服務(wù)中我們的天氣數(shù)據(jù)直接從緩存中進(jìn)行獲取,若在緩存中獲取不到對(duì)應(yīng)城市的數(shù)據(jù),則直接拋出錯(cuò)誤。
@Service public class WeatherDataServiceImpl implements WeatherDataService { private final static Logger logger = LoggerFactory.getLogger(WeatherDataServiceImpl.class); private static final String WEATHER_URI = "http://wthrcdn.etouch.cn/weather_mini?"; @Autowired private StringRedisTemplate stringRedisTemplate; @Override public WeatherResponse getDataByCityId(String cityId) { String uri = WEATHER_URI + "citykey=" + cityId; return this.doGetWeahter(uri); } @Override public WeatherResponse getDataByCityName(String cityName) { String uri = WEATHER_URI + "city=" + cityName; return this.doGetWeahter(uri); } private WeatherResponse doGetWeahter(String uri) { String key = uri; String strBody = null; ObjectMapper mapper = new ObjectMapper(); WeatherResponse resp = null; ValueOperationsops = stringRedisTemplate.opsForValue(); // 先查緩存,緩存有的取緩存中的數(shù)據(jù) if (stringRedisTemplate.hasKey(key)) { logger.info("Redis has data"); strBody = ops.get(key); } else { logger.info("Redis don"t has data"); // 緩存沒有,拋出異常 throw new RuntimeException("Don"t has data!"); } try { resp = mapper.readValue(strBody, WeatherResponse.class); } catch (IOException e) { //e.printStackTrace(); logger.error("Error!",e); } 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); } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/73603.html
摘要:本章主要講解天氣預(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è)簡(jiǎn)單的天氣預(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ù),...
摘要:照例附上項(xiàng)目鏈接本項(xiàng)目實(shí)現(xiàn)的是將一個(gè)簡(jiǎn)單的天氣預(yù)報(bào)系統(tǒng)一步一步改造成一個(gè)微服務(wù)系統(tǒng)的過程,第一節(jié)將介紹普通天氣預(yù)報(bào)系統(tǒng)的簡(jiǎn)單實(shí)現(xiàn)。創(chuàng)建在其中提供如下接口根據(jù)城市獲取城市天氣數(shù)據(jù)的接口。配置創(chuàng)建的配置類。 照例附上項(xiàng)目github鏈接 本項(xiàng)目實(shí)現(xiàn)的是將一個(gè)簡(jiǎn)單的天氣預(yù)報(bào)系統(tǒng)一步一步改造成一個(gè)SpringCloud微服務(wù)系統(tǒng)的過程,第一節(jié)將介紹普通天氣預(yù)報(bào)系統(tǒng)的簡(jiǎn)單實(shí)現(xiàn)。 數(shù)據(jù)來源: 數(shù)...
摘要:接下來繼續(xù)介紹三種架構(gòu)模式,分別是查詢分離模式微服務(wù)模式多級(jí)緩存模式。分布式應(yīng)用程序可以基于實(shí)現(xiàn)諸如數(shù)據(jù)發(fā)布訂閱負(fù)載均衡命名服務(wù)分布式協(xié)調(diào)通知集群管理選舉分布式鎖和分布式隊(duì)列等功能。 SpringCloud 分布式配置 SpringCloud 分布式配置 史上最簡(jiǎn)單的 SpringCloud 教程 | 第九篇: 服務(wù)鏈路追蹤 (Spring Cloud Sleuth) 史上最簡(jiǎn)單的 S...
摘要:它就是史上最簡(jiǎn)單的教程第三篇服務(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)載請(qǐng)注明出處本文提綱一、自動(dòng)配置二、自定義屬性三、ran...
閱讀 2552·2021-10-13 09:40
閱讀 3448·2019-08-30 13:46
閱讀 1189·2019-08-29 14:05
閱讀 3017·2019-08-29 12:48
閱讀 3725·2019-08-26 13:28
閱讀 2216·2019-08-26 11:34
閱讀 2356·2019-08-23 18:11
閱讀 1254·2019-08-23 12:26