最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringCloud Hystrix熔斷器使用方法介紹

 更新時(shí)間:2023年03月20日 08:32:07   作者:小乞丐程序員  
通過hystrix可以解決雪崩效應(yīng)問題,它提供了資源隔離、降級(jí)機(jī)制、融斷、緩存等功能。接下來通過本文給大家分享SpringCloud集成Hystrix熔斷,感興趣的朋友一起看看吧

Hystrix(hi si ju ke si)概述

Hystix 是 Netflix 開源的一個(gè)延遲和容錯(cuò)庫,用于隔離訪問遠(yuǎn)程服務(wù)、第三方庫,防止出現(xiàn)級(jí)聯(lián)失敗(雪崩)。

雪崩:一個(gè)服務(wù)失敗,導(dǎo)致整條鏈路的服務(wù)都失敗的情形。

Hystix 主要功能

  • 隔離
  • 降級(jí)
  • 熔斷
  • 限流

隔離

  • 線程池隔離
  • 信號(hào)量隔離

Hystrix 降級(jí)

Hystix 降級(jí):當(dāng)服務(wù)發(fā)生異?;蛘{(diào)用超時(shí),返回默認(rèn)數(shù)據(jù)

Hystrix降級(jí)-服務(wù)提供方

  • 在服務(wù)提供方,引入 hystrix 依賴
  • 定義降級(jí)方法
  • 使用 @HystrixCommand 注解配置降級(jí)方法
  • 在啟動(dòng)類上開啟Hystrix功能:@EnableCircuitBreaker

初始化程序和Fiegn程序一致

需要修改的程序

package com.itheima.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
 * 啟動(dòng)類
 */
@EnableEurekaClient //該注解 在新版本中可以省略
@SpringBootApplication
@EnableCircuitBreaker ///開啟Hystrix功能
public class ProviderApp {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApp.class,args);
    }
}

測(cè)試的程序

package com.itheima.provider.controller;
import com.itheima.provider.domain.Goods;
import com.itheima.provider.service.GoodsService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
 * Goods Controller 服務(wù)提供方
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Value("${server.port}")
    private int port;
    /**
     * 降級(jí):
     *   1 出現(xiàn)異常
     *   2 調(diào)用服務(wù)超時(shí)
     *      默認(rèn)1s超時(shí)
     *
     *@HystrixCommand(fallbackMethod = "findOne_fallback")
     * fallbackMethod 指定降級(jí)后的名稱
     * @param id
     * @return
     */
    @GetMapping("/findOne/{id}")
    @HystrixCommand(fallbackMethod = "findOne_fallback",commandProperties = {
            //設(shè)置Hystrix的超時(shí)時(shí)間 默認(rèn)1s
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })///指定降級(jí)后的方法
    public Goods findOne(@PathVariable("id") int id)  {
        ///1 造個(gè)異常
        //int i =3/0;
        try {
            Thread.sleep(2000);//sleep interrupted
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Goods goods = goodsService.findOne(id);
        goods.setTitle(goods.getTitle() + ":" + port);//將端口號(hào),設(shè)置到了 商品標(biāo)題上
        return goods;
    }
    /**
     * 定義降級(jí)放啊
     * 1 方法的返回值需要和原方法一樣
     * 2 方法參數(shù)需要和原方法一樣
     */
    public Goods findOne_fallback(int id) {
        Goods goods = new Goods();
        goods.setTitle("降級(jí)了~~~");
        return goods;
    }
}

指定坐標(biāo)

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

Hystrix降級(jí)-服務(wù)消費(fèi)方

  • feign 組件已經(jīng)集成了 hystrix 組件。
  • 定義feign 調(diào)用接口實(shí)現(xiàn)類,復(fù)寫方法,即降級(jí)方法
  • 在 @FeignClient 注解中使用 fallback 屬性設(shè)置降級(jí)處理類。
  • 配置開啟 feign.hystrix.enabled = true

provider與Fiegin一致

application.yml修改

server:
  port: 9000

eureka:
  instance:
    hostname: localhost # 主機(jī)名
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
spring:
  application:
    name: hystrix-consumer # 設(shè)置當(dāng)前應(yīng)用的名稱。將來會(huì)在eureka中Application顯示。將來需要使用該名稱來獲取路徑
#開啟feign對(duì)hystrix支持
feign:
  hystrix:
    enabled: true

ConsumerApp修改

package com.itheima.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableDiscoveryClient // 激活DiscoveryClient
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients //開啟Feign的功能
public class ConsumerApp {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApp.class,args);
    }
}

