SpringCloud——Hystrix 知识篇

目录

Hystrix- 注意

Hystrix-0 Hystrix 的官方简介

Hystrix-1 Hystrix 的开发步骤

Hystrix-1.1 Hystrix fallbackMethod进入

Hystrix-2.1 Health——commandProperties 

Hystrix-2.2 Health——autuactor

Indicator及Metrics Stream

Hystrix——feign和fallback方法的@Component

如何禁用单个FegionClient的Hystrix的支持

Feign使用fallbackFactory属性打印fallback异常


  • Hystrix- 注意

使用Hystrix必须注入autuactor依赖,不然启动不起来

  • Hystrix-0 Hystrix 的官方简介

  1. 官方文档:https://projects.spring.io/spring-cloud/spring-cloud.html#_circuit_breaker_hystrix_clients
  2. javanica文档:https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica
  • Hystrix-1 Hystrix 的开发步骤

  1. 1、添加pom.xml依赖
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
  1. 2、 启动类添加注解@EnableCircuitBreaker
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class Hystrix001Application {

   @Bean
   public RestTemplate restTemplate() {
      return new RestTemplate();
   }
   public static void main(String[] args) {
      SpringApplication.run(Hystrix001Application.class, args);
   }

}
  1. 3、方法名称中添加注解@HystrixCommand,并且自定义返回方法【返回方法的参数返回值、入参一定要和注解相同】
@RestController
public class MovieController {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${user.userServicePath}")
    private String userServicePath;

    @GetMapping("/movie/{id}")
    @HystrixCommand(fallbackMethod = "findByIdfallback")
    public User findById(@PathVariable Long id) {
        return this.restTemplate.getForObject(this.userServicePath + id, User.class);
    }

    public User findByIdfallback() {
        User user = new User();
        user.setId(1110504125L);
        user.setName("xuhy");
        System.out.println("断啦");
        return user;
    }
}
  1. 4、其他内容
  • 实体类User.java
//因为通过RestTemplate调用不需要jpa的注解内容
public class User {
    private Long id;

    private String username;

    private String name;

    private Short age;

    private BigDecimal balance;

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Short getAge() {
        return this.age;
    }

    public void setAge(Short age) {
        this.age = age;
    }

    public BigDecimal getBalance() {
        return this.balance;
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }

}
  • application.yml
server:
  port: 7901
user:
  userServicePath: http://localhost:7900/simple/
eureka:
  client:
    serviceUrl:
      defaultZone: http://user:123@localhost:8761/eureka

  instance:
      prefer-ip-address: true
      instance-id: ${spring.application.name}:${spring.application.instance-id:${server.port}}
spring:
  application:
    name: hystrix-001
  1. 5、测试结果http://192.168.4.160:7901/movie/1

{
    "id": 0,
    "username": null,
    "name": "xuhy",
    "age": null,
    "balance": null
}

  • Hystrix-1.1 Hystrix fallbackMethod进入

将用户服务等其中一份服务断掉,则会直接进入fallbackMethod方法中,控制台会打印断啦两个字。

  • Hystrix-2.1 Health——commandProperties 

  1. .1、官方文档:https://projects.spring.io/spring-cloud/spring-cloud.html#_circuit_breaker_hystrix_clients
@HystrixCommand(fallbackMethod = "stubMyService",
    commandProperties = {
      @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")
    }
)
  1. 2、内容演示

      

 

  • Hystrix-2.2 Health——autuactor

  1. 启动项目
  2. 访问health——http://192.168.4.160:7900/health
    {
        "description": "Spring Cloud Eureka Discovery Client",
        "status": "UP",
        "discoveryComposite": {
            "description": "Spring Cloud Eureka Discovery Client",
            "status": "UP",
            "discoveryClient": {
                "description": "Spring Cloud Eureka Discovery Client",
                "status": "UP",
                "services": [
                    "microservice-provider-user",
                    "microservice-consumer-movie-ribbon-with-hystrix"
                ]
            },
            "eureka": {
                "description": "Remote status from Eureka server",
                "status": "UP",
                "applications": {
                    "MICROSERVICE-CONSUMER-MOVIE-RIBBON-WITH-HYSTRIX": 1,
                    "MICROSERVICE-PROVIDER-USER": 1
                }
            }
        },
        "diskSpace": {
            "status": "UP",
            "total": 446141005824,
            "free": 395148431360,
            "threshold": 10485760
        },
        "db": {
            "status": "UP",
            "database": "H2",
            "hello": 1
        },
        "refreshScope": {
            "status": "UP"
        },
        "hystrix": {
            "status": "UP"
        }
    }
  3.  把用户微服务停掉——http://192.168.4.160:8010/movie/1
    {
        "id": 0,
        "username": null,
        "name": null,
        "age": null,
        "balance": null
    }
  4.  多次请求到不再请求的状态
  5.  我这边怎么请求都是up状态,所以直接用老师的事例图片了。openC..表示当前打开的断路器是findById
  6.  要想使用health必须是要有autuactor的支持,如果pom文件没有添加,启动都启动不起来哦!!!!
  • Indicator及Metrics Stream

  1. 官方文档:https://projects.spring.io/spring-cloud/spring-cloud.html#_circuit_breaker_hystrix_clients
  2. MICROSERVICE-CONSUMER-MOVIE-RIBBON-WITH-HYSTRIX方访问/hystrix.stream发现浏览器一直旋转
  3. 现在用户微服务启动成功之后,访问movie/1再次访问/hystrix.stream就有啦
  • Hystrix——feign和fallback方法的@Component

  1. 1 官方例子
@FeignClient(name = "hello", fallback = HystrixClientFallback.class)
protected interface HystrixClient {
    @RequestMapping(method = RequestMethod.GET, value = "/hello")
    Hello iFailSometimes();
}

static class HystrixClientFallback implements HystrixClient {
    @Override
    public Hello iFailSometimes() {
        return new Hello("fallback");
    }
}
  1. 2 改编自己非public(写在一起或者不一起都行)

写在外部public

注意:如果你没有添加@Component在HystrixClientFallback中会报错

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'movieController': Unsatisfied dependency expressed through field 'userFeignClient'; nested exception is
  • 如何禁用单个FegionClient的Hystrix的支持

  1. ​​​​​​​​​​​​​​link
  • Feign使用fallbackFactory属性打印fallback异常

  1. link

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值