在我们请求出错的时候,会转到我们的fallback方法,这个实现是通过在应用启动的时候,我们开始了@EnableCircuitBreaker注解,这个注解会通过AOP拦截所有的HystrixCommand方法,将HystrixCommand整合到springboot的容器中,并且将注解标注的方法放入hystrix的线程中,一旦失败,通过反射调用fallback方法来实现。
创建dashboard project上述代码我们看了Hystrix实现熔断的2种方式,接下来我们来实现请求监控的图形化界面,创建mscx-ad-dashboard,Let's code.
依然遵从我们springboot项目的三部曲:
加依赖
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> <version>1.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> <version>1.2.7.RELEASE</version> </dependency> <!--eureka client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
加注解
/** * AdDashboardApplication for Hystrix Dashboard 启动类 * * @author <a href="http://www.likecs.com/mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a> * @since 2019/8/15 */ @SpringBootApplication @EnableDiscoveryClient @EnableHystrixDashboard public class AdDashboardApplication { public static void main(String[] args) { SpringApplication.run(AdDashboardApplication.class, args); } }
改配置
server: port: 1234 spring: application: name: mscx-ad-dashboard eureka: client: service-url: defaultZone: :7777/eureka/,:8888/eureka/,:9999/eureka/ management: endpoints: web: exposure: include: "*"`直接启动,可以看到如下页面:
添加要监控的服务地址: