SpringCloud微服務(wù)架構(gòu)實(shí)戰(zhàn):天氣預(yù)報(bào)微服務(wù)的實(shí)現(xiàn)
天氣預(yù)報(bào)微服務(wù)的實(shí)現(xiàn)
天氣預(yù)報(bào)微服務(wù)包含了數(shù)據(jù)展示組件。數(shù)據(jù)展示組件用于將數(shù)據(jù)模型展示為用戶(hù)能夠理解的UI界面。
在micro-weather-report應(yīng)用的基礎(chǔ)上,我們將對(duì)其進(jìn)行逐步的拆分,形成一個(gè)新的微服務(wù)msa-weather-report-server應(yīng)用。
所需環(huán)境
為了演示本例子,需要采用如下開(kāi)發(fā)環(huán)境。
.JDK8。
. Gradle 4.0。
Spring Boot Web Starter 2.0.0.M4。
Spring Boot Thymeleaf Starter 2.0.0.M4。
-Thymeleaf 3.0.7.RELEASE。
. Bootstrap 4.0.0-beta.2。
修改天氣預(yù)報(bào)服務(wù)接口及實(shí)現(xiàn)
在
com.waylau.spring.cloud.weather.service包下,我們之前已經(jīng)定義了該應(yīng)用的天氣預(yù)報(bào)服務(wù)接口WeatherReportService,其方法定義保持不變。
public interface WeatherReportService {
/**
·根據(jù)城市工D查詢(xún)天氣信息
*
*@param cityId
*@return
*/
weather getDataByCityId(String cityId);
}對(duì)于該微服務(wù)而言,我們并不需要同步天氣的業(yè)務(wù)需求,所以把之前定義的syncDataByCityld方法刪除了。
WeatherReportServicelmpl是對(duì)WeatherReportService接口的實(shí)現(xiàn),由于之前是依賴(lài)于Weath-erDataService來(lái)查詢(xún)數(shù)據(jù)的,改為微服務(wù)架構(gòu)之后,這里數(shù)據(jù)的來(lái)源將由天氣數(shù)據(jù)API微服務(wù)來(lái)提供,所以這塊代碼也要做出相應(yīng)的調(diào)整。
@Service
public class WeatherReportServiceImpl implements WeatherReportService {
aoverride
public Weather getDataByCityId(string cityId){
//TODO改為由天氣數(shù)據(jù)API微服務(wù)來(lái)提供數(shù)據(jù)
weather data =new Weather(;
data.setAqi("81");
data.setCity("深圳");
data.setGanmao("各項(xiàng)氣象條件適宜,無(wú)明顯降溫過(guò)程,發(fā)生感冒機(jī)率較低。");
data.setWendu("22");
List forecastList = new ArrayList<>();
Forecast forecast =new Forecast(;
forecast.setDate("29日星期天");
forecast.setType("多云");
forecast.setFengxiang("無(wú)持續(xù)風(fēng)向");
forecast.setHigh("高溫27℃");
forecast.setLow("低溫20℃");
forecastList.add(forecast);
forecast =new Forecast();
forecast.setDate("29日星期天");
forecast.setType("多云");
forecast.setFengxiang("無(wú)持續(xù)風(fēng)向");
forecast.setHigh("高溫27℃");
forecast.setLow("低溫20℃");
forecastList.add(forecast);
forecast =new Forecast();
forecast.setDate( "30日星期一");
forecast.setType("多云");
forecast.setFengxiang("無(wú)持續(xù)風(fēng)向");
forecast.setHigh("高溫27℃");
forecast.setLow("低溫20℃");
forecastList.add(forecast);
forecast =new Forecast(;
forecast.setDate("31日星期二");
forecast.setType("多云");
forecast.setEengxiang("無(wú)持續(xù)風(fēng)向");
forecast.setHigh("高溫27℃");
forecast.setLow("低溫20℃");
forecastList.add(forecast);
forecast =new Forecast(;
forecast.setDate("1日星期三");
forecast.setType("多云");
forecast.setFengxiang("無(wú)持續(xù)風(fēng)向");
forecast.setHigh("高溫27℃");
forecast.setLow("低溫20℃");
forecastList.add(forecast);
forecast =new Forecast(;
forecast.setDate("2日星期四");
forecast.setType("多云");
forecast.setFengxiang("無(wú)持續(xù)風(fēng)向");
forecast.setHigh("高溫27℃");
forecast.setLow("低溫20℃");
forecastList.add(forecast);
data. setForecast(forecastList);
return data;
}
} 由于目前暫時(shí)還不能從天氣數(shù)據(jù)API微服務(wù)獲取數(shù)據(jù),所以為了讓程序能夠正常運(yùn)行下去,我們?cè)诖a里面編寫(xiě)了一些仿造的數(shù)據(jù),以提供接口返回?cái)?shù)據(jù)。
除上述WeatherReportServicelmpl、WeatherReportService外,其他服務(wù)層的代碼都可以刪除了。
調(diào)整控制層的代碼
除了WeatherReportController外,其他控制層的代碼都不需要了。
WeatherReportController由于對(duì)城市ID列表有依賴(lài),所以這塊的代碼邏輯需要調(diào)整。
@RestController
@RequestMapping("/report")
public class WeatherReportController {
private final static Logger logger = LoggerFactory.getLogger(Weather
ReportController.class);
@Autowired
private WeatherReportService weatherReportService;
@GetMapping("/cityId/{cityld}")
public ModelAndView getReportByCityId(CPathVariable("cityId") String
cityId, Model model) throws Exception{
//TODO改為由城市數(shù)據(jù)API微服務(wù)來(lái)提供數(shù)據(jù)
ListcityList=null;
try
//TODO 調(diào)用城市數(shù)據(jù)API
cityList = new ArrayList<>();
City city=new City(;
city.setCityId("101280601");
city.setCityName("深圳");
cityList.add(city);
city -new City();
city.setcityld("101280301");
city.setCityName("惠州");
cityList.add(city);
}catch(Exception e){
logger.error("獲取城市信息異常!",e);
throw new RuntimeException("獲取城市信息異常!",e);
}
model.addAttribute("title","老衛(wèi)的天氣預(yù)報(bào)");
model. addAttribute("cityid", cityId);
model.addAttribute( "cityList", cityList);
model.addAttribute ("report", weatherReportService.getDataByCity
id(cityId));
return new ModelAndView ("weather/report", "reportModel",
model);
} 由于目前暫時(shí)還不能從城市數(shù)據(jù)API微服務(wù)獲取數(shù)據(jù),所以為了讓程序能夠正常運(yùn)行下去,我們?cè)诖a里面編寫(xiě)了一些仿造的數(shù)據(jù),以提供接口返回?cái)?shù)據(jù)。
刪除配置類(lèi)、天氣數(shù)據(jù)同步任務(wù)和工具類(lèi)
配置類(lèi)RestConfiguration、QuartzConfiguration及任務(wù)類(lèi)WeatherDataSyncJob、工具類(lèi)Xml-Builder的代碼都可以刪除了。
清理值對(duì)象
值對(duì)象我們需要保留解析天氣相關(guān)的類(lèi)及城市信息相關(guān)的類(lèi),值對(duì)象CityList是可以刪除的。
需要注意的是,由于天氣數(shù)據(jù)采集微服務(wù)并未涉及對(duì)XML數(shù)據(jù)的解析,所以之前在City上添加的相關(guān)的JABX注解,都是可以一并刪除的。
以下是新的City類(lèi)。
public class City {
private String cityId;
private string cityName;
private string citycode;
private String province;
//省略getter/setter方法
}清理測(cè)試用例和配置文件
已經(jīng)刪除的服務(wù)接口的相關(guān)測(cè)試用例自然也是要一并刪除的。修改build.gradle文件中的依賴(lài),其中Apache Http Client、Quartz、Reids的依賴(lài)也一并刪除。
資源目錄下的citylist.xml文件也不再需要,可以刪除了。
測(cè)試和運(yùn)行
運(yùn)行應(yīng)用,通過(guò)瀏覽器訪問(wèn)
http:/localhost:8080/report/cityld/101280601頁(yè)面,就能看到如下的頁(yè)面效果,如圖7-4所示。

當(dāng)然,我們的數(shù)據(jù)都是仿造的,在城市列表里面只能看到兩條數(shù)據(jù),而且即便選擇了其他城市,也只是返回相同的仿造的城市天氣信息。
本篇內(nèi)容給大家講解的是天氣預(yù)報(bào)微服務(wù)的實(shí)現(xiàn)
下篇文章給大家講解城市數(shù)據(jù)API微服務(wù)的實(shí)現(xiàn);
覺(jué)得文章不錯(cuò)的朋友可以轉(zhuǎn)發(fā)此文關(guān)注小編;
感謝大家的支持!!
本文就是愿天堂沒(méi)有BUG給大家分享的內(nèi)容,大家有收獲的話可以分享下,想學(xué)習(xí)更多的話可以到微信公眾號(hào)里找我,我等你哦。
