我们使用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);
}
}
下面在进行测试应该就没什么问题了