本文共 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 ListlistCity() 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") ListlistCity() 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
Redis
IDE
上运行micro-weather-eureka-server
8081
和8082
端口运行msa-weather-collection-eureka-feign
8083
和8084
端口运行msa-weather-data-eureka
8085
和8086
端口运行msa-weather-city-eureka
8087
和8088
端口运行msa-weather-report-eureka-feign-gateway-hystrix
8089
端口运行msa-weather-eureka-client-zuul
访问http://localhost:8761
页面,可以看到Eureka
的管理页面:
http://localhost:8088/report/cityId/101020100
页面: 在页面中切换选中城市: 8085
和8086
两个端口的msa-weather-city-eureka
微服务刷新http://localhost:8088/report/cityId/101020100
页面:
8083
和8084
两个端口的msa-weather-data-eureka
微服务刷新http://localhost:8088/report/cityId/101020100
页面:
转载地址:http://ywswi.baihongyu.com/