Feign方式使用Hystrix

返回目录

https://blog.csdn.net/BW_Bear/article/details/88746646

源码位置:

微服务注册管理:

https://github.com/zhaowei-zhang/CloudTest/tree/master/使用Hystrix实现容错/microservice-discovery-eureka

生产者:

https://github.com/zhaowei-zhang/CloudTest/tree/master/使用Hystrix实现容错/microservice-provider-user

Feign的Hystrix整合:

https://github.com/zhaowei-zhang/CloudTest/tree/master/使用Hystrix实现容错/microservice-consumer-movie-feign-hystrix-fallback

1.打开Feign的Hystrix支持

从application.yml中添加

  feign:
      hystrix:
        enabled: true

2.Frign接口修改

2.1 创建回退类

本次创建的时内部类,创建public class也是可以的

 @Component
    class FeignClientFallback implements UserFeignClient{
        @Override
        public User findById(Long id) {
            System.err.println("进入回退方法");
            User user = new User();
            user.setId(-1L);
            user.setUsername("游客");
            return user;
        }
    }

2.2 在接口注解FeignClient中加参数

@FeignClient(name = "microservice-provider-user",
        fallback = FeignClientFallback.class)
public interface UserFeignClient {

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public User findById(@PathVariable("id")Long id);
}

3.测试

启动eureka服务、生产者服务、消费者服务;
通过消费者服务接口调用,正常显示数据
Feign方式使用Hystrix_第1张图片
关闭生产者服务,
Feign方式使用Hystrix_第2张图片
再次通过消费这服务接口调用,显示回退数据

Feign方式使用Hystrix_第3张图片

Feign方式使用Hystrix_第4张图片

Feign方式使用Hystrix,通过FallbackFactory检查回退原因

https://blog.csdn.net/BW_Bear/article/details/88769082

返回目录

https://blog.csdn.net/BW_Bear/article/details/88746646

你可能感兴趣的:(SpringCloud)