解决springCloud2020 feign 熔断失效的问题

我们使用spring cloud做服务熔断时,断开服务进行测试,结果发现提示
在这里插入图片描述

这是由于feign中没有开启hystrix,于是我们按照网上的教程添加配置进行开启

feign.hystrix.enabled=true

结果发现没有任何用处,这是因为feign.hystrix.enabled已经找不到了,对应新的控制装配的是

# 开启hystrix
feign.circuitbreaker.enabled=true

下面贴出我的maven配置

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cloud</groupId>
    <artifactId>fegin-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>fegin-consumer</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <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-starter-eureka</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>2.2.9.RELEASE</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2020.0.2</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>

application.properties配置

server.port=9001
spring.application.name=feign-consumer
# 注册服务的时候使用服务的ip地址
eureka.instance.prefer-ip-address=false
# 关闭默认使用的Ribbon的负载均衡
spring.cloud.loadbalancer.ribbon.enabled=false
hello-service.ribbon.ConnectTimeout=500
hello-service.ribbon.ReadTimeout=2000
# 开启重试机制
hello-service.ribbon.OkToRetryOnAllOperations=true
# 尝试更换两次实例重试,失败之后进行重试
hello-service.ribbon.MaxAutoRetriesNextServer=2
# 先尝试访问首选实例一次,失败之后才更换实例访问
hello-service.ribbon.MaxAutoRetries=1
# 开启hystrix
feign.circuitbreaker.enabled=true
# 注册服务
eureka.client.service-url.defaultZone=http://peer1:1111/eureka/,http://peer2:1112/eureka/
# 设置日志级别
logging.level.root=info

主配置类的设置

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

@SpringBootApplication
@EnableDiscoveryClient
//开启Feign
@EnableFeignClients
//开启断路器
@EnableCircuitBreaker
public class FeginConsumerApplication {

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

}

下面在进行测试应该就没什么问题了

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
Spring Cloud Alibaba提供了一个名为Sentinel的熔断降级框架,可以方便地与Spring Cloud集成。下面是使用Spring Cloud Alibaba Sentinel进行熔断降级的步骤: 1. 添加依赖 在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2.2.3.RELEASE</version> </dependency> ``` 2. 配置Sentinel 在`application.yml`文件中添加以下配置: ```yaml spring: cloud: sentinel: transport: dashboard: localhost:8080 #Sentinel Dashboard地址 port: 8719 #Sentinel客户端监听端口号 ## 开启Sentinel熔断降级 feign: sentinel: enabled: true ``` 3. 配置熔断降级规则 在`resources`目录下创建一个名为`META-INF`的文件夹,然后在该文件夹下创建一个名为`flowrule`的文件,文件名可以自己定义。在该文件中添加以下内容,这里以配置一个`Hello World`的熔断降级规则为例: ```json [ { "resource": "hello", //资源名称,可以是方法名或URL "count": 5, //阈值 "grade": 0, //熔断策略,0表示平均响应时间,1表示异常比率 "timeWindow": 5, //时间窗口,单位是秒 "minRequestAmount": 5, //最小请求数量 "slowRatioThreshold": 0 //慢调用比例阈值 } ] ``` 4. 使用@SentinelResource注解 在需要进行熔断降级的方法上添加`@SentinelResource`注解,并指定资源名称,例如: ```java @SentinelResource(value = "hello", fallback = "fallback") public String hello() { return "Hello World"; } public String fallback() { return "fallback"; } ``` 其中`fallback`方法为熔断降级时的备选方法。 5. 启动Sentinel Dashboard 在命令行中输入以下命令启动Sentinel Dashboard: ```bash java -jar sentinel-dashboard-1.8.2.jar ``` 访问`http://localhost:8080`即可进入Sentinel Dashboard。 6. 启动应用程序 启动应用程序后,可以在Sentinel Dashboard中看到应用程序的熔断降级情况。当资源的请求次数超过阈值时,Sentinel将自动触发熔断降级策略,调用`fallback`方法,返回备选结果。 以上就是使用Spring Cloud Alibaba Sentinel进行熔断降级的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZNineSun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值