内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

SpringCloud中如何使用Hystrix熔斷器

本篇文章為大家展示了SpringCloud中如何使用Hystrix熔斷器,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)公司,專注為中小企業(yè)提供官網(wǎng)建設(shè)、營(yíng)銷型網(wǎng)站制作、成都響應(yīng)式網(wǎng)站建設(shè)公司、展示型成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)等服務(wù),幫助中小企業(yè)通過網(wǎng)站體現(xiàn)價(jià)值、有效益。幫助企業(yè)快速建站、解決網(wǎng)站建設(shè)與網(wǎng)站營(yíng)銷推廣問題。

一、簡(jiǎn)介

什么是Hystrix
  1. 單詞本身意思就是斷路器,發(fā)音 [h?st'r?ks] ,怎么讀聽這里 https://www.koolearn.com/dict/wd_73657.html

  2. Hystrix是NetFlix公司開源的項(xiàng)目,提供了熔斷器的功能,能夠阻止分布式系統(tǒng)中出現(xiàn)的聯(lián)動(dòng)故障。

  3. Hystrix通過隔離服務(wù)的訪問點(diǎn)阻止聯(lián)動(dòng)故障,并且提供故障解決方案,提高了分布式系統(tǒng)的彈性和容錯(cuò)。

Hystrix所解決的問題
  1. 防止服務(wù)故障導(dǎo)致資源耗盡。

    通過熔斷機(jī)制,可以防止單個(gè)服務(wù)故障導(dǎo)致影響整個(gè)Servlet容器的線程資源。

  2. 避免過長(zhǎng)等待。

    快速失敗機(jī)制,如果某個(gè)服務(wù)故障,則接下來該服務(wù)不會(huì)處理請(qǐng)求,快速的返回失敗。

  3. 增加服務(wù)彈性。

    • 提供了回退方案,請(qǐng)求發(fā)生故障時(shí),可以調(diào)用fallback處理。

    • 提供熔斷機(jī)制,防止故障傳播到其他服務(wù)。

    • 熔斷后不斷嘗試,特定條件下可以自動(dòng)修復(fù)服務(wù)。

  4. 提供監(jiān)控。

    提供熔斷器的監(jiān)控組件 Hystrix Dashboard,可以實(shí)時(shí)監(jiān)控?cái)嗦菲鞯臓顟B(tài)。

二、簡(jiǎn)單使用

無論是Ribbon使用還是Feign使用,都需要前面兩步

  1. pom.xml中增加依賴

<!-- hystrix熔斷器依賴 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
  1. 啟動(dòng)類增加注解表示開啟熔斷器功能

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
public class ServerApplication {

    private static final Logger logger = LoggerFactory.getLogger(ServerApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class,args);
        logger.info("hystrix server start up finished !");
    }

}
Ribbon搭配使用
@Autowired
private RestTemplate restTemplate;

/**
 * 每個(gè)需要熔斷的遠(yuǎn)程方法上,增加注解,定義回調(diào)
 * @HystrixCommand定義服務(wù)降級(jí),這里的fallbackMethod服務(wù)調(diào)用失敗后調(diào)用的方法。
 * @return
 */
@HystrixCommand(fallbackMethod = "hiError")
public String getHi(){
    return restTemplate.getForObject("http://hystrix-service/hi", String.class);
}

/**
 * fallbackMethod指向此方法,表示遠(yuǎn)程調(diào)用失敗后,快速的使用此方法替代
 */
public String hiError(){
    return "error";
}
Fiegn搭配使用
  1. 在遠(yuǎn)程服務(wù)的FeignClient的注解上增加回調(diào),定義回調(diào)類

@FeignClient(value = "common-service", 
             configuration = FeignConfiguration.class, 
             fallback = RemoteCommonServiceCallback.class)
public interface RemoteCommonServiceClient {
    /**
     * 調(diào)用遠(yuǎn)程方法
     * @return
     */
    @GetMapping(value = "/hello")
    String remoteCommonServiceHello();

}
  1. 實(shí)現(xiàn)FeignClient中定義的回調(diào)類

/**
 * <p>
 *   @Component注冊(cè)為一個(gè)組件,實(shí)現(xiàn)RemoteCommonServiceClient,重寫方法定義回調(diào)
 * <p>
 * @author Calvin
 * @date 2019/10/24
 * @since 1.0
 */
@Component
public class RemoteCommonServiceCallback implements RemoteCommonServiceClient {

