SpringCloud微服務(wù)架構(gòu)開發(fā)實(shí)戰(zhàn):實(shí)現(xiàn)微服務(wù)熔斷機(jī)制
實(shí)現(xiàn)微服務(wù)的熔斷機(jī)制
我們?cè)谏瞎?jié)已經(jīng)基本了解了如何將Hystrix 集成進(jìn)應(yīng)用。我們也通過(guò)一個(gè)簡(jiǎn)單的例子,知道了如何通過(guò)Hystrix技術(shù)實(shí)現(xiàn)自己的斷路器。總的來(lái)說(shuō),使用Hystrix是非常簡(jiǎn)單的。
本節(jié)我們將基于Hystrix技術(shù)來(lái)改造天氣預(yù)報(bào)系統(tǒng),使我們的服務(wù)在調(diào)用核心數(shù)據(jù)服務(wù)時(shí),能夠啟用熔斷機(jī)制,從而保護(hù)應(yīng)用。
我們將要修改之前的天氣預(yù)報(bào)微服務(wù)msa-weather- report- eureka feign-gateway,由于該服務(wù)分別依賴了天氣數(shù)據(jù)API微服務(wù)msa-weather-data -eureka及城市數(shù)據(jù)API微服務(wù)msa-weather-city-eure-ka,所以,在調(diào)用這兩個(gè)服務(wù)過(guò)程中,假如調(diào)用失敗,就啟用斷路器。
新的天氣預(yù)報(bào)微服務(wù)命名為
msa-weather-eport-eureka-feign-gateway-hytrix。

