博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Cloud学习笔记20——天气预报系统微服务实现 API 网关
阅读量:3941 次
发布时间:2019-05-24

本文共 4626 字,大约阅读时间需要 15 分钟。

API网关可以用来统一处理用户的所有请求,会根据不同请求路径将请求路由到不同的微服务中去

集成Zuul

在这里插入图片描述

创建项目

以之前的micro-weather-eureka-client-zuul为蓝本,创建msa-weather-eureka-client-zuul项目

在这里插入图片描述

修改源码

修改application.properties配置文件:

#应用名称spring.application.name=msa-weather-eureka-client-zuul#注册服务器的URLeureka.client.service-url.defaultZone=http://localhost:8761/eureka/#zuul本身的配置#设置需要路由的URL,此处设置hi资源的路径zuul.routes.city.path=/city/**#访问city应用时将请求转发到某应用的应用名称zuul.routes.city.service-id=msa-weather-city-eureka#zuul本身的配置#设置需要路由的URL,此处设置weather资源的路径zuul.routes.data.path=/data/**#访问city应用时将请求转发到某应用的应用名称zuul.routes.data.service-id=msa-weather-data-eureka

天气预报微服务重构

创建项目

以之前的msa-weather-report-eureka-feign为蓝本,创建msa-weather-report-eureka-feign-gateway项目

在这里插入图片描述
com.study.spring.cloud.weather.service包下新建DataClient接口:

package com.study.spring.cloud.weather.service;import com.study.spring.cloud.weather.vo.City;import com.study.spring.cloud.weather.vo.WeatherResponse;import org.springframework.cloud.netflix.feign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import java.util.List;@FeignClient("msa-weather-eureka-client-zuul")public interface DataClient {
//获取城市列表 @GetMapping("/city/cities") List
listCity() throws Exception; //根据城市id查询天气数据 @GetMapping("/data/weather/cityId/{cityId}") WeatherResponse getDataByCityId(@PathVariable("cityId") String cityId);}

删除com.study.spring.cloud.weather.service包下的CityClientWeatherDataClient两个接口

修改com.study.spring.cloud.weather.controller包下的WeatherReportController类:

package com.study.spring.cloud.weather.controller;import com.study.spring.cloud.weather.service.DataClient;import com.study.spring.cloud.weather.service.WeatherReportService;import com.study.spring.cloud.weather.vo.City;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.servlet.ModelAndView;import java.util.List;@RestController@RequestMapping("/report")public class WeatherReportController {
//在应用中添加日志 private final static Logger logger=LoggerFactory.getLogger(WeatherReportController.class); @Autowired private WeatherReportService weatherReportService; @Autowired private DataClient dataClient; @GetMapping("/cityId/{cityId}") //@PathVariable:标识从路径中获取参数 public ModelAndView getReportByCityId(@PathVariable("cityId") String cityId,Model model) throws Exception {
//获取城市列表 List
cityList=null; try {
//由城市数据API微服务来提供数据 cityList = dataClient.listCity(); } 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); } }

修改com.study.spring.cloud.weather.service包下的WeatherReportServiceImpl类:

package com.study.spring.cloud.weather.service;import com.study.spring.cloud.weather.vo.Weather;import com.study.spring.cloud.weather.vo.WeatherResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class WeatherReportServiceImpl implements WeatherReportService {
@Autowired private DataClient dataClient; @Override public Weather getDataByCityId(String cityId) {
//由天气数据API微服务来提供数据 WeatherResponse resp=dataClient.getDataByCityId(cityId); Weather data=resp.getData(); return data; }}

修改application.properties配置文件:

#热部署静态文件spring.thymeleaf.cache=false#应用名称spring.application.name=msa-weather-report-eureka-feign-gateway#注册服务器的URLeureka.client.service-url.defaultZone=http://localhost:8761/eureka/#请求服务时的超时时间feign.client.config.feignName.connect-timeout=5000#读数据时的超时时间feign.client.config.feignName.read-timeout=5000

运行

先通过IDE运行micro-weather-eureka-server

运行Redis

通过命令行在80818082端口运行msa-weather-collection-eureka-feign

通过命令行在80838084端口运行msa-weather-data-eureka

通过命令行在80858086端口运行msa-weather-city-eureka

通过命令行在80878088端口运行msa-weather-report-eureka-feign-gateway

通过命令行在8089端口运行msa-weather-eureka-client-zuul

访问http://localhost:8761页面,可以看到Eureka的管理页面:

在这里插入图片描述
访问http://localhost:8088/report/cityId/101020100页面:
在这里插入图片描述
在页面中切换选中城市:
在这里插入图片描述
如果访问页面时报错“Doesn't hava data”,可能是由于启动msa-weather-collection-eureka-feign时还未启动msa-weather-city-eureka,无法获取城市数据,可以将8081端口的运行先停掉,再重启,就可以了

转载地址:http://wwswi.baihongyu.com/

你可能感兴趣的文章
html5 常用
查看>>
node-webkit:开发桌面+WEB混合型应用的神器
查看>>
Hybird APP 开发 总结
查看>>
创业公司进行股权激励要注意的四大问题
查看>>
Ext各组件属性配置(上) -- 中文注释
查看>>
document.forms用法
查看>>
[手机知道] 用IE7调试 JS报没有权限
查看>>
JS 定义数组
查看>>
PHP解决多线程同时读写一个文件的…
查看>>
PHP一段上传文件的代码
查看>>
猴子排队算法
查看>>
猴子排队算法
查看>>
查询系统负载信息 Linux 命令详解
查看>>
增强 SSH 安全性的 7 条技巧
查看>>
this作用域、javascript面向…
查看>>
提高网页在IE和Firefox上的…
查看>>
提高网页在IE和Firefox上的…
查看>>
php的正则表达式 '/\b\w…
查看>>
ThinkPHP的标签制作及标签调用解析…
查看>>
jQuery.proxy()代理、回调方法
查看>>