1. 程式人生 > >SpringCloud[Finchley版本]的feign開啟熔斷不起作用解決

SpringCloud[Finchley版本]的feign開啟熔斷不起作用解決

1.版本和依賴

首先SpringCloud,有ribbon和feign兩種方式,版本也有很多
在這裡插入圖片描述

而SpringCloud和SpringBoot之間版本差異也有會有問題,總之依賴之間各個版本之間相容很差,能找到一份能用的依賴就成功了一大半.

我這邊使用的SpringCloud和SpringBoot的版本號如下:

<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>

<parent>
    <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> </parent>

完整的pom依賴如下:

  1. 定義一個父模組, 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.zgd.spring-cloud</groupId> <artifactId>spring-cloud-part</artifactId> <packaging>pom</packaging>
<version>1.0-SNAPSHOT</version> <name>spring-cloud-part</name> <modules> <module>eureka-client</module> <module>eureka-server</module> <module>service-feign</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> <version>2.0.4.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.1.RELEASE</version> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
  1. 一個子模組,註冊中心:
<?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>
    <packaging>jar</packaging>
    <artifactId>service-ribbon</artifactId>

    <name>service-ribbon</name>
    <parent>
        <groupId>com.zgd.spring-cloud</groupId>
        <artifactId>spring-cloud-part</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

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

</project>
  1. 子模組服務生產者
<?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>

    <groupId>com.zgd.springcloud</groupId>
    <artifactId>eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-client</name>

    <parent>
        <groupId>com.zgd.spring-cloud</groupId>
        <artifactId>spring-cloud-part</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>


    <dependencies>
        <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-web</artifactId>
        </dependency>
    </dependencies>

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

  1. 子模組feign消費者
<?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>
    <packaging>jar</packaging>
    <artifactId>service-feign</artifactId>

    <name>service-feign</name>

    <parent>
        <groupId>com.zgd.spring-cloud</groupId>
        <artifactId>spring-cloud-part</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

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


</project>

2. 配置

網上方法:

  1. 在啟動類上加註解@EnableHystrix
  2. 在配置檔案中加入
feign:
	hystrix:
    	enabled: true#開啟Feign Hystrix 支援
  1. 在feign介面上,表明fallback熔斷類
@FeignClient(name = "client01",fallback = FeignServiceImpl.class)
public interface FeignService{
	...
	
}
  1. 熔斷實現類
@Component
public class FeignServiceImpl implements FeignService {
	...
}

但是這樣熔斷並沒有起作用.

我在網上找了一圈,又是換依賴又是換版本號,始終沒解決這個問題.
後面發現其實很簡單:

  1. feign已經預設配置了熔斷,不需要加註解@EnableHystrix
  2. 配置檔案是這樣的:
feign.hystrix.enabled: true

再試試.成功!

我的熔斷類,熔斷的話會列印熔斷

@Component
public class FeignServiceImpl implements FeignService {

    @Override
    public String hiService(String name) {
        return name+",這裡被熔斷了";
    }

    @Override
    public String timeOut(int mills) {
        System.out.println("熔斷");
        return "熔斷了";
    }
}

在這裡插入圖片描述