天天看點

spring-cloud學習(四)———hystrix以及hytrisDashboard監控儀表盤

Hystrix

1.概念

官網:https://github.com/Netflix/Hystrix/wiki
    
    什麼是Hystrix?
    在分布式環境中,許多服務依賴項中的一些不可避免地會失敗。Hystrix是一個庫,可通過添加延遲容錯和容錯邏輯來幫助您控制這些分布式服務之間的互動。Hystrix通過隔離服務之間的通路點,阻止它們之間的級聯故障以及提供後備選項來實作這一目标,所有這些都可以提高系統的整體彈性。
           
1.1.雪崩效應
**分布式系統環境下,服務間類似依賴非常常見,一個業務調用通常依賴多個基礎服務。當某個服務出現無法通路、異常、逾時等問題時,使用者的請求将會被阻塞,當有大批量請求調用該服務時,有可能造成大量的阻塞現象,并且這種不可用可能沿請求調用鍊向上傳遞,這種現象被稱為雪崩效應。==**
           

原因:

  1. 硬體故障:如伺服器當機,機房斷電,光纖被挖斷等。
  2. 流量激增:如異常流量,重試加大流量等。
  3. 緩存穿透:一般發生在應用重新開機,所有緩存失效時,以及短時間内大量緩存失效時。大量的緩存不命中,使請求直擊後端服務,造成服務提供者超負荷運作,引起服務不可用。
  4. 程式BUG:如程式邏輯導緻記憶體洩漏,JVM長時間FullGC等。
  5. 同步等待:服務間采用同步調用模式,同步等待造成的資源耗盡。

政策:

  1. 硬體故障:多機房容災、異地多活等。
  2. 流量激增:服務自動擴容、流量控制(限流、關閉重試)等。
  3. 緩存穿透:緩存預加載、緩存異步加載等。
  4. 程式BUG:修改程式bug、及時釋放資源等。
  5. 同步等待:資源隔離、MQ解耦、不可用服務調用快速失敗等。資源隔離通常指不同服務調用采用不同的線程池;不可用服務調用快速失敗一般通過熔斷器模式結合逾時機制實作。
1.2.Hystrix作用
  1. 通過第三方用戶端庫通路(通常通過網絡)依賴關系,以防止和控制延遲和故障。
  2. 在複雜的分布式系統中停止級聯故障。
  3. 快速失敗并迅速恢複。
  4. 在可能的情況下,後退并優雅地降級。
  5. 實作近實時監控,警報和操作控制。

2.GAV(pom)

<!--Hystrix:斷路器-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    <!-- hystrix儀表盤 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    </dependency>
           

3.主啟動類上

@EnableCircuitBreaker//對hystrixR熔斷機制的支援
    @EnableHystrixDashboard //hystrixR儀表盤
           

4.controller

package com.qin.eurekaconsumer.controller;
    
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author  WZB
     * @date 2019/4/22 21:48
     * @description 服務熔斷
     */
    @RestController
    @RequestMapping("/hystrix/consumer/")
    public class ConsumerEurekaHystrixController {
    
        @Autowired
        private RestTemplate restTemplate;
    
        /*
         *服務名
         *
         * */
        private static final String SERVICE_NAME = "http://EUREKA-PROVIDER";
    
        @RequestMapping("getProviderTest")
        @HystrixCommand(fallbackMethod ="dealExceptionByHystrix")
        public List<String> getProviderTest() {
            //手動抛出異常,模拟斷路器功能
            int x=2/0;
            List<String> o= restTemplate.getForObject(SERVICE_NAME+"/eurekacluster/getResult", List.class);
            System.out.println(o);
            return o;
        }
    
        public List<String> dealExceptionByHystrix(){
            List<String> list=new ArrayList<>();
            list.add("hystrix測試");
            return  list;
        }
    }
           

5.Feign支援

@FeignClient(name = "hello", fallback = HystrixClientFallback.class)
    protected interface HystrixClient {
        @RequestMapping(method = RequestMethod.GET, value = "/hello")
        Hello iFailSometimes();
    }
    
    static class HystrixClientFallback implements HystrixClient {
        @Override
        public Hello iFailSometimes() {
            return new Hello("fallback");
        }
    }
           

二.HystrixDashboard監控

1. 被監控的springcloud項目

被監控的項目如上1至5步驟,注意:

1.1. 主啟動類上注入如下Bean
@Bean
        public ServletRegistrationBean getServlet() {
            HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
            ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
            registrationBean.setLoadOnStartup(1);
            //springboot2.0 版本
            //registrationBean.addUrlMappings("/actuator/hystrix.stream");
            //springboot  低版本
            registrationBean.addUrlMappings("/hystrix.stream");
            registrationBean.setName("HystrixMetricsStreamServlet");
            return registrationBean;
        }
           
1.2. yml配置
# 監控權限
    management:
      endpoint:
        hystrix:
          stream:
            enabled: true
           

2. HystrixDashboard監控項目

2.1. GAV(pom)
<!-- hystrix儀表盤 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    </dependency>
           

3. 測試

啟動被監控項目-----啟動監控項目—通路監控項目(如http://localhost:8787/hystrix)------填入被監控項目的IP及port和字尾(如 http://localhost:6767/hystrix.stream 或者http://localhost:6767/actuator/hystrix.stream)

spring-cloud學習(四)———hystrix以及hytrisDashboard監控儀表盤

顯示被監控服務的通路狀态

spring-cloud學習(四)———hystrix以及hytrisDashboard監控儀表盤