springcloud系列16——Hystrix Health Indicator及Metrics Stream

传播安全上下文或使用Spring Scopes

参考springcloud官方文档:http://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_propagating_the_security_context_or_using_spring_scopes

springcloud原文:

If you want some thread local context to propagate into a @HystrixCommand the default declaration will not work because it executes the command in a thread pool (in case of timeouts). You can switch Hystrix to use the same thread as the caller using some configuration, or directly in the annotation, by asking it to use a different “Isolation Strategy”.

翻译:

如果你想要一些线程本地上下文传播到@HystrixCommand,默认声明将不起作用,因为它在线程池中执行命令(在超时的情况下)。 您可以使用某种配置将Hystrix切换为与调用方使用相同的线程,或者直接在注释中请求它使用不同的“隔离策略”。

比如:

@HystrixCommand(fallbackMethod = "stubMyService",
    commandProperties = {
      @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")
    }
)
...

springcloud原文:

The same thing applies if you are using @SessionScope or @RequestScope. You will know when you need to do this because of a runtime exception that says it can’t find the scoped context.

You also have the option to set the hystrix.shareSecurityContext property to true. Doing so will auto configure an Hystrix concurrency strategy plugin hook who will transfer the SecurityContext from your main thread to the one used by the Hystrix command. Hystrix does not allow multiple hystrix concurrency strategy to be registered so an extension mechanism is available by declaring your own HystrixConcurrencyStrategy as a Spring bean. Spring Cloud will lookup for your implementation within the Spring context and wrap it inside its own plugin.

翻译:

如果使用@SessionScope或@RequestScope,则同样适用。 你需要知道,只有在产生一个运行时异常,并且它告诉你无法找到范围内的上下文时,你才应该这样做。

您也可以选择将hystrix.shareSecurityContext属性设置为true。 这样做会自动配置一个Hystrix并发策略插件钩子,他可以将SecurityContext从主线程传输到Hystrix命令使用的钩子。 Hystrix不允许注册多个hystrix并发策略,因此通过将自己的HystrixConcurrencyStrategy声明为Spring bean,可以使用扩展机制。 Spring Cloud将在Spring上下文中查找您的实现,并将其包装在自己的插件中。

健康指标

spring-boot的actuator提供了/health端点来查看Hystrix状态。使用也很简单,在pom.xml增加依赖即可:

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

因为在公网环境,有些敏感数据不适合直接查看。所以,默认情况下,访问类似http://localhost:8081/metrics会看到下面的错误:

所以,建议对这些查看敏感数据的使用跟应用不同的端口。
示例如下:

management:
  security:
    enabled: false # 关闭安全检查
  port: 1101 # 管理端口
  context-path: /admin # 管理上线文路径

如果在公网环境,建议在防火墙上做下限制,仅允许8081进来,1101用于内网访问即可,这样相对比较安全,也不用繁琐的输入密码。

在应用启动时的控制台日志,我们可以看到,spring-boot-acturator提供了非常多的指标。

springboot-acturator的相关内容参考:http://www.cnblogs.com/yjmyzz/p/spring-boot-actuator-tutorial.html

配置了springboot-actuator后,我们可以通过/health来查看Hystrix的健康状态。

这个表示hystrix的断路器未打开。

我们将user服务关闭,然后疯狂刷新/user/1多次(20次)以上,再次访问/admin/health。

会看到:

可以看到,hystrix的状态是CIRCUIT_OPEN,标识断路器打开了。

Hystrix监控

/health端点只能看到断路器的整体状态,但对细节展示不详细。从上面我们会看到一个/hystrix.stream的端点,访问该端点可以看到详细的数据。页面会一直持续刷新,可以看到最新数据。

Hystrix Dashboard

通过/hystrix.stream可以实时看到最新的监控数据,但密密麻麻的文字,看起来不太方便。spring cloud提供了一个hystrix-dashboard的功能,可以以图形化界面展示这些数据。

使用步骤如下:
a.添加spring-cloud-starter-hystrix-dashboard的依赖

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

b.在启动类上增加@EnableHystrixDashboard注解

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

c.重新启动movie-hystrix模块
d.浏览器输入movie-hystrix应用端口/hystrix,可以看到下面的页面

输入下面的信息:

其实就是制定/hystrix.stream的地址,刷新的间隔。

然后就可以看到下面的界面:

可以看到,此时断路器开关是关闭的。

我们停止user模块的服务,并且狂刷页面一会儿。

可以看到,此时断路器打开了。

这显然比纯文字友好多了。还有一个问题,如果有多个hystrix.stream地址同时监控,或者把多个地址的数据汇总起来,该怎么弄?github上有一个turbine ,就是专门为解决这个问题的,大家可以自行研究下。

参考:https://www.cnblogs.com/yjmyzz/p/spring-cloud-hystrix-tutorial.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值