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也没问题。
但是关闭服务端,就不调用继承的类的方法。