资讯专栏INFORMATION COLUMN

天气预报微服务 | 从0开始构建SpringCloud微服务(8)

hyuan / 1917人阅读

摘要:本章主要讲解天气预报微服务的实现。获取城市列表改为由城市数据微服务来提供数据改为由城市数据微服务提供数据深圳猪猪的天气预报

照例附上项目github链接

本项目实现的是将一个简单的天气预报系统一步一步改造成一个SpringCloud微服务系统的过程,本节主要讲的是单块架构改造成微服务架构的过程,最终将原来单块架构的天气预报服务拆分为四个微服务:城市数据API微服务,天气数据采集微服务,天气数据API微服务,天气预报微服务。

本章主要讲解天气预报微服务的实现。


天气预报微服务的实现 配置pom文件

对原来单块架构的天气预报服务进行改进,去除多余的依赖,最终的pom文件如下:

</>复制代码

  1. 4.0.0
  2. com.demo
  3. sifoudemo02
  4. 0.0.1-SNAPSHOT
  5. jar
  6. sifoudemo02
  7. Demo project for Spring Boot
  8. org.springframework.boot
  9. spring-boot-starter-parent
  10. 2.0.5.RELEASE
  11. UTF-8
  12. UTF-8
  13. 1.8
  14. org.springframework.boot
  15. spring-boot-devtools
  16. true
  17. org.springframework.boot
  18. spring-boot-starter-web
  19. org.springframework.boot
  20. spring-boot-starter-test
  21. test
  22. org.slf4j
  23. slf4j-jdk14
  24. 1.7.7
  25. org.springframework.boot
  26. spring-boot-starter-thymeleaf
  27. org.springframework.boot
  28. spring-boot-maven-plugin
  29. true



提供接口

在Service中提供根据城市Id获取天气数据的方法。这里的天气数据后期将会由天气数据API尾服务从缓存中获取。

</>复制代码

  1. @Service
  2. public class WeatherReportServiceImpl implements WeatherReportService {
  3. @Override
  4. public Weather getDataByCityId(String cityId) {
  5. // TODO 改为由天气数据API微服务来提供
  6. Weather data = new Weather();
  7. data.setAqi("81");
  8. data.setCity("深圳");
  9. data.setGanmao("容易感冒!多穿衣");
  10. data.setWendu("22");
  11. List forecastList = new ArrayList<>();
  12. Forecast forecast = new Forecast();
  13. forecast.setDate("25日星期天");
  14. forecast.setType("晴");
  15. forecast.setFengxiang("无风");
  16. forecast.setHigh("高温 11度");
  17. forecast.setLow("低温 1度");
  18. forecastList.add(forecast);
  19. forecast = new Forecast();
  20. forecast.setDate("26日星期天");
  21. forecast.setType("晴");
  22. forecast.setFengxiang("无风");
  23. forecast.setHigh("高温 11度");
  24. forecast.setLow("低温 1度");
  25. forecastList.add(forecast);
  26. forecast = new Forecast();
  27. forecast.setDate("27日星期天");
  28. forecast.setType("晴");
  29. forecast.setFengxiang("无风");
  30. forecast.setHigh("高温 11度");
  31. forecast.setLow("低温 1度");
  32. forecastList.add(forecast);
  33. forecast = new Forecast();
  34. forecast.setDate("28日星期天");
  35. forecast.setType("晴");
  36. forecast.setFengxiang("无风");
  37. forecast.setHigh("高温 11度");
  38. forecast.setLow("低温 1度");
  39. forecastList.add(forecast);
  40. forecast = new Forecast();
  41. forecast.setDate("29日星期天");
  42. forecast.setType("晴");
  43. forecast.setFengxiang("无风");
  44. forecast.setHigh("高温 11度");
  45. forecast.setLow("低温 1度");
  46. forecastList.add(forecast);
  47. data.setForecast(forecastList);
  48. return data;
  49. }
  50. }

