爷可是个天才 2022-04-11 01:59 采纳率: 81%
浏览 32
已结题

Hystrix降级失败,关闭服务端之后,消费端没有降级,而是报connect timed out,如何解决?

controller代码


@RestController
public class HystrixCont {

    @Autowired
    private HystrixService hystrixService;

    @GetMapping("/contsumer/hystrix/zc")
    public  String  hystrix_ok(){
      return   hystrixService.hystrix_ok()+ UUID.randomUUID().toString();
    }

    @HystrixCommand(fallbackMethod = "hystrix_3m_two",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")
    })
    @GetMapping("/contsumer/hystrix/3m")
    public  String  hystrix_3m(){
        return  hystrixService.hystrix_3m();
    }

    public  String  hystrix_3m_two(){
        return  "超时";
    }
}


opfeign接口代码

@Component
@FeignClient(value = "cloud-provider-service",fallbackFactory = FlHystrix.class)
public interface HystrixService {



    @GetMapping("/payment/hystrix/zc")
    public  String  hystrix_ok();



    @GetMapping("/payment/hystrix/3m")
    public  String  hystrix_3m();
}


实现HystrixService,做降级的代码

@Component
public class FlHystrix implements HystrixService{

    @Override
    public String hystrix_ok() {
        return "hystrix_ok";
    }

    @Override
    public String hystrix_3m() {
        return "hystrix_3m";
    }
}

主启动类

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
public class HystrixOrder80 {
    public static void main(String[] args) {
        SpringApplication.run(HystrixOrder80.class,args);
    }
}

yml配置

server:
  port: 8000

spring:
  application:
    name: cloud-consumer-order
#  cloud:
#    circuitbreaker:
#      hystrix:
#        enabled: true

eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
        defaultZone: http://eureka7001.com:7001/eureka

#feign结合hystrix使用
feign:
  hystrix:
    enabled: true



其中,我看到有人说feign没有与hystrix一起了,开启hystrix的方式改变了,变成我注释的那段


#  cloud:
#    circuitbreaker:
#      hystrix:
#        enabled: true

但是还是不生效。

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

    <artifactId>cloud-consumer-Feign-Hystrix-order80</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <!--       hystrix(服务降级)-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <!--        openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>3.0.2</version>
        </dependency>
        <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>
        <!--        actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--        热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

</project>

我在消费端controller做@HystrixCommand的超时降级没有问题。opfeign也没问题。
但是关闭服务端,就不调用继承的类的方法。

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 4月19日
    • 创建了问题 4月11日

    悬赏问题

    • ¥15 onlyoffice编辑完后立即下载,下载的不是最新编辑的文档
    • ¥15 求caverdock使用教程
    • ¥15 Coze智能助手搭建过程中的问题请教
    • ¥15 12864只亮屏 不显示汉字
    • ¥20 三极管1000倍放大电路
    • ¥15 vscode报错如何解决
    • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
    • ¥15 python随机森林对两个excel表格读取,shap报错
    • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
    • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。