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

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

创建项目

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

在这里插入图片描述
修改build.gradle配置,添加Hystrix依赖:

//依赖关系dependencies {
//该依赖用于编译阶段 compile('org.springframework.boot:spring-boot-starter-web') //添加Spring Boot Thymeleaf Starter的依赖 compile('org.springframework.boot:spring-boot-starter-thymeleaf') //Eureka Client compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client') //Feign compile('org.springframework.cloud:spring-cloud-starter-openfeign:2.0.0.M3') //Hystrix compile('org.springframework.cloud:spring-cloud-starter-netflix-hystrix') //该依赖用于测试阶段 testCompile('org.springframework.boot:spring-boot-starter-test')}

修改com.study.spring.cloud.weather包下的Application类,加入@EnableCircuitBreaker注解:

package com.study.spring.cloud.weather;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.feign.EnableFeignClients;/* * @SpringBootApplication注解声明Spring Boot应用 * 作用等同于@Configuration, @EnableAutoConfiguration, @ComponentScan, * 简化Spring配置*/@SpringBootApplication//启用可发现的客户端@EnableDiscoveryClient//启用Feign@EnableFeignClients//启用Hystrix@EnableCircuitBreaker//Application类一定要处于整个工程的根目录下,这样它才能根据配置去扫描子节点下的Spring的Beanpublic class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args); }}

com.study.spring.cloud.weather.service包下新建类DataClientFallback

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.stereotype.Component;import java.util.ArrayList;import java.util.List;//声明为Bean@Componentpublic class DataClientFallback implements DataClient {
@Override public List
listCity() throws Exception {
List
cityList=null; cityList=new ArrayList<>(); City city=new City(); city.setCityId("101020100"); city.setCityName("上海"); cityList.add(city); city=new City(); city.setCityId("101010100"); city.setCityName("北京"); cityList.add(city); return cityList; } @Override public WeatherResponse getDataByCityId(String cityId) {
return null; }}

修改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=null; if(resp!=null){
data=resp.getData(); } return data; }}

修改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(name = "msa-weather-eureka-client-zuul",fallback = DataClientFallback.class)public interface DataClient {
//获取城市列表 @GetMapping("/city/cities") List
listCity() throws Exception; //根据城市id查询天气数据 @GetMapping("/data/weather/cityId/{cityId}") WeatherResponse getDataByCityId(@PathVariable("cityId") String cityId);}

修改前端页面report.html

    
天气预报

天气

城市名称

空气质量指数:

当前温度:

温馨提示:

日期

天气类型

最高温度

最低温度

风向

天气数据API微服务暂不可用!

修改application.properties配置文件:

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

运行

  1. 启动Redis
  2. IDE上运行micro-weather-eureka-server
  3. 通过命令行指定80818082端口运行msa-weather-collection-eureka-feign
  4. 通过命令行指定80838084端口运行msa-weather-data-eureka
  5. 通过命令行指定80858086端口运行msa-weather-city-eureka
  6. 通过命令行指定80878088端口运行msa-weather-report-eureka-feign-gateway-hystrix
  7. 通过命令行指定8089端口运行msa-weather-eureka-client-zuul

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

在这里插入图片描述
访问http://localhost:8088/report/cityId/101020100页面:
在这里插入图片描述
在页面中切换选中城市:
在这里插入图片描述

  1. 停掉80858086两个端口的msa-weather-city-eureka微服务

刷新http://localhost:8088/report/cityId/101020100页面:

在这里插入图片描述

  1. 停掉80838084两个端口的msa-weather-data-eureka微服务

刷新http://localhost:8088/report/cityId/101020100页面:

在这里插入图片描述

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

你可能感兴趣的文章
J2ME游戏源代码免费下载——国外Digiment公司商业化代码
查看>>
手机银行技术应用探讨
查看>>
角色扮演游戏引擎的设计原理
查看>>
j2me开发FAQ整理
查看>>
J2ME程序开发新手入门九大要点
查看>>
双向搜索算法
查看>>
日本GAME製作方式
查看>>
移动行业术语资料
查看>>
3G到来将全面颠覆SP、CP游戏规则
查看>>
射击游戏中跟踪弹及小角度移动的开发
查看>>
完美的软件项目开发团队结构
查看>>
数学的重要性
查看>>
how Google routed around Sun’s IP-based licensing restrictions on Java ME
查看>>
JAVA面试题最全集
查看>>
JAVA面试题集
查看>>
Embedded System Interview Questions:
查看>>
The Standalone Programmer:Tips from the trenches
查看>>
优化C代码常用的几招
查看>>
Embedded firmware interview questions
查看>>
一道微软亚洲工程院C语言笔试题的解答
查看>>