1.maven依赖
<!--springboot的版本和springcloud的版本一定要对应,否则会报错 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
</parent>
<!-- 管理springcloud依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--引入feign客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--引入hystrix依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2.application.yml文件配置
#feign客户端配置
feign:
hystrix:
#设置feign开启hystrix(服务保护)
enabled: true
#ribbon配置
ribbon:
#ribbon的超时时间要大于hystrix的超时时间,否则 hystrix自定义的超时时间毫无意义
ReadTimeout: 5000
ConnectTimeout: 5000
#hystrix配置
hystrix:
command:
default:
execution:
isolation:
thread:
#feign整合hystrix 光设置Hystrix 超时没用的要配合ribbon超时
timeoutInMilliseconds: 3000
circuitBreaker:
#默认20 ,熔断的阈值,如何user服务报错满足3次,熔断器就会打开,就算order之后请求正确的数据也不行。
requestVolumeThreshold: 3
#默认5S , 等5S之后熔断器会处于半开状态,然后下一次请求的正确和错误讲决定熔断器是否真的关闭和是否继续打开
sleepWindowInMilliseconds: 8000
3.在启动类加上@EnableFeignClients @EnableHystrix
4.新建eureka服务端项目,并且启动
可以参考:https://blog.csdn.net/qq_34707456/article/details/103641736
5.新建一个生产者项目
设置注册到eureka的服务名为:producer
然后新建一个controller并添加如下方法
6.新建消费者项目
新建一个feign客户端处理接口:
/**
* @author oulei
* @feign客户端调用接口
* @FeignClient name为注册中心服务的名字 fallback:调用服务失败时的服务降级处理方法
* @date 2019/12/17 17:38
*/
@FeignClient(name="producer",fallback = TestFeignFallBack.class)
public interface ITestFeign {
@RequestMapping("/getinfo")
String getInfo();
}
新建服务降级处理方法:
@Component
public class TestFeignFallBack implements ITestFeign {
@Override
public String getInfo() {
return "hystrix--服务器忙,请稍后再试!";
}
}