更改配置
要使用Hystrix,最簡(jiǎn)單的方式莫過(guò)于添加Hystrix依賴。
dependencies {
//添加Spring Cloud Starter Netflix Hystrix依賴
compile ('org. springframework. cloud: spring-cloud- starter-netflix-
hystrix')
}使用Hystrix
要啟用Hystrix,最簡(jiǎn)單的方式就是在應(yīng)用的根目錄的Application類上添加
org.springframe-work.cloud.client.circuitbreaker. EnableCircuitBreaker注解。
package com. waylau. spring. cloud. weather;
import org. springframework.boot . SpringAppl ication;
import org. springf r amework . boot . autoconfigure . Spr ingBootApplication;
import org. spr ing f ramework. cloud. client. circuitbreaker . Enable
CircuitBreaker;
import org. springframework.cloud.client. discovery . EnableDiscovery
Client;
import org. springframework.cloud. netflix. feign . EnableFeignClients;
/**
主應(yīng)用程序.
@since 1.0.0 2017年11月12日
* @author Way Lau
*/
@Spr ingBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
public class Application {
public static void main(String[] args) {
SpringAppl ication. run (Application.class, args) ;
}
}實(shí)現(xiàn)斷路器
Feign內(nèi)建支持了對(duì)于Hystrix回調(diào)函數(shù)的支持。我們只需要在@FeignClient注解的fllback 中聲明要回調(diào)的類。
package com.waylau. spring.cloud. weather .service;
import java.util.List;
import org . springf ramework. cloud. netflix. feign. FeignClient;
import org. springframework. web .bind. annotation. GetMapping;
import org. springf ramework. web .bind. annotation. PathVariable;
import com.waylau. spring.cloud.weather.vo.city;
import com. waylau. spring.cloud.weather .vo.WeatherResponse;
/**
★訪問(wèn)數(shù)據(jù)的客戶端.
* @since 1.0.0 2017年11月6日
Cauthor Way Lau
*/
@FeignClient (name= "msa-weather-eureka-client-zuul", fallback=DataClient
Fallback.class)
public interface DataClient {
/**
獲取城市列表
* @return
athrows Exception
*/
@GetMapping("/city/cities")
List listCity() throws Exception;
*根據(jù)城市ID查詢天氣數(shù)據(jù)
@param cityId .
★@return
@GetMapping ("/data/weather/cityId/ {cityId}")
WeatherResponse getDataByCityId (@PathVariable("cityId") String
cityId) ;
} 在上述代碼中,回調(diào)指向了DataClientFallback 類,該類是一個(gè) Spring bean,實(shí)現(xiàn)了DataClient接口的方法。DataClientFallback 中的方法,即為斷路器需要返回的執(zhí)行方法。
package com. waylau. spring. cloud . weather . service;
import java.util.ArrayList;
import java.util.List;
import org.springframework. stereotype. Component;
import com. waylau.spring.cloud. weather. vo.City;
import com. waylau. spring.cloud . weather . vo. WeatherResponse;
/**
DataClient Fallback.
@since 1.0.0 2017年11月13日
@author Way Lau
@Component
public class DataClientFallback implements DataClient {
@Override
public List listCity() throws Exception {
List cityList = null;
cityList = new ArrayList<>() ;
City city = new City() ;
city .setCityId ("101280601");
city. setCityName ("深圳") ;
cityList.add(city) ;
city = new City();
city. setCityId("101280301") ;
city. setCityName ("惠州");
cityList.add(city) ;
return cityList;
}
@Override
public WeatherResponse getDataByCityId(String cityId) {
return new WeatherResponse () ;
}
}其中:
●listCity方法:在調(diào)用城市數(shù)據(jù)API微服務(wù)時(shí)需要實(shí)現(xiàn)斷路器。在城市數(shù)據(jù)API微服務(wù)失敗時(shí),,我們就響應(yīng)默認(rèn)的城市列表給客戶端;
●getDataByCityld方法:在調(diào)用天氣數(shù)據(jù)API微服務(wù)時(shí)需要實(shí)現(xiàn)斷路器。在調(diào)用天氣數(shù)據(jù)API微服務(wù)失敗時(shí),我們就響應(yīng)默認(rèn)的null給客戶端。
修改report.html頁(yè)面
...
<div
th:if="$ {reportModel. report} != nul1">
<div class="row">
<h1 class="text-success" th:text="$ {reportModel. report.city}">
h1>
div>
<div class=" row">
<p>
空氣質(zhì)量指數(shù): <span th:text="$ {reportModel. report.aqi}">
span>
p>
div>
<div class="row">
<p>
當(dāng)前溫度: <span th: text="$ {reportModel . report . wendu}">span>
p>
div>
<div class="row">
<p>
溫馨提示: <span th: text="$ { reportModel . report.ganmao}">
span>
p>
div>
<div class="row">
<div class="card border-info" th:each=" forecast : $ { reportModel .
report. forecast}">
<div class="card-body text- info">
p class="card-text" th:text="${ forecast.date}">周五p>
p class="card-text" th: text="$ { forecast. type}">晴轉(zhuǎn)多云
p>
<p class="card-text" th:text="$ {forecast.high}">高溫
28Cp>
P class="card-text" th:text="${ forecast.low}">低溫
21Cp>
Kp class="card-text" th:text="$ {forecast. fengxiang}">無(wú)
持續(xù)風(fēng)向微風(fēng)p>
div>
div>
div>
div>
<div th:if="${ reportModel . report} == null">
<div class="row">
天氣數(shù)據(jù)API服務(wù)暫時(shí)不可用!
p>
div>
div>
...在該頁(yè)面中,我們會(huì)用Thymeleaf條件運(yùn)算符和比較表達(dá)式來(lái)對(duì)模型中report (天氣數(shù)據(jù))進(jìn)行判斷,如果不為空,就將天氣信息顯示出來(lái);否則,就顯示“天氣數(shù)據(jù)API服務(wù)暫時(shí)不可用!”字樣。
修改應(yīng)用配置
應(yīng)用配置修改如下。
#熱部署靜態(tài)文件
spring. thymeleaf . cache=false
spring. application. name: msa-weather- report-eureka-feign-gateway-
hystrix
eureka. cl ient. serviceUrl . defaultZone: http:/ /localhost:8761/eureka/
feign.client. config. fe ignName . connectTimeout: 5000
feign.client . config. feignName . readT imeout: 5000
feign.hystrix .enabled=true其中: feign.hystrix.enabled 用于啟用在Feign客戶端中使用Hystrix。那么所有的Feign客戶端異常,都會(huì)導(dǎo)致斷路器的啟用。
運(yùn)行、測(cè)試
先啟動(dòng)Redis服務(wù)器。
再依次啟動(dòng)以下服務(wù)。
java -jar micro-weather-eureka-server-1.0.0.jar --server .port=8761
java -jar msa-weather-collection-eureka-feign-1.0.0.jar --server.
port=8081
java -jar msa-weather -collection-eureka-feign-1.0.0.jar --server.
port=8082
java -jar msa-weather -data-eureka-1.0.0.jar --server .port=8083
java -jar msa-weather-data-eureka-1.0.0.jar --server . port-8084
java -jar msa-weather-city-eureka-1.0.0.jar --server .port=8085
java -jar msa-weather-city-eureka-1.0.0.jar --server .port=8086
java -jar msa-weather-report-eureka- feign-gateway-hystrix-1.0.0.jar
--server .port=8087
java -jar msa-weather-report-eureka- feign-gateway-hystrix-1.0.0.jar
-- server . port=8088
java -jar msa-weather-eureka-client-zuul-1.0.0.jar --server .port=8089我們關(guān)閉天氣數(shù)據(jù)API微服務(wù),以模擬天氣數(shù)據(jù)API微服務(wù)故障的場(chǎng)景。在界面上,我們能看到如圖15-4所示的默認(rèn)信息。

我們關(guān)閉城市數(shù)據(jù)API微服務(wù),以模擬城市數(shù)據(jù)API微服務(wù)故障的場(chǎng)景。在界面上,我們能看到如圖15-5所示的城市列表。

本篇文章內(nèi)容給大家講解的是實(shí)現(xiàn)微服務(wù)的熔斷機(jī)制
下篇文章給大家講解的是分布式消息總線;
覺(jué)得文章不錯(cuò)的朋友可以轉(zhuǎn)發(fā)此文關(guān)注小編;
感謝大家的支持!
本文就是愿天堂沒(méi)有BUG給大家分享的內(nèi)容,大家有收獲的話可以分享下,想學(xué)習(xí)更多的話可以到微信公眾號(hào)里找我,我等你哦。