    @Override
    public String remoteCommonServiceHello() {
        return "something error, this is callback";
    }
}
  1. application.yml中開啟Feign對(duì)Hystrix的支持

feign:
  hystrix:
    enabled: true

具體測(cè)試,可以通過停掉需要被調(diào)用的遠(yuǎn)程服務(wù)來測(cè)試,或者自己制造一些,服務(wù)在某段時(shí)間不可用,使線程睡眠等操作

三、監(jiān)控

Hystrix Dashboard
  1. 增加依賴

<!-- Springboot監(jiān)控 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Hystrix Dashboard -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
  1. 啟動(dòng)類增加注解開啟dashboard

/**
 * <p>
 *     啟動(dòng)類,集合了服務(wù)注冊(cè)、FeignClient聲明式調(diào)用、開啟熔斷器、開啟熔斷器監(jiān)控
 * </p>
 *
 * @author Calvin
 * @date 2019/10/24
 * @since 1.0
 */
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
public class HystrixServerApplication {

    private static final Logger logger = LoggerFactory.getLogger(HystrixServerApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(HystrixServerApplication.class,args);
        logger.info("hystrix server start up finished !");
    }

}
  1. 監(jiān)控頁面觀察

  • step1:瀏覽器輸入http://localhost:8080/hystrix SpringCloud中如何使用Hystrix熔斷器

  • step2:頁面填入 http://localhost:8080/hystrix.stream Dalay和Title自己填 SpringCloud中如何使用Hystrix熔斷器

  • step3: 沒有step3,以下是踩過的坑

    1. 地址欄輸入的和頁面填進(jìn)去的東西一定不是同一個(gè),輸錯(cuò)了只能看到一直在ping,就像這樣
      SpringCloud中如何使用Hystrix熔斷器

    2. 剛進(jìn)去的時(shí)候是loading狀態(tài)的時(shí)候表著急,就像下圖,你只需要調(diào)用一下遠(yuǎn)程服務(wù)
      SpringCloud中如何使用Hystrix熔斷器

    3. 指標(biāo)意義
      SpringCloud中如何使用Hystrix熔斷器
      注:此圖來源 https://blog.csdn.net/wm6752062/article/details/86136204 侵刪

Turbine聚合監(jiān)控
  1. 新建服務(wù)turbine-monitor

  2. pom.xml

<parent>
    <artifactId>hystrix-test</artifactId>
    <groupId>com.calvin.hystrix</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>turbine-monitor</artifactId>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-turbine</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
  1. TurbineMonitorApplication

/**
 * <p>
 *     啟動(dòng)類
 * </p>
 *
 * @author Calvin
 * @date 2019/10/24
 * @since
 */

@SpringBootApplication
@EnableTurbine
@EnableHystrix
@EnableHystrixDashboard
public class MonitorApplication {

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

}
  1. application.yml

spring:
  application:
    name: turbine-monitor
server:
  port: 8040
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8010/eureka/
turbine:
# 需要監(jiān)控的服務(wù)名
  app-config: common-service, hystrix-service
# 服務(wù)名的集群
  cluster-name-expression: new String("default")
  1. 啟動(dòng)服務(wù)

  • 瀏覽器:http://localhost:8040/hystrix
    SpringCloud中如何使用Hystrix熔斷器


  • 調(diào)用遠(yuǎn)程服務(wù)
    SpringCloud中如何使用Hystrix熔斷器

上述內(nèi)容就是SpringCloud中如何使用Hystrix熔斷器,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

文章題目:SpringCloud中如何使用Hystrix熔斷器
URL分享:http://www.rwnh.cn/article46/jdjehg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站全網(wǎng)營(yíng)銷推廣、面包屑導(dǎo)航、自適應(yīng)網(wǎng)站網(wǎng)站導(dǎo)航、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
广丰县| 莱州市| 含山县| 柳林县| 翼城县| 古交市| 陇西县| 江津市| 昌邑市| 上饶市| 哈尔滨市| 古交市| 吴堡县| 北海市| 句容市| 清水河县| 永吉县| 栾城县| 锡林郭勒盟| 垣曲县| 怀来县| 九江县| 于田县| 黄冈市| 怀化市| 靖州| 德令哈市| 嘉义县| 石台县| 凯里市| 建阳市| 苏州市| 徐州市| 嫩江县| 格尔木市| 德惠市| 林州市| 曲沃县| 亚东县| 平和县| 长丰县|