9 Hystrix服務熔斷

9.1 服務熔斷的核心知識

9.1.1 雪崩效應

9.1.2 服務隔離

 

9.1.3 熔斷降級

9.1.4  服務限流

9.2 Hyxtrix

9.3 Rest實現服務熔斷

9.3.1 對RestTemplate的支持

引入Hystrix的依賴

       <!--引入Hystrix依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

在啓動類中激活Hystrix

package xx.study.sc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
//激活Hystrix
@EnableCircuitBreaker
public class RestOrderApplication {
    /**
     * 使用spring 提供的RestTemplate發送http請求到商品服務
     *    1。創建RestTemplate對象交給容器管理
     *    2。在使用的時候,調用其方法完成操作
     *
     */
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return  new RestTemplate();
    }


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

配置熔斷觸發的降級邏輯

在需要收到保護的接口上使用@HystrixCommand配置

package xx.study.sc.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import xx.study.sc.feign.ProductFeignClient;

import java.util.List;

@RestController
@RequestMapping("/order")
@DefaultProperties(defaultFallback = "defaultFallBack")
public class OrderController {
    //注入
    @Autowired
    private RestTemplate restTemplate;
  /**
     * 使用註解 熔斷保護
     * 調用遠程服務
     */
    @HystrixCommand(fallbackMethod = "orderFallBack")
    @RequestMapping(value = "/buyTea",method = RequestMethod.GET)
    public String  buyTea(@RequestParam String name){
        name=restTemplate.getForObject("http://service-product/product/buy?name= "+name,String.class);
        String returnVal="從註冊中心收到"+name+"!!!";
        return returnVal;
    }
    /**
     * 使用註解 默認降級方法
     * 調用遠程服務
     */
    @HystrixCommand
    @RequestMapping(value = "/buyBanana",method = RequestMethod.GET)
    public String  buyBanana(@RequestParam String name){
        name=restTemplate.getForObject("http://service-product/product/buy?name= "+name,String.class);
        String returnVal="從註冊中心收到"+name+"!!!";
        return returnVal;
    }

    /**
     * 降級方法
     *   和需要收到的方法返回值一致 方法參數一致
     * @return
     */
    public String orderFallBack(String name){

       return name+"沒有了,降級了";
    }
    /**
     * 指定同一降級方法
     */
    public String defaultFallBack(){

        return "降級了";
    }



}

 application.properties配置

#連接下游服務的超時時間默認1s,超過這個時間觸發降級邏輯
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000

9.3.2 對Feign支持

引入Hystrix的依賴(feign已經繼承了Hystrix)

在feign中配置開啓Hystrix

#開啓對hystirix支持
feign.hystrix.enabled=true

自定義一個接口的實現類,這個實現類就是熔斷觸發的降級邏輯

package xx.study.sc.feign;

import org.springframework.stereotype.Component;

@Component
public class ProductFeignClientCallBack implements ProductFeignClient {
    @Override
    public String buy(String name) {
        return "feign調用,觸發降級了";
    }
}

修改FeignClient接口添加降級方法的支持

package xx.study.sc.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**聲明需要調用的微服務名稱
 * @FeignClient
 *    name:服務提供者的名稱
 *    fallback
 *
 */
@FeignClient(name="service-product",fallback = ProductFeignClientCallBack.class)
public interface ProductFeignClient {
    /**
     * 配置需要調用的微服務接口
     * 訪問路徑要寫全  類+方法
     */
    @RequestMapping(value = "/product/buy",method = RequestMethod.GET)
    public String buy(@RequestParam String name);

}

9.4 Hystrix的監控平臺

 

引入依賴

        <!--hystrix監控平臺依賴-->
        <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>

啓動類配置


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.openfeign.EnableFeignClients;


@SpringBootApplication
//激活feign
@EnableFeignClients
//激活Hystrix
@EnableCircuitBreaker
public class FeignOrderApplication {



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

application.properties配置

#暴露hystrix所有接口  監控平臺接口
management.endpoints.web.exposure.include=*

訪問查看

http://localhost:9003/actuator/hystrix.stream

9.4.1 搭建Hystrix DashBoard監控

添加依賴(同上)

啓動類激活即可

package xx.study.sc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;


@SpringBootApplication
//激活feign
@EnableFeignClients
//激活Hystrix
@EnableCircuitBreaker
//激活hystrix的web監控平臺
@EnableHystrixDashboard
public class FeignOrderApplication {



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

訪問http://localhost:9003/hystrix

輸入localhost:9003/actuator/hystrix.stream

點擊Monitor Stream 進行監測

 

 

 9.4.2 斷路器聚合監控 Turbine

1 新建項目 hystrix_turbine

2 引入依賴

<?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>spring_cloud_demo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>hystrix_turbine</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</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>
    </dependencies>


</project>

3 application.properties配置

server.port=9004
#服務名稱
spring.application.name=hystrix-turbine
#註冊中心訪問地址
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
#使用ip地址註冊
eureka.instance.prefer-ip-address=true
#監控列表 多個之間用逗號分隔
turbine.app-config=ervice-order,service-product
turbine.cluster-name-expression=default

4 啓動類配置

package xx.study.sc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

@SpringBootApplication
//turbine配置
@EnableTurbine
@EnableHystrixDashboard
public class HystrixTurbineApplication {
    public static void main(String[] args) {
        SpringApplication.run(HystrixTurbineApplication.class);
    }
}

5 啓動程序訪問

  http://localhost:9004/hystrix/

9.5 熔斷器的狀態

9.5.1 熔斷器的狀態說明

9.5.2 熔斷器的隔離策略

 9.6 源碼

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章