修改GoodsFeignClient方法

package com.itheima.consumer.feign;
import com.itheima.consumer.domain.Goods;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(value = "HYSTRIX-PROVIDER",fallback = GoodsFeignClientFallback.class)
public interface GoodsFeignClient {
    @GetMapping("/goods/findOne/{id}")
    public Goods findGoodsById(@PathVariable("id") int id);
}

復(fù)寫方法

package com.itheima.consumer.feign;
import com.itheima.consumer.domain.Goods;
import org.springframework.stereotype.Component;
/**
 * Fegin 客戶端降級(jí)處理類
 * 1 定義類 實(shí)現(xiàn)Feign 客戶端接口
 * 2 使用@Component注解將該類的Bean加入SpringIOC容器
 */
@Component
public class GoodsFeignClientFallback implements  GoodsFeignClient{
    @Override
    public Goods findGoodsById(int id) {
        Goods goods = new Goods();
        goods.setTitle("又被降級(jí)了~~");
        return goods;
    }
}

Hystrix 熔斷

Hystrix 熔斷機(jī)制,用于監(jiān)控微服務(wù)調(diào)用情況,當(dāng)失敗的情況達(dá)到預(yù)定的閾值(5秒失敗20次),會(huì)打開斷路器,拒絕所有請(qǐng)求,直到服務(wù)恢復(fù)正常為止。

  • circuitBreaker.sleepWindowInMilliseconds:監(jiān)控時(shí)間
  • circuitBreaker.requestVolumeThreshold:失敗次數(shù)
  • circuitBreaker.errorThresholdPercentage:失敗率

//設(shè)置Hystrix的超時(shí)時(shí)間 默認(rèn)1s
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000"),
//監(jiān)控的時(shí)間 默認(rèn)5000ms
@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "1000"),
//失敗次數(shù),默認(rèn)20次
@HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "2"),
//失敗率 百分之50
@HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "100")

Hystrix 熔斷監(jiān)控

  • Hystrix 提供了 Hystrix-dashboard 功能,用于實(shí)時(shí)監(jiān)控微服務(wù)運(yùn)行狀態(tài)。
  • 但是Hystrix-dashboard只能監(jiān)控一個(gè)微服務(wù)。
  • Netflix 還提供了 Turbine ,進(jìn)行聚合監(jiān)控。

Turbine聚合監(jiān)控

搭建監(jiān)控模塊

1. 創(chuàng)建監(jiān)控模塊

創(chuàng)建hystrix-monitor模塊,使用Turbine聚合監(jiān)控多個(gè)Hystrix dashboard功能,

2. 引入Turbine聚合監(jiān)控起步依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>hystrix-parent</artifactId>
        <groupId>com.itheima</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>hystrix-monitor</artifactId>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
    	<!--單獨(dú)熔斷監(jiān)控-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
		<!--聚合熔斷監(jiān)控-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3. 修改application.yml

spring:
  application.name: hystrix-monitor
server:
  port: 8769
turbine:
  combine-host-port: true
  # 配置需要監(jiān)控的服務(wù)名稱列表
  app-config: hystrix-provider,hystrix-consumer
  cluster-name-expression: "'default'"
  aggregator:
    cluster-config: default
  #instanceUrlSuffix: /actuator/hystrix.stream
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

4. 創(chuàng)建啟動(dòng)類

@SpringBootApplication
@EnableEurekaClient
@EnableTurbine //開啟Turbine 很聚合監(jiān)控功能
@EnableHystrixDashboard //開啟Hystrix儀表盤監(jiān)控功能
public class HystrixMonitorApp {
    public static void main(String[] args) {
        SpringApplication.run(HystrixMonitorApp.class, args);
    }
}

修改被監(jiān)控模塊

需要分別修改 hystrix-provider 和 hystrix-consumer 模塊:

1、導(dǎo)入依賴:

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

2、配置Bean

此處為了方便,將其配置在啟動(dòng)類中。

@Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }

3、啟動(dòng)類上添加注解@EnableHystrixDashboard

@EnableDiscoveryClient
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients 
@EnableHystrixDashboard // 開啟Hystrix儀表盤監(jiān)控功能
public class ConsumerApp {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApp.class,args);
    }
    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

啟動(dòng)測(cè)試

1、啟動(dòng)服務(wù):

  • eureka-server
  • hystrix-provider
  • hystrix-consumer
  • hystrix-monitor

2、訪問:

