spring-boot 2.6.3
spring-cloud-alibaba 2021.0.1
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
// Spring Cloud 2020版本以后,默认移除了对Netflix的依赖,其中就包括Ribbon,官方默认推荐使用Spring Cloud Loadbalancer正式替换Ribbon,并成为了Spring Cloud负载均衡器的唯一实现。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
# Feign 配置
feign:
client:
config:
default:
#简历连接所用的时间,适用于网络状况正常的情况下,两端连接所需要的时间
ConnectTimeOut: 5000
#指建立连接后从服务端读取到可用资源所用的时间
ReadTimeOut: 10000
在启动类上使用 @EnableFeignClients 注解
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
feign:
hystrix:
enabled: true
feign:
circuitbreaker:
enabled: true
在启动类型上使用 @EnableHystrix 注解开启Hystrix的使用
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
// 开启sentinel的支持
feign:
sentinel:
enabled: true
在启动类上使用 @EnableFeignClients 注解开启OpenFeign的使用
// 注解属性解释-name :服务端服务名称;fallback :当feign调用失败后触发的保底方法,防止服务调用失败影响整个功能的使用
@FeignClient(name = "user-server",fallback = FallbackClient.class,path = "/user")
public class UserClient {
@Post("/test")
String test();
}
@Component
public class UserFallbackClient implements UserClient{
@Override
public String test() {
return "降级feign远程调用系统日志服务异常后的降级方法";
}
}
@FeignClient(name = "user-server",fallbackFactory= FallbackClient.class,path = "/user")
public class UserClient {
@Post("/test")
String test();
}
@Component
public class UserFallbackClient implements FallbackFactory<UserClient>{
@Override
public LogClient create(Throwable cause) {
return new UserClient() {
@Override
public String test() {
return "降级feign远程调用系统日志服务异常后的降级方法";
}
};
}
}