openfeig @FeignClient随笔

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.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
        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;
    }
}

熔断入口
openfeig @FeignClient随笔_第1张图片

正常入口
openfeig @FeignClient随笔_第2张图片

你可能感兴趣的:(spring,Cloud,Alibaba,docker,容器,运维)