在Controller中提供根据城市Id获取相关天气预报数据并进行前端UI界面展示的接口。

</>复制代码

  1. @RestController
  2. @RequestMapping("/report")
  3. public class WeatherReportController {
  4. private final static Logger logger = LoggerFactory.getLogger(WeatherReportController.class);
  5. @Autowired
  6. private WeatherReportService weatherReportService;
  7. @GetMapping("/cityId/{cityId}")
  8. public ModelAndView getReportByCityId(@PathVariable("cityId") String cityId, Model model) throws Exception {
  9. // 获取城市ID列表
  10. // TODO 改为由城市数据API微服务来提供数据
  11. List cityList = null;
  12. try {
  13. // TODO 改为由城市数据API微服务提供数据
  14. cityList = new ArrayList<>();
  15. City city = new City();
  16. city.setCityId("101280601");
  17. city.setCityName("深圳");
  18. cityList.add(city);
  19. } catch (Exception e) {
  20. logger.error("Exception!", e);
  21. }
  22. model.addAttribute("title", "猪猪的天气预报");
  23. model.addAttribute("cityId", cityId);
  24. model.addAttribute("cityList", cityList);
  25. model.addAttribute("report", weatherReportService.getDataByCityId(cityId));
  26. return new ModelAndView("weather/report", "reportModel", model);
  27. }
  28. }



文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/73600.html

相关文章

  • 天气数据API服务 | 0开始构建SpringCloud服务(7)

    摘要:本章主要讲解天气数据微服务的实现。在我们拆分成微服务架构之后调用第三方接口的行为由天气数据采集微服务中的定时任务进行。因此在天气数据微服务中我们的天气数据直接从缓存中进行获取,若在缓存中获取不到对应城市的数据,则直接抛出错误。 照例附上项目github链接 本项目实现的是将一个简单的天气预报系统一步一步改造成一个SpringCloud微服务系统的过程,本节主要讲的是单块架构改造成微服务...

    zorro 评论0 收藏0
  • 0开始构建SpringCloud服务(1)

    摘要:照例附上项目链接本项目实现的是将一个简单的天气预报系统一步一步改造成一个微服务系统的过程,第一节将介绍普通天气预报系统的简单实现。创建在其中提供如下接口根据城市获取城市天气数据的接口。配置创建的配置类。 照例附上项目github链接 本项目实现的是将一个简单的天气预报系统一步一步改造成一个SpringCloud微服务系统的过程,第一节将介绍普通天气预报系统的简单实现。 数据来源: 数...

    Joonas 评论0 收藏0
  • 架构~服务

    摘要:接下来继续介绍三种架构模式,分别是查询分离模式微服务模式多级缓存模式。分布式应用程序可以基于实现诸如数据发布订阅负载均衡命名服务分布式协调通知集群管理选举分布式锁和分布式队列等功能。 SpringCloud 分布式配置 SpringCloud 分布式配置 史上最简单的 SpringCloud 教程 | 第九篇: 服务链路追踪 (Spring Cloud Sleuth) 史上最简单的 S...

    xinhaip 评论0 收藏0
  • 架构~服务 - 收藏集 - 掘金

    摘要:它就是史上最简单的教程第三篇服务消费者后端掘金上一篇文章,讲述了通过去消费服务,这篇文章主要讲述通过去消费服务。概览和架构设计掘金技术征文后端掘金是基于的一整套实现微服务的框架。 Spring Boot 配置文件 – 在坑中实践 - 后端 - 掘金作者:泥瓦匠链接:Spring Boot 配置文件 – 在坑中实践版权归作者所有,转载请注明出处本文提纲一、自动配置二、自定义属性三、ran...

    church 评论0 收藏0
  • SpringCloud构建服务架构:服务注册与发现

    摘要:创建服务注册中心创建一个基础的工程,命名为,并在中引入需要的依赖内容通过注解启动一个服务注册中心提供给其他应用进行对话。 1.Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种简单的开发方...

    lakeside 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<