微服务springcloud—为Feign禁用Hystrix、Hystrix的监控、Feign项目的Hystrix监控

为Feign禁用Hystrix

在Spring Cloud中,只要Hystrix在项目的classpath中,Feign就会使用断路器包裹Feign客户端的所有方法。这样虽然方便,但是很多场景下不需要该功能。
如何为Feign客户端禁用Hystrix呢?

为指定Feign客户端禁用Hystrix
借助Feign的自定义配置,可轻松为指定名称的Feign客户端禁用Hystrix。
在这里插入图片描述

全局禁用Hystrix
只需要在application.yml中配置feign.hystrix.enabled = false即可。

Hystrix的监控

Feign项目的Hystrix监控
1.启动microservice-discovery-eureka。
2.启动microservice-provider-user。
3.启动microservice-consumer-movie-ribbon-hystrix。
4.访问http://localhost:8010/actuator/hystrix.stream,可以看到浏览器一直处于请求的状态,页面空白。这是因为此时项目中注解了@HystrixCommand的方法还没有被执行,因此也没有任何的监控数据。
5.访问http://localhost:8010/user/1后,再次访问http://loclhost:8010/actuator/hystrix.stream可以看到页面会重复出现类似于以下的内容。
在这里插入图片描述

Feign项目的Hystrix监控

启动之前的microservice-consumer-movie-feign-hystrix-fallback项目,并使用类似的方式测试,然后访问http://loclhost:8010/actuator/hystrix.stream发现返回404,这是为什么呢?
查看项目的依赖树就会发现,项目甚至连Hystrix-metrics-event-stream的依赖都没有,那么如何解决问题呢?

1.复制项目microservice-consumer-movie-feign-hystrix-fallback,改为microservice-consumer-movie-feign-hystrix-fallback-stream。

2.为项目添加spring-cloud-starter-netflix-hystrix的依赖

	   <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

3.在启动类上添加@EnableCircuitBreaker,这样使用/actuator/hystrix.stream端点监控Hystrix了。

本文大部分内容转载自周立的《Spring Cloud与Docker微服务架构实战》

猜你喜欢

转载自blog.csdn.net/weixin_43439494/article/details/83658283