spring cloud netflix (08) Feign模式下使用熔斷器(Hystrix)防止服務雪崩

前言

完整知識點:spring cloud netflix 系列技術棧

在spring cloud netflix中使用ribbon和feign作爲服務與服務之間的通信機制,這種通信方式是同步的HTTP通信方式,在大訪問量的情況下會發生阻塞,直觀體現就是標籤頁一直在等待響應,這就會有一個響應時間。而熔斷器的作用就是:在發現服務不可用時,立即做出反應,避免請求一直處於等待響應的狀態,從而避免出現響應時間,減輕服務器壓力,避免出現雪崩。

  • 沒有熔斷器,響應處於等待狀態
    在這裏插入圖片描述
  • 使用熔斷器,發現服務不可用,立即做出反應,避免等待(阻塞)
    在這裏插入圖片描述

Hystrix熔斷器

通過在前言中的理解,我們知道了熔斷器的作用是:當服務與服務之間的通信出現問題時,熔斷器會立即做出響應,避免發生阻塞。

Hystrix + Feign

  • Hystrix的pom依賴
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
  • 完整的pom依賴
<?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">
    <modelVersion>4.0.0</modelVersion>

    <!-- 關聯統一的依賴管理 -->
    <parent>
        <groupId>com.baiyang</groupId>
        <artifactId>spring-cloud-netflix-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <!-- 指向統一的依賴管理的位置 -->
        <relativePath>../spring-cloud-netflix-dependencies/pom.xml</relativePath>
    </parent>

    <groupId>com.baiyang</groupId>
    <artifactId>spring-cloud-netflix-web-admin-feign</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!-- Spring Boot Begin -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot End -->

        <!-- Spring Cloud Begin -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <!-- Spring Cloud End -->

        <!-- 解決 thymeleaf 模板引擎一定要執行嚴格的 html5 格式校驗問題 -->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- 配置程序的入口類 -->
                    <mainClass>com.baiyang.spring.cloud.netflix.web.admin.feign.AdminWebFeignApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  • Feign默認集成了Hystrix,在application.yml中開啓即可
spring:
  application:
    name: spring-cloud-netflix-web-admin-feign

server:
  port: 8765

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

# Feign默認集成了Hystrix
feign:
  hystrix:
    enabled: true

thymeleaf:
    cache: false
    mode: LEGACYHTML5
    encoding: UTF-8
    servlet:
      content-type: text/html
  • 在feign模式下,熔斷機制是通過實現需要被熔斷的接口來實現熔斷機制的,也就是說,在接口實現類中完成熔斷機制

1:實現需要被熔斷的接口

package com.baiyang.spring.cloud.netflix.web.admin.feign.service.hystrix;

import com.baiyang.spring.cloud.netflix.web.admin.feign.service.AdminFeignService;
import org.springframework.stereotype.Component;

@Component
public class AdminFeignServiceHystrix implements AdminFeignService {
    @Override
    public String hello(String message) {
        return String.format("您輸入的信息是 %s , 但是請求失敗。", message);
    }
}

2:在接口中調用熔斷機制

package com.baiyang.spring.cloud.netflix.web.admin.feign.service;

import com.baiyang.spring.cloud.netflix.web.admin.feign.service.hystrix.AdminFeignServiceHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "spring-cloud-netflix-server-admin",fallback = AdminFeignServiceHystrix.class)
public interface AdminFeignService {

    @GetMapping("hello")
    String hello(@RequestParam(value = "message")String message);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章