springcloud教程第3篇:Hystrix----熔斷器

獲取更多內(nèi)容

獲取更多內(nèi)容請訪問: https://juntech.top/

為了保證服務的高可用性,一般將單個服務進行集群部署,但由于自身原因或網(wǎng)絡原因,服務不能保證100%能用,如果單個服務出現(xiàn)問題,會造成線程阻塞,極易導致服務崩潰,造成雪崩效應。

為了解決這個問題,提出了熔斷器概念----Hystrix!

一、斷路器簡介

Netflix開源了Hystrix組件,實現(xiàn)了斷路器模式,SpringCloud對這一組件進行了整合。 在微服務架構中,一個請求需要調用多個服務是非常常見的,如下圖:

HystrixGraph.png

較底層的服務如果出現(xiàn)故障,會導致連鎖故障。當對特定的服務的調用的不可用達到一個閥值(Hystric 是5秒20次) 斷路器將會被打開。

HystrixFallback.png

斷路打開后,可用避免連鎖故障,fallback方法可以直接返回一個固定值。

二、準備工作

這篇文章基于服務消費者文章的工程,首先啟動工程,啟動eureka-server 工程;啟動service-hi工程,它的端口為8762。

三、在ribbon使用斷路器

改造serice-ribbon 工程的代碼,首先在pox.xml文件中加入spring-cloud-starter-hystrix的起步依賴:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

在程序的啟動類ServiceRibbonApplication 加@EnableHystrix注解開啟Hystrix:

@SpringBootApplication
@EnableDiscoveryClient
//開啟熔斷規(guī)則
@EnableHystrix
public class ServiceRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceRibbonApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

改造HelloService類,在hiService方法上加上@HystrixCommand注解。

@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;

@HystrixCommand(fallbackMethod = "hiError")
public String hiService(String name) {
    return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
}

public String hiError(String name) {
    return "hi,"+name+",sorry,error!";
}
}

如果發(fā)生熔斷就會返回hiError方法,即顯示:"hi,"+name+",sorry,error!";

如果正常則繼續(xù)使用hiService(String name)方法,顯示:restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);獲得的json數(shù)據(jù)

啟動:service-ribbon 工程,當我們訪問http://localhost:8764/hi?name=jd,瀏覽器顯示:

hi,jd,i am from port:8762

此時關閉 service-hi 工程,當我們再訪問http://localhost:8764/hi?name=jd,瀏覽器會顯示:

hi ,jd,orry,error!(這是由于網(wǎng)絡原因,服務已關閉,網(wǎng)絡不可達,)

這就說明當 service-hi 工程不可用的時候,service-ribbon調用 service-hi的API接口時,會執(zhí)行快速失敗,直接返回一組字符串,而不是等待響應超時,這很好的控制了容器的線程阻塞。

接下來,在Feign中使用熔斷器

四、Feign中使用斷路器

Feign是自帶斷路器的,在D版本的Spring Cloud中,它沒有默認打開。需要在配置文件中配置打開它,在配置文件加以下代碼:

feign.hystrix.enabled=true

基于service-feign工程進行改造,只需要在FeignClient的SchedualServiceHi接口的注解中加上fallback的指定類就行了,代碼如下:

@FeignClient(value = "service-hi",fallback = SchedualServiceHiHystric.class)
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

參數(shù)解析:

? value = "service-hi":在注冊中心的要使用的服務名

? fallback = SchedualServiceHiHystric.class:只要發(fā)生熔斷,就返回該類里面對應的方法

SchedualServiceHiHystric需要實現(xiàn)SchedualServiceHi 接口,并注入到Ioc容器中,代碼如下:

@Component
//需要注冊為一個組件
public class SchedualServiceHiHystric implements SchedualServiceHi {
    @Override
    public String sayHiFromClientOne(String name) {
        return "sorry "+name;
    }
}

啟動service-feign工程,瀏覽器打開http://localhost:8765/hi?name=jd,注意此時service-hi工程沒有啟動,網(wǎng)頁顯示:

sorry jd

打開service-hi工程,再次訪問,瀏覽器顯示:

hi jd,i am from port:8762

這證明斷路器起到作用了。

五、Hystrix Dashboard (斷路器:Hystrix 儀表盤)

基于service-ribbon 改造,F(xiàn)eign的改造和這一樣。

首選在pom.xml引入spring-cloud-starter-hystrix-dashboard的起步依賴:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
    </dependency>

在主程序啟動類中加入@EnableHystrixDashboard注解,開啟hystrixDashboard:

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class ServiceRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceRibbonApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

打開瀏覽器:訪問http://localhost:8764/hystrix,界面如下

Paste_Image.png

點擊monitor stream,進入下一個界面,訪問:http://localhost:8764/hi?name=jd

此時會出現(xiàn)監(jiān)控界面:

Paste_Image.png

六、參考資料

更多詳情

設置為vip可見的都可訪問下面鏈接地址,即可觀看原文
更多詳情請訪問: juntech

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容