springcloud入门之熔断监测hystrix monitor

springcloud提供的熔断监测,实际上是对项目中用到feign或者ribbon远程服务调用并开启熔断机制hystrix的服务,也就是说这个服务必须满足两个条件,第一是作为一个服务消费者来消费服务的,第二是需要开启熔断机制,只有满足这两个条件的服务才可以被熔断监测平台来监测到,也就是说一般的服务提供者是无需做熔断监测的。

熔断监测,可以单独运行,因此单独开启一个项目,springcloud入门系列里面熔断监测项目叫monitor,他需要监测的服务就是这里的网关项目gateway,因为只有他才调用远程服务feign和robbin,而且开启了熔断机制。

熔断监测,可以监测单应用,也可以监测集群,这个示例只是用来监测单实例应用的。他就需要依赖spring-cloud-starter-netflix-hystrix-dashboard即可,另外,如果需要监测集群,可以加入spring-cloud-starter-netflix-turbine,项目依赖如下所示:

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

编写一个启动类:

package com.xxx.monitor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
@EnableTurbine
public class MonitorApplication {
    public static void main(String[] args) {
        SpringApplication.run(MonitorApplication.class,args);
    }
}

配置文件application.yml

server:
  port: 8989

bootstrap.yml 

spring:
  application:
    name: monitor
eureka:
  instance:
    non-secure-port: ${server.port:8989}
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

启动项目,我们通过浏览器访问http://localhost:8989/hystrix得到如下页面:

这个页面就是我们的熔断监测仪表盘,需要监测哪个服务,我们需要在第一个输入框中按照我们应用的类型输入相关服务hystrix监测地址,这里是单应用,因此我们需要按照提示的Single Hystrix App的样子输入服务监测地址。 因为我们的gateway服务对外暴露的端口是80,访问时可以直接输入主机名,忽略端口号,如:http://localhost,后面还需要加上/actuator/hystrix.stream

另外,因为springcloud版本的问题,我们输入要监测的服务地址http://localhost/actuator/hystrix.stream,访问页面会出现错误:

 Unable to connect to Command Metric Stream.

出现这个错误的原因是springcloud对作为eureka client的项目访问做了权限设置,默认只容许访问actuator/info,actuator/health这两个endpoint,而actuator/hystrix.stream这个uri被限制访问,这个错误的解决办法有两种,都是在需要做熔断监测的项目里面修改,这里就是gateway项目了,只有他开启了熔断机制。

第一种解决办法,bootstrap.yml中增加一个配置项,对actuator/hystrix.stream请求放开:

management:
  endpoints:
    web:
      exposure:
        include: "*"

第二种解决办法,增加一个bean配置,同样是需要放开actuator/hystrix.stream请求:

@Bean
public ServletRegistrationBean getServlet(){
	HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
	ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
	registrationBean.setLoadOnStartup(1);
	registrationBean.addUrlMappings("/actuator/hystrix.stream");
	registrationBean.setName("HystrixMetricsStreamServlet");
	return registrationBean;
}

这个bean可以配置在启动类中,也可以单独配置在一个有@Configuration注解的类中。

停止gateway项目,重新启动,我们再次访问刚才的页面,就不会出现:Unable to connect to Command Metric Stream错误了。而是出现loading,这是因为我们还没有访问我们开启熔断机制的服务。

 

接着,我们切换到我们的gateway项目的主页,通过点击get message按钮,访问一下get()方法,这样再过来看这个监测页面,就变成了如下所示的样子:

至此,springcloud入门项目就都介绍完了,我们通过示例,构建了一个简单的应用,通过这些应用以及他们的角色了解了springcloud作为一个微服务解决方案在实际中的应用。如果有什么问题,可以回过去看springcloud入门系列的其他项目:

springcloud入门之服务发现

springcloud入门之配置服务

springcloud入门之通过feign实现负载均衡

springcloud入门之通过ribbon实现负载均衡

springcloud入门之网关服务

项目源代码:https://github.com/buejee/springcloud

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luffy5459

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值