在瀏覽器訪問http://localhost:8769/hystrix/ 進(jìn)入Hystrix Dashboard界面

界面中輸入監(jiān)控的Url地址 http://localhost:8769/turbine.stream,監(jiān)控時(shí)間間隔2000毫秒和title,如下圖

  • 實(shí)心圓:它有顏色和大小之分,分別代表實(shí)例的監(jiān)控程度和流量大小。如上圖所示,它的健康度從綠色、黃色、橙色、紅色遞減。通過該實(shí)心圓的展示,我們就可以在大量的實(shí)例中快速的發(fā)現(xiàn)故障實(shí)例和高壓力實(shí)例。
  • 曲線:用來記錄 2 分鐘內(nèi)流量的相對(duì)變化,我們可以通過它來觀察到流量的上升和下降趨勢(shì)。

到此這篇關(guān)于SpringCloud Hystrix熔斷器使用方法介紹的文章就介紹到這了,更多相關(guān)SpringCloud Hystrix熔斷器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • springboot加載注入bean的幾種方式

    springboot加載注入bean的幾種方式

    本文主要介紹了springboot加載注入bean的幾種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • JAVA返回PDF文件流并進(jìn)行下載的實(shí)現(xiàn)方法

    JAVA返回PDF文件流并進(jìn)行下載的實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于JAVA返回PDF文件流并進(jìn)行下載的實(shí)現(xiàn)方法,PDF文件流下載是通過HTTP協(xié)議將服務(wù)器上的PDF文件以流的方式發(fā)送給客戶端,供客戶端保存到本地磁盤或直接在瀏覽器中打開,需要的朋友可以參考下
    2024-02-02
  • 詳解Java中==和equals()的區(qū)別

    詳解Java中==和equals()的區(qū)別

    這篇文章主要介紹了Java中==和equals()的區(qū)別,,==可以使用在基本數(shù)據(jù)類型變量和引用數(shù)據(jù)類型變量中,equals()是方法,只能用于引用數(shù)據(jù)類型,需要的朋友可以參考下
    2021-11-11
  • Java 根據(jù)貸款年限對(duì)應(yīng)利率計(jì)算功能實(shí)現(xiàn)解析

    Java 根據(jù)貸款年限對(duì)應(yīng)利率計(jì)算功能實(shí)現(xiàn)解析

    這篇文章主要介紹了Java 根據(jù)貸款年限對(duì)應(yīng)利率計(jì)算功能實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • mybatis中mapper代理的生成過程全面分析

    mybatis中mapper代理的生成過程全面分析

    這篇文章主要為大家介紹了mybatis中mapper代理的生成過程全面分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • Java結(jié)合Vue項(xiàng)目打包并進(jìn)行服務(wù)器部署

    Java結(jié)合Vue項(xiàng)目打包并進(jìn)行服務(wù)器部署

    本文主要介紹了Java結(jié)合Vue項(xiàng)目打包并進(jìn)行服務(wù)器部署,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • java?使用readLine()?亂碼的解決

    java?使用readLine()?亂碼的解決

    這篇文章主要介紹了java使用readLine()亂碼的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 深入Synchronized和java.util.concurrent.locks.Lock的區(qū)別詳解

    深入Synchronized和java.util.concurrent.locks.Lock的區(qū)別詳解

    本篇文章是對(duì)Synchronized和java.util.concurrent.locks.Lock的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • Java 并發(fā)編程:volatile的使用及其原理解析

    Java 并發(fā)編程:volatile的使用及其原理解析

    下面小編就為大家?guī)硪黄狫ava 并發(fā)編程:volatile的使用及其原理解析。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-05-05
  • 基于binarywang封裝的微信工具包生成二維碼

    基于binarywang封裝的微信工具包生成二維碼

    這篇文章主要介紹了基于binarywang封裝的微信工具包生成二維碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11

最新評(píng)論

龙江县| 布尔津县| 罗定市| 万荣县| 剑河县| 大城县| 阳山县| 芜湖县| 金昌市| 镇远县| 南川市| 通城县| 孝感市| 峨眉山市| 昌宁县| 加查县| 琼结县| 洱源县| 浦东新区| 南宁市| 南川市| 岳池县| 湾仔区| 长岭县| 祁东县| 永新县| 缙云县| 常山县| 甘孜| 夏河县| 阿拉善盟| 塔城市| 延川县| 吉木萨尔县| 武胜县| 南开区| 孟州市| 内黄县| 科尔| 项城市| 光泽县|