openfeig @FeignClient随笔
//@RequestMapping("hello") //path 属性可以代替这个
@FeignClient(name = "openfeig-goods",path = "hello")
public interface GoodsClienService {
@GetMapping("{message}")
String hello(@PathVariable String message);
}
客户端降级
feign:
# 开启 客户端熔断降级,需要jar包
hystrix:
enabled: true
<!-- 引入断路器依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
对外访问的入口
@GetMapping("fusing/{message}")
public String fusing(@PathVariable String message){
return cartService.fusing(message);
}
远程接口,消费者那边 1/0 抛出异常
//客户端降级 fallback 需要配置开启
@FeignClient(name = "openfeig-goods",
path = "hello",
fallback = GoodsClientFallback.class)
public interface GoodsClienService {
//熔断测试
@GetMapping("fusing/{message}")
public String fusing(@PathVariable String message);
}
触发熔断后处理的类
fallback
@Component
public class GoodsClientFallback implements GoodsClienService{
@Override
public String hello(String message) {
message += " 服务异常触发熔断";
return "hello 正常方法,不会熔断";
}
@Override
public String fusing(String message) {
message += " 服务异常触发熔断";
System.out.println(message);
return message;
}
}
熔断入口
正常入口