spring boot 使用Hystrix-dashboard 监控Feign的单个应用

上一篇,使用了Feign的熔断器Hystrix,去对Consumer进行了改造,使其拥有了对服务异常的处理能力。

接下来要做对服务的访问情况进行监控

Hystrix-dashboard 熔断监控,在实际集群中同服务的节点有许多个,这里仅作单个服务节点的监控,集群中的监控会在下一篇有讲

对消费者Consumer进行改造

 pom.xml 新增3个依赖

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

启动类

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ApplicationStart {

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }

    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();    //监控实例
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);    //servlet注册接口
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/actuator/hystrix.stream");   //路径
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }

    public static void main(String[] args) {
        SpringApplication.run(ApplicationStart.class);
    }
}

1.新增注解

@EnableHystrixDashboard      开启熔断监控
@EnableCircuitBreaker     开启断路器

2.注册servlet,实例化HystrixMetricsStreamServlet(较新版本需要添加,低版本略过)

启动Consumer,访问http://localhost:8051/hystrix    (8051是我这边在application配置文件中设置的端口)

 看到这种界面,说明启动成功了,这里有3个提示

查看默认集群:http://turbine-hostname:port/turbine.stream

查看指定集群:http://turbine-hostname:port/turbine.stream?cluster=[clusterName]

查看本应用:http://hystrix-app:port/actuator/hystrix.stream

现在只查看本应用的访问情况,在输出框内输入:http://localhost:8051/actuator/hystrix.stream ,点击monitor stream

 如果显示loading,访问一下应用地址,我这边是:http://localhost:8051/message/remote/hello/   之后再回去看就有了

如果想单独将监控数据拿出来,自己做预警,如邮件通知,短信通知,可以直接调用接口http://localhost:8051/actuator/hystrix.stream,可以得到

猜你喜欢

转载自www.cnblogs.com/yhood/p/11573980.html
今日推荐