资讯专栏INFORMATION COLUMN

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

hyuan / 1703人阅读

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

照例附上项目github链接

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

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


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

对原来单块架构的天气预报服务进行改进,去除多余的依赖,最终的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中提供根据城市Id获取天气数据的方法。这里的天气数据后期将会由天气数据API尾服务从缓存中获取。

@Service
public class WeatherReportServiceImpl implements WeatherReportService {
    
    @Override
    public Weather getDataByCityId(String cityId) {
        // TODO 改为由天气数据API微服务来提供
        Weather data = new Weather();
        data.setAqi("81");
        data.setCity("深圳");
        data.setGanmao("容易感冒!多穿衣");
        data.setWendu("22");
        
        List forecastList = new ArrayList<>();
        
        Forecast forecast = new Forecast();
        forecast.setDate("25日星期天");
        forecast.setType("晴");
        forecast.setFengxiang("无风");
        forecast.setHigh("高温 11度");
        forecast.setLow("低温 1度");
        forecastList.add(forecast);
        
        forecast = new Forecast();
        forecast.setDate("26日星期天");
        forecast.setType("晴");
        forecast.setFengxiang("无风");
        forecast.setHigh("高温 11度");
        forecast.setLow("低温 1度");
        forecastList.add(forecast);
        
        forecast = new Forecast();
        forecast.setDate("27日星期天");
        forecast.setType("晴");
        forecast.setFengxiang("无风");
        forecast.setHigh("高温 11度");
        forecast.setLow("低温 1度");
        forecastList.add(forecast);
        
        forecast = new Forecast();
        forecast.setDate("28日星期天");
        forecast.setType("晴");
        forecast.setFengxiang("无风");
        forecast.setHigh("高温 11度");
        forecast.setLow("低温 1度");
        forecastList.add(forecast);
        
        forecast = new Forecast();
        forecast.setDate("29日星期天");
        forecast.setType("晴");
        forecast.setFengxiang("无风");
        forecast.setHigh("高温 11度");
        forecast.setLow("低温 1度");
        forecastList.add(forecast);
        
        data.setForecast(forecastList);
        return data;
    }

}

在Controller中提供根据城市Id获取相关天气预报数据并进行前端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 改为由城市数据API微服务来提供数据
        List cityList = null;
        
        try {
            
            // TODO 改为由城市数据API微服务提供数据
            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", "猪猪的天气预报");
        model.addAttribute("cityId", cityId);
        model.addAttribute("cityList", cityList);
        model.addAttribute("report", weatherReportService.getDataByCityId(cityId));
        return new ModelAndView("weather/report", "reportModel", model);
    }

}



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

转载请注明本文地址: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元